Minecraft Wiki
Advertisement

Documentation may be created at User:Kanegasi/editcounter.js/doc.

Note: After saving, you have to bypass your browser's cache to see the changes.

Google Chrome, Firefox, Microsoft Edge, and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button.
For details and instructions about other browsers, see Wikipedia:Bypass your cache.

/***********************************************************************************/
/* 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 () {
// Add "Edit counter" link in Toolbox
    var $editCountLink = $('<a>Edit counter</a>').attr({ id: 't-editcounter', href: '#', title: 'Run edit counter script' }),
    $('#t-contributions').after($('<li></li>').html($editCountLink));
    $('#t-editcounter').click(function () {
// Change "Edit counter" to "Calculating..." after click
        $(this).html('Calculating...');
// Set up variables
        var brace = '{',
            edits = [],
            edittoken,
            movelog = [],
            namespaces = [],
            nextmove = '',
            nextprotect = '',
            nextuc = '',
            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?]';
// 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: function (siResponse) {
                for (var 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;
                    }
                }
            }
        });
// 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: function (moveResponse) {
                    for (var 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';
                    }
                }
            });
        }
// 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: function (protectResponse) {
                    for (var 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';
                    }
                }
            });
        }
// Loop through namespaces counting edits and subtracting page moves and patrols
        for (var 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: 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';
                        }
                    }
                });
            }
        }
        for (var i = 0; i <= namespaces.length; i++) {
            if (edits[i] > 0) {
                table += namespaces[i] + ' !! ';
            }
        }
        table += 'Total\n|-\n| ';
        for (var i = 0; i < namespaces.length; i++) {
            if (edits[i] > 0) {
                table += brace + brace + 'subst:formatnum:' + edits[i] + '}} || ';
            }
        }
 // Get total edit count as seen on Preferences page
        $.ajax({
            url: '/api.php?action=query&meta=userinfo&uiprop=editcount&format=json',
            dataType: 'json',
            async: false,
            success: function (uiResponse) {
                table += brace + brace + 'subst:formatnum:' + uiResponse.query.userinfo.editcount + '}}\n|}';
            }
        });
        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: function (propResponse) {
                for (var page in propResponse.query.pages) {
                    edittoken = propResponse.query.pages[page].edittoken.replace(/\+\\$/g, '%2B%5C');
                }
            }
        });
// Post new count results to page
        $.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: function () {
                succeeded = true;
            }
        });
// Change "Calculating..." to reflect status of operation
        if (succeeded) {
            $(this).html('Done!');
        } else {
            $(this).html('Error!');
        }
    });
});
Advertisement