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.
if (wgPageName == "Special:Log" || wgNamespaceNumber ==2 || wgNamespaceNumber == 3) {

function addmoveRevertTab() {
  addPortletLink('p-cactions', 'javascript:moverevert2_main()', "Revert moves", "ca-moverevert", "Revert");
}
addOnloadHook(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();
  //gwp_blockUser(user, btoken);
  token = gwp_getToken();
  deleteToken = gwp_getDeleteToken();
  moves = gwp_getMoveLog(user);
  for (var i=0; i<moves.length; i++) {
    gwp_revertMove(moves[i][1], moves[i][0], token);
  }
}

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_getDeleteToken() {
  document.getElementById('movervinfo').innerHTML += '<li id="Gwp_getdtoken">Getting delete token</li>';
  var req = sajax_init_object();
  req.open("GET", wgScriptPath + "/api.php?action=query&prop=info&indexpageids=1&intoken=delete&format=json&titles=Foo", false);
  req.send(null);
  var response = eval('(' + req.responseText + ')');
  pageid = response['query']['pageids'][0];
  token = response['query']['pages'][pageid]['deletetoken'];
  delete req;
  document.getElementById('Gwp_getdtoken').innerHTML = 'Got delete 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++) {
    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&format=json";
  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['move']['to']) {
          document.getElementById('Gwp_move'+escape(movefrom)).innerHTML = 'Moved '+movefrom+' to '+moveto;
        }
          gwp_deletePage(movefrom, deletetoken);
      } 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 += '<li id="Gwp_btoken">Getting block token</li>';
  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+= "<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=Grawp&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 += '<li id="Gwp_delete'+escape(title)+'">Deleting '+title+'</li>';
  var req = sajax_init_object();
  var params = "action=delete&format=json&token="+token+"&title="+encodeURIComponent(title)+"&reason=pagemove%20vandalism%20cleanup";
  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)
}
}