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').attr({ id: 't-editcounter', href: '#', title: 'Script locked and loaded' });    $('#t-contributions').after($('').html($editCountLink));    $('#t-editcounter').click(function (countstuff) { // Change "Edit counter" to "Calculating..." after click $(this).html('Calculating...'); // Set up variables var bracesleft = '', edits = [], edittoken, movelog = [], namespaces = [], nextml = '', nextpl = '', nextuc = '', protectlog = [], succeeded = false, 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 (siResponse) {                for (var ns in siResponse.query.namespaces) {                    if (siResponse.query.namespaces[ns].id >= 0) {                        if (siResponse.query.namespaces[ns].id != namespaces.length) { break; }                        if (siResponse.query.namespaces[ns].id === 0) {                            namespaces[siResponse.query.namespaces[ns].id] = 'Main';                        } else {                            namespaces[siResponse.query.namespaces[ns].id] = siResponse.query.namespaces[ns]['*'];                        }                        movelog[siResponse.query.namespaces[ns].id] = 0;                        protectlog[siResponse.query.namespaces[ns].id] = 0; }               }            },            error: function (mainfail) { alert( 'Error: Initialization failed.' ); $(this).html('Error!'); return; }       }); // 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 (mlfail) { alert( 'Error: Move log request failed.' ); $(this).html('Error!'); return; }           });        } // 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 (plfail) { alert( 'Error: Protect log request failed.' ); $(this).html('Error!'); return; }           });        } // 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: '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[i] = ucResponse.query.usercontribs.length - movelog[i] - protectlog[i]; if (ucResponse["query-continue"]) { nextuc = '&ucstart=' + ucResponse["query-continue"].usercontribs.ucstart; } else { nextuc = 'stop'; }                   },                    error: function (ucfail) { alert( 'Error: User contributions request failed.' ); $(this).html('Error!'); return; }               });            }        }        for (var i = 0; i < namespaces.length; i++) {            if (edits[i] > 0) {                table3 += namespaces[i] + ' || ';            }        }        for (var i = 0; i < namespaces.length; i++) {            if (edits[i] > 0) {                table5 += bracesleft + 'subst:formatnum:' + edits[i] + 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 (uiResponse) { table5 += bracesleft + 'subst:formatnum:' + uiResponse.query.userinfo.editcount + bracesright + '\n'; },           error: function (totalfail) { alert( 'Error: Total count request failed.' ); $(this).html('Error!'); return; }       }); // 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 (tokenfail) { alert( 'Error: Token request failed.' ); $(this).html('Error!'); return; }       }); // 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 (data) { if (data.edit && data.edit.result == 'Success') { succeeded = true; } else if (data.error) { alert( 'Error: API returned error code "' + data.error.code + '": ' + data.error.info ); } else { alert( 'Error: Unknown result from API.' ); }           },            error: function (postfail) { alert( 'Error: Post request failed.' ); }       }); // Change "Calculating..." to reflect status of operation        if (succeeded) {            $(this).html('Done!');        } else {            $(this).html('Error!');        }    }); });