User:Syunsyunminmin/massdelete.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// forked version of enWS Gadget massdelete. Set for global sysops to mass delete
mw.loader.using(['mediawiki.api', 'mediawiki.Title'], function () {
    "use strict";

    var config = mw.config.get(['wgNamespaceNumber', 'wgTitle', 'wgUserGroups', 'wgGlobalGroups', '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 = $('<ul>');
                for(var x = 0; x < failed.length; x++) {
                    // Link the titles in the "failed" array
                    var failedTitle = mw.Title.newFromText(failed[x]);
                    var $failedItem = $('<li>');
                    if (failedTitle) {
                        $failedItem.append( $('<a>')
                            .attr('href', failedTitle.getUrl())
                            .text(failed[x])
                        );
                    } else {
                        $failedItem.text(failed[x]);
                    }
                    $failedItem.append(document.createTextNode(': ' + error[x]));
                    $failedList.append($failedItem);
                }
                $('#wpMassDeleteFailedContainer')
                    .append($('<br />'))
                    .append($('<b>')
                        .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 = '<h3 id="siteSub">Lifted From Wikimedia Incubator - revision of 2015-10-28</h3><br />' +
        '<div>' +
            '<form id="wpMassDelete" name="wpMassDelete">' +
            '<p>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 <a href="/wiki/Special:Nuke">Special:Nuke</a>!</p>' +
            '<p>If you are deleting <b>more than 10 pages</b>, please <a href="/wiki/Special:UserRights">flag yourself</a> as a bot first to avoid flooding <a href="/wiki/Special:RecentChanges">Special:RecentChanges</a>, thank you!</p>' +
            '<p>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!</p>' +
            '<div id="wpMassDeleteFailedContainer"></div>' +
            '<br />' +
                'Pages to delete (one on each line, please):<br />' +
                    '<textarea tabindex="1" accesskey="," name="wpMassDeletePages" id="wpMassDeletePages" rows="15" cols="80"></textarea>' +
                '<br /><table style="background-color:transparent">' +
                    '<tr><td>Common reasons:</td>' +
                        '<td><select id="wpMassDeleteReasons">' +
                            '<optgroup label="Other reason">' +
                                '<option value="other">Other reason</option>' +
                            '</optgroup>' +
                            '<optgroup label="Criteria for speedy deletion">' +
                                '<optgroup label="Common delete reasons">' +
                                    '<option value="Author request">Author request</option>' +
                                    '<option value="Beyond scope">Beyond scope</option>' +
                                    '<option value="Copyright violation">Copyright violation</option>' +
                                    '<option value="Unneeded redirect">Unneeded redirect(s)</option>' +
                                    '<option value="Vandalism">Vandalism or spam</option>' +
                                '</optgroup>' +
                                '<optgroup label="Routine maintenance">' +
                                    '<option value="Request at [[m:Steward requests/Miscellaneous]]">Steward requests/Miscellaneous</option>' +
                                    '<option value="Process deletion">Process deletion</option>' +
                                    '<option value="Orphaned talkpage">Orphaned talkpage</option>' +
                                    '<option value="Redundant/extraneous after bulk-action">Redundant/extraneous after bulk-action</option>' +
                                '</optgroup>' +
                            '</optgroup>' +
                        '</select></td></tr>' +
                    '<tr><td>Other/additional reason:</td>' +
                    '<td><input type="text" id="wpMassDeleteReason" name="wpMassDeleteReason" size="42" maxlength="255" /></td></tr>' +
                    '<tr><td><input type="button" id="wpMassDeleteSubmit" name="wpMassDeleteSubmit" value="Delete" /></td>' +
            '</form>' +
        '</div>';
        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" &&
        (/global-sysop/.test(config.wgGlobalGroups) || /sysop/.test(config.wgUserGroups))
    ) {
        massdeleteform();
    }

});