User:XXN/massrestore.js

/*global $, mw */ ///An updated version of User:Timotheus Canens/massrestore.js (adapted from User:Animum/massdelete.js)

if (mw.config.get("wgNamespaceNumber") == -1       && mw.config.get("wgTitle").toLowerCase == "massrestore"        && /sysop/.test(mw.config.get("wgUserGroups"))) { /**    * Mediawiki takes WAY too long for the document ready to load; this will * listen for the actual body being loaded before that. */   new Promise(resolve => {        $(resolve);        var interval = window.setInterval(function  { if ($("#footer")[0] && $("#mw-navigation")[0]) { resolve; window.clearInterval(interval); }       }, 100);    }).then(function  {        $("h1").html("Mass-restoration tool");        document.title = "Tim's mass-restoration tool - Wikipedia, the free encyclopedia";        $(mw.config.get("skin") == "cologneblue" ? "#article" : "#bodyContent").html( "From Wikipedia, the free encyclopedia

"               + ""                + "If you abuse this tool, it's your fault, not mine."                + " "                + "

"               + "Pages to restore(one on each line, please): "               + " "                + "

"               + "  Reason: "                + "   "                + "   " + " ");        $("#wpMassRestoreSubmit").click(function  {            $("#wpMassRestoreSubmit").attr("disabled", true);            var wpMassRestoreReason = $("#wpMassRestoreReason").val;            var restored = 0;            var errors = {};            $.ajax({                url: mw.config.get("wgScriptPath") + "/api.php",                data: {                    format: "json",                    action: "query",                    prop: "info",                    intoken: "edit",                    titles: $("#wpMassRestorePages").val.split("\n").join("|") }           }).then(function (data) { Promise.all($.map(data.query.pages, function (info) { function fail(e) { errors[info.title] = e;                   } return $.ajax({                       method: "post",                        url: mw.config.get("wgScriptPath") + "/api.php",                        data: {                            format:"json",                            action: "undelete",                            reason: wpMassRestoreReason,                            token: info.edittoken,                            title: info.title                        }                    }).then(function (subdata) {                        // If restored, update the restored count and the button.                        if (subdata.undelete) {                            $("#wpMassRestoreSubmit").val(++restored);                        } else {                            fail(subdata.error.info);                        }                    }, fail); })).then($.noop, $.noop).then(function {                    $("#wpMassRestoreSubmit").val("Done (" + restored + ")");

if (!$.isEmptyObject(errors)) { var ul = $(""); $.each(errors, function (title, e) {                           ul.append("" + title + ": " + e + "");                        }); $("#wpMassRestoreFailedContainer").html(                       		" Failed restorations: ").append(ul); }               });            });        });    }); }