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. \			 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: \ ');				},				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 loadPagesInCategory(titles, index, callback) {		var title = titles[index];		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); loadPagesInCategory(titles, index + 1, callback); } else { loadPagesInCategory.apply(this, arguments); }		}).fail(function { console.log('An error occurred. Callee: loadPagesInCategory'); console.log(arguments); loadPagesInCategory.apply(this, arguments); });	}	function editPages(titles, index, fn, callback) {		if (index == 0 && editInit) {			return;		}		editInit = true;		var title = titles[index];		console.log(titles.length, title);		Api.get({ action: 'query', prop: 'revisions', rvprop: 'content', titles: title }).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]['*'], newContent = fn(content, title, index); if (content == newContent) { console.log('No changes made; this could be a bug in the script.'); if (!titles[index + 1]) { callback; } else { editPages(titles, index + 1, fn, callback); }					return; }				Api.post({					action: 'edit',					title: title,					text: newContent,					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 {						console.log(edit);						editPages.apply(this, arguments);					}				}).fail(function(err) {					console.log('An error occurred. Callee: editPages, POST edit');					console.log(err, arguments);					editPages.apply(this, arguments);				}); } else { console.log(d); editPages.apply(this, arguments); }		}).fail(function(err) { console.log('An error occurred. Callee: editPages'); console.log(err, arguments); editPages.apply(this, arguments); });	}	function deletePages(titles, index, callback) {		if (index == 0 && deleteInit) {			console.log('Attempted to init editPages but deleteInit is set to true.');			return;		}		deleteInit = true;		var title = titles[index],		token = mw.user.tokens.get('editToken');		console.log(titles.length, 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 { deletePages.apply(this, arguments); }		}).fail(function { console.log('An error occurred. Callee: deletePages'); console.log(err, arguments); 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 toReturn = content;				$.each(categories, function(i, cat) { var sRegEx = '(\\[\\[(' + filtered.join('|') + '):' + cat + '\\]\\]|\\[\\[(' + filtered.join('|') + '):' + cat + '\\|.*?\\]\\])\n?'; var regex = new RegExp(sRegEx, 'gi'); if (regex.test(toReturn)) toReturn = toReturn.replace(regex, ''); });				return toReturn;			}, function {				if ($('#purge-categories-delete-categories').is(':checked')) {					categories = categories.map(function(el) { return 'Category:' + el; });					deletePages(categories, 0, done);				} else {					done;				}			}); });	}	function done {		$.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('poweruser') > -1) { massCategorizationDelay = 1000; importScriptPage('MediaWiki:MassCategorization/code.js', 'dev'); }