User:Kanegasi/editcounter.js

/***********************************************************************************/ /* Edit counter script (original)                                                 */ /*  author:        User:Najzere @ strategywiki.org                                */ /*  source:        http://strategywiki.org/wiki/User:Najzere/edit_counter.js      */ /*  documentation: http://strategywiki.org/wiki/User_talk:Najzere/edit_counter.js */ /*  contact:       http://strategywiki.org/wiki/User_talk:Najzere                 */ /*  license:       CC-BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)  */ /* Modified version for this wiki                                                 */ /*  author:        ultradude25                                                    */ /*  source:        User:Ultradude25/editcounter.js                            */ /* Note from Kanegasi:                                                            */ /* THIS VERSION HERE IS HIGHLY MODIFIED FOR ME, PLEASE USE ULTRADUDE25'S SCRIPT!!! */ /***********************************************************************************/

$(document).ready(function {    'use strict';    var brace = '{',        $editCountLink = $('Edit counter').attr({ id: 't-editcounter', href: '#', title: 'Run edit counter script' }),        edits = [],        edittoken,        event,        i,        movelog = [],        namespaces = [],        nextmove = ,        nextprotect = ,        nextuc = '',        ns,        page,        protectlog = [],        succeeded = false,        table = '  Edit Count \n{| class="wikitable" style="text-align: center; margin: 0 auto"\n|-\n! ',        tableTop = 'edit counter.js Why don\'t the numbers match the total?';    function siResponse {        for (ns in siResponse.query.namespaces) {            if (siResponse.query.namespaces[ns].id > -1) {                if (siResponse.query.namespaces[ns].id !== namespaces.length) { break; }               if (siResponse.query.namespaces[ns].id === 0) { namespaces[siResponse.query.namespaces[ns].id] = 'Main'; } else if (siResponse.query.namespaces[ns].id === 4) { namespaces[siResponse.query.namespaces[ns].id] = 'Minecraft Wiki'; } else if (siResponse.query.namespaces[ns].id === 5) { namespaces[siResponse.query.namespaces[ns].id] = 'Minecraft Wiki talk'; } else { namespaces[siResponse.query.namespaces[ns].id] = siResponse.query.namespaces[ns].canonical; }               movelog[siResponse.query.namespaces[ns].id] = 0; protectlog[siResponse.query.namespaces[ns].id] = 0; }       }    }    function moveResponse { for (event in moveResponse.query.logevents) { movelog[moveResponse.query.logevents[event].ns] += 1; }       if (moveResponse["query-continue"]) { nextmove = '&lestart=' + moveResponse["query-continue"].logevents.lestart; } else { nextmove = 'stop'; }   }    function protectResponse { for (event in protectResponse.query.logevents) { protectlog[protectResponse.query.logevents[event].ns] += 1; }       if (protectResponse["query-continue"]) { nextprotect = '&lestart=' + protectResponse["query-continue"].logevents.lestart; } else { nextprotect = 'stop'; }   }    function ucResponse { edits[i] = ucResponse.query.usercontribs.length - movelog[i] - protectlog[i]; if (ucResponse["query-continue"]) { nextuc = '&ucstart=' + ucResponse["query-continue"].usercontribs.ucstart; } else { nextuc = 'stop'; }   }    function uiResponse { table += brace + brace + 'subst:formatnum:' + uiResponse.query.userinfo.editcount + '}}\n|}'; }   function propResponse { edittoken = propResponse.query.pages[page].edittoken.replace(/\+\\$/g, '%2B%5C'); }   function succeed { succeeded = true; }   $('#t-contributions').after($('').html($editCountLink)); $('#t-editcounter').click(function {        $(this).html('Calculating...');        $.ajax({ url: '/api.php?action=query&meta=siteinfo&siprop=namespaces&format=json', dataType: 'json', async: false, success: siResponse });       while (nextmove !== 'stop') {            $.ajax({ url: '/api.php?action=query&list=logevents&letype=move&leuser=Kanegasi&lelimit=max&leprop=title&format=json' + nextmove, dataType: 'json', async: false, success: moveResponse });       }        while (nextprotect !== 'stop') {            $.ajax({ url: '/api.php?action=query&list=logevents&letype=protect&leuser=Kanegasi&lelimit=max&leprop=title&format=json' + nextprotect, dataType: 'json', async: false, success: protectResponse });       }        for (i = 0; i < namespaces.length; i++) {            nextuc = '';            while (nextuc !== 'stop') {                $.ajax({ url: '/api.php?action=query&list=usercontribs&ucuser=Kanegasi&uclimit=max&ucdir=newer&format=json&ucnamespace=' + i + nextuc, dataType: 'json', async: false, success: ucResponse });           }        }        for (i = 0; i <= namespaces.length; i++) {            if (edits[i] > 0) {                table += namespaces[i] + ' !! ';            }        }        table += 'Total\n|-\n| ';        for (i = 0; i < namespaces.length; i++) {            if (edits[i] > 0) {                table += brace + brace + 'subst:formatnum:' + edits[i] + '}} || ';            }        }        $.ajax({ url: '/api.php?action=query&meta=userinfo&uiprop=editcount&format=json', dataType: 'json', async: false, success: uiResponse });       table += ' Last updated: ' + brace + brace + 'subst:#time:F j, Y}}. Generated with ' + tableTop + ' \n ';        $.ajax({ url: '/api.php?action=query&prop=info|revisions&intoken=edit&format=json&titles=User:Kanegasi/edit_count', dataType: 'json', async: false, success: propResponse });       $.ajax({ url: '/api.php?action=edit&title=User:Kanegasi/edit_count&summary=updated&minor=1&recreate=1&bot=1&text=' + escape(table) + '&token=' + edittoken, type: 'POST', async: false, success: succeed });       if (succeeded) {            $(this).html('Done!');        } else {            $(this).html('Error!');        }    }); });