User:Kanegasi/editcounter.js

/***********************************************************************************/ /*                                                                                */ /*                            MEDIAWIKI EDITCOUNTER v41                            */ /*                                                                                */ /* Original credit:                                                                */ /*  author:        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/)  */ /* Minecraft Wiki credit:                                                         */ /*  author:        ultradude25 @ minecraftwiki.net                                */ /*  source:        http://minecraftwiki.net/wiki/User:Ultradude25/editcounter.js  */ /* Current version credit:                                                        */ /*  author:        Kanegasi @ minecraftwiki.net                                   */ /*  source:        http://minecraftwiki.net/wiki/User:Kanegasi/editcounter.js     */ /***********************************************************************************/

$(document).ready(function { // Add new link labeled "Edit counter v41" after "User contributions" link in side menu    if (!$('#t-contributions').length) {        alert('Missing "User contributions" link in the side menu');        return;    }    var $editCountLink = $('Edit counter v41').attr( { id: 't-editcounter', href: '#', title: 'Run edit counter script' } );    $('#t-contributions').after($('').html($editCountLink)); // Change link to read "Processing..." when clicked    $('#t-editcounter').click(function  { $('#t-editcounter').html('Processing...'); // Set up variables if (pagename === '') { var pagename = 'edit count'; } if (tableclass === ) { var tableclass = ; } if (tablecolortop === '') { var tablecolortop = '729FCF'; } else { tablecolortop = tablecolortop.replace(/#/g, ''); }       if (tablecolorbottom === '') { var tablecolorbottom = 'FFFFFF'; } else { tablecolorbottom = tablecolorbottom.replace(/#/g, ''); }       var bracesleft = '', captcha = true, edits = [], edittoken = '', logevents = [], namespaces = [], newpage = true, next = '', oi = '}}}} ', table = '', table1 = '{| class="' + tableclass + '" cellpadding=5 cellspacing=1 style="margin: 0 auto; text-align: center"\n', table2 = '|- style="background-color: #' + tablecolortop + '; font-size: small"\n| ', table3 = '', table4 = '|- style="background-color: #' + tablecolorbottom + '; 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:', usergroup = mw.config.get('wgUserGroups'), username = mw.config.get('wgUserName'), wikiname = mw.config.get('wgSiteName'); // Check if user autoconfirmed if (/autoconfirmed/.exec(usergroup)) { captcha = false; } else { $('#t-editcounter').html('Error!'); alert("Sorry, you're not autoconfirmed yet, so captcha is required to make new pages. Go create the page at: User:" + username + "/" + pagename + " and then try again."); return; } // Initialize namespace, logevent, and edit 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] = wikiname;                        } else if (siResponse.query.namespaces[ns].id == 5) {                            namespaces[siResponse.query.namespaces[ns].id] = wikiname + ' talk'; } else { namespaces[siResponse.query.namespaces[ns].id] = siResponse.query.namespaces[ns].canonical; }                   logevents[siResponse.query.namespaces[ns].id] = siResponse.query.namespaces[ns].id; edits[siResponse.query.namespaces[ns].id] = siResponse.query.namespaces[ns].id; }               }            },            error: function (xhr, textStatus, error) { $('#t-editcounter').html('Error!'); alert(xhr.statusText); alert(textStatus); alert(error); }       }); // Fill logevent array        next = '';        while (next != 'stop') {            $.ajax({ url: '/api.php?action=query&list=logevents&leuser=' + escape(username) + '&lelimit=max&ledir=newer&leprop=title' + next + '&format=json', dataType: 'json', async: false, success: function (logResponse) { for (var event in logResponse.query.logevents) { logevents[logResponse.query.logevents[event].ns] += 1; }                   if (logResponse["query-continue"]) { next = '&lestart=' + logResponse["query-continue"].logevents.lestart; } else { next = 'stop'; }               },                error: function (xhr, textStatus, error) { $('#t-editcounter').html('Error!'); alert(xhr.statusText); alert(textStatus); alert(error); }           });        } // Loop through namespaces and user contributions, counting edits and subtracting above log values        next = '';        while (next != 'stop') {            $.ajax({ url: '/api.php?action=query&list=usercontribs&ucuser=' + escape(username) + '&uclimit=max&ucdir=newer&ucprop=title' + next + '&format=json', dataType: 'json', async: false, success: function (ucResponse) { for (var event in ucResponse.query.usercontribs) { edits[ucResponse.query.usercontribs[event].ns] += 1; }                   if (ucResponse["query-continue"]) { next = '&ucstart=' + ucResponse["query-continue"].usercontribs.ucstart; } else { next = 'stop'; }               },                error: function (xhr, textStatus, error){ $('#t-editcounter').html('Error!'); alert(xhr.statusText); alert(textStatus); alert(error); }           });        }        for (var i = 0; i < namespaces.length; i++) {            edits[i] -= logevents[i];            if (edits[i] > 0) {                table3 += namespaces[i] + ' || ';                table5 += bracesleft + 'subst:formatnum:' + edits[i] + bracesright + ' || ';            }        } // Retrieve 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 (totalResponse) { table5 += totaledits + totalResponse.query.userinfo.editcount + oi + '\n'; },           error: function (xhr, textStatus, error){ $('#t-editcounter').html('Error!'); alert(xhr.statusText); alert(textStatus); alert(error); }       }); // Arrange data into formatted table        table3 += 'Total\n';        table9 += timestamp + oi + '\n';        table = table1 + table2 + table3 + table4 + table5 + table6 + table7 + table8 + table9 + table10 + table11; // Retrieve edit token and check if page exists        $.ajax({ url: '/api.php?action=query&prop=info&intoken=edit&titles=User%3A' + escape(username) + '%2F' + escape(pagename) + '&format=json', dataType: 'json', async: false, success: function (propResponse) { for (var page in propResponse.query.pages) { edittoken = propResponse.query.pages[page].edittoken.replace(/\+\\$/g, '%2B%5C'); if (propResponse.query.pages[page]["lastrevid"]) { newpage = false; }               }            },            error: function (xhr, textStatus, error){ $('#t-editcounter').html('Error!'); alert(xhr.statusText); alert(textStatus); alert(error); }       }); // Submit post request to page        if (!captcha && !newpage) {            $.ajax({ url: '/api.php?action=edit&title=User%3A' + escape(username) + '%2F' + escape(pagename) + '&summary=%5B%5B' + escape(scriptpage) + '%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){ $('#t-editcounter').html('Error!'); alert(xhr.statusText); alert(textStatus); alert(error); }           });        } else {            $('#t-editcounter').html('Error!');            alert("Sorry, you're not autoconfirmed yet, so captcha is required to make new pages. Go create the page at: User:" + username + "/" + pagename + " and then try again.");            return;        }    }); });