User:JohnBlackburne/common.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
mw.loader.using( [ 'mediawiki.util', 'mediawiki.api' ] ).then( function() {
	var step = 1;
	var count;
	var wait;
	function postPurge(cat, addParams) {
		var apiParams = $.extend({
			action: 'purge', 
			generator: 'categorymembers',
  			'gcmtitle': cat,
  			'gcmlimit': step,
			forcelinkupdate: 1
		}, addParams);
		new mw.Api().post(apiParams)
			.fail(function() {
				alert("Fail");
			})
			.done(function(d) {
			console.log(d);
			count += step;
			if (d.warnings === undefined && d["continue"] !== undefined
					&& d["continue"].gcmcontinue) {
				mw.notify(count + " pages were updated");
				setTimeout(function() {
						postPurge(cat, d["continue"]);
					}, wait);
			} else {
				alert("Done!");
				document.location.reload();
		}});
	}
	if (mw.config.get('wgNamespaceNumber') == 14) {
		wait = 1000;
		new mw.Api().get({
    		meta: 'userinfo',
    		uiprop: 'ratelimits'
		}).done(function(d) {
			if (d && d.query && d.query.userinfo && d.query.userinfo.ratelimits
    				&& d.query.userinfo.ratelimits.purge)
				wait = 2000;
				$(mw.util.addPortletLink('p-cactions', '#', 'refresh', 'pt-refresh'))
					.click(function() {
						count = 0;
						postPurge(mw.config.get('wgPageName')
							.replace(/_/g, " "));
					});
		} );
	}
});