MediaWiki:DiscordIntegrator/install.js

(function ($) {   if (!['sysop', 'bureaucrat'].some(function(v){return window.wgUserGroups.indexOf(v)>-1})) return;    var textDi = 'dev:DiscordIntegrator/code.js',        textDii = 'dev:DiscordIntegrator/install.js',        template = 'Template:DiscordIntegrator';    var templateContent = '' +        '   \n' +         '\n' +         '== Description ==\n' +         'This template is used as a utility for making usage of DiscordIntegrator easier. In order to integrate the widget, it first needs to be enabled, for instructions on how to do that check \"\'\'Enabling the widget\'\'\" section in the documentation.\n' +         '== Parameters ==\n' +         '{| class=\"wikitable\" style=\"width: 100%; text-align: center\"\n' +         '! Option\n' +         '! Type\n' +         '! What it changes\n' +         '! Default\n' + '|-\n' + '! id\n' + '| String\n' + '| ID of the server. For more information on how to find Discord server ID, see \"Enabling the widget\" section in the documentation \'\'\'This parameter is required for the widget to work\'\'\'\n' + '| N/A\n' + '|-\n' + '! loggedIn\n' + '| Boolean\n' + '| If set to true, widget will only work for logged in users\n' + '| N/A\n' + '|-\n' + '! theme\n' + '| String\n' + '| Theme of the widget. Can be set to either  or. If invalid option specified the default is used\n' + '| dark\n' + '|-\n' + '! width\n' + '| String\n' + '| Width of the widget.\n' + '| 100%\n' + '|-\n' + '! height\n' + '| String\n' + '| Height of the widget.\n' + '| 90%\n' + '|-\n' + '! usePTB</tt>\n' + '| Anything\n' + '| If to use the widget from Public Test Build server instead of the normal one\n' + '| N/A\n' + '|}\n' + '== See also ==\n' + '* w:c:dev:DiscordIntegrator for complete documentation\n' + ' ';

function getVal (data, key) { //travers through object tree var ret = [], r;       for (var k in data) { if (data[k] instanceof Object) { r = getVal(data[k], key); if (r) { ret = ret.concat(r); }           } else { if (k === key) { ret.push(data[k]); }           } //if obj } //for k in data return ret; } //getVal function chkTemplate (template) { var api = new mw.Uri('/api.php'); if (!template) template = 'template:DiscordIntegrator'; return new Promise(function(resolve, reject) {           $.getJSON(api.extend({                action:'query',                titles: 'Template:DiscordIntegrator',                prop: 'info',                intoken: 'edit',                format: 'json'            }))            .done(function(data) { if (data.query.pages['-1']) { data.token = data.query.pages['-1'].edittoken; reject(data); return; }               data.token = getVal(data, 'edittoken')[0]; resolve(data); })           .fail(function(data) { reject(data); });//gejson query template       }); }//chktemplate function createTemplate (template, token) { var mwApi = new mw.Api; if (!template) template = 'Template:DiscordIntegrator'; if (!token) token = mw.user.tokens.get('editToken'); return new Promise(function(resolve, reject) {           mwApi.post({ action: 'edit', title: template, text: templateContent, summary: 'integration of integrator template', recreate: true, bot: true, minor: true, token: token })           .done(function(data) { mw.log('integrator create template done', data); resolve(data); })           .fail(function(data) { mw.log('integrator create template fail', data); reject(data); });//mwapi post template       });//promise }//createtemplate function removeMe { //remove from importjs var token, api = new mw.Uri('/api.php'); mwApi = new mw.Api; token = mw.user.tokens.get('editToken'); $.getJSON(api.extend({ action: 'query', titles: 'MediaWiki:ImportJS', prop: 'info|revisions', rvprop: 'content', intoken: 'edit', format: 'json' }))       .done(function(data) {            var content = ;            token = getVal(data, 'editToken')[0] || token;            content = getVal(data, '*')[0] || ;            content = content.replace(textDii, '').replace('\n\n', '\n');            if (content.indexOf(textDi) === -1)                content = textDi + '\n' + content;            log('removeme content', content);            mwApi.post({ action: 'edit', title: 'MediaWiki:ImportJS', text: content, summary: 'integration of integrator template. import clean up', recreate: true, bot: true, minor: true, token: token })           .done(function(data) { mw.log('removeme.post ok', data); showBanner('ImportJS is clean. Follow <a href="/wiki/MediaWiki:ImportJS">MediaWiki:ImportJS</a> to check result.'); })           .fail(function(data) { mw.log('removeme.post fail', data); showBanner('Can\'t edit ImportJS. Follow <a href="/wiki/MediaWiki:ImportJS">MediaWiki:ImportJS</a> and do it manually.'); });//post importjs       }) .fail(function(data) {           mw.log('removeme fail', data);            showBanner('Something wrong happened. Follow <a href="/wiki/MediaWiki:ImportJS">MediaWiki:ImportJS</a> and do something.');       });//getjson importjs }//removeme function showBanner (text) { mw.loader.using('ext.bannerNotifications').done(function {           var banner = new BannerNotification;            banner.setContent(text);            banner.show;        });//mw.loader.using }//showbanner function main { chkTemplate.then(           function(data) {                mw.log('dii.main.chktemplate resolve', data);                showBanner('follow <a href="/wiki/MediaWiki:Custom-DiscordIntegrator-config-id">MediaWiki:Custom-DiscordIntegrator-config-id</a> to add id.');            },//resolve            function(data) {                mw.log('dii.main.chktemplate reject', data);                //showBanner('template not found. trying to create. follow <a href="/wiki/' + template + '">' + template + '</a> to check result.');               createTemplate.then( showBanner.bind(this, 'template created. follow <a href="/wiki/MediaWiki:Custom-DiscordIntegrator-config-id">MediaWiki:Custom-DiscordIntegrator-config-id</a> to add id.'), showBanner.bind(this, 'failed to create template') );//createtemplate           }//reject        );//chktemplate removeMe; }//main mw.loader.using('mediawiki.api').then(main); }(jQuery));