User:Kanegasi/editcounter.js

/***********************************************************************************/ /*                                                                                */ /*                            MEDIAWIKI EDITCOUNTER v38                            */ /*                                                                                */ /* 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 v38" after "User contributions" link in side menu    if ($('#t-contributions').length == 0) {        alert('Missing "User contributions" link in the side menu');        return;    }    var $editCountLink = $('Edit counter v38').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 (typeof pagename == 'undefined') { var pagename = 'edit count'; } if (typeof tableclass == 'undefined') { var tableclass = ''; } if (typeof tablecolortop == 'undefined') { var tablecolortop = '729FCF'; } else { tablecolortop = tablecolortop.replace(/#/g, ''); }       if (typeof tablecolorbottom == 'undefined') { var tablecolorbottom = 'FFFFFF'; } else { tablecolorbottom = tablecolorbottom.replace(/#/g, ''); }       var bracesleft = '', captcha = true, edits = [], edittoken = '', movelog = [], namespaces = [], newpage = true, next = '', oi = '}}}} ', protectlog = [], wip = '\n', 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:', scriptpage = mw.config.get('wgTitle'), usergroup = mw.config.get('wgUserGroups'), username = mw.config.get('wgUserName'), wikiname = mw.config.get('wgSiteName'); // Check if user autoconfirmed if (usergroup.match(/autoconfirmed/g)) { 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, movelog, protectlog, 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) {                        edits[siResponse.query.namespaces[ns].id] = siResponse.query.namespaces[ns].id;                        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; }                   movelog[siResponse.query.namespaces[ns].id] = 0; protectlog[siResponse.query.namespaces[ns].id] = 0; }               }            },            error: function (xhr, textStatus, error) { $('#t-editcounter').html('Error!'); alert(xhr.statusText); alert(textStatus); alert(error); }       }); // Fill movelog array        next = '';        while (next != 'stop') {            $.ajax({ url: '/api.php?action=query&list=logevents&letype=move&leuser=' + escape(username) + '&lelimit=max&ledir=newer&leprop=title' + next + '&format=json', 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"]) { next = '&lestart=' + moveResponse["query-continue"].logevents.lestart; } else { next = 'stop'; }               },                error: function (xhr, textStatus, error) { $('#t-editcounter').html('Error!'); alert(xhr.statusText); alert(textStatus); alert(error); }           });        } // Fill protectlog array        next = '';        while (next != 'stop') {            $.ajax({ url: '/api.php?action=query&list=logevents&letype=protect&leuser=' + escape(username) + '&lelimit=max&ledir=newer&leprop=title' + next + '&format=json', 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"]) { next = '&lestart=' + protectResponse["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 i in ucResponse.query.usercontribs) { edits[ucResponse.query.usercontribs[i].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++) {            if (edits[i] > 0) {                table3 += namespaces[i] + ' || ';            }        }        for (var i = 0; i < namespaces.length; i++) {            if (edits[i] > 0) {                edits[i] = movelog[i] - protectlog[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 = wip + 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%5BUser%3A' + 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;        }    }); });