MediaWiki:MassPatrol.js

/* * Name:        MassPatrol * Description: Allows to massively patrol edits * Author:      Rendann * Support:     Aenn, BertH, Your Own Waifu * Files used: * Scripts used: * https://dev.wikia.com/wiki/MediaWiki:AjaxPatrol/code.js */ (function ($, mw) {	'use strict';

// Load Protection. if (window.MassPatrolLoaded) return;

window.MassPatrolLoaded = true;

const strings = { patrol: 'Mass Patrol', patrolling: 'Patrolling...', patrolled: 'Edits patrolled: $1', patrolledNothing: 'Nothing to patrol', unpatrolled: 'This edit has not yet been patrolled.' };

//Prepare PageCfg var pageConfig = mw.config.get(['wgDiffOldId', 'wgDiffNewId', 'wgPageName', 'wgAction']);

function init { var api = new mw.Api; var MassPatrol = { revids:[], patroltoken:'', total: 0, init: function { // Not on Diff page or History Page. if (pageConfig.wgDiffNewId === null && pageConfig.wgAction !== 'history') return;

// Load API. api.get({					format: 'json',					action: 'query',					list: 'recentchanges',					rcprop: 'ids|patrolled',					rcdir: 'newer',					rclimit: 'max',					rctitle: pageConfig.wgPageName,					meta: 'tokens',					type: 'patrol'				}).then(MassPatrol.apiLoad); },			apiLoad: function(data) { // Grab RC into array. var rcArray = data.query.recentchanges;

// If history page, fork off. if (pageConfig.wgAction === 'history') { return MassPatrol.history(rcArray.filter(function(e) { return !('patrolled' in e); }));				}

// Bulk of Mass Patrol code. MassPatrol.patroltoken = data.query.tokens.patroltoken; var capture = false;

for (var i = 0; i < rcArray.length; i++) { var each = rcArray[i];

if (each.revid >= (pageConfig.wgDiffOldId || 0)) capture = true; // Start Capture. if (capture && !('patrolled' in each)) // Capturing starts, and unpatrolled. MassPatrol.revids.push(each.revid); if (each.revid >= pageConfig.wgDiffNewId) break; // End early. }

// Set max patrol length (for end counter: will be shift-ing this array). MassPatrol.total = MassPatrol.revids.length;

// Build HTML For Mass Patrol button. var button = $(' ', {					id: 'massPatrol'				}).append('[',$('',{ href: '#', text: strings.patrol }), ']');

// Cannot be added directly to the button as it will not render in new page edits. // @todo: update image. document.addEventListener('click', function(event) {					if (event.target && event.target.closest('#massPatrol') && document.querySelector('#massPatrol a')) {						event.preventDefault;						document.querySelector('#massPatrol a').outerHTML = '';						MassPatrol.patrolRecursive;					}				});

// Can patrol, and has at least 1 patrol, apply built HTML. if (MassPatrol.patroltoken.length > 2 && MassPatrol.total >= 1) $('#mw-diff-ntitle4').append(' ',button); },			history: function(data) { var unpatrolled = $(' ',{					class: 'unpatrolled',					title: strings.unpatrolled,					text: '\xa0!\xa0'				}); var obj = $('li[data-mw-revid]').filter(function(i, obj) {					return data.find(function(e) { return e.revid == $(obj).data('mw-revid'); });				}).prepend(unpatrolled); },			patrolRecursive: function { if (MassPatrol.revids.length === 0) { return $('#massPatrol').text('[' + strings.patrolled.replace('$1', MassPatrol.total) + ']'); }				api.post({					action: 'patrol',					format: 'json',					revid: MassPatrol.revids.shift,					token: MassPatrol.patroltoken				}).then(MassPatrol.patrolRecursive); }		};		MassPatrol.init; }

// Add Hook when finish loading I18n-js and API is ready for use. mw.loader.using('mediawiki.api').done(function {		init;	}); })(window.jQuery, window.mediaWiki);