User:Splarka/ajaxbatchdelete.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.

/* Ajax batch delete thingy, version [0.0.1a]
Originally from: http://en.wikipedia.org/wiki/User:Splarka/ajaxbatchdelete.js
 
To do:
* Better checking on deletion, currently just checks if the response contains the deletion form
* Better XML error reporting?
* Better list handling (lazily stores list in the textarea currently)
* Possibly could recycle delete tokens (have to verify)
* MORE TESTING
*/
 
addOnloadHook(function() {
  addPortletLink('p-tb','/wiki/Special:AjaxBatchDelete','Batch Delete');
});
 
if(wgPageName == 'Special:AjaxBatchDelete') {
  document.title = 'Ajax Batch Deletion';
  addOnloadHook(abdForm);
}
 
function abdForm() {
  //subvert this Special: page to our own needs.
  var con = (document.getElementById('content')) ? document.getElementById('content') : document.getElementById('mw_content')
  var bcon = (document.getElementById('bodyContent')) ? document.getElementById('bodyContent') : document.getElementById('mw_contentholder')
  var fh = getElementsByClassName(con,'h1','firstHeading')[0];
  while(fh.firstChild) fh.removeChild(fh.firstChild)
  fh.appendChild(document.createTextNode('Ajax Batch Deletion'));
  for(var i=0;i<bcon.childNodes.length;i++) {
    bcur = bcon.childNodes[i];
    if(bcur.id != 'siteSub' && bcur.id != 'contentSub' && bcur.className != 'visualClear') {
      while(bcur.firstChild) bcur.removeChild(bcur.firstChild)
      if(bcur.nodeType == 3) bcur.nodeValue = '';
    }
  }
 
  //generate content
  var form = document.createElement('form');
   form.appendChild(document.createTextNode('List of pages to delete:'));
   form.appendChild(document.createElement('p'));
   var txt = document.createElement('textarea');
    txt.style.height = '20em';
    txt.style.width = '50%';
    txt.setAttribute('id','adb-textarea');
   form.appendChild(txt);
   form.appendChild(document.createElement('p'));
   var lab1 = document.createElement('label');
    lab1.setAttribute('for','adb-reason')
    lab1.appendChild(document.createTextNode('Deletion reason: '));
   form.appendChild(lab1);
   var inp1 = document.createElement('input');
    inp1.style.width = '20em';
    inp1.setAttribute('type','text');
    inp1.setAttribute('id','adb-reason');
   form.appendChild(inp1);
   form.appendChild(document.createElement('p'));
   var sub1 = document.createElement('input');
    sub1.setAttribute('type','button');
    sub1.setAttribute('id','adb-button1');
    sub1.setAttribute('value','start');
    sub1.setAttribute('onclick','abdStart()');
   form.appendChild(sub1);
  bcon.appendChild(form);
  var pre = document.createElement('pre');
   pre.setAttribute('id','adb-output');
  bcon.appendChild(pre);
}
 
function abdStart(previousResult) {
  var txt = document.getElementById('adb-textarea');
  txt.setAttribute('disabled','disabled');
  if(previousResult) {
    txt.value = txt.value.replace(/.*(\n|$)/,'');
    var pre = document.getElementById('adb-output');
    pre.appendChild(document.createTextNode('* ' + previousResult + '\n'));
  }
  if(txt.value == '') {
    txt.removeAttribute('disabled');
  } else {
    var list = txt.value.split('\n');
    getDeleteToken(list[0]);
  }
}
 
function adbDelete(page,token) {
  var url = wgServer + wgScriptPath + '/index.php?title=' + page + '&action=delete'
  var data = 'wpReason=' + encodeURIComponent(document.getElementById('adb-reason').value) + '&wpEditToken=' + encodeURIComponent(token)
  postXML(url,data,page);
}
 
function getDeleteToken(page) {
  var url = wgServer + wgScriptPath + '/api.php?action=query&format=xml&prop=info&intoken=delete&titles=' + encodeURIComponent(page);
  if (window.XMLHttpRequest) { // Non-IE browsers
    var req = new XMLHttpRequest();
    if(req.overrideMimeType) req.overrideMimeType('text/html')
  } else if (window.ActiveXObject) { // IE
    var req = new ActiveXObject('Microsoft.XMLHTTP');
  }
  if(req) {
    req.onreadystatechange = function() {
      if(req.readyState == 4) {
        if(req.status == 200) {
          var txt = req.responseText;
          var token = txt.match(/deletetoken\=\"[0-9a-f]{32}\+\\\"/)[0];
          token = token.match(/[0-9a-f]{32}\+\\/)[0];
          adbDelete(page,token);
         } else {
          alert('Error fetching token: ' + req.statusText);
        }
      }
    }
    try {
      req.open('GET', url, true);
      req.send('');
    } catch (e) {
      alert('Error:' + e);
    }
  } else {
    alert('XMLHTTPRequest not supported');
  }
}
 
function postXML(url,data,page) {
  if (window.XMLHttpRequest) { // Non-IE browsers
    var req = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // IE
    var req = new ActiveXObject('Microsoft.XMLHTTP');
  }
  if(req) {
    req.onreadystatechange = function() {
      if(req.readyState == 4) {
        if(req.status == 200) {
          var result = decodeURIComponent(page) + ' - '
          result += (req.responseText.search(/id\=\"deleteconfirm\"/) == -1) ? 'probably deleted' : 'not deleted'
          abdStart(result);
        } else {
          abdStart('Error: ' + req.statusText);
        }
      }
    }
    try {
      req.open('POST', url, true);
      req.setRequestHeader('Method', 'POST ' + url + ' HTTP/1.1');
      req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
      req.setRequestHeader('Content-length', data.length);
      req.setRequestHeader('Connection', 'close');
      req.send(data);
    } catch (e) {
      abdStart('Error: ' + e);
    }
  } else {
    alert('XMLHTTPRequest not supported');
  }
}