User:Ozuzanna/nuke.js

/*
 * Nuke
 * @description reverse engineered version of the Nuke extension, rewritten in JavaScript
 * https://www.mediawiki.org/wiki/Extension:Nuke
 * Is faster than the original Nuke when deleting and won't freeze up when deleting many pages
 * @author Ozuzanna
 * @TODO fix files

if (mw.config.get('wgCanonicalSpecialPageName') === "Contributions" && mw.config.get('wgUserGroups').indexOf('sysop') != -1) $('#contentSub a:nth-child(8)').after(' | Nuke');

if (mw.config.get('wgCanonicalSpecialPageName') == "Blankpage" && $.getUrlVar('blankspecial') == "nuke" && mw.config.get('wgUserGroups').indexOf('sysop') != -1) { var articlePath = mw.config.get('wgArticlePath').slice(0,-2), mainpage = mw.config.get('wgMainpage'), token = mw.user.tokens.get('editToken'), Api = new mw.Api, RCTitles, escapedTitle, maxLimit, count; $('.AdminDashboardGeneralHeader h1').text('Nuke'); document.title = document.title.replace('Blank page','Nuke'); if ($.getUrlVar('nukeuser')) { var user = $.getUrlVar('nukeuser'), deleteReason = window.nukeDeleteReason || "Mass removal of pages created by " + user; $('#mw-content-text p').html('Switch to Nuke main form The following pages were created by ' + user + '; put in a comment and hit the button to delete them. Reason for deletion:  Delete Delete'); $('#nuke-status').html('Getting pages... please wait '); var UCJSON = { action: 'query', list: 'usercontribs', ucuser: user, uclimit: 5000 };   if ($.getUrlVar('nukenamespace')) UCJSON.ucnamespace = $.getUrlVar('nukenamespace');

Api.get(UCJSON) .done(function(d) {     if (!d.error) {        var usercontribs = d.query.usercontribs;        maxLimit = $.getUrlVar('nukelimit') || 5000;        count = 0;        for (var i in usercontribs) {          if (usercontribs[i].hasOwnProperty('new') && count < maxLimit) {            escapedTitle = usercontribs[i].title.replace(/"/g,'&#34;').replace(/'/g,"&#39;");            if (!$.getUrlVar('nukematch') || new RegExp($.getUrlVar('nukematch')).test(usercontribs[i].title)) {              $('#nuke-query-results').append(' ' + escapedTitle + '</li>');              count++;            }          }        }        if (!$('.nuke-query-result').length) {	  $('#nuke-status').html(' No results found ');        }        else { $('#nuke-status').empty; }     }      else { $('#nuke-status').html(' Failed to get query results: ' + d.error.code + ' '); }   })    .fail(function { $('#nuke-status').html(' Failed to get query results '); }); }  else {    $('#mw-content-text p').html('This tool allows for mass deletions of pages recently added by a given user or IP address. Input the username or IP address to get a list of pages to delete, or leave blank for all users. Username, IP address or blank: <input type="text" id="nuke-username"/> Pattern for the page name: <input type="text" id="nuke-match"/> Limit to namespace: <select id="nuke-namespace"> All Main  Talk  User  User talk  File  File talk  Template  Template talk  Help  Help talk  Category  Category talk  Maximum number of pages: <input type="text" id="nuke-max" value="500"/> Go</a> Delete</a>  Delete</a>');    $('#nuke-rc').click(function { if ($('#nuke-username').val) { var locationStr = articlePath + 'Special:Blankpage?blankspecial=nuke&nukeuser=' + $('#nuke-username').val;

if ($('#nuke-namespace').val != "all") locationStr += '&nukenamespace=' + $('#nuke-namespace').val;

if ($.isNumeric($('#nuke-max').val) && $('#nuke-max').val > 0) locationStr += '&nukelimit=' + $('#nuke-max').val;

if ($('#nuke-match').val) locationStr += '&nukematch=' + $('#nuke-match').val;

location.replace(locationStr); return; }

$('#nuke-query-results').empty;

var RCJSON = { action: 'query', list: 'recentchanges', rcnamespace: $('#nuke-namespace').val, rclimit: 5000 };

switch ($('#nuke-namespace').val) { case "all": RCJSON.rctype = 'new|log'; break;

case 6: RCJSON.rctype = 'log'; break;

default: RCJSON.rctype = 'new'; break; }     if ($('#nuke-namespace').val == "all") delete RCJSON.rcnamespace; $('#nuke-status').html('Getting pages... please wait <img src="http://slot1.images.wikia.nocookie.net/__cb62004/common/skins/common/progress-wheel.gif"/>');

Api.get(RCJSON) .done(function(d) {       if (!d.error) {          var recentchanges = d.query.recentchanges;          RCTitles = []; //to prevent putting dupes in the RC          maxLimit = $('#nuke-max').val || 5000;          count = 0;          for (var i in recentchanges) {	    if (recentchanges[i].type == "new" || (recentchanges[i].type == "log" && recentchanges[i].ns == 6) && $.inArray(recentchanges[i].title,RCTitles) == -1) {               if (count < maxLimit && (!$('#nuke-match').val || new RegExp($('#nuke-match').val).test(recentchanges[i].title))) {                count++;                RCTitles.push(recentchanges[i].title);                escapedTitle = recentchanges[i].title.replace(/"/g,"&#34;").replace(/'/g,"&#39;");                $('#nuke-query-results').append('<li class="nuke-query-result"><input type="checkbox" class="nuke-title-check" checked="checked"/>  ' + escapedTitle + '</a></li>'); }           }          }          if (!$('.nuke-query-result').length) { $('#nuke-status').html(' No results found '); }         else { $('#nuke-status').empty; }       }	else { $('#nuke-status').html(' Failed to get query results: ' + d.error.code + ' '); }     })      .fail(function { $('#nuke-status').html(' Failed to get query results '); });   });    $('.nuke-submit').click(function {      if (!$('.nuke-query-result').length)        return;      $('.nuke-submit').attr('disabled','disabled');      $('#nuke-status').html('Deleting pages... please wait <img src="http://slot1.images.wikia.nocookie.net/__cb62004/common/skins/common/progress-wheel.gif"/>');     $('.nuke-title-check:checked').each(function(i) { var title = $(this).parent.find('a').text; setTimeout(function {         Api.post({ action: 'delete', title: title, reason: $('#nuke-delete-reason').val || '', bot: true, token: token })         .done(function(d) { if (!d.error) { console.log('Deletion of ' + title + ' successful!'); } else { console.log('Failed to delete ' + title + ': '+ d.error.code); }         })          .fail(function { console.log('Failed to delete ' + title); });         if (i === $('.nuke-title-check:checked').length - 1) {            setTimeout(function { location.replace(articlePath + mainpage); }, 1000);            }        }, i*100); });   });  } }