User:Animum/massdelete.js

mw.loader.using(['mediawiki.api', 'mediawiki.Title'], function {	"use strict";

var config = mw.config.get(['wgNamespaceNumber', 'wgTitle', 'wgUserGroups', 'skin']);

function removeBlanks(arr) { var ret = []; var i, len; for (i = 0, len = arr.length; i < len; i++) { var s = arr[i]; s = s.trim; if (s) { ret.push(s); }		}		return ret; }

function doMassDelete { document.getElementById("wpMassDeleteSubmit").disabled = true; var articles = document.getElementById("wpMassDeletePages").value.split("\n"); articles = removeBlanks(articles); if (!articles.length) { return; }		var api = new mw.Api, wpMassDeleteReasons = document.getElementById("wpMassDeleteReasons").value, wpMassDeleteReason = document.getElementById("wpMassDeleteReason").value, deleted = 0, failed = [], error = [], deferreds = [], reason = wpMassDeleteReasons == "other" ? wpMassDeleteReason : wpMassDeleteReasons + (wpMassDeleteReason ? " (" + wpMassDeleteReason + ")" : ""), onSuccess = function { deleted++; document.getElementById("wpMassDeleteSubmit").value = "(" + deleted + ")"; };

function makeDeleteFunc(article) { return function { return $.Deferred(function (deferred) {					var promise = api.postWithToken('csrf', { format: 'json', action: 'delete', title: article, reason: reason });					promise.done(onSuccess);					promise.fail(function (code, obj) { failed.push(article); error.push(obj.error.info); });					promise.always(function { deferred.resolve; });				});			};		}

// Make a chain of deferred objects. We chain them rather than execute them in		// parallel so that we don't make 1000 simultaneous delete requests and bring the // site down. We use deferred objects rather than the promise objects returned // from the API request so that the chain continues even if some articles gave // errors. var deferred = makeDeleteFunc(articles[0]); for (var i = 1, len = articles.length; i < len; i++) { deferred = deferred.then(makeDeleteFunc(articles[i])); }

// Show the output and do cleanup once all the requests are done. $.when(deferred).then(function {			document.getElementById("wpMassDeleteSubmit").value = "Done (" + deleted + ")";			if (failed.length) {				var $failedList = $('');				for(var x = 0; x < failed.length; x++) {					// Link the titles in the "failed" array					var failedTitle = mw.Title.newFromText(failed[x]);					var $failedItem = $('');					if (failedTitle) {						$failedItem.append( $('') .attr('href', failedTitle.getUrl) .text(failed[x]) );					} else {						$failedItem.text(failed[x]);					}					$failedItem.append(document.createTextNode(': ' + error[x]));					$failedList.append($failedItem);				}				$('#wpMassDeleteFailedContainer')					.append($(' '))					.append($('') .text('Failed deletions:') )					.append($failedList);			}		}); }	function massdeleteform { var bodyContent; switch (mw.config.get('skin')) { case 'modern': bodyContent = 'mw_contentholder'; break; case 'cologneblue': bodyContent = 'article'; break; case 'monobook': case 'vector': default: bodyContent = 'bodyContent'; break; }		document.getElementsByTagName("h1")[0].textContent = "Animum's mass-deletion tool"; document.title = "Animum's mass-deletion tool - Wikipedia, the free encyclopedia"; document.getElementById(bodyContent).innerHTML = 'From Wikipedia, the free encyclopedia

' +			'' + 'If you abuse this tool, it\'s your fault, not mine.' + ' ' +			'

' +				'Pages to delete (one on each line, please): ' +					' ' + '

' +					' Common reasons: ' + ' ' + '' + ' Other reason ' + ' ' +							'' + '' + 'G1: Patent nonsense ' + 'G2: Test page ' + 'G3: Vandalism ' + 'G3: Hoax ' + 'G4: Repost ' + '<option value="G5: Creation by a blocked or banned user in violation of block or ban">G5: Banned ' + '<option value="G6: Housekeeping and routine (non-controversial) cleanup">G6: Maintenance ' + '<option value="G7: One author who has requested deletion or blanked the page">G7: Author ' + '<option value="G8: Page dependent on a deleted or nonexistent page">G8: Orphaned talk page ' + '<option value="G10: Attack page or negative unsourced BLP">G10: Attack page ' + '<option value="G11: Unambiguous advertising or promotion">G11: Advertising ' + '<option value="G12: Unambiguous copyright infringement">G12: Copyvio ' + '<option value="G13: Abandoned draft or Articles for Creation submission – to retrieve it, see WP:REFUND/G13">G13: Abandoned draft ' + ' ' +								'<optgroup label="Articles">' + '<option value="A1: Short article without enough context to identify the subject">A1: No context ' + '<option value="A2: Article in a foreign language that exists on another project">A2: Foreign ' + '<option value="A3: Article that has no meaningful, substantive content">A3: No content ' + '<option value="A7: No credible indication of importance (individuals, animals, organizations, web content, events)">A7: Non-notable individual, animal, organization, web content, or event ' + '<option value="A9: Music recording by redlinked artist and no indication of importance or significance">A9: Non-notable recording by redlinked artist ' + '<option value="A10: Recently created article that duplicates an existing topic">A10: Recently created article that duplicates an existing topic ' + '<option value="A11: Made up by article creator or an associate, and no indication of importance/significance">A11: Made up ' + ' ' +								'<optgroup label="Redirects">' + '<option value="R2: Cross-namespace redirect from mainspace">R2: Cross-namespace ' + '<option value="R3: Recently created, implausible redirect">R3: Implausible redirect ' + ' ' +								'<optgroup label="Images and other media">' + '<option value="F1: File redundant to another on Wikipedia">F1: Redundant ' + '<option value="F2: Corrupt or empty file, or a file description page for a file on Commons">F2: Corrupt, empty. or Commons ' + '<option value="F3: File with improper license">F3: File with improper license ' + '<option value="F4: Lack of licensing information">F4: Lack of licensing information ' + '<option value="F5: Unused non-free media">F5: Unfree and unused ' + '<option value="F6: Non-free file without fair-use rationale">F6: No rationale ' + '<option value="F7: Invalid fair-use claim">F7: Bad fair use rationale ' + '<option value="F8: Media file available on Commons">F8: On Commons ' + '<option value="F9: File copyright violation">F9: File copyvio ' + '<option value="F11: No evidence of permission">F11: No permission ' + ' ' +								'<optgroup label="Categories">' + '<option value="C1: Empty category">C1: Empty ' + '<option value="C2: Speedy renaming">C2: Speedy rename ' + '<option value="G8: Populated by deleted or retargeted template">G8: Populated by deleted or retargeted template ' + ' ' +								'<optgroup label="User namespace">' + '<option value="U1: User request to delete page in own userspace">U1: User requests deletion ' + '<option value="U2: Userpage or subpage of a nonexistent user">U2: Non-existent user ' + '<option value="U5: Misuse of Wikipedia as a web host">U5: Misuse as webhost ' + ' ' +								'<optgroup label="Templates">' + '<option value="G8: Component or documentation of a deleted template">G8: component of deleted template ' + ' ' +								'<optgroup label="Other">' + '<option value="WP:PROD: Nominated for seven days with no objection">PRODded for more than 7 days without objection ' + '<option value="WP:BLPPROD: Nominated for seven days with no reliable sources present in the article">BLPPRODded for more than seven days without a source ' + '<option value="Listed at Copyright problems for over seven days">Listed at Copyright problems for over seven days ' + ' ' +							' ' +						' ' +				'  Other/additional reason: ' + ' <input type="text" id="wpMassDeleteReason" name="wpMassDeleteReason" maxlength="255" /> ' + ' <input type="button" id="wpMassDeleteSubmit" name="wpMassDeleteSubmit" value="Delete" /> ' + ' ';		document.getElementById("wpMassDeleteReasons").onchange = function { var maxlength = (document.getElementById("wpMassDeleteReasons").value == "other" ? 255 : 252-document.getElementById("wpMassDeleteReasons").value.length); //It's 252 because of the three characters (" ") in addition to the selected summary. document.getElementById("wpMassDeleteReason").setAttribute("maxlength", maxlength); };		document.getElementById("wpMassDeleteSubmit").addEventListener("click", function (e) {			doMassDelete;		}); }	if (config.wgNamespaceNumber == -1 &&		config.wgTitle.toLowerCase == "massdelete" &&		/sysop/.test(config.wgUserGroups)	) { massdeleteform; }

});