User:Kanegasi/editcounter.js

/***********************************************************************************/ /*                                                                                */ /*                            MEDIAWIKI EDITCOUNTER v46                            */ /*                                                                                */ /* 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 v46" 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 v46').attr( { id: 't-editcounter', href: '#', title: 'Run edit counter script' } );    $('#t-contributions').after($('').html($editCountLink));    $('#t-editcounter').click(function  { $('#t-editcounter').html('Processing...'); // Set up variables if (!transcludeall) { var counttransclude = ' {{#ifeq:{{{transcludesection|count}}}|count|', datetransclude = ' {{#ifeq:{{{transcludesection|date}}}|date|', transcludeend = '}} ', transcludeall1 = '', transcludeall2 = ''; } else { var counttransclude = '', datetransclude = '', transcludeend = '', transcludeall1 = ' ', transcludeall2 = ' '; }       var bracesleft = '', captcha = true, edits = [], edittoken = '', logevents = [], namespaces = [], newpage = true, next = '', table = '', table1 = transcludeall1 + '{| ' + tableconfig + '\n', table2 = '|- ' + toprowconfig + '\n| ', table3 = '', table4 = '|- ' + bottomrowconfig + '\n| ', table5 = '', table6 = ' ', table7 = '[http://www.minecraftwiki.net/wiki/User:Kanegasi/editcounter.js ', timestamp = datetransclude + bracesleft + 'subst:#time:F j, Y' + bracesright + transcludeend, totaledits = counttransclude + bracesleft + '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 + bracesright + transcludeend + '\n|}\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';        table7 += timestamp + '] ';        table = table1 + table2 + table3 + table4 + table5 + table6 + table7; // 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;        }    }); });