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 23:16, 5 October 2021 (document). 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, template pages,  and on
// "Special:WhatLinksHere". Makes forceupdate nulledit on all pages in the
// category, all transclusing pages, 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) {
		var step = target.gtilimit || target.gcmlimit || target.glhlimit || 1;
		mw.notify("Fetching " + target.generator + "...", { tag: "bubble"+count } );
		var apiParams = $.extend({
				action: 'purge', 
				forcerecursivelinkupdate: 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"].gticontinue
					|| 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') == 10) 
		|| (mw.config.get('wgNamespaceNumber') == 14) 
		|| (mw.config.get("wgCanonicalSpecialPageName") == "Whatlinkshere") )
	{
		new mw.Api().get({
    		meta: 'userinfo',
    		uiprop: 'ratelimits'
		}).done( function(d) {
			var linkTitle;
			var step = 1;
			var target = mw.config.get("wgRelevantPageName").replace(/_/g, " ");
			if (mw.config.get('wgNamespaceNumber') == 10) {
				target = {
					generator: 'transcludedin',
	  				titles: target,
	  				gtilimit: step
				};
				linkTitle = "Refresh transcluding pages";
			} else if (mw.config.get('wgNamespaceNumber') == 14) {
				target = {
					generator: 'categorymembers',
	  				gcmtitle: target,
	  				gcmlimit: step
				};
				linkTitle = "Refresh category members";
			} else {
				target = {
					generator: 'linkshere',
					titles: target,
	  				glhlimit: step
				};
				linkTitle = "Refresh linking pages";
			}
			$(mw.util.addPortletLink('p-cactions', '#', linkTitle, 'pt-refresh'))
				.click(function() {
					postPurge(target, 0, getWait(d));
				});
		} );
	}
});