User:Ahecht/Scripts/refresh.js: Difference between revisions

Content deleted Content added
purge 50 at a time
Tag: Reverted
var api = new mw.Api( { userAgent: 'refresh/0.0.1' } );
 
(14 intermediate revisions by the same user not shown)
Line 5:
 
mw.loader.using( [ 'mediawiki.util', 'mediawiki.api' ] ).then( function() {
var api = new mw.Api( { userAgent: 'refresh/0.0.1' } );
var pageList = [];
const reuse = ( typeof refreshReuseBubble === 'undefined' ? false : refreshReuseBubble );
function getWaitshuffleArray(d, type, totalCountarray) {
console.log('Shuffling array...');
var wait=2000;
if for (dlet &&i d= array.querylength &&- d.query.userinfo1; i >= 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
function getWait(d, totalCount) {
var wait={edit: 8000, purge: 2100};
if (d && d?.query?.userinfo) {
var ratelimits = d.query.userinfo?.ratelimits;
if (d.query.userinfo.rights
&& d.query.userinfo.rights.includes("noratelimit")) {
wait = {edit: 1, purge: 1};
} else if (d.query.userinfo.ratelimits) {
['edit', 'purge'].forEach ( (v) => {
&& d.query.userinfo.ratelimits[type]
&& var u = d.query.userinfo.ratelimits?.[typev]?.user;
if (u && u?.hits && u?.seconds) {
&& d.query.userinfo.ratelimits[type].user.hits
console.log(v + " rate limit: hits=" + u.hits + ", seconds=" + u.seconds);
&& d.query.userinfo.ratelimits[type].user.seconds)
if (u.hits < totalCount) {
{
wait[v] = Math.ceil( (u.seconds/u.hits) * 1050 );
var hits = d.query.userinfo.ratelimits[type].user.hits;
} else {
var seconds = d.query.userinfo.ratelimits[type].user.seconds;
console.log(type totalCount+ " rateitems limit:to hits="refresh +is hitsless +than ", seconds=" + secondsu.hits);
wait[v] = 1;
if (hits < totalCount) {
}
wait = Math.ceil( (seconds/hits) * 1000 );
} else {
} );
console.log(totalCount+" items to refresh is less than "+hits);
wait = 1;
}
}
}
console.log("UsingMillisecond " + wait + " milliseconds waitwaits between queries:");console.log(wait);
return wait;
}
Line 40 ⟶ 48:
setTimeout(function() {
doRefresh(action, count, totalCount, wait);
}, wait[action]);
} else if (confirm("Done!\n\nReload page?") == true) {
document.___location.reload();
} else {
mw.notify("Done!", {type: 'success', tag: "bubble" + (reuse ? 0 : 'done')});
}
}
Line 48 ⟶ 58:
function postFail(code, error) {
console.error(error);
var err = error?.textStatus || code;
if (confirm("Error performing " + action + ":" + code + "!\n\nContinue?") == true) {
var errMsg;
//if (confirm("Error performing " + action + ": " + err + "!\n\nContinue?") == true) {
errMsg = "Continuing after "+err+" error...";
postDone();
//} else {
// errMsg = "Aborted due to "+err+" error!";
//}
mw.notify(errMsg, { tag: "bubble" + (reuse ? 0 : "error"), type: 'error',
autoHideSeconds: (action == 'purge') ? 'long' : 'short' } );
}
function postSuccess() {
var tag = reuse ? 0 : count;
count = count + ( (action == "purge") ? ((apiParams.titles.match(/\|/g) || []).length + 1) : 1 );
mw.notify(count + " of " + totalCount + " page(s) were updated", {+ tag:((action == "bubblepurge"+tag) }? "purged" : "edited");, {
tag: "bubble" + (reuse ? 0 : "count"),
autoHideSeconds: (action == 'purge') ? 'long' : 'short'
} );
postDone();
}
Line 63 ⟶ 82:
if (action == "purge") {
// 'purge' is always once every two second unless 'noratelimit',
apiParams.titles = pageList.splice(0, 50).join('|');
// so give editors with higher 'edit' rate a boost.
// More than 3 at a time leads to timeouts.
var numPages = wait.edit < 1000 ? 3 : 1;
apiParams.titles = pageList.splice(0, numPages).join('|');
console.log(apiParams.titles);
apiParams.forcerecursivelinkupdate = "1";
new mw.Api().post(//apiParams).fail(postFail).done(postSuccess)forcelinkupdate = "1";
api.post(apiParams).fail(postFail).done(postSuccess);
} else {
apiParams.title = pageList.shift();
console.log(apiParams.title);
apiParams.watchlist = "nochange";
apiParams.nocreate = "1";
apiParams.appendtext = "";
new mw.Api()api.postWithEditToken(apiParams).fail(postFail).done(postSuccess);
}
}
Line 84 ⟶ 110:
target,
addParams);
var list = target?.list || "pages";
new mw.Api().post(queryParams).fail(function(code, error) {
if (queryParams.cmsort && window.refreshCatSort) queryParams.cmsort = window.refreshCatSort;
if (queryParams.cmdir && window.refreshCatDir) queryParams.cmdir = window.refreshCatDir;
if (queryParams.srsort && window.refreshSearchSort) queryParams.srsort = window.refreshSearchSort;
console.log(queryParams);
api.get(queryParams).fail(function(code, error) {
console.error(error);
alert("Error fetching page titles: " + code + "!");
} ).done(function(q) {
if(q && q.warnings === undefined && q?.query?.[list] &&!== q.query.pagesundefined) {
for (var page in q.query[list].pages)forEach(page => {
if (qpage.querytitle) pageList.pages[push(page].title) {;
});
pageList.push(q.query.pages[page].title);
if (q?.continue?.continue !== undefined) {
}
getList(action, target, q.continue);
}
if (q["continue"] !== undefined
&& (q["continue"].gticontinue
|| q["continue"].gcmcontinue
|| q["continue"].glhcontinue
)
) {
getList(action, target, q["continue"]);
} else {
if (window.refreshShuffle) shuffleArray(pageList);
console.log(pageList);
new mw.Api()api.get( {
meta: 'userinfo',
uiprop: 'ratelimits|rights'
} ).fail( function(e) {
console.error(e);
doRefresh(action, 0, pageList.length, 1000{edit: 8000, purge: 2100});
} ).done( function(ui) {
doRefresh(action,var 0,len pageList.length, getWait(ui, action,= pageList.length));
mw.notification.autoHideLimit = len;
mw.notify('Performing '+action+' on '+len+' page(s)...', {
autoHideSeconds: (action == 'purge') ? 'long' : 'short',
tage: 'bubble0'
} );
doRefresh(action, 0, len, getWait(ui, len));
} );
}
Line 117 ⟶ 148:
}
var linkshere = mw.config.get("wgCanonicalSpecialPageName") == "Whatlinkshere";
var sparams = new URLSearchParams(window.___location.search);
var search= (mw.config.get("wgCanonicalSpecialPageName") == "Search" &&
sparams.get('search') && sparams.get('search') != '');
if ( (mw.config.get('wgNamespaceNumber') == 10)
|| (mw.config.get('wgNamespaceNumber') == 14)
|| (mw.config.get('wgNamespaceNumber') == 828)
|| linkshere || search)
|| (mw.config.get("wgCanonicalSpecialPageName") == "Whatlinkshere") )
{
var linkTitle="linking pages", toolTipText="that link to this page.";
var target = mw.config.get("wgRelevantPageName").replace(/_/g, " ");
var targetNS = mw.Title.newFromText(target).getNamespaceId();
var query = {
if ( (targetNS == 10) || (targetNS == 828) ){
target = {
generator: 'transcludedin',
titles: target,
gtilimit: 'max'
};
linkTitle = "transcluding pages";
toolTipText = "that transclude this template.";
} else if (targetNS == 14) {
target = {
generator: 'categorymembers',
gcmtitle: target,
gcmlimit: 'max'
};
linkTitle = "category members";
toolTipText = "in this category.";
} else {
target = {
generator: 'linkshere',
titles: target,
glhlimit: 'max'
};
if (search) {
linkTitle = "linking pages";
toolTipTextlinkTitle = "thatsearch link to this page.results";
toolTipText = "in this search result.";
query = {
list: 'search',
srsearch: sparams.get('search'),
srlimit: 'max',
srsort: 'last_edit_desc',
srprop: ''
};
nslist = [];
for(const e of sparams.entries()) {
var nsp = e[0].match(/^ns(\d+)/);
if (nsp?.[1] && e[1] == 1) nslist.push(nsp[1]);
}
if (nslist.length > 0 ) query.srnamespace = nslist.join("|");
 
} else if ( (targetNS == 10) || (targetNS == 828) ){
if ( linkshere ) {
toolTipText = "that link to this template.";
} else {
query = {
generator: 'transcludedin',
titles: target,
gtilimit: 'max'
};
linkTitle = "transcluding pages";
toolTipText = "that transclude this template.";
}
} else if (targetNS == 14) {
if ( linkshere ) {
toolTipText = "that link to this category.";
} else {
query = {
list: 'categorymembers',
cmtitle: target,
cmlimit: 'max',
cmsort: 'timestamp',
cmdir: 'desc'
};
linkTitle = "category members";
toolTipText = "in this category.";
}
}
$(mw.util.addPortletLink('p-cactions', '#', 'Purge ' + linkTitle, 'pt-refresh-purge', 'Perform a "forcelinkupdate" purge on all pages ' + toolTipText))
.click(function() {
getList("purge", targetquery);
});
$(mw.util.addPortletLink('p-cactions', '#', 'Null edit ' + linkTitle, 'pt-refresh-null', 'Perform a null edit on all pages ' + toolTipText))
.click(function() {
getList("edit", targetquery);
});
}