বিষয়বস্তুতে চলুন

মিডিয়াউইকি:Gadget-massdelete.js

উইকিবই থেকে

লক্ষ্য করুন: প্রকাশ করার পর, পরিবর্তনগুলো দেখতে আপনাকে আপনার ব্রাউজারের ক্যাশে পরিষ্কার করার প্রয়োজন হতে পারে।

  • ফায়ারফক্স / সাফারি: পুনরায় লোড-এ ক্লিক করার সময় শিফট টিপে ধরে রাখুন, অথবা হয় Ctrl-F5 বা Ctrl-R টিপুন (ম্যাকে ⌘-R টিপুন)
  • গুগল ক্রোম: Ctrl-Shift-R (ম্যাকে ⌘-Shift-R) টিপুন
  • এজ: Ctrl ধরে রাখা অবস্থায় Refresh-এ ক্লিক করুন, অথবা Ctrl-F5 টিপুন।
  • অপেরা: Ctrl-F5 টিপুন।
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 = $('<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</option>' +
                                '</optgroup>' +
                                '<optgroup label="Routine maintenance">' +
                                    '<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>' +
                                    '<option value="Source file status change">Source file status change</option>' +
                                    '<option value="Superior transcription exists">Superior transcription exists</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" &&
        /sysop/.test(config.wgUserGroups)
    ) {
        massdeleteform();
    }

});