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 21:42, 24 January 2022 (Restore simplification (looks like I edited the wrong version)). 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) {
		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(code, error) {
				console.error(error);
				alert("Error purging page: " + code + "!");
			})
			.done(function(d) {
				console.log(d);
				mw.notify((count + 1) + " pages were updated", { tag: "bubble"+count } );
				count += 1;
				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) {
		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(code, error) {
				console.error(error);
				alert("Error fetching page title: " + code + "!");
			})
			.done(function(q) {
				if(q && q.query && q.query.pages && q.query.pages[0] &&
					q.query.pages[0].title) {
					console.log(q);
					var editParams = {
						action: 'edit',
						title: q.query.pages[0].title,
						watchlist: 'nochange',
						nocreate: '1',
						appendtext: ''
						};
						console.log(editParams);
					new mw.Api().postWithToken("csrf", editParams )
						.fail(function(code, error) {
							console.error(error);
							alert("Error performing null edit:" + code + "!");
						})
						.done( function(e) {
						console.log(e);
						mw.notify((count + 1) + " pages were updated", { tag: "bubble"+count } );
						count += 1;
						
						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 {
					console.error(q);
					alert("Error: Page title not found in API response!");
				}
			} );
	}
	
	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 target = mw.config.get("wgRelevantPageName").replace(/_/g, " ");
			if (mw.config.get('wgNamespaceNumber') == 10) {
				target = {
					generator: 'transcludedin',
	  				titles: target,
	  				gtilimit: 1
				};
				linkTitle = "transcluding pages";
				toolTipText = "that transclude this template.";
			} else if (mw.config.get('wgNamespaceNumber') == 14) {
				target = {
					generator: 'categorymembers',
	  				gcmtitle: target,
	  				gcmlimit: 1
				};
				linkTitle = "category members";
				toolTipText = "in this category.";
			} else {
				target = {
					generator: 'linkshere',
					titles: target,
	  				glhlimit: 1
				};
				linkTitle = "linking pages";
				toolTipText = "that link to this page.";
			}
			$(mw.util.addPortletLink('p-cactions', '#', 'Purge ' + linkTitle, 'pt-refresh-purge', 'Perform a "forcelinkupdate" purge on all pages ' + toolTipText))
				.click(function() {
					postPurge(target, 0, getWait(d, "purge"));
				});
			$(mw.util.addPortletLink('p-cactions', '#', 'Null edit ' + linkTitle, 'pt-refresh-null', 'Perform a null edit on all pages ' + toolTipText))
				.click(function() {
					postNull(target, 0, getWait(d, "edit"));
				});
		} );
	}
});