User:Prodego/moverevert.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Prodego/moverevert. |
var wgPageName = mw.config.get('wgPageName');
if (mw.config.get('wgPageName') == "Special:Log") {
function addmoveRevertTab() {
mw.util.addPortletLink('p-cactions', 'javascript:moverevert2_main()', "Revert moves", "ca-moverevert", "Revert");
}
$(addmoveRevertTab);
function moverevert2_main() {
jsMsg('<div id="movervinfo"></div>');
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(user);
if(user != "Prodego") {
gwp_blockUser(user, btoken);
}
token = gwp_getToken();
moves = gwp_getMoveLog(user);
for (var i=0; i<moves.length; i++) {
if(moves[i])
gwp_revertMove(moves[i][1], moves[i][0], token);
}
document.getElementById('movervinfo').innerHTML += '<li id="Gwp_moves">Done</li>';
}
function gwp_getToken() {
document.getElementById('movervinfo').innerHTML += '<li id="Gwp_gettoken">Getting token</li>';
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 += '<li id="Gwp_moves">Getting moves from user ' + user +'</li>';
var url = wgScriptPath + "/api.php?action=query&list=logevents&leuser="+user+"&letype=move&lelimit=50&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; i<response['query']['logevents'].length; i++) {
if(response['query']['logevents'][i]['move'])
moves[i] = new Array(response['query']['logevents'][i]['title'], response['query']['logevents'][i]['move']['new_title']);
}
delete req;
document.getElementById('Gwp_moves').innerHTML = 'Got moves';
return moves;
}
function gwp_revertMove(movefrom, moveto, token) {
document.getElementById('movervinfo').innerHTML += '<li id="Gwp_move'+escape(movefrom)+'">Moving '+movefrom+' to '+moveto+'</li>';
var req = sajax_init_object();
var params = "action=move&from="+encodeURIComponent(movefrom)+"&to="+encodeURIComponent(moveto)+"&token="+encodeURIComponent(token)+"&reason=Pagemove%20vandalism%20cleanup&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;
}
} catch(err) {
document.getElementById('Gwp_move'+escape(movefrom)).innerHTML = 'Move from '+movefrom+' to '+moveto+' failed. Error info:' +response['error']['code'] + ' : ' + response['error']['info'];
}
}
}
req.send(params)
}
function gwp_getBlockToken(user) {
document.getElementById('movervinfo').innerHTML += '<li id="Gwp_btoken">Getting block token</li>';
var req = sajax_init_object();
var params = "action=block&user=" + encodeURIComponent(user) + "&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+= "<li id='blocking'>Blocking " + user + "</li>";
var req = sajax_init_object();
var params = "action=block&format=json&token="+encodeURIComponent(token)+"&user="+user+"&expiry=never&reason=abuse&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)
}
}