MediaWiki:Gadget-autodelete.js

From Uncyclopedia, the content-free encyclopedia
Jump to navigation Jump to search

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

  • Internet Explorer: hold down the Ctrl key and click the Refresh or Reload button, or press Ctrl+F5.
  • Firefox: hold down the Shift key while clicking Reload; alternatively press Ctrl+F5 or Ctrl-Shift-R.
  • Opera, Konqueror and Safari users can just click the Reload button.
  • Chrome: press Ctrl+F5 or Shift+F5
/**
 * Autodelete 4.1.1
 * 
 * Autodelete is a script for deleting articles with one click, either with the
 * reasons provided or one's own custom reason. Originally developed by Spang,
 * later revised by Lyrithya and Llwy-ar-lawr. Now adds its own dropdown menu
 * for deletion links.
 * 
 * Depends on Sparkle ([[MediaWiki:Gadget-sparkle.js]]).
 * 
 * To add your own predefined reason(s) to the list, add this to
 * [[Special:MyPage/common.js]]:

deleteCustom = [
	['Some text to display', 'Some reason'],
	['Some other text to display', 'Some other reason']
];

 * (Note the commas after all but the last entry.)
 */

del = {};

// Suffix for identifying gadget
del.suffix = " ([[MediaWiki:Gadget-autodelete.js|autodelete]])";

// Portlet to use with Sparkle functions
switch (mw.config.get('skin')) {
	case 'vector':
		del.portletArea = 'right-navigation';
		del.portletId = 'p-delete';
		del.portletName = 'Delete';
		del.portletType = 'menu';
		del.portletNext = 'p-search';
		break;
	case 'timeless':
		del.portletArea = '#page-tools .sidebar-inner';
		del.portletId = 'p-delete';
		del.portletName = 'Delete';
		del.portletType = null;
		del.portletNext = 'p-userpagetools';
		break;
	default:
		del.portletArea = null;
		del.portletId = 'p-cactions';
		del.portletName = null;
		del.portletType = null;
		del.portletNext = null;
}

del.autoDelete = function() {
	// Check if we're on a special page; if so, do nothing
	if (mw.config.get("wgNamespaceNumber") == -1) {
		return;
	}

	// Create dropdown
	Sparkle.addPortlet(del.portletArea, del.portletId, del.portletName, del.portletType, del.portletNext);

	// Main array for deletion reasons
	var deleteMain = [];
	deleteMain[0] = ['Fails QA', "Fails QA. Read the [[BGBU|beginner\'s guide]] and [[HTBFANJS]]."];
	deleteMain[1] = ['Vanity', 'Vanity. See [[UN:VAIN]].'];
	deleteMain[2] = ['VFD', '[[VFD|Voted for deletion]]'];
	deleteMain[3] = ['Expired ICU', '[[Uncyclopedia:Intensive Care Unit|ICU]] expired'];
	deleteMain[4] = ['Abandoned WIP', 'Abandoned WIP'];
	deleteMain[5] = ['Vigilance', 'Vigilance.'];

	// Include user customizations
	if (typeof(deleteCustom) != "undefined") {
		for (var i = 0; i < deleteCustom.length; i++) {
			deleteMain[deleteMain.length] = deleteCustom[i];
		}
	}

	// Format information for Sparkle.addPortletLinks, using the deletion summary
	// as the tooltip
	var deleteBox = [];
	for (var j = 0; j < deleteMain.length; j++) {
		deleteBox[j] = [del.reason, deleteMain[j][0], '', deleteMain[j][1], deleteMain[j][1]];
	}

	// Custom reason
	if (document.cookie.indexOf('lastDelReason') != -1 ) {
		var cStart = document.cookie.indexOf('lastDelReason') + 14;
		var cEnd = document.cookie.indexOf(';', cStart);
		if (cEnd == -1) cEnd = document.cookie.length;
		lastReason = unescape(document.cookie.substring(cStart, cEnd));
		deleteBox[deleteBox.length] = [del.reason, 'last: '
		+ lastReason.substr(0,10) + '...', '', '', lastReason];
		deleteBox[deleteBox.length] = [del.reason, 'Custom...', '', 'Use a custom reason'];
	}
	else deleteBox[deleteBox.length] = [del.reason, 'Custom...', '', 'Use a custom reason'];

	// Add the deletion links to our custom dropdown
	Sparkle.addPortletLinks(del.portletId, deleteBox);
};

// Make API request to delete the page
del.reason = function(reason) {
	// Name of page with spaces (used in messages)
	var title = mw.config.get("wgPageName").replace(/_/g, " ");
	
	// If "reason" is set, delete the page with this reason
	if (reason !== undefined) {
		mw.notify("Deleting page...");
		var api = new mw.Api();
		api.post({
			action: "delete",
			title: mw.config.get("wgPageName"),
			reason: reason + del.suffix,
			token: mw.user.tokens.get("csrfToken")
		}).done(function() {
			mw.notify("\"" + title + "\" has been deleted.");
			setTimeout(function() {
				window.location = mw.config.get("wgServer") + "/wiki/" + mw.config.get("wgPageName");
			}, 1500);
				
			// Also delete talk page if it exists
			var api = new mw.Api();
			var talkpage = mw.config.get("wgFormattedNamespaces")[mw.config.get("wgNamespaceNumber") + 1] + ":"
				+ mw.config.get("wgTitle");
			api.post ({
				action: 'delete',
				title: talkpage,
				reason: "Orphaned talk page" + del.suffix,
				token: mw.user.tokens.get('csrfToken')
			}).done(function() {
				mw.notify("\"" + talkpage.replace(/_/g, " ") + "\" has been deleted.");
			});
		}).fail(function() {
			mw.notify("Failed to delete \"" + title + "\".");
		});
	} else {
		// If "reason" is not set, prompt the user for a custom one
		var reason = prompt('Enter deletion reason');
		if (reason !== null) {
			document.cookie = 'lastDelReason=' + escape(reason) + '; path=/';
			var api = new mw.Api();
			api.post({
				action: "delete",
				title: mw.config.get("wgPageName"),
				reason: reason + del.suffix,
				token: mw.user.tokens.get("csrfToken")
			}).done(function() {
				mw.notify("\"" + title + "\" has been deleted.");
				setTimeout(function() {
					window.location = mw.config.get("wgServer") + "/wiki/" + mw.config.get("wgPageName");
				}, 1500);
				
				// Also delete talk page if it exists
				var api = new mw.Api();
				var talkpage = mw.config.get("wgFormattedNamespaces")[mw.config.get("wgNamespaceNumber") + 1] + ":"
					+ mw.config.get("wgTitle");
				api.post ({
					action: 'delete',
					title: talkpage,
					reason: "Orphaned talk page" + del.suffix,
					token: mw.user.tokens.get('csrfToken')
				}).done(function() {
					mw.notify("\"" + talkpage.replace(/_/g, " ") + "\" has been deleted.");
				});
			}).fail(function() {
				mw.notify("Failed to delete \"" + title + "\".");
			});
		}
	}
};

$(del.autoDelete);