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 v36').attr( { id: 't-editcounter', href: '#', title: 'Run edit counter script' } );    $('#t-contributions').after($('').html($editCountLink));    $('#t-editcounter').click(function  { $('#t-editcounter').html('Calculating...'); // Set up variables var bracesleft = '', edits = [], edittoken = '', limits = 0, namespaces = [], next = '', oi = '}}}} ', 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|}', timestamp = ' {{#ifeq:{{{transcludesection|date}}}|date|{{subst:#time:F j, Y', totaledits = ' {{#ifeq:{{{transcludesection|count}}}|count|{{subst:formatnum:'; // Initialize namespace array $.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 > -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 {                            namespaces[siResponse.query.namespaces[ns].id] = siResponse.query.namespaces[ns]['*'];                        }                    }                }            },            error: function (xhr, textStatus, error) { alert(xhr.statusText); alert(textStatus); alert(error); }       });        $('#t-editcounter').html('Initialized...'); // Loop through namespaces counting edits        for (var i = 0; i < namespaces.length; i++) {            next = '';            while (next != 'stop') {                $.ajax({ url: 'http://www.minecraftwiki.net/api.php?action=query&list=usercontribs&ucuser=kanegasi&uclimit=max&ucdir=newer&format=json&ucnamespace=' + i + next, dataType: 'json', async: false, success: function (ucResponse) { limits = ucResponse.limits.usercontribs; edits[i] = ucResponse.query.usercontribs.length; if (ucResponse["query-continue"]) { next = '&ucstart=' + ucResponse["query-continue"].usercontribs.ucstart; } else { next = 'stop'; }                   },                    error: function (xhr, textStatus, error){ alert(xhr.statusText); alert(textStatus); alert(error); }               });            }        }        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) {                if (i === 0) {                    edits[i] += limits;                }                table5 += bracesleft + 'subst:formatnum:' + edits[i] + bracesright + ' || ';            }        }        $('#t-editcounter').html('Filled arrays...'); // 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 += totaledits + totalResponse.query.userinfo.editcount + oi + '\n'; },           error: function (xhr, textStatus, error){ alert(xhr.statusText); alert(textStatus); alert(error); }       });        $('#t-editcounter').html('Got total count...'); // Build the table for the page        table3 += 'Total\n';        table9 += timestamp + oi + '\n';        table = table1 + table2 + table3 + table4 + table5 + table6 + table7 + table8 + table9 + table10 + table11;        $('#t-editcounter').html('Built table...'); // 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); }       });        $('#t-editcounter').html('Got edit token...'); // 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 { $('#t-editcounter').html('Success!'); },           error: function (xhr, textStatus, error){ alert(xhr.statusText); alert(textStatus); alert(error); }       });    }); });