User:BDavis (WMF)/Scripts/GlobalWatchlistReset.js
< User:BDavis (WMF) | Scripts
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
(function ($, mw) {
/**
* Iterate over all SiteMatrix wikis.
* @source https://meta.wikimedia.org/wiki/User:Krinkle/Scripts/iterate-sitematrix.js
*/
if (mw.siteMatrix === undefined) {
mw.siteMatrix = {};
}
mw.siteMatrix.Iterator = function Iterate(options) {
var methods, i, current, list, instance = this;
if (!options.centralApiPath || !options.onIteration) {
throw new Error('Invalid arguments');
}
if (!(this instanceof mw.siteMatrix.Iterator)) {
throw new TypeError('Illegal function call');
}
instance.start = function start() {
if (i !== undefined) {
throw new Error('Cannot start twice');
}
$.getJSON(options.centralApiPath + '?format=json&action=sitematrix&callback=?', function (data) {
i = 0;
list = [];
if (!data || !data.sitematrix) {
return;
}
$.each(data.sitematrix, function (key, value) {
var wi, wo;
var group;
if (key === 'count') return;
group = key === 'specials' ? value : value.site;
if ($.isArray(group) && group.length) {
for (wi = 0; wi < group.length; wi += 1) {
// Only public wikis
if (group[wi].private === undefined && group[wi].closed === undefined && group[wi].fishbowl === undefined) {
list.push(group[wi]);
}
}
}
});
instance.next();
});
};
instance.next = function next() {
if (i < list.length) {
current = list[i];
options.onIteration(instance, current, i, list.length);
i += 1;
} else {
options.onComplete(instance, current, list.length);
}
};
return instance;
};
function initGlobalWatchlistReset() {
var $content, $fieldset, $subtitle, $table,
$status, $btnStart, $progress, $done, $log,
globalaccountinfo;
$content = $('#bodyContent').empty();
$fieldset = $('<fieldset>');
$subtitle = $('<div id="contentSub"></div>');
$table = $(
'<table class="wikitable" style="width: 100%;"><tbody>'
+ '<tr><th>Status</th><th>Progress</th></tr>'
+ '<tr>'
+ '<td id="mw-gwlreset-status" style="width: 80%;">Ready for action! <button id="mw-gwlreset-start">Start</button></td>'
+ '<td style="vertical-align: top;"><span id="mw-gwlreset-progress">0%</span>'
+ '<span id="mw-gwlreset-done" style="float: right;"></span>'
+ '</td>'
+ '</tr>'
+ '<tr>'
+ '<td colspan="2" id="mw-gwlreset-log" style="padding-top: 1em;"><ul></ul></td>'
+ '</tr></tbody></table>'
);
$status = $table.find('#mw-gwlreset-status');
$btnStart = $table.find('#mw-gwlreset-start');
$log = $table.find('#mw-gwlreset-log > ul');
$progress = $table.find('#mw-gwlreset-progress');
$done = $table.find('#mw-gwlreset-done');
function doUpdate(msg, iterationNr, listLength) {
$status.text(msg);
$log.prepend('<li>' + new Date().toString().replace(/^\w+ /, '').replace(/:[^:]+$/, '') + ': ' + mw.html.escape(msg) + '</li>');
if (iterationNr && listLength) {
$progress.text((Math.round(((iterationNr) / listLength) * 100 * 10) / 10) + '%');
$done.text('(' + iterationNr + '/' + listLength + ' wikis)');
}
}
// Build front-end
$('#firstHeading').text('Global watchlist reset');
document.title = 'Global watchlist reset - ' + mw.config.get('wgSiteName');
$fieldset.text('Reset all of your watchlists').append($table);
// Bind events
$btnStart.click(function (e) {
$(this).remove();
doUpdate('Initializing...');
var iterator = new mw.siteMatrix.Iterator({
centralApiPath: mw.util.wikiScript('api'),
onIteration: function (instance, wikiObj, iterationNr, listLength) {
// console.log(wikiObj);
$status.text('Attempting ' + wikiObj.dbname + ' ...');
var api = new mw.ForeignApi(wikiObj.url + '/w/api.php');
api.postWithToken(
'csrf',
{
action: 'setnotificationtimestamp',
entirewatchlist: true,
assert: 'user',
formatversion: 2
}
).done(function () {
// console.log(arguments);
doUpdate(wikiObj.dbname + ': done', iterationNr, listLength);
}).fail(function () {
console.log(arguments);
doUpdate(wikiObj.dbname + ': ' + arguments[0], iterationNr, listLength);
}).always(function () {
setTimeout(instance.next, 1);
});
},
onComplete: function (instance, wikiObj, listLength) {
doUpdate('Finished.', listLength, listLength);
$status.html('Reset ' + attemptedList.length + ' watchlists.');
}
});
iterator.start();
});
$content.append($subtitle, $fieldset);
}
// Enqueue init
if (mw.config.get('wgCanonicalSpecialPageName') === 'Blankpage' && mw.config.get('wgTitle').indexOf('/globalwatchlistreset') > 2) {
mw.loader.using(['mediawiki.util','mediawiki.ForeignApi'], initGlobalWatchlistReset);
}
}(jQuery, mediaWiki));