MediaWiki:PageFormatter/code.js

//////////////////////////////////////////////////////////////////////////////////////      //                                                                                  //      // Title: Page Formatter. //    // Function: Reformats a template to look appropriate, Replace automatically,etc. //             //--//   // Authors: Ditto Creeper Bot, KockaAdmiralac (Debug), Andrey Andrey (Beta Tester)  // // Inspiration: Pain actually, I saw the fiasco others were going through and I had // // take action. ^-^                                                                // ////////////////////////////////////////////////////////////////////////////////////// // Variables. realpipeSpace = false || window.pipeSpace; // Button Placement. $('.wikia-page-header .header-column.header-title').append('Clean Up! '); $('#PageCleaner').on('click', function {   var CleanMod = ' .modalWrapper{width: initial !important;\    height: auto;\    margin-left: -225px;\    top: 50px;\    z-index: 5001102;}  Page Formatter   table#PageClean td button {width:100%}   ';    $.showCustomModal('', CleanMod);    var ds;    $('.TempExtr').on('click', function { mw.loader.using('mediawiki.api').then(function {           ds = new mw.Api;            // Work around the api.            var API = new mw.Api;            ds.get({ action: 'query', prop: 'revisions', titles: wgPageName, rvprop: 'content', rvlimit: 500, cb: new Date.getTime }).done(function(r) { for (var i in r.query.pages) { var intialPrompt = prompt("There are " + r.query.pages[i].revisions[0]["*"].match("{{").length + " templates in this page, which should I re-format?", "1"); //This lets you select the thing, make sure the number's above 0. var finalPrompt = parseInt(intialPrompt); var something = r.query.pages[i].revisions[0]["*"].replace(/\=\"/gm, 'VzV"').replace(/ \| /gm, '\|').replace(/\| /gm, '\|').replace(/ /g, '_') //replace(/\[N\]'); // Don't judge me with this. ;v; startingPoint = new RegExp(/\w*\W*\[*.\|*\]/,"gm"); // I learned a new thing. ^-^                   EndingPoint = something.match(startingPoint); // Had no idea it was that hard. for (i = 0; i < 90; i++) { pipetoReplace = '|' + EndingPoint[i]; pipeReplacedBy = 'XzX' + EndingPoint[i]; // Why not? ^-^                       something = something.replace(pipetoReplace, pipeReplacedBy) }                   var TemplateOneName = something.split("{{")[finalPrompt]; // Duh? var TemplateTwoName = TemplateOneName.split("|")[0]; // Getting the template's name with this. var initialContentOne = something.split("{{")[finalPrompt]; var initialContentTwo = initialContentOne.split("}}")[0]; var finalContent = initialContentTwo //.replace(TemplateTwoName, ''); var stageOne = finalContent.replace(/\n/gm, '').replace(/\|/g, '\n|'); //.replace(/\n\n/gm, '\n'); var stageTwo = stageOne.split('\n'); lastNumber = 0; for (stem = 0; stem < stageTwo.length; stem++) { // This creates the spacing. stageThree = stageOne.split('\n')[stem].search("="); if (lastNumber < stageThree) { lastNumber = stageThree; // Stores the larges spaced equal sign. }                   }                    stageFour = stageOne.replace(/_=_/gm, '=').replace(/=/gm, ' = ').replace(/  /gm, ' ').replace(/\n/gm, '\n'), // This cleans the whole thing. stageSeven = [], stageNine = [], stageEleven = [], stageEight = [], stageEleven[0] = '', stageFifteen = '', stageFourteen = [], stageThirteen = '', stageSixteen = 0; for (boom = 1; boom < stageFour.split('\n').length; boom++) { // You may notice the stages? stageFive = stageFour.split('\n')[boom].search(" "); stageSix = lastNumber - stageFive - 1; // Removes one space because of stageFour. stageSeven[boom] = stageFour.split('\n')[boom].split('=')[1]; // This is the text after the equal sign. helpme = boom; // Second choice. stageFifteen = ''; // Resetter. I don't want more spaces. ;v; if (stageSix !== 0) { for (doom = 0; doom <= stageSix; doom++) { stageFifteen = ' ' + stageFifteen; // This is where the magic happens. }                       } else { stageFifteen = ' ' }                       stageNine[boom] = stageFour.split('\n')[boom].split('=')[0].replace(/ /gm, ''); stageEleven[boom] = stageNine[boom] + stageFifteen; plzhelp = boom + 1; helpme = boom; if (stageFour.split('\n')[boom].search('=') !== -1) { // If there's an equal sign. stageEight[helpme] = stageEleven[helpme].replace(/\n\n/gm, '\n') + '=' + stageSeven[helpme].replace(/ /gm, ' ') + '\n'; } else { //otherwise, ignore. stageEight[helpme] = stageFour.split('\n')[boom].replace(/\n\n/gm, '\n') + '\n'; }                   }                    stageTwelve = stageEight.join(''); //Join the entire array as a whole string. stageTwelve = stageTwelve.replace(/XzX/gm, '\|').replace(/VzV/gm, '='); // Replaces my boi. for (stageTwelveBeta = 0; stageTwelveBeta < 90; stageTwelveBeta++) { stageNineteen = stageTwelve.replace(/undefined/gm, '').replace(/\n\n/gm, '\n').replace(/= /gm, '= ').replace(/=      /gm, '= ').replace(/=  /gm, '= ').replace(/=    /gm, '= ').replace(/=    /gm, '= ').replace(/=  /gm, '= ').replace(/=  /gm, '= ').replace(/_/gm, ' ').replace(/\=  /gm, '= '); // Cleans out any errors. }                   if (realpipeSpace == true) { //$.showCustomModal $('#CleanTextArea').val('{{' + TemplateTwoName + stageNineteen.replace(/\n\|/gm, '\n \| ').replace(/\n\|/gm, '\n \| ').replace(/ \|  /gm, ' \| ') + '}}') // console.log('{{' + TemplateTwoName + stageNineteen.replace(/\n\|/gm, '\n \| ').replace(/\n\|/gm, '\n \| ').replace(/ \|  /gm, ' \| ') + '}}') new BannerNotification('Successfully extracted from ' + wgPageName,'confirm','',4000).show; } else { // console.log('{{' + TemplateTwoName + stageNineteen + '}}') //$.showCustomModal $('#CleanTextArea').val('{{' + TemplateTwoName + stageNineteen + '}}'); new BannerNotification('Successfully extracted from ' + wgPageName,'confirm','',4000).show; }               }            })        });    });    $('#ReformTemp').on('click', function { //This lets you select the thing, make sure the number's above 0. TextCLAr = $('#CleanTextArea').val; var finalPrompt = 1; var something = TextCLAr.replace(/\=\"/gm, 'VzV"').replace(/ \| /gm, '\|').replace(/\| /gm, '\|').replace(/ /g, '_') //replace(/{{/gm, '\[M\]{{').replace(/}}/gm, '}}\[N\]'); // Don't judge me with this. ;v; startingPoint = new RegExp(/\w*\W*\[*.\|*\]/,"gm"); // I learned a new thing. ^-^       EndingPoint = something.match(startingPoint); // Had no idea it was that hard. for (i = 0; i < 90; i++) { pipetoReplace = '|' + EndingPoint[i]; pipeReplacedBy = 'XzX' + EndingPoint[i]; // Why not? ^-^           something = something.replace(pipetoReplace, pipeReplacedBy) }       var TemplateOneName = something.split("{{")[finalPrompt]; // Duh? var TemplateTwoName = TemplateOneName.split("|")[0]; // Getting the template's name with this. var initialContentOne = something.split("{{")[finalPrompt]; var initialContentTwo = initialContentOne.split("}}")[0]; var finalContent = initialContentTwo //.replace(TemplateTwoName, ''); var stageOne = finalContent.replace(/\n/gm, '').replace(/\|/g, '\n|'); //.replace(/\n\n/gm, '\n'); var stageTwo = stageOne.split('\n'); lastNumber = 0; for (stem = 0; stem < stageTwo.length; stem++) { // This creates the spacing. stageThree = stageOne.split('\n')[stem].search("="); if (lastNumber < stageThree) { lastNumber = stageThree; // Stores the larges spaced equal sign. }       }        stageFour = stageOne.replace(/_=_/gm, '=').replace(/=/gm, ' = ').replace(/  /gm, ' ').replace(/\n/gm, '\n'), // This cleans the whole thing. stageSeven = [], stageNine = [], stageEleven = [], stageEight = [], stageEleven[0] = '', stageFifteen = '', stageFourteen = [], stageThirteen = '', stageSixteen = 0; for (boom = 1; boom < stageFour.split('\n').length; boom++) { // You may notice the stages? stageFive = stageFour.split('\n')[boom].search(" "); stageSix = lastNumber - stageFive - 1; // Removes one space because of stageFour. stageSeven[boom] = stageFour.split('\n')[boom].split('=')[1]; // This is the text after the equal sign. helpme = boom; // Second choice. stageFifteen = ''; // Resetter. I don't want more spaces. ;v; if (stageSix !== 0) { for (doom = 0; doom <= stageSix; doom++) { stageFifteen = ' ' + stageFifteen; // This is where the magic happens. }           } else { stageFifteen = ' ' }           stageNine[boom] = stageFour.split('\n')[boom].split('=')[0].replace(/ /gm, ''); stageEleven[boom] = stageNine[boom] + stageFifteen; plzhelp = boom + 1; helpme = boom; if (stageFour.split('\n')[boom].search('=') !== -1) { // If there's an equal sign. stageEight[helpme] = stageEleven[helpme].replace(/\n\n/gm, '\n') + '=' + stageSeven[helpme].replace(/ /gm, ' ') + '\n'; } else { //otherwise, ignore. stageEight[helpme] = stageFour.split('\n')[boom].replace(/\n\n/gm, '\n') + '\n'; }       }        stageTwelve = stageEight.join(''); //Join the entire array as a whole string. stageTwelve = stageTwelve.replace(/XzX/gm, '\|').replace(/VzV/gm, '='); // Replaces my boi. for (stageTwelveBeta = 0; stageTwelveBeta < 90; stageTwelveBeta++) { stageNineteen = stageTwelve.replace(/undefined/gm, '').replace(/\n\n/gm, '\n').replace(/= /gm, '= ').replace(/=      /gm, '= ').replace(/=  /gm, '= ').replace(/=    /gm, '= ').replace(/=    /gm, '= ').replace(/=  /gm, '= ').replace(/=  /gm, '= ').replace(/_/gm, ' ').replace(/\=  /gm, '= '); // Cleans out any errors. }       if (realpipeSpace == true) { //$.showCustomModal $('#CleanTextArea').val('{{' + TemplateTwoName + stageNineteen.replace(/\n\|/gm, '\n \| ').replace(/\n\|/gm, '\n \| ').replace(/ \|  /gm, ' \| ') + '}}') console.log('{{' + TemplateTwoName + stageNineteen.replace(/\n\|/gm, '\n \| ').replace(/\n\|/gm, '\n \| ').replace(/ \|  /gm, ' \| ') + '}}'); new BannerNotification('Successfully reformatted','notify','',4000).show; } else { // console.log('{{' + TemplateTwoName + stageNineteen + '}}') //$.showCustomModal $('#CleanTextArea').val('{{' + TemplateTwoName + stageNineteen + '}}'); new BannerNotification('Successfully reformatted','notify','',4000).show; }   })

$('#reformHeads').on('click', function {       mw.loader.using('mediawiki.api').then(function { ds = new mw.Api; // Work around the api. var API = new mw.Api; ds.get({               action: 'query',                prop: 'revisions',                titles: wgPageName,                rvprop: 'content',                rvlimit: 500,                cb: new Date.getTime            }).done(function(r) {                for (var i in r.query.pages) {                    var AsteriskText = new RegExp(/\w*\W*\*[^\s]*\w*\W*/,"gm");                    var AsteriskMatch = r.query.pages[i].revisions[0]["*"].match(AsteriskText);                    var AlterCleanText = r.query.pages[i].revisions[0]["*"];                    if (r.query.pages[i].revisions[0]["*"].match(AsteriskText) !== null) {                        for (AsteriskLength = 0; AsteriskLength < AsteriskMatch.length; AsteriskLength++) {                            AlterCleanText = AlterCleanText.replace(AsteriskMatch[AsteriskLength], AsteriskMatch[AsteriskLength].replace(/\*/gm, '\* ').replace(/\n\* \* /gm, '\n\*\* ').replace(/  /gm, ' ')); }                   }                    for (ei = 0; ei < 20; ei++) { AlterCleanText = AlterCleanText.replace(AlterCleanText, AlterCleanText.replace(/\n\=\=\=\=/gm, '\n==== ').replace(/\=\=\=\=\n/gm, ' ====\n').replace(/\n\=\=\=/gm, "\n=== ").replace(/\=\=\=\n/gm, " ===\n").replace(/\n\=\=\= /gm, "\n=== ").replace(/  \=\=\=\n/gm, " ===\n").replace(/\n\=\=/gm, "\n== ").replace(/\=\=\n/gm, " ==\n").replace(/\n\=\=  /gm, "\n== ").replace(/\n\=\= \=  /gm, "\n=== ").replace(/  \= \==\n/gm, " ===\\n").replace(/\=\=\= \=\n/gm, "====\n").replace(/  \=\=\n/gm, " ==\n").replace(/  \= \=\=/gm, ' ===').replace(/== =/gm, '===').replace(/= ==/gm, '===').replace(/\= \=\=\=/gm, "====").replace(/  ====/gm, ' ====').replace(/\n====/gm, '\n==== ').replace(/====  /gm, '==== ')); } window.CP; CPfinal = window.CP || { :  }; if (CPfinal !== '') { Object.getOwnPropertyNames(CPfinal).forEach(function(val, idx, array) {       AlterCleanText = AlterCleanText.replace(RegExp(val, "gm"), CPfinal[val]);    }); } // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames. var ds; mw.loader.using('mediawiki.api').then(function {                       ds = new mw.Api;                        var API = new mw.Api;                        ds.post({ action: 'edit', title: wgPageName, text: AlterCleanText, summary: 'Headers and Asterisks reformatted Automatically via Page Formatter Script.', token: mw.user.tokens.values.editToken }).done(function{ new BannerNotification('Successfully ' + wgPageName + '\'s headers and asterisks were reformatted.','confirm','',4000).show; });                   });                }            });        });    });    $('#PageDump').on('click', function { mw.loader.using('mediawiki.api').then(function {           ds = new mw.Api;            // Work around the api.            var API = new mw.Api;            ds.get({ action: 'query', prop: 'revisions', titles: wgPageName, rvprop: 'content', rvlimit: 500, cb: new Date.getTime }).done(function(r) { for (var i in r.query.pages) { $('#CleanTextArea').val(r.query.pages[i].revisions[0]["*"]); new BannerNotification('Successfully extracted Content from ' + wgPageName,'confirm','',4000).show; }           });        });    });    $('#SaveDump').on('click', function { var ds; mw.loader.using('mediawiki.api').then(function {           ds = new mw.Api;            var API = new mw.Api;            ds.post({ action: 'edit', title: wgPageName, text: $('#CleanTextArea').val, summary: 'Edited via Page Formatter Script.', token: mw.user.tokens.values.editToken }).done(function{ new BannerNotification('Successfully Saved content to ' + wgPageName,'confirm','',4000).show; });       });    });    $('#AppendDump').on('click', function { var ds; mw.loader.using('mediawiki.api').then(function {           ds = new mw.Api;            var API = new mw.Api;            ds.post({ action: 'edit', title: wgPageName, appendtext: $('#CleanTextArea').val, summary: 'Edited via Page Formatter Script.', token: mw.user.tokens.values.editToken }).done(function{ new BannerNotification('Successfully appended content to ' + wgPageName,'confirm','',4000).show; });       });    });    $('#PrependDump').on('click', function { var ds; mw.loader.using('mediawiki.api').then(function {           ds = new mw.Api;            var API = new mw.Api;            ds.post({ action: 'edit', title: wgPageName, prependtext: $('#CleanTextArea').val, summary: 'Edited via Page Formatter Script.', token: mw.user.tokens.values.editToken }).done(function{ new BannerNotification('Successfully prepended content to ' + wgPageName,'confirm','',4000).show; });       });    });    $('#ChngPg').on('click', function { wgPageName = prompt("Which page would you like to edit to or Get Content from?"); new BannerNotification('Successfully changed editing to ' + wgPageName,'confirm','',4000).show; });   $('#GetPageData').on('click',function { mw.loader.using('mediawiki.api').then(function {           ds = new mw.Api;            // Work around the api.            var API = new mw.Api;            ds.get({ action: 'query', prop: 'revisions', titles: wgPageName, rvprop: 'ids|flags|timestamp|user|userid|size|sha1|comment|parsedcomment|content|tags', rvlimit: 500, cb: new Date.getTime }).done(function(r) { for (var i in r.query.pages) { //                                    console.log(r) $.showCustomModal('', ' #PageLD td {\   border: 2px black groove;\    background: rgba(0, 0, 0, 0.08);\    box-shadow: 0px 0px 2px black;\ }#PageLD td:nth-of-type(1){\    text-align:center\ }.modalWrapper{width: 80% !important;\    margin-left: 9.2% !important;\    top: 3% !important;\    left: 0px !important;\    z-index: 5001104;} Page Details  '); new BannerNotification('Successfully loaded content from ' + wgPageName,'confirm','',4000).show; }           });        });    }) });