User:Kanonkas/moverevert2.js

if (wgPageName == "Special:Log") { function addmoveRevertTab { addPortletLink('p-cactions', 'javascript:moverevert2_main', "Revert moves sup", "ca-moverevert", "Revert"); } addOnloadHook(addmoveRevertTab); function moverevert2_main { jsMsg(' '); if (wgPageName == "Special:Log" && document.URL.indexOf('&page=') != -1) { var user = document.URL.substring(document.URL.indexOf('&user=')+6,document.URL.indexOf('&page=')); } else if (wgPageName == "Special:Log"){ var user = document.URL.substring(document.URL.indexOf('&user=')+6); } else { var user = encodeURIComponent(wgTitle); } btoken = gwp_getBlockToken; gwp_blockUser(user, btoken); token = gwp_getToken; moves = gwp_getMoveLog(user); for (var i=0; iDone'; } function gwp_getToken { document.getElementById('movervinfo').innerHTML += 'Getting token'; var req = sajax_init_object; req.open("GET", wgScriptPath + "/api.php?action=query&prop=info&indexpageids=1&intoken=edit&format=json&titles=Foo", false); req.send(null); var response = eval('(' + req.responseText + ')'); pageid = response['query']['pageids'][0]; token = response['query']['pages'][pageid]['edittoken']; delete req; document.getElementById('Gwp_gettoken').innerHTML = 'Got token'; return token; } function gwp_getMoveLog(user) { document.getElementById('movervinfo').innerHTML += 'Getting moves from user ' + user +''; var url = wgScriptPath + "/api.php?action=query&list=logevents&leuser="+user+"&letype=move&lelimit=30&leprop=title|details&format=json" var req = sajax_init_object; req.open("GET", url, false); req.send(null); var response = eval('(' + req.responseText + ')'); var moves = new Array; for (var i=0; iMoving '+movefrom+' to '+moveto+''; var req = sajax_init_object; var params = "action=move&from="+encodeURIComponent(movefrom)+"&to="+encodeURIComponent(moveto)+"&token="+encodeURIComponent(token)+"&reason=G3:%20page-move%20vandalism&noredirect=1&format=json"; url = wgScriptPath + "/api.php"; req.open("POST", url, false); req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); req.setRequestHeader("Content-length", params.length); req.setRequestHeader("Connection", "close"); req.onreadystatechange = function { if(req.readyState == 4 && req.status == 200) { response = eval('(' + req.responseText + ')'); try { if (response['move']['to']) { document.getElementById('Gwp_move'+escape(movefrom)).innerHTML = 'Moved '+movefrom+' to '+moveto; gwp_deletePage(movefrom.replace(/'/g, "\\'").replace(/"/g, '\\"'), encodeURIComponent(token) ); }     } catch(err) { document.getElementById('Gwp_move'+escape(movefrom)).innerHTML = 'Move from '+movefrom+' to '+moveto+' failed. Error info:' +response['error']['code'] + ' : ' + response['error']['info']; if (response['error']['code'] == "articleexists") { setTimeout("gwp_deletePage('"+movefrom.replace(/'/g, "\\'").replace(/"/g, '\\"')+"', '"+encodeURIComponent(token)+"')", 500); }     }    }  }  req.send(params) } function gwp_getBlockToken { document.getElementById('movervinfo').innerHTML += 'Getting block token'; var req = sajax_init_object; var params = "action=block&gettoken=1&format=json"; req.open("POST", wgScriptPath + "/api.php?", false); req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); req.setRequestHeader("Content-length", params.length); req.setRequestHeader("Connection", "close"); req.send(params); response = eval('(' + req.responseText + ')'); token = response['block']['blocktoken']; delete req; document.getElementById('Gwp_btoken').innerHTML = 'Got block token'; return token; } function gwp_blockUser(user, token) { document.getElementById('movervinfo').innerHTML+= "Blocking " + user + ""; var req = sajax_init_object; var params = "action=block&format=json&token="+encodeURIComponent(token)+"&user="+user+"&expiry=never&reason=Vandalism-only account used for page-move vandalism&nocreate=1&autoblock=1&noemail=1"; url = wgScriptPath + "/api.php"; req.open("POST", url, true); req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); req.setRequestHeader("Content-length", params.length); req.setRequestHeader("Connection", "close"); req.onreadystatechange = function { if(req.readyState == 4 && req.status == 200) { response = eval('(' + req.responseText + ')'); try { document.getElementById('blocking').innerHTML+= response['block']['user']+" has been blocked"; } catch(err) { document.getElementById('blocking').innerHTML+= "Error " + response['error']['info']; }     delete req; } }  req.send(params) } function gwp_deletePage(title, token) { document.getElementById('movervinfo').innerHTML += 'Deleting '+title+''; var req = sajax_init_object; var params = "action=delete&format=json&token="+token+"&title="+encodeURIComponent(title)+"&reason=G3%20:%20Page-move%20vandalism"; url = wgScriptPath + "/api.php"; req.open("POST", url, true); req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); req.setRequestHeader("Content-length", params.length); req.setRequestHeader("Connection", "close"); req.onreadystatechange = function { if(req.readyState == 4 && req.status == 200) { response = eval('(' + req.responseText + ')'); try { if (response['delete']['title']) { document.getElementById('Gwp_delete'+escape(title)).innerHTML = 'Deleted '+title; }     } catch(err) { document.getElementById('Gwp_delete'+escape(title)).innerHTML = 'Deletion error on '+title+ ' : ' +response['error']['info']; }   }  }  req.send(params) } }