Jump to content

User:Ahecht/Scripts/refresh.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ahecht (talk | contribs) at 19:13, 5 October 2021 (formatting). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// Add "refresh" option on category pages and on "Special:WhatLinksHere".
// Makes forceupdate nulledit on all pages in the category or all linked pages.
// Based on [https://phabricator.wikimedia.org/T170039#3473755] and [[:he:User:IKhitron/101.js]]

mw.loader.using( [ 'mediawiki.util', 'mediawiki.api' ] ).then( function() {
	function getWait(d) {
		var wait=1000;
		if (d && d.query && d.query.userinfo && d.query.userinfo.ratelimits
				&& d.query.userinfo.ratelimits.purge)
		{
			if (d.query.userinfo.ratelimits.purge.user
				&& d.query.userinfo.ratelimits.purge.user.hits
				&& d.query.userinfo.ratelimits.purge.user.seconds)
			{
				var hits = d.query.userinfo.ratelimits.purge.user.hits;
				var seconds = d.query.userinfo.ratelimits.purge.user.seconds;
				wait = Math.ceil( (seconds/hits) * 1000 );
			} else {
				wait = 2000;
			}
		}
		return wait;
	}
	function postPurge(target, count, wait, addParams) {
		step = target.gcmlimit || target.glhlimit || 1;
		mw.notify("Fetching " + target.generator + "...", { tag: "bubble"+count } );
		var apiParams = $.extend({
				action: 'purge', 
				forcelinkupdate: 1
			},
			target,
			addParams);
		console.log(apiParams);
		new mw.Api().post(apiParams)
			.fail(function(e) {
				console.log(e);
				alert("Fail!");
			})
			.done(function(d) {
			console.log(d);
			mw.notify((count + 1) + " pages were updated", { tag: "bubble"+count } );
			count += step;
			if (d.warnings === undefined && d["continue"] !== undefined
					&& (d["continue"].gcmcontinue || d["continue"].glhcontinue)) {
				setTimeout(function() {
						postPurge(target, count, wait, d["continue"]);
					}, wait);
			} else {
				alert("Done!");
				document.location.reload();
			}
		});
	}
	if ( (mw.config.get('wgNamespaceNumber') == 14) || (mw.config.get("wgCanonicalSpecialPageName") == "Whatlinkshere") ){
		new mw.Api().get({
    		meta: 'userinfo',
    		uiprop: 'ratelimits'
		}).done( function(d) {
			var target = mw.config.get("wgRelevantPageName").replace(/_/g, " ");
			if (mw.config.get('wgNamespaceNumber') == 14) {
				target = {
					generator: 'categorymembers',
	  				gcmtitle: target,
	  				gcmlimit: 1
				};
			} else {
				target = {
					generator: 'linkshere',
					titles: target,
	  				glhlimit: 1
				};
			}
			$(mw.util.addPortletLink('p-cactions', '#', 'Refresh links', 'pt-refresh'))
				.click(function() {
					postPurge(target, 0, getWait(d));
				});
		} );
	}
});