MediaWiki:Common.js: Difference between revisions
Training materials: admin-only Delete file; purge after library edits |
Fix library Remove/Delete buttons for Media: /images/ links |
||
| Line 3,509: | Line 3,509: | ||
} | } | ||
function filenameFromHref(href) { | function filenameFromHref(href, titleAttr) { | ||
if (titleAttr && /\.[a-z0-9]{2,5}$/i.test(titleAttr)) { | |||
return String(titleAttr).replace(/ /g, '_'); | |||
} | |||
if (!href) { | if (!href) { | ||
return ''; | return ''; | ||
| Line 3,517: | Line 3,520: | ||
} catch (e) { /* ignore */ } | } catch (e) { /* ignore */ } | ||
var m = href.match(/\/wiki\/(?:File|Media):([^?#]+)/i) || | var m = href.match(/\/wiki\/(?:File|Media):([^?#]+)/i) || | ||
href.match(/[?&]title=(?:File|Media):([^&#]+)/i); | href.match(/[?&]title=(?:File|Media):([^&#]+)/i) || | ||
href.match(/\/images\/(?:thumb\/)?[^/]+\/[^/]+\/([^/?#]+)$/i); | |||
if (!m) { | if (!m) { | ||
return ''; | return ''; | ||
| Line 3,525: | Line 3,529: | ||
function enhanceLibraryManage() { | function enhanceLibraryManage() { | ||
if (!mw.config.get('wgUserName')) { | |||
return; | return; | ||
} | } | ||
var $heading = $('.mw-parser-output .mw-heading, .mw-parser-output h2').filter(function () { | var $heading = $('.mw-parser-output .mw-heading, .mw-parser-output h2').filter(function () { | ||
return $(this).text().replace(/\s*\[ | return /^Library\b/i.test($(this).text().replace(/\s*\[.*$/, '').trim()); | ||
}).first(); | }).first(); | ||
if (!$heading.length) { | if (!$heading.length) { | ||
| Line 3,544: | Line 3,547: | ||
$list.data('rbCampManualManage', 1); | $list.data('rbCampManualManage', 1); | ||
var groups = mw.config.get('wgUserGroups') || []; | |||
var canDeleteFile = groups.indexOf('sysop') !== -1; | |||
var api = new mw.Api(); | var api = new mw.Api(); | ||
var | |||
$list.children('li').each(function () { | |||
var $li = $(this); | |||
if ($li.find('.camp-manual-lib-actions').length) { | |||
return; | |||
} | |||
var $a = $li.find('a.internal, a[href*="/images/"], a[href*="File:"], a[href*="Media:"]').first(); | |||
var | if (!$a.length) { | ||
$a = $li.find('a[href]').first(); | |||
} | |||
var filename = filenameFromHref($a.attr('href'), $a.attr('title')); | |||
if (!filename) { | |||
return; | |||
if ( | } | ||
var $actions = $('<span>', { 'class': 'camp-manual-lib-actions' }); | |||
var $remove = $('<button>', { | |||
type: 'button', | |||
'class': 'camp-manual-lib-remove', | |||
text: 'Remove' | |||
}).attr('title', 'Remove from this list (file stays on the wiki)'); | |||
$actions.append($remove); | |||
if (canDeleteFile) { | |||
$actions.append($('<button>', { | |||
type: 'button', | |||
'class': 'camp-manual-lib-delete', | |||
text: 'Delete file' | |||
}).attr('title', 'Delete the file from the wiki (administrators only)')); | |||
} | |||
$li.append($actions); | |||
$remove.on('click', function () { | |||
if (!window.confirm('Remove “' + filename.replace(/_/g, ' ') + '” from the Library list?\n(The file will remain on the wiki.)')) { | |||
return; | return; | ||
} | } | ||
$actions.find('button').prop('disabled', true); | |||
removeFromLibrary(api, filename).done(function () { | |||
window.location.reload(); | |||
}).fail(function (code, data) { | |||
window.alert(failMessage(code, data)); | |||
$actions.find('button').prop('disabled', false); | |||
}); | |||
}); | |||
if (!filename) { | |||
$actions.on('click', '.camp-manual-lib-delete', function () { | |||
if (!window.confirm('Permanently delete file “' + filename.replace(/_/g, ' ') + '” from the wiki?')) { | |||
return; | return; | ||
} | } | ||
$actions.find('button').prop('disabled', true); | |||
deleteLibraryFile(api, filename).done(function () { | |||
window.location.reload(); | |||
}).fail(function (code, data) { | |||
window.alert(failMessage(code, data)); | |||
$actions.find('button').prop('disabled', false); | |||
}); | }); | ||
}); | }); | ||