Minecraft Wiki
mNo edit summary
mNo edit summary
Line 18: Line 18:
 
// Set up variables
 
// Set up variables
 
var brace = '{',
 
var brace = '{',
editCountLink = ('<a>Edit counter</a>').attr({ id: 't-editcounter', href: '#', title: 'Run edit counter script' }),
+
$editCountLink = $('<a>Edit counter</a>').attr({ id: 't-editcounter', href: '#', title: 'Run edit counter script' }),
 
edits = [],
 
edits = [],
 
edittoken,
 
edittoken,

Revision as of 22:23, 1 January 2013

/***********************************************************************************/
/* 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 ULTRADUDE'S SCRIPT!!! */
/***********************************************************************************/

$(document).ready(function () {
    'use strict';

// Set up variables
    var brace = '{',
        $editCountLink = $('<a>Edit counter</a>').attr({ id: 't-editcounter', href: '#', title: 'Run edit counter script' }),
        edits = [],
        edittoken,
        event,
        uc,
        ml,
        movelog = [],
        namespaces = [],
        nextmove = '',
        nextprotect = '',
        nextuc = '',
        ns,
        page,
        pl,
        protectlog = [],
        succeeded = false,
        table = '<div style="text-align: center; margin-bottom: 1em"><span style="font-weight: bold">Edit Count</span>\n{| class="wikitable" style="text-align: center; margin: 0 auto"\n|-\n! ',
        tableTop = '[http://strategywiki.org/wiki/User_talk:Najzere/edit_counter.js edit counter.js] [http://strategywiki.org/wiki/User_talk:Najzere/edit_counter.js#Bugs Why don\'t the numbers match the total?]',
        ucns = 0;

// Set up functions
    function siResponse() {
        if (siResponse.query.namespaces[ns].id > -1) {
            if (siResponse.query.namespaces[ns].id === namespaces.length) {
                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() {
        movelog[moveResponse.query.logevents[event].ns] += 1;
        if (moveResponse["query-continue"]) {
            nextmove = '&lestart=' + moveResponse["query-continue"].logevents.lestart;
        } else {
            nextmove = 'stop';
        }
    }
    function protectResponse() {
        protectlog[protectResponse.query.logevents[event].ns] += 1;
        if (protectResponse["query-continue"]) {
            nextprotect = '&lestart=' + protectResponse["query-continue"].logevents.lestart;
        } else {
            nextprotect = 'stop';
        }
    }
    function ucResponse() {
        edits[uc] = ucResponse.query.usercontribs.length - movelog[ml] - protectlog[pl];
        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;
    }

// Add a new link after "User contributions" in the toolbox
    $('#t-contributions').after($('<li></li>').html($editCountLink));

    $('#t-editcounter').click(function () {

// Change edit counter link to let the user know it's thinking
        $(this).html('Calculating...');

// Initialize namespace, move log and patrol log arrays
        $.ajax({
            url: '/api.php?action=query&meta=siteinfo&siprop=namespaces&format=json',
            dataType: 'json',
            async: false,
            success: siResponse
        });
    });

// Fill move log array
    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
        });
    }

// Fill protect log array
    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
        });
    }

// Loop through namespaces counting edits and subtracting page moves and patrols
    for (ucns = 0; ucns < namespaces.length; ucns++) {
        nextuc = '';
        while (nextuc !== 'stop') {
            $.ajax({
                url: '/api.php?action=query&list=usercontribs&ucuser=Kanegasi&uclimit=max&ucdir=newer&format=json&ucnamespace=' + ucns + nextuc,
                dataType: 'json',
                async: false,
                success: ucResponse
            });
        }

        for (ucns = 0; ucns <= namespaces.length; ucns++) {
            if (edits[uc] > 0) {
                table += namespaces[ucns] + ' !! ';
            }
        }
        table += 'Total\n|-\n| ';

        for (ucns = 0; ucns < namespaces.length; ucns++) {
            if (edits[uc] > 0) {
                table += brace + brace + 'subst:formatnum:' + edits[uc] + '}} || ';
            }
        }

// Get full edit count, as tracked by MW, add it to table
        $.ajax({
            url: '/api.php?action=query&meta=userinfo&uiprop=editcount&format=json',
            dataType: 'json',
            async: false,
            success: uiResponse
        });

        table += '<span style="font-size: x-small">Last updated: ' + brace + brace + 'subst:#time:F j, Y}}. Generated with ' + tableTop + '</span>\n</div>';

// Get edit token
        $.ajax({
            url: '/api.php?action=query&prop=info|revisions&intoken=edit&format=json&titles=User:Kanegasi/edit_count',
            dataType: 'json',
            async: false,
            success: propResponse
        });

// Edit the target page with the table
        $.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
        });

// change edit count link to reflect the status of the page save
        if (succeeded) {
            $(this).html('Done!');
        } else {
            $(this).html('Error!');
        }

    }
});