User:Samuele2002/Massrestore.js

From Wikipedia

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
 * Copy from [[w:User:Timotheus_Canens/massprotect.js]]
 * https://en.wikipedia.org/wiki/User:Timotheus_Canens/massprotect.js
 * [[w:Special:PermaLink/851214226]]
 * @license 
 * @author [[w:en:User:Timotheus_Canens]]
 */
 ///Adapted from [[User:Animum/massdelete.js]]
function doMassRestore() {
    document.getElementById("wpMassRestoreSubmit").disabled = true;
    var articles = document.getElementById("wpMassRestorePages").value.split("\n");
    if(articles.length == 0) return;
    var wpMassRestoreReason = document.getElementById("wpMassRestoreReason").value, restored = 0, failed = new Array(), error = new Array();
    for(i=0;i<articles.length;i++) {
        var article = articles[i];
        if(article.length > 0) {
            var req = new XMLHttpRequest();
            req.open("GET", wgScriptPath + "/api.php?format=json&action=query&prop=info&intoken=edit&titles=" + encodeURIComponent(article), false);
            req.send(null);
            var response = eval("(" + req.responseText + ")").query.pages;
            for(var index in response) {
                var info = response[index];
                var token = info.edittoken;
                var postdata = "format=json"
                             + "&action=undelete"
                             + "&title=" + encodeURIComponent(article)
                             + "&reason=" + encodeURIComponent(wpMassRestoreReason)
                             + "&token=" + encodeURIComponent(token);
                var req = new XMLHttpRequest();
                req.open("POST", wgScriptPath + "/api.php", false);
                req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                req.setRequestHeader("Content-length", postdata.length);
                req.send(postdata);
                if(eval("(" + req.responseText + ")").undelete) { //If restored, update the restored count and the button.
                    restored++;
                    document.getElementById("wpMassRestoreSubmit").value = "(" + restored + ")";
                } else { //If not deleted, add the title to the "failed" array and a description of the error to the "error" array.
                    failed.push(article);
                    error.push(eval("(" + req.responseText + ")").error.info);
                }
            }
        }
        if(!articles[i+1]) {
            document.getElementById("wpMassRestoreSubmit").value = "Done (" + restored + ")";
            if(failed.length > 0) {
                var linkedList = "";
                for(x=0; x<failed.length; x++) {
                    linkedList += "<li><a href=\"" + wgScript + "?title=" + encodeURIComponent(failed[x]) + "\">" + failed[x] + "</a>: " + error[x] + "</li>"; //Links the titles in the "failed" array
                }
                document.getElementById("wpMassRestoreFailedContainer").innerHTML += '<br /><b>Failed deletions:</b><ul>' + linkedList + '</ul>';
            }
            
        }
    }
}
 
function massrestoreform() {
    var bodyContent = (skin == "cologneblue" ? "article" : "bodyContent");
    document.getElementsByTagName("h1")[0].textContent = "Tim's mass-restoration tool";
    document.title = "Tim's mass-restoration tool - Wikipedia, the free encyclopedia";
    document.getElementById(bodyContent).innerHTML = '<h3 id="siteSub">From Wikipedia, the free encyclopedia</h3><br /><br />'
        + '<form id="wpMassRestore" name="wpMassRestore">'
        + '<b>If you abuse this tool, it\'s <i>your</i> fault, not mine.</b>'
        + '<div id="wpMassRestoreFailedContainer"></div>'
        + '<br /><br />'
            + 'Pages to restore(one on each line, please):<br />'
                + '<textarea tabindex="1" accesskey="," name="wpMassRestorePages" id="wpMassRestorePages" rows="10" cols="80"></textarea>'
            + '<br /><br /><table style="background-color:transparent">'
            + '<tr><td>Reason:</td>'
                + '<td><input type="text" id="wpMassRestoreReason" name="wpMassRestoreReason" maxlength="255" /></td></tr>'
                + '<tr><td><input type="button" id="wpMassRestoreSubmit" name="wpMassRestoreSubmit" value="Restore" onclick="doMassRestore()" /></td>'
        + '</form>';
}
 
if(mw.config.get("wgNamespaceNumber") == -1 && (mw.config.get("wgPageName") == "Special:Massrestore" || mw.config.get("wgPageName") == "Special:MassRestore") && /sysop/.test(mw.config.get("wgUserGroups"))) $(massrestoreform);