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 { // Add 'Edit counter' link in Toolbox    var $editCountLink = $('Edit counter v30').attr({ id: 't-editcounter', href: '#', title: 'Run edit counter script' });    $('#t-contributions').after($('').html($editCountLink));    $('#t-editcounter').click(function  { $(this).html('Calculating...'); // Set up variables var bracesleft = '', edits = [], edittoken = '', limit = 0, movelog = [], namespaces = [], nextml = '', nextpl = '', nextuc = '', protectlog = [], table = '', table1 = '{| cellpadding=5 cellspacing=1 style="margin: 0 auto; text-align: center"\n', table2 = '|- style="background-color: #729FCF; font-size: small"\n| ', table3 = '', table4 = '|- style="background-color: #FFFFFF; font-size: large"\n| ', table5 = '', table6 = '|- style="line-height: 1em"\n', table7 = '| colspan=20 |\n', table8 = '{| style="font-size: xx-small" width=100%\n', table9 = '| align=left width=30% | ', table10 = '| align=center | editcounter.js\n', table11 = '| align=right width=30% | count discrepancy\n|}\n|}'; // Initialize namespace, move log, and patrol log arrays $.ajax({           url: 'http://www.minecraftwiki.net/api.php?action=query&meta=siteinfo&siprop=namespaces&format=json',            dataType: 'json',            async: false,            success: function (initResponse) {                for (var ns in initResponse.query.namespaces) {                    if (initResponse.query.namespaces[ns].id > -1) {                        if (initResponse.query.namespaces[ns].id != namespaces.length) { break; }                        if (initResponse.query.namespaces[ns].id === 0) {                            namespaces[initResponse.query.namespaces[ns].id] = 'Main';                        } else {                            namespaces[initResponse.query.namespaces[ns].id] = initResponse.query.namespaces[ns]['*'];                        }                        movelog[initResponse.query.namespaces[ns].id] = 0;                        protectlog[initResponse.query.namespaces[ns].id] = 0; }               }            },            error: function (xhr, textStatus, error) { alert(xhr.statusText); alert(textStatus); alert(error); }       }); // Fill move log array        while (nextml != 'stop') {            $.ajax({ url: 'http://www.minecraftwiki.net/api.php?action=query&list=logevents&letype=move&leuser=kanegasi&lelimit=max&leprop=title&format=json' + nextml, 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']) { nextml = '&lestart=' + moveResponse['query-continue'].logevents.lestart; } else { nextml = 'stop'; }               },                error: function(xhr, textStatus, error){ alert(xhr.statusText); alert(textStatus); alert(error); }           });        } // Fill protect log array        while (nextpl != 'stop') {            $.ajax({ url: 'http://www.minecraftwiki.net/api.php?action=query&list=logevents&letype=protect&leuser=kanegasi&lelimit=max&leprop=title&format=json' + nextpl, 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']) { nextpl = '&lestart=' + protectResponse['query-continue'].logevents.lestart; } else { nextpl = 'stop'; }               },                error: function(xhr, textStatus, error){ alert(xhr.statusText); alert(textStatus); alert(error); }           });        } // Loop through namespaces counting edits and subtracting page moves and patrols        for (var count = 0; count < namespaces.length; count++) {            nextuc = '';            while (nextuc != 'stop') {                $.ajax({ url: 'http://www.minecraftwiki.net/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[count] = ucResponse.query.usercontribs.length - movelog[count] - protectlog[count]; limit = ucResponse.limits.usercontribs; if (ucResponse['query-continue']) { nextuc = '&ucstart=' + ucResponse['query-continue'].usercontribs.ucstart; } else { nextuc = 'stop'; }                   },                    error: function(xhr, textStatus, error){ alert(xhr.statusText); alert(textStatus); alert(error); }               });            }        }        for (var names = 0; names < namespaces.length; names++) {            if (edits[names] > 0) {                if ('undefined' in edits[names]) {                    namespaces[names] = 'Main';                }                table3 += namespaces[names] + ' || ';            }        }        for (var numbers = 0; numbers < namespaces.length; numbers++) {            if (edits[numbers] > 0) {                if ('undefined' in edits[numbers]) {                    edits[numbers] = limit;                }                table5 += bracesleft + 'subst:formatnum:' + edits[numbers] + bracesright + ' || ';            }        } // Get total edit count as seen on Preferences page        $.ajax({ url: 'http://www.minecraftwiki.net/api.php?action=query&meta=userinfo&uiprop=editcount&format=json', dataType: 'json', async: false, success: function (totalResponse) { table5 += bracesleft + 'subst:formatnum:' + totalResponse.query.userinfo.editcount + bracesright + '\n'; },           error: function(xhr, textStatus, error){ alert(xhr.statusText); alert(textStatus); alert(error); }       }); // Build the table for the page        table3 += 'Total\n';        table9 += bracesleft + 'subst:#time:F j, Y' + bracesright + '\n';        table = table1 + table2 + table3 + table4 + table5 + table6 + table7 + table8 + table9 + table10 + table11; // Get edit token        $.ajax({ url: 'http://www.minecraftwiki.net/api.php?action=query&prop=info&intoken=edit&format=json&titles=User%3AKanegasi%2Fedit%20count', dataType: 'json', async: false, success: function (propResponse) { for (var page in propResponse.query.pages) { edittoken = propResponse.query.pages[page].edittoken.replace(/\+\\$/g, '%2B%5C'); }           },            error: function(xhr, textStatus, error){ alert(xhr.statusText); alert(textStatus); alert(error); }       }); // Post new count table to page        $.ajax({ url: 'http://www.minecraftwiki.net/api.php?action=edit&title=User%3AKanegasi%2Fedit%20count&summary=%5B%5BUser%3AKanegasi%2Feditcounter%2Ejs%7Cupdated%5D%5D&bot=1&minor=1&recreate=1&text=' + escape(table) + '&token=' + edittoken, type: 'POST', async: false, success: function (postResponse) { if (postResponse.edit.result == 'Success') { alert( 'Success!' ); } else if (postResponse.error) { alert( 'Error: API returned error code "' + postResponse.error.code + '": ' + postResponse.error.info ); } else { alert( 'Error: Unknown result from API.' ); }           },            error: function(xhr, textStatus, error){ alert(xhr.statusText); alert(textStatus); alert(error); }       });        $(this).html('Done!');    }); });