User:Botniac/common.js

/*
 * Mass Protect
 * @description Protect listed multiple pages
 * @author Ozank Cx

/* eslint-env browser,jquery */ /* global mw */

console.log('MassProtect v2.0.19');

mw.loader.using(['mediawiki.util', 'mediawiki.api'], function {  if ($('#t-mp').length) { return; }

var ug = mw.config.get('wgUserGroups'); if (ug.indexOf('sysop') + ug.indexOf('vstf') + ug.indexOf('staff') + ug.indexOf('helper') + ug.indexOf('content-moderator') > -5) { var FormHTML = '\  \ \         Protection: \ Edit: \  \ All \ Autoconfirmed \ Admin \ \         \          Move: \  \ All \ Autoconfirmed \ Admin \ \         \          Upload: \  \ All \ Autoconfirmed \ Admin \ \         \          Create: \  \ (Existing page) \ All \ Autoconfirmed \ Admin \ \         \          \          Expiry time: \  \ \         \          Reason: \  \ \         \          Put the name of each page you want to protect on a separate line:. \          \ \         Any errors encountered will appear below  \ \     ',      token = mw.user.tokens.get('editToken'), Api = new mw.Api, delay = window.massProtectDelay || 1000;

// Support for Monobook if (mw.config.get('skin') === 'monobook') { mw.util.addPortletLink('p-tb', '#', 'Mass Protect', 't-mp'); } else { $('#my-tools-menu').prepend(' Mass Protect</a></li>'); }

$('#t-mp').click(function {      $.showCustomModal('Mass Protect', FormHTML, { id: 'form-mass-protect', width: 500, buttons: [{ message: 'Cancel', handler: function { $('#form-mass-protect').closeModal; }       }, {          message: 'Add category contents', defaultButton: true, handler: function { addCategoryContents; }       }, {          id: 'startButton', message: 'Initiate', defaultButton: true, handler: function { init; }       }]      });    });

function init { var txt = document.getElementById('text-mass-protect'), pages = txt.value.split('\n'), currentPage = pages[0];

document.getElementById('startButton').setAttribute('disabled', 'disabled');

if (!currentPage) { document.getElementById('startButton').removeAttribute('disabled'); $.showCustomModal('Finished!', 'Nothing left to do, or next line is blank.', {         id: 'mass-protect-complete',          width: 200,          buttons: [{            message: 'Close',            defaultButton: true,            handler: function  {              $('#mass-protect-complete').closeModal;            }          }]        }); } else { process(currentPage); }     pages = pages.slice(1, pages.length); txt.value = pages.join('\n'); }

function addCategoryContents { var category = prompt('Please enter the category name (no category prefix):'); Api.get({       action: 'query',        list: 'categorymembers',        cmtitle: 'Category:' + category,        cmlimit: 5000      }) .done(function (d) {         if (!d.error) {            var data = d.query;

for (var i in data.categorymembers) { var currTitles = $('#text-mass-protect').val; $('#text-mass-protect').val(currTitles + data.categorymembers[i].title + '\n'); }         } else { $('#text-error-output').append('Failed to get contents of ' + category + ' : ' + d.error.code + ' '); }       })        .fail(function  { $('#text-error-output').append('Failed to get contents of ' + category + '! '); });   }

function process (page) { Api.post({       action: 'protect',        expiry: $('#protect-expiry').val || $('#protect-expiry').attr('placeholder'),        protections: $('#protection-create').val || [$('#protection-edit').val, $('#protection-move').val, $('#protection-upload').val].filter(Boolean).join('|'),        watchlist: 'nochange',        title: page,        reason: $('#protect-reason').val,        token: token      }) .done(function (d) {         if (!d.error) {            console.log('Protection of ' + page + ' successful!');          } else {            console.log('Failed to protect ' + page + ': ' + d.error.code);            $('#text-error-output').append('Failed to protect ' + page + ': ' + d.error.code + ' ');          }        }) .fail(function {          console.log('Failed to protect ' + page);          $('#text-error-output').append('Failed to protect ' + page + ' ');        }); setTimeout(init, delay); } } });