User:Alissa the Wise Wolf/common.js

/* Please inform me for any bugs. */ mw.loader.using('mediawiki.api').then(function {	if (!$ || !mw || $('#purge-categories').length) 		return;	$('')		.html('Purge categories')		.prependTo('#my-tools-menu')		.click(function { $.showCustomModal('Purge Categories', 'Enter each category to remove in a separate line. (No category prefix) \			 Summary: \			', {				id: 'purge-categories-modal',				callback: function($modal) {					var ug = mw.config.get('wgUserGroups'),					canDelete = ug.indexOf('sysop') + ug.indexOf('vstf') + ug.indexOf('staff') + ug.indexOf('helper') > -4;					if (canDelete)						$('#purge-categories-summary').after('  \ Delete categories after purging is done Delete summary: \ ');					$(' ')						.text('Progress will be displayed here.')						.insertAfter(canDelete ? '#purge-categories-delete-summary' : '#purge-categories-summary');					$(' ')						.text('Errors will be displayed here.')						.insertAfter('#purge-categories-message');					mw.util.addCSS('#purge-categories-message {\ background-color: rgb(255, 251, 230);\ font-weight: bold;\ margin: 10px;\ padding: 10px;\ width: 80%;\ }\					#purge-categories-errors {\ background-color: rgb(255, 240, 240);\ border: 1px solid rgb(255, 215, 215);\ color: rgb(255, 0, 0);\ margin: 10px;\ padding: 10px;\ width: 80%;\ max-height: 13em;\ overflow-y: scroll;\ }\					/* Poor Mozilla users */\ div#purge-categories-errors::-webkit-scrollbar {\ width: 12px;\ height: 12px;\ }\					div#purge-categories-errors::-webkit-scrollbar-thumb {\ background: red;\ }');				},				buttons: [{					message: 'Cancel',					handler: function {						$('#purge-categories-modal').closeModal;					}				}, {					id: 'start-button',					message: 'Start',					defaultButton: true,					handler: init				}]			}); });	var Api = new mw.Api,	pages = [],	summary = '',	editInit = false,	deleteInit = false;	function message(msg) {		document.getElementById('purge-categories-message').innerHTML = msg;	}	function error(error) {		var el = document.getElementById('purge-categories-errors');		if (el.innerHTML == 'Errors will be displayed here.')			el.innerHTML = error;		var br = document.createElement('br'),		textNode = document.createTextNode(error);		el.appendChild(br);               el.appendChild(textNode);	}	function loadPagesInCategory(titles, index, callback) {		var title = titles[index];		message('Fetching pages under the category: ' + title);		Api.get({ action: 'query', list: 'categorymembers', cmtitle: 'Category:' + title, cmlimit: 5000, cb: $.now }).done(function(data) { if (!data.error) { pages = pages.concat(data.query.categorymembers.map(function(el) { return el.title; }));				if (!titles[index + 1]) { callback(pages); } else { loadPagesInCategory(titles, index + 1, callback); }			} else { error('An error occurred while fetching category members for ' + title + ': ' + data.error.info); loadPagesInCategory.apply(this, arguments); }		}).fail(function { error('Categorymembers API call for ' + title + ' timed out. Retrying...'); loadPagesInCategory.apply(this, arguments); });	}	function editPages(titles, index, fn, callback) {		if (index == 0 && editInit) {			return;		}		editInit = true;		var title = titles[index];		message('Fetching page content of ' + title);		Api.get({ action: 'query', prop: 'revisions', rvprop: 'content', titles: title, cb: $.now }).done(function(d) { if (!$.isPlainObject(d)) return; if (!d.error) { //console.log(d); var token = mw.user.tokens.get('editToken'), p = d.query.pages, content = p[Object.keys(p)[0]].revisions[0]['*'], obj = fn(content, title, index); if (content == obj.content) { error('No changes made in ' + title + '; this could be a bug in the script, or another user edited the page.'); if (!titles[index + 1]) { callback; } else { editPages(titles, index + 1, fn, callback); }					return; }				message('Editing page ' + title + '. Categories replaced are: ' + obj.replaced.join(' '))				Api.post({					action: 'edit',					title: title,					text: obj.content,					summary: summary,					token: token				}).done(function(edit) {					if (!edit.error) {						console.log('Successfully edited page ' + title + '!');						if (!titles[index + 1]) {							callback;						} else {							editPages(titles, index + 1, fn, callback);						}					} else {						error('An error occurred while editing ' + title + ': ' + edit.error.info);						editPages.apply(this, arguments);					}				}).fail(function(err) {					error('API query for editing ' + title + ' has timed out. Retrying...');					editPages.apply(this, arguments);				}); } else { error('An error occurred while fetching content for ' + title + ': ' + d.error.info) editPages.apply(this, arguments); }		}).fail(function(err) { error('API query for fetching page content of ' + title + ' has timed out. Retrying...'); editPages.apply(this, arguments); });	}	function deletePages(titles, index, callback) {		if (index == 0 && deleteInit) {			return;		}		deleteInit = true;		var title = titles[index],		token = mw.user.tokens.get('editToken');		message('Deleting ' + title + '...')		Api.post({ action: 'delete', title: title, reason: $('#purge-categories-delete-summary').val.trim, token: token }).done(function(d) { if (!d.error) { console.log('Successfully deleted page ' + title + '!'); if (!titles[index + 1]) { callback; } else { deletePages(titles, index + 1, callback); }			} else { error('An error occurred while editing ' + title + ': ' + d.error.info); deletePages.apply(this, arguments); }		}).fail(function { error('API timed out while editing ' + title + '. Retrying...'); deletePages.apply(this, arguments); });	}	function init {		summary = $('#purge-categories-summary').val.trim;		var categories = $('#purge-categories-pages').val.split('\n');		loadPagesInCategory(categories, 0, function(pgs) { var obj = mw.config.get('wgNamespaceIds'), objKeys = Object.keys(obj), filtered = []; for (var i in objKeys) { var key = objKeys[i]; if (obj[key] == 14) filtered.push(key); }			pgs = _.uniq(pgs); editPages(pgs, 0, function(content, title, index) {				var obj = {					content: content,					replaced: []				};				$.each(categories, function(i, cat) { cat = cat.split(/‏|‎/).join('').trim; // ignore this, it's a stupid unicode character that slips in sometimes var sRegEx = '(\\[\\[(' + filtered.join('|') + '):' + cat + '\\]\\]|\\[\\[(' + filtered.join('|') + '):' + cat + '\\|.*?\\]\\])\\n?'; var regex = new RegExp(sRegEx, 'gi'); if (regex.test(obj.content)) { obj.replaced.push(cat); obj.content = obj.content.replace(regex, ''); }				});				return obj;			}, function {				if ($('#purge-categories-delete-categories').is(':checked')) {					categories = categories.map(function(el) { return 'Category:' + el; });					deletePages(categories, 0, done);				} else {					done;				}			}); });	}	function done {		message('Done!');		editInit = false;		deleteInit = false;		$.showCustomModal('Done!', 'Nothing left to do.', { id: 'purge-categories-done', buttons: [{ message: 'Close', handler: function { $('#purge-categories-done').closeModal; }			}]		});	} }); linkSweepConfirmation = true; LinkSweeperDelay = 1000; massUserRightsDelay = 1000; importScriptPage('MassUserRights/code.js', 'dev'); WHAMDelay = 1000; importArticles({   type: 'script',    articles: [        'u:dev:Mediawiki:QuickToolsv2/code.js',        'u:dev:MediaWiki:WHAM/code.2.js',        'u:dev:MediaWiki:AjaxBatchDelete/code.2.js',        'u:dev:Linksweeper/code.js',        'w:c:dev:SpellCheckModule/code.js',        'u:dev:AutoEditPages/code.js',    ] }); if (mw.config.get("wgUserGroups").indexOf('sysop') > -1) { massCategorizationDelay = 1000; importScriptPage('MediaWiki:MassCategorization/code.js', 'dev'); } importScriptPage('Mediawiki:ChatEditTools/code.js', 'dev');