User:Tulsi/massdelete.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/** This script is adapted from [[:en:User:Animum/massdelete.js]]
 * by [[User:Tulsi Bhagat]] <https://meta.wikimedia.org/wiki/User:Tulsi_Bhagat>
 * Special thanks to [[User:~riley]]
 */
mw.loader.using(['mediawiki.api', 'mediawiki.Title'], function () {
	"use strict";

	var config = mw.config.get(['wgNamespaceNumber', 'wgTitle', 'wgUserGroups', 'wgGlobalGroups', '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('delete', {
						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 = $('<ul>');
				for(var x = 0; x < failed.length; x++) {
					// Link the titles in the "failed" array
					var failedTitle = mw.Title.newFromText(failed[x]);
					var $failedItem = $('<li>');
					if (failedTitle) {
						$failedItem.append( $('<a>')
							.attr('href', failedTitle.getUrl())
							.text(failed[x])
						);
					} else {
						$failedItem.text(failed[x]);
					}
					$failedItem.append(document.createTextNode(': ' + error[x]));
					$failedList.append($failedItem);
				}
				$('#wpMassDeleteFailedContainer')
					.append($('<br />'))
					.append($('<b>')
						.text('Failed deletions:')
					)
					.append($failedList);
			}
		});
	}
	 
	function massdeleteform() {
		var bodyContent = (config.skin == "cologneblue" ? "article" : "bodyContent");
		document.getElementsByTagName("h1")[0].textContent = "Mass Delete";
		document.title = "Mass Delete, by Tulsi Bhagat";
		document.getElementById(bodyContent).innerHTML = '<h3 id="author">Authored by Tulsi Bhagat</h3><br />' +
			'<form id="wpMassDelete" name="wpMassDelete">' +
			'This special page allows you to mass delete pages quickly and easily.<br/><b>Please be careful while using it, and you are fully responsible for any mistakes.</b>' +
			'<div id="wpMassDeleteFailedContainer"></div>' +
			'<br />' +
				'List the pages to be deleted (one on each line, please):<br />' +
					'<textarea tabindex="1" accesskey="," name="wpMassDeletePages" id="wpMassDeletePages" rows="10" cols="80"></textarea>' +
				'<br /><br /><table style="background-color:transparent">' +
					'<tr><td>Common reasons:</td>' +
						'<td><select id="wpMassDeleteReasons">' +
							'<optgroup label="Other reason">' +
								'<option value="other">Other reason</option>' +
							'</optgroup>' +
							'<optgroup label="Global sysop/Steward reasons">' +
								'<optgroup label="Global sysop reasons">' +
									'<option value="Spam or promotional ([[:m:GS|global sysop]] action)">Spam</option>' +
									'<option value="Vandalism ([[:m:GS|global sysop]] action)">Vandalism</option>' +
									'<option value="No useful content ([[:m:GS|global sysop]] action)">No useful content</option>' +
									'<option value="Nonsense ([[:m:GS|global sysop]] action)">Nonsense</option>' +
									'<option value="Test page ([[:m:GS|global sysop]] action)">Test page</option>' +
									'<option value="Not written in this project\'s language ([[:m:GS|global sysop]] action)">Not written in this project\'s language</option>' +
									'<option value="Requested by the author ([[:m:GS|global sysop]] action)">Requested by the author</option>' +
									'<option value="Outside the project\'s scope ([[:m:GS|global sysop]] action)">Outside the project\'s scope</option>' +
									'<option value="Empty ([[:m:GS|global sysop]] action)">Empty</option>' +
									'<option value="Unneeded or broken redircet ([[:m:GS|global sysop]] action)">Unneeded or broken redircet</option>' +
								'</optgroup>' +
								'<optgroup label="Steward reasons">' +
									'<option value="Spam or promotional ([[:m:Stewards|steward]] action)">Spam</option>' +
									'<option value="Vandalism ([[:m:Stewards|steward]] action)">Vandalism</option>' +
									'<option value="No useful content ([[:m:Stewards|steward]] action)">No useful content</option>' +
									'<option value="Nonsense ([[:m:Stewards|steward]] action)">Nonsense</option>' +
									'<option value="Test page ([[:m:Stewards|steward]] action)">Test page</option>' +
									'<option value="Not written in this project\'s language ([[:m:Stewards|steward]] action)">Not written in this project\'s language</option>' +
									'<option value="Requested by the author ([[:m:Stewards|steward]] action)">Requested by the author</option>' +
									'<option value="Outside the project\'s scope ([[:m:Stewards|steward]] action)">Outside the project\'s scope</option>' +
									'<option value="Empty ([[:m:Stewards|steward]] action)">Empty</option>' +
									'<option value="Unneeded or broken redircet ([[:m:Stewards|steward]] action)">Unneeded or broken redircet</option>' +
								'</optgroup>' +
							'</optgroup>' +
						'</select></td></tr>' +
				'<tr><td>Other/additional reason:</td>' +
					'<td><input type="text" id="wpMassDeleteReason" name="wpMassDeleteReason" maxlength="255" /></td></tr>' +
					'<tr><td><input type="button" id="wpMassDeleteSubmit" name="wpMassDeleteSubmit" value="Delete" /></td>' +
			'</form>';
		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(mw.config.get("wgNamespaceNumber") == -1 && (mw.config.get("wgTitle") == "Massdelete" || mw.config.get("wgTitle") == "MassDelete") && (/sysop/.test(mw.config.get("wgUserGroups")) || (/global-sysop/.test(mw.config.get("wgGlobalGroups")) || (/steward/.test(mw.config.get("wgGlobalGroups")) ) ) ) ) $(massdeleteform);
	{
		var newElement = mw.util.addPortletLink( 'p-cactions', '/wiki/Special:MassDelete', 'Mass Delete', 'ca-massdelete', 'Go for Mass Delete' );
	}
} );