Jump to content

User:Animum/moveRV.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Animum (talk | contribs) at 20:08, 11 August 2008 (create). 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.
var UrlParameters = new Array ();
 
function readparams() {
  var asReadInUrlParameters;
  var asReadInUrlParameter;
 
  // Get URL parameters
  asReadInUrlParameters = location.search.substring(1, location.search.length).split("&");
  for (i = 0; i < asReadInUrlParameters.length; i++) {
    asReadInUrlParameter = asReadInUrlParameters[i].split("=");
    UrlParameters[decodeURIComponent(asReadInUrlParameter[0])] = decodeURIComponent(asReadInUrlParameter[1]);
  }
}

readparams();

function formatResponse(response) { //This bit was stolen from Gracenotes.  Thanks, Gracenotes!
 	try {
 		response = response.query.pages;
 		for (var property in response)
 			return response[property];
 	} catch (e) {
 	}
}

function zeroPad(str) {
    return ("0" + str).slice(-2);
}

function revertmoves() { //A _HEAVILY_ modified version of Voice of All's move-reversion script.
    document.getElementById("contentSub").innerHTML += "<br /><b>Working... (this may take a while)</b>";
    var l = document.getElementById('bodyContent').getElementsByTagName('li');
    var NEXT_INDEX = 0;
    var completedMoves = 0;
    var completedDeletions = 0;
    for (var i=NEXT_INDEX; i < l.length; i++) {  
        var t = l[i].innerHTML;
        if (t.indexOf('moved') != -1) {
            var li_a = l[i].getElementsByTagName('a');
            for (k = (/sysop/.test(wgUserGroups) ? 6 : 5);k<li_a.length;k++)  { // Cheap, etc., but works.
                if(li_a[k].href.indexOf('title=Special:MovePage&wpOldTitle=') != -1) {
                    var article = li_a[k].href.split("wpNewTitle=")[1].split("&")[0];
                    var req = sajax_init_object();
                    req.open("GET", wgScriptPath + "/api.php?format=json&action=query&prop=revisions&titles=" + article + "&rvprop=comment&limit=1", false);
                    req.send(null);
                    var pageInfo = formatResponse(eval("(" + req.responseText + ")"));
                    if(pageInfo.revisions != undefined) {
                        if(pageInfo.revisions[0].comment.indexOf("moved [[" + decodeURIComponent(li_a[k].href.split("wpOldTitle=")[1].split("&")[0].replace(/_/g, " ")) + "]] to [[" + decodeURIComponent(article.replace(/_/g, " ")) + "]]") != -1) {
                            var shouldAbort = true; //Has already been reverted.
                        } else {
                            shouldAbort = false;
                        }
                    } else {
                        shouldAbort = false; //If page doesn't exist, we don't want to move it.
                    }
                    var article2 = li_a[k].href.split("wpOldTitle=")[1].split("&")[0];
                        var req = sajax_init_object();
                        req.open("GET", wgScriptPath + "/api.php?format=json&action=query&prop=revisions&titles=" + article2 + "&rvprop=comment&limit=1", false);
                        req.send(null);
                            var pageInfo = formatResponse(eval("(" + req.responseText + ")"));
                            if(pageInfo.revisions == undefined) {
                                 shouldAbort = true; //Page to move doesn't exist.
                            }
                        var req = sajax_init_object();
                        req.open("GET", wgScriptPath + "/api.php?format=json&action=query&prop=info&intoken=move|delete&titles=" + li_a[k].href.split("&wpOldTitle=")[1].split("&wpNewTitle")[0], false);
                        req.send(null);
                            var summary = "Script-assisted page-move revert.";
                            var info = formatResponse(eval("(" + req.responseText + ")"));
                            var edittoken = info.movetoken;
                            var deletetoken = info.deletetoken;
              	            var postdata = li_a[k].href.split("title=Special:MovePage&")[1].replace(/revert/g, encodeURIComponent(summary))
                                       + "&wpEditToken=" + encodeURIComponent(edittoken)
                                       + "&wpMove=Move+page"
                                       + "&wpConfirm=1";
                        var req = sajax_init_object();
                        req.open("POST", wgScriptPath + "/index.php?title=Special:MovePage&action=submit", false);
                        req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                        req.setRequestHeader("Content-length", postdata.length);     
                        if(shouldAbort == false) { //If nobody else has already reverted, we do                   
                            req.send(postdata);
 
                            var dt = new Date();
                            var timestamp = dt.getUTCFullYear() + zeroPad(dt.getUTCMonth() + 1) + zeroPad(dt.getUTCDate()) + zeroPad(dt.getUTCHours()) + zeroPad(dt.getUTCMinutes()) + zeroPad(dt.getUTCSeconds());
 
                            var div = document.createElement("div");
                            div.id = "completedMoveCount";
                            document.getElementById("contentSub").appendChild(div);
 
                            var req = sajax_init_object();
                            req.open("GET", wgScriptPath + "/api.php?format=json&action=query&list=logevents&letype=move&leuser=" + wgUserName + "&letitle=" + unescape(article2) + "&lelimit=1&ledir=newer&lestart=" + timestamp, false);
                            req.send(null);
                            if(eval("(" + req.responseText + ")").query.logevents[0]) {
                                if(eval("(" + req.responseText + ")").query.logevents[0].user == wgUserName && eval("(" + req.responseText + ")").query.logevents[0].comment == summary) {

                                    var delsummary = "Cleanup of redirects created from page-move vandalism.";
                                    var postdata = "wpReason=" + encodeURIComponent(delsummary)
                                                 + "&wpEditToken=" + encodeURIComponent(deletetoken);
                                    var req = sajax_init_object();
                                    req.open("POST", wgScript + "?title=" + article2 + "&action=delete", false);
                                    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                                    req.setRequestHeader("Content-length", postdata.length);
                                    req.send(postdata);

                                    completedMoves = completedMoves+1;
                                    document.getElementById("completedMoveCount").innerHTML = "<b>" + completedMoves + (completedMoves == 1 ? " move and deletion has" : " moves and deletions have") + " been completed.</b>";
                                }
                            }
                        }
                }
              	NEXT_INDEX = NEXT_INDEX+1;
                break;
            }
        }
    }
    document.getElementById("contentSub").innerHTML += "<b>Cleanup finished.</b>"; //Done.
}

function doMoveRV() {
    var domove = confirm("All of the moves listed on this page will be reverted, and the resulting redirects will be deleted.\n\nClick \"OK\" to proceed or \"Cancel\" to abort.";
    if(domove == true) {
        revertmoves();
    } else {
        return;
    }
}

addOnloadHook(function() {
    if(wgNamespaceNumber == -1 && wgCanonicalSpecialPageName == "Log" && UrlParameters["type"] == "move") {
        addPortletLink("p-cactions", "javascript:doMoveRV()", "pagemove", "ca-pagemove");
    }
});