MediaWiki:MassPatrol/code.js

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

var i18n = { en: { patrol: 'Mass Patrol', patrolling: 'Patrolling...', patrolled: 'Edits patrolled: $1', patrolledNothing: 'Nothing to patrol' },       ru: { patrol: 'Массовое патрулирование', patrolling: 'В процессе...', patrolled: 'Правок отпатрулировано: $1', patrolledNothing: 'Нечего патрулировать' }   };

var MP = { _state: 0,

defines: $.extend({           CONSECUTIVE_DIFF: 1,            NON_CONSECUTIVE_DIFF: 2,            SPECIAL_NEW_PAGES: 4        }), config: mw.config.get([           'wgPageName',            'wgArticleId',            'wgUserGroups',            'wgContentLanguage'        ]), };   i18n = i18n[MP.config.wgContentLanguage] || i18n.en;

var MPFunctions = { init: function { if ( $('.patrollink').exists ) { MP._state = MP.defines.CONSECUTIVE_DIFF; // most probably diff with multiple non-consecutive versions } else if ( document.URL.match(/.+\/index\.php\?title=.+&diff=\d+&oldid=\d+/) && MPFunctions.isAllowedTo ) { MP._state = MP.defines.NON_CONSECUTIVE_DIFF; // special NewPages page } else if ( MP.config.wgPageName.match(/.+\:NewPages/) && MPFunctions.isAllowedTo ) { MP._state = MP.defines.SPECIAL_NEW_PAGES; } else { return; }

MPFunctions.addLink; },

fetchData: function { $.get('/api.php', {               action: 'query',                list: 'recentchanges',                rctoken: 'patrol',                rcshow: '!patrolled',                rctype: 'edit|new',                rclimit: 500,                format: 'json'                }, function (d) {                    if (MPFunctions.isSpecialNewPages) {                        MPFunctions.processSpecial(d, d.query.recentchanges[0].patroltoken);                    } else {                        MPFunctions.processDiff(d, d.query.recentchanges[0].patroltoken);                    }            }); },

processDiff: function(data, token) { var k = 0;

for ( var i = 0; i < Object.keys(data.query.recentchanges).length; i++ ) { if ( data.query.recentchanges[i].pageid === MP.config.wgArticleId ) { MPFunctions.patrolPage( data.query.recentchanges[i].rcid, token ); k += 1; }           }

MPFunctions.result(k); },

processSpecial: function(data, token) { var titles = []; var k = 0;

$('.not-patrolled').each(function {                   titles.push( $(this).find('.mw-newpages-pagename').attr('title') );                    $(this).removeClass('not-patrolled');                });

if (titles.length <= 0) { MPFunctions.result(0); return; }

for ( var i = 0; i < Object.keys(data.query.recentchanges).length; i++ ) { if ( titles.indexOf(data.query.recentchanges[i].title) > -1 ) { MPFunctions.patrolPage( data.query.recentchanges[i].rcid, token ); k += 1; }               }

MPFunctions.result(k); },

patrolPage: function(rcid, token) { $.post('/api.php', {               action: 'patrol',                token: token,                rcid: rcid                }, function  {            }); },

addLink: function { if ( MPFunctions.isConsecutiveDiff ) { $('.patrollink').after( ' [ ' + i18n.patrol + ']' ); } else if ( MPFunctions.isNonConsecutiveDiff ) { $('.diff-multi').append( ' [ ' + i18n.patrol + ']' ); } else { $('.mw-submit').append( ' [ ' + i18n.patrol + ']' ); }

$( '.masspatrol' ).on('click', function {                $('.masspatrol').html('');               MPFunctions.fetchData;            }); },

result: function(k) { var msg;

if (k > 0) { msg = i18n.patrolled.replace('$1', k); } else if (k <= 0) { msg = i18n.patrolledNothing; }

$('.patrollink').empty; $('.masspatrol').css('color', 'grey').text(msg); },

isAllowedTo: function { if (MP.config.wgUserGroups.indexOf('sysop') +               MP.config.wgUserGroups.indexOf('content-moderator') +                MP.config.wgUserGroups.indexOf('vstf') +                MP.config.wgUserGroups.indexOf('helper') +                MP.config.wgUserGroups.indexOf('staff') !== -5) { return true; }

return false; },

isConsecutiveDiff: function { if ( MP._state === MP.defines.CONSECUTIVE_DIFF ) { return true; }

return false; },

isNonConsecutiveDiff: function { if ( MP._state === MP.defines.NON_CONSECUTIVE_DIFF ) { return true; }

return false; },

isSpecialNewPages: function { if ( MP._state === MP.defines.SPECIAL_NEW_PAGES ) { return true; }

return false; }   };

$(MPFunctions.init); })(jQuery, mediaWiki);