MediaWiki:Gadget-massdelete.js

mw.loader.using(['mediawiki.api', 'mediawiki.Title'], function {    "use strict";

var config = mw.config.get(['wgNamespaceNumber', 'wgTitle', 'wgUserGroups', 'skin']);

function removeBlanks(arr) { var ret = []; var i, len; for (i = 0, len = arr.length; i < len; i++) { var s = arr[i]; s = s.trim; if (s) { ret.push(s); }       }        return ret; }

function doMassDelete { document.getElementById("wpMassDeleteSubmit").disabled = true; var articles = document.getElementById("wpMassDeletePages").value.split("\n"); articles = removeBlanks(articles); if (!articles.length) { return; }       var api = new mw.Api, wpMassDeleteReasons = document.getElementById("wpMassDeleteReasons").value, wpMassDeleteReason = document.getElementById("wpMassDeleteReason").value, deleted = 0, failed = [], error = [], deferreds = [], reason = wpMassDeleteReasons == "other" ? wpMassDeleteReason : wpMassDeleteReasons + (wpMassDeleteReason ? " (" + wpMassDeleteReason + ")" : ""), onSuccess = function { deleted++; document.getElementById("wpMassDeleteSubmit").value = "(" + deleted + ")"; };

function makeDeleteFunc(article) { return function { return $.Deferred(function (deferred) {                   var promise = api.postWithToken('delete', { format: 'json', action: 'delete', title: article, reason: reason });                   promise.done(onSuccess);                    promise.fail(function (code, obj) { failed.push(article); error.push(obj.error.info); });                   promise.always(function  { deferred.resolve; });               });            };        }

// Make a chain of deferred objects. We chain them rather than execute them in       // parallel so that we don't make 1000 simultaneous delete requests and bring the // site down. We use deferred objects rather than the promise objects returned // from the API request so that the chain continues even if some articles gave // errors. var deferred = makeDeleteFunc(articles[0]); for (var i = 1, len = articles.length; i < len; i++) { deferred = deferred.then(makeDeleteFunc(articles[i])); }

// Show the output and do cleanup once all the requests are done. $.when(deferred).then(function {            document.getElementById("wpMassDeleteSubmit").value = "Done (" + deleted + ")";            if (failed.length) {                var $failedList = $('');                for(var x = 0; x < failed.length; x++) {                    // Link the titles in the "failed" array                    var failedTitle = mw.Title.newFromText(failed[x]);                    var $failedItem = $('');                    if (failedTitle) {                        $failedItem.append( $('') .attr('href', failedTitle.getUrl) .text(failed[x]) );                   } else {                        $failedItem.text(failed[x]);                    }                    $failedItem.append(document.createTextNode(': ' + error[x]));                    $failedList.append($failedItem);                }                $('#wpMassDeleteFailedContainer')                    .append($(' '))                    .append($('') .text('Failed deletions:') )                   .append($failedList);            }        }); }   function massdeleteform { var bodyContent = (config.skin == "cologneblue" ? "article" : "bodyContent"); document.getElementsByTagName("h1")[0].textContent = "Mass delete pages"; document.title = "Mass delete pages"; document.getElementById(bodyContent).innerHTML = 'Lifted From Wikimedia Incubator - revision of 2015-10-28 ' +       ' ' +            '' + ' Welcome to the mass deletion form, the page which you can use to mass delete a couple of pages. If you are deleting multiple pages that were created by one user, please use Special:Nuke! ' +           ' If you are deleting more than 10 pages, please flag yourself as a bot first to avoid flooding Special:RecentChanges, thank you! ' +           ' Please add the list of pages you would like to delete in the text box below, listing one page in one line. Thank you for using this tool! ' +           ' ' +            ' ' +                'Pages to delete (one on each line, please): ' +                   ' ' + ' ' +                   '  Common reasons: ' + ' ' + '' + ' Other reason ' + ' ' +                           '' + '<optgroup label="Common delete reasons">' + '<option value="Author request">Author request ' + '<option value="Beyond scope">Beyond scope ' + '<option value="Copyright violation">Copyright violation ' + '<option value="Unneeded redirect">Unneeded redirect(s) ' + '<option value="Vandalism">Vandalism ' + ' ' +                               '<optgroup label="Routine maintenance">' + '<option value="Process deletion">Process deletion ' + '<option value="Orphaned talkpage">Orphaned talkpage ' + '<option value="Redundant/extraneous after bulk-action">Redundant/extraneous after bulk-action ' + '<option value="Source file status change">Source file status change ' + '<option value="Superior transcription exists">Superior transcription exists ' + ' ' +                           ' ' +                        '  ' +                    '  Other/additional reason: ' + ' <input type="text" id="wpMassDeleteReason" name="wpMassDeleteReason" size="42" maxlength="255" /> ' + ' <input type="button" id="wpMassDeleteSubmit" name="wpMassDeleteSubmit" value="Delete" /> ' + ' ' +       ' ';        document.getElementById("wpMassDeleteReasons").onchange = function { var maxlength = (document.getElementById("wpMassDeleteReasons").value == "other" ? 255 : 252-document.getElementById("wpMassDeleteReasons").value.length); //It's 252 because of the three characters (" ") in addition to the selected summary. document.getElementById("wpMassDeleteReason").setAttribute("maxlength", maxlength); };       document.getElementById("wpMassDeleteSubmit").addEventListener("click", function (e) {            doMassDelete;        }); }   if (config.wgNamespaceNumber == -1 &&        config.wgTitle.toLowerCase == "massdelete" &&        /sysop/.test(config.wgUserGroups)    ) { massdeleteform; }

});