Jump to content

User:Ahecht/Scripts/massmove.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ahecht (talk | contribs) at 14:20, 19 March 2015 (Created page with '// Adapted from User:Plastikspork/massmove.js function doMassMove() { document.getElementById("wpMassMoveSubmit").disabled = true; var articles = doc...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Adapted from [[User:Plastikspork/massmove.js]]
function doMassMove() {
    document.getElementById("wpMassMoveSubmit").disabled = true;
    var articles = document.getElementById("wpMassMovePages").value.split("\n");
    if(articles.length < 1) return;
    var wpMassMoveReason = document.getElementById("wpMassMoveReason").value;
    var wpMassMovePrefix1 = document.getElementById("wpMassMovePrefix1").value;
    var wpMassMovePrefix2 = document.getElementById("wpMassMovePrefix2").value;
    var wpMassMoveMoveTalk = document.getElementById("wpMassMoveMoveTalk").checked;
    var wpMassMoveNoRedirect = document.getElementById("wpMassMoveNoRedirect").checked;
    var wpMassMoveWatch = document.getElementById("wpMassMoveWatch").value;
    var moved = 0, failed = new Array(), error = new Array();
    for(i=0;i<articles.length;i++) {
        var article = articles[i];
        if(article.length > 0) {
            var req = sajax_init_object();
            req.open("GET", wgScriptPath + "/api.php?format=json&action=query&prop=info&intoken=move&titles=" + encodeURIComponent(article), false);
            req.send(null);
            var response = eval("(" + req.responseText + ")").query.pages;
            for(var index in response) {
                var info = response[index];
                var movetoken = info.movetoken;
                var moveto = wpMassMovePrefix2 + article.replace(new RegExp('^' + wpMassMovePrefix1, ""), '');
                var postdata = "format=json"
                             + "&action=move"
                             + "&movetalk=" + (wpMassMoveMoveTalk ? '1' : '0')
                             + "&noredirect=" + (wpMassMoveNoRedirect ? '1' : '0')
                             + "&watchlist=" + wpMassMoveWatch
                             + "&from=" + encodeURIComponent(article)
                             + "&to=" + encodeURIComponent(moveto)
                             + "&reason=" + encodeURIComponent(wpMassMoveReason)
                             + "&token=" + encodeURIComponent(movetoken);
                req = sajax_init_object();
                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 + ")")['move']) { //If moved, update the moved count and the button.
                    moved++;
                    document.getElementById("wpMassMoveSubmit").value = "(" + moved + ")";
                } else { //If not moved, 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("wpMassMoveSubmit").value = "Done (" + moved + ")";
            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("wpMassMoveFailedContainer").innerHTML += '<br /><b>Failed moves:</b><ul>' + linkedList + '</ul>';
            }
 
        }
    }
}
 
function massmoveform() {
    var bodyContent = (skin == "cologneblue" ? "article" : "bodyContent");
    document.getElementsByTagName("h1")[0].textContent = "Plastikspork's mass-move tool";
    document.title = "Plastikspork's mass-move tool - Wikipedia, the free encyclopedia";
    document.getElementById(bodyContent).innerHTML = '<h3 id="siteSub">From Wikipedia, the free encyclopedia</h3>'
        + '<p>Adapted from Animum\'s mass-delete tool and Timotheus Canens\'s mass-edit tool</p>'
        + '<br /><br />'
        + '<form id="wpMassMove" name="wpMassMove">'
        + '<b>If you abuse this tool, it\'s <i>your</i> fault, not mine.</b>'
        + '<div id="wpMassMoveFailedContainer"></div>'
        + '<br /><br />'
            + 'Pages to move (one on each line, please):<br />'
                + '<textarea tabindex="1" accesskey="," name="wpMassMovePages" id="wpMassMovePages" rows="10" cols="80"></textarea>'
            + '<br /><br /><table style="background-color:transparent">'
            + '<tr><td>Prefix to remove from the old name (e.g., Template:):</td>'
                + '<td><input type="text" id="wpMassMovePrefix1" name="wpMassMovePrefix1" maxlength="255" /></td></tr>'
            + '<tr><td>Prefix to add to the new name (e.g., User:Plastikspork/):</td>'
                + '<td><input type="text" id="wpMassMovePrefix2" name="wpMassMovePrefix2" maxlength="255" /></td></tr>'
            + '<tr><td>Move talk:</td>'
                + '<td><input type="checkbox" id="wpMassMoveMoveTalk" name="wpMassMoveMoveTalk"/></td></tr>'
            + '<tr><td>No redirect:</td>'
                + '<td><input type="checkbox" id="wpMassMoveNoRedirect" name="wpMassMoveNoRedirect"/></td></tr>'
            + '<tr><td>Watch Pages:</td>'
                    + '<td><select id="wpMassMoveWatch">'
                        + '<option value="nochange">No change</option>'
                        + '<option value="preferences">User preferences</option>'
                        + '<option value="watch">Add to watch list</option>'
                        + '<option value="unwatch">Remove from watch list</option>'
                    + '</select></td></tr>'
            + '<tr><td>Edit summary:</td>'
                + '<td><input type="text" id="wpMassMoveReason" name="wpMassMoveReason" maxlength="255" /></td></tr>'
                + '<tr><td><input type="button" id="wpMassMoveSubmit" name="wpMassMoveSubmit" value="Move" onclick="doMassMove()" /></td>'
        + '</form>';
    document.getElementById("wpMassMoveReasons").onchange = function() {
        var maxlength = (document.getElementById("wpMassMoveReasons").value == "other" ? 255 : 252-document.getElementById("wpMassMoveReasons").value.length); //It's 252 because of the three characters (" ()") in addition to the selected summary.
        document.getElementById("wpMassMoveReason").setAttribute("maxlength", maxlength);
    }
}
 
if(wgNamespaceNumber == -1 && (wgPageName == "Special:Massmove" || wgPageName == "Special:MassMove")) addOnloadHook(massmoveform);