User:Ainz Ooal Gown/PatrolPageRevisions.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/**
 * Forked from [[MediaWiki:PatrolPageRevisions.js]]
 *
 * Adds button that when pressed patrols all revisions of one page
 *
 */
/* global $, mw, OO */
mw.loader.using('mediawiki.api').then(function () {
	// Request rights data of the current user using a GET call to mw.api		
	var  api = new mw.Api(),
		rightsData = api.get({
		action: "query",
		format: "json",
		list: "users",
		formatversion: "2",
		usprop: "rights",
		ususers: mw.config.get('wgUserName')
		}),
		ppr, pprBtn, pt;
	// When the response is ready, then function
	$.when(rightsData, $.ready).then(function (rightsDataResults) {
		if (
			rightsDataResults[0].query.users[0].rights &&
			mw.config.get('wgAction') === 'history' &&
			rightsDataResults[0].query.users[0].rights.indexOf('patrol') !== -1
			// Load this script only if current user has patrol rights and if the action is history
		) {
			// User rights validation ended. PPR script code begins

			$(document).ready(function () {
				'use strict';

				function firstItem(o) {
					for (var i in o) {
						if (o.hasOwnProperty(i)) {
							return o[i];
						}
					}
				}


				ppr = {
					init: function () {
						pt = mw.user.tokens.get('patrolToken');
						if (!pt) return mw.log('[[MediaWiki:PatrolPageRevisions.js]] found no patrol token for you. Are you still a patroller?');

						pprBtn = new OO.ui.ButtonWidget({
							label: 'Patrol all revisions',
							title: 'Patrols up to 500 revisions of this page.',
							flags: 'progressive',
							id: 'pprBtn'
						});

						$(pprBtn.$element).insertAfter($('.mw-history-compareselectedversions-button').first()).click(ppr.doPatrol);


					},
					doPatrol: function (e) {
						e.preventDefault();
						//Disable the button and change the label once the button is clicked
						pprBtn.setDisabled(true);
						pprBtn.setLabel('Patrolling …');

						mw.loader.getScript('https://commons.wikimedia.org/w/load.php?modules=ext.gadget.libAPI').then(function () {
							mw.libs.commons.api.$query({
								'action': 'query',
								'prop': 'revisions',
								'titles': mw.config.get('wgPageName').replace(/_/g, ' '),
								'rvprop': 'ids',
								'rvlimit': 500
							}).done(function (r) {
								var pg = firstItem(r.query.pages),
									reqOk = 0,
									reqExpired = 0,
									reqFailed = 0,
									reqIssued = 0;

								$.each(pg.revisions, function (i, rev) {
									// FIXME: AND NOW ... WE DO EVIL! WE'RE FLOODING THE API.
									reqIssued++;
									api.postWithToken('patrol', {
										'format': 'json',
										'action': 'patrol',
										'revid': rev.revid
									}).done(function (r) {

										if (r && r.patrol) {
											reqOk++;
										} else if (r && r.error && (r.error.code === 'notpatrollable' || r.error.code === 'nosuchrevid' || r.error.code === 'noautopatrol')) {
											reqExpired++;
										} else {
											reqFailed++;
										}

									}).fail(function () {
										reqFailed++;
									}).always(function () {
										if (reqOk + reqFailed + reqExpired === reqIssued) {
											//Change the label to show the results after the request is completed
											pprBtn.setLabel('TOTAL: ' + reqIssued + ' Ok:' + reqOk + ' (Expired:' + reqExpired + ') Failed:' + reqFailed);
											return;

										}
									});
								});
							});
						});
					}
				};

				mw.loader.using(['oojs-ui-core', 'user.options'], ppr.init);

			});
			// PPR script ends

		}
	});
});