MediaWiki:MassCreate/code.js

//MassCreate - Count of Howard //01-01-17 - Initial revision

const mwVariables = mw.config.get([       'wgUserLanguage',        'wgUserGroups'    ]);
 * (function($, mw) {

const i18n = { 'en': { //English itemTitle: 'MassCreate', modalTitle1: 'Enter desired content template or string', modalTitle2: 'Enter pages, separated by commas only (no spaces)', modalCancel: 'Cancel', modalCreate: 'Create', modalClear: 'Clear', modalTemplateplaceholder: '', modalPagesplaceholder: 'Example 1,Example 2,Example 3', modalHeader: 'Notice: To prevent abuse, use of MassCreate is limited to local staff, Fandom Staff, Fandom Helpers, and VSTF. Input values and length are restricted for security reasons, and existing pages will not be overwritten.', modalLog: 'MassCreate log', modalSuccess: 'Success: Page successfully created!', modalFailure: 'Error: Page not created. Please try again.', modalError: 'Error: Use of some characters is prohibited for security reasons.', modalUserRights: 'Error: Incorrect user rights group.', modalExists: 'Error: MassCreate will not overwrite existing pages.', mcSummary: 'Creating from template', mcScript: 'script' }   };

const lang = mwVariables.wgUserLanguage; const textContent = i18n.hasOwnProperty(lang) ? i18n[lang] : i18n.en;

//Security stuff - This script can cause a lot of damage if abused, so users and input values are restricted const userGroups = /(bureaucrat|sysop|content-moderator|rollback|staff|helper|vstf)/.test(mwVariables.wgUserGroups.join(' ')); const pagesChars = /^[a-zA-Z0-9-. ]*$/;   const templateChars = /^[a-zA-Z0-9-{}: ]*$/; const maxchars = 500;

const modalHTML = '\  \ ' + textContent.modalHeader + ' \ \ \       \            ' + textContent.modalTitle1 + ' \ \                \ \           \ \            ' + textContent.modalTitle2 + ' \ \                \ \           \        \ \        \    \    ' + textContent.modalLog + ' \ ';

mw.util.addCSS(       '#mc-template-value,' +        '#mc-pages-value {' +            'height: 30px;' +            'width: 100%;' +            'padding: 0;' +        '}' +        '#mc-log {' +            'height: 100px;' +            'width: 98%;' +            'border: 1px solid black;' +            'font-family: monospace;' +            'background: #fff;' +            'color: #aeaeae;' +            'overflow: scroll;' +            'padding:5px;' +        '}'    );

const mcItem = mw.html.element('li', {       class: 'overflow',        id: 'mc-item'    });

const mcItemLink = mw.html.element('a', {       href: '#'    }, textContent.itemTitle);

function init { $(mcItem).appendTo('.toolbar .tools'); $(mcItemLink).appendTo('#mc-item'); $('#mc-item').click(function {           if (document.contains(document.getElementById('mc-modal-window'))) {                document.getElementById('mc-modal-window').remove;            }

$.showCustomModal(textContent.itemTitle, modalHTML, {               id: 'mc-modal-window',                width: 500,                buttons: [{                    message: textContent.modalCancel,                    handler: function {                        $('#mc-modal-window').closeModal;                    }                }, {                    message: textContent.modalClear,                    handler: function {                        document.getElementById("mc-modal-form").reset;                    }                }, {                    message: textContent.modalCreate,                    handler: function {                        if (!userGroups) {                            document.getElementById("mc-modal-form").reset;                            $('#mc-log').append(textContent.modalUserRights + ' ');                            return;                        } else { var inputTemplate = document.getElementById('mc-template-value').value; var inputPages = document.getElementById('mc-pages-value').value; var inputPagesArray = inputPages.split(/[,]+/);

checkInput(inputTemplate, inputPagesArray); }                   }                }]            });        });    }

function checkInput(template, array) { if(!templateChars.test(template)) { document.getElementById("mc-modal-form").reset; $('#mc-log').append(textContent.modalError + ' '); return; }

$.each(array, function(index, value) {           if(!pagesChars.test(value)) {                document.getElementById("mc-modal-form").reset;                $('#mc-log').append(textContent.modalError + ' ');                return;            } else {                var XMLrequest = new XMLHttpRequest;                XMLrequest.open('GET', '/api.php?action=query&titles=' + value + '&format=xml', true);                XMLrequest.onload = function {                    if (this.readyState == 4 && this.status == 200) {                        var xml = this.responseXML;                        var x = xml.getElementsByTagName("page")[0];                        if (x.hasAttribute("missing") || x.hasAttribute("invalid")) {                            createPages(template, value);                        } else {                            document.getElementById("mc-modal-form").reset; $('#mc-log').append(textContent.modalExists + ' '); }                   }                };                XMLrequest.send; }       });    }

function createPages(temp, page) { $.ajax({           type: 'POST',            url: mw.util.wikiScript('api'),            dataType: 'json',            data: {                action: 'edit',                title: page,                summary: textContent.mcSummary + ' (' + textContent.mcScript + ')',                text: temp,                format: 'json',                token: mw.user.tokens.get('editToken')            }        }).success(function (data) {            document.getElementById("mc-modal-form").reset;            $('#mc-log').append(textContent.modalSuccess + ' ');        }).fail(function (data){            document.getElementById("mc-modal-form").reset;            $('#mc-log').append(textContent.modalFailure + ' ');        }); }   init; })(this.jQuery, this.mediaWiki);