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 16:19, 18 January 2022 (fix logging). 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, type) {
		var wait=1000;
		if (d && d.query && d.query.userinfo && d.query.userinfo.ratelimits
				&& d.query.userinfo.ratelimits[type])
		{
			if (d.query.userinfo.ratelimits[type].user
				&& d.query.userinfo.ratelimits[type].user.hits
				&& d.query.userinfo.ratelimits[type].user.seconds)
			{
				var hits = d.query.userinfo.ratelimits[type].user.hits;
				var seconds = d.query.userinfo.ratelimits[type].user.seconds;
				console.log(type + " rate limit: hits=" + hits + ", seconds=" + seconds);
				wait = Math.ceil( (seconds/hits) * 1000 );
				console.log("Using " + wait + " milliseconds wait between queries");
			} 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 {
					if (confirm("Done!\n\nReload page?") == true) {
						document.location.reload();
					}
				}
		});
	}
	
	function postNull(target, count, wait, addParams) {
		var step = target.gtilimit || target.gcmlimit || target.glhlimit || 1;
		mw.notify("Fetching " + target.generator + "...", { tag: "bubble"+count } );
		var queryParams = $.extend({
				action: 'query', 
				formatversion: '2',
			},
			target,
			addParams);
		console.log(queryParams);
		new mw.Api().post(queryParams)
			.fail(function(e) {
				console.log(e);
				alert("Fail!");
			})
			.done(function(q) {
				console.log(q);
				if(q && q.query && q.pages && q.pages[0] && q.pages[0].title) {
					var editParams = {
						action: 'edit',
						title: q.pages[0].title,
						watchlist: 'nochange',
						nocreate: '1',
						appendtext: ''
						};
					new mw.Api().postWithToken("csrf", editParams )
						.fail(function(e) {
							console.log(e);
							alert("Fail!");
						})
						.done( function(e) {
						console.log(e);
						mw.notify((count + 1) + " pages were updated", { tag: "bubble"+count } );
						count += step;
						
						if (q.warnings === undefined && q["continue"] !== undefined
								&& (q["continue"].gticontinue
								|| q["continue"].gcmcontinue
								|| q["continue"].glhcontinue)) {
							setTimeout(function() {
									postNull(target, count, wait, q["continue"]);
								}, wait);
						} else {
							if (confirm("Done!\n\nReload page?") == true) {
								document.location.reload();
							}
						}
					} );
				} else {
					alert("Fail!");
				}
			} );
	}
	
	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 = "transcluding pages";
			} else if (mw.config.get('wgNamespaceNumber') == 14) {
				target = {
					generator: 'categorymembers',
	  				gcmtitle: target,
	  				gcmlimit: step
				};
				linkTitle = "category members";
			} else {
				target = {
					generator: 'linkshere',
					titles: target,
	  				glhlimit: step
				};
				linkTitle = "linking pages";
			}
			$(mw.util.addPortletLink('p-cactions', '#', 'Purge ' + linkTitle, 'pt-refresh-purge'))
				.click(function() {
					postPurge(target, 0, getWait(d, "purge"));
				});
			$(mw.util.addPortletLink('p-cactions', '#', 'Null edit ' + linkTitle, 'pt-refresh-null'))
				.click(function() {
					postNull(target, 0, getWait(d, "edit"));
				});
		} );
	}
});