User:Ahecht/Scripts/refresh.js
Appearance
< User:Ahecht | Scripts
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Ahecht/Scripts/refresh. |
// 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"));
});
} );
}
});