User:Kanegasi/editcounter.js

/*************************************************************************************/ /*                                                                                  */ /*                             MEDIAWIKI EDITCOUNTER v51                             */ /*                                                                                  */ /*      license:   CC-BY-SA 3.0  http://creativecommons.org/licenses/by-sa/3.0/      */ /*                                                                                  */ /* 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                   */ /* 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       */ /*  documentation: http://minecraftwiki.net/wiki/User_talk:Kanegasi/editcounter.js  */ /*  contact:       http://minecraftwiki.net/wiki/User_talk:Kanegasi                 */ /*                                                                                  */ /*************************************************************************************/

$(document).ready(function { // Add new link labeled "Edit counter v51" 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 v51').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 var autoConfirmed = mw.config.get('wgUserGroups'), userName = mw.config.get('wgUserName'), wikiName = mw.config.get('wgSiteName'); if (typeof pageName === 'undefined') { var pageName = 'edit count'; } if (typeof scriptPage === 'undefined') { var scriptPage = 'User%3A' + escape(userName) + '%2F' + escape(pageName); } if (typeof transcludeAll === 'undefined') { var transcludeAll = false; } if (typeof mainDivAttrib === 'undefined') { var mainDivAttrib = 'style="margin-bottom: 1em; text-align: center"'; } if (typeof tableCaption === 'undefined') { var tableCaption = ''; } if (typeof mainTableAttrib === 'undefined') { var mainTableAttrib = 'class="wikitable" style="margin: 0 auto"'; } if (typeof topRowAttrib === 'undefined') { var topRowAttrib = ''; } if (typeof bottomRowAttrib === 'undefined') { var bottomRowAttrib = ''; } var countTransclude = '', dateTransclude = '', transcludeEnd = '', mainDivCont = ''; if (transcludeAll == false) { countTransclude = ' {{#ifeq:{{{transcludesection|count}}}|count|'; dateTransclude = ' {{#ifeq:{{{transcludesection|date}}}|date|'; transcludeEnd = '}} '; }       if (mainDivAttrib == '') { mainDivCont = ' '; } else { mainDivCont = ''; } var bracesLeft = '', captcha = true, edits = [], editToken = '', logEvents = [], logUploads = [], nameSpaces = [], newPage = true, next = '', tableAll = '', tableRow1 = mainDivCont + '\n', tableRow2 = '{| ' + mainTableAttrib + '\n', tableRow3 = '|%2B ' + tableCaption + '\n', tableRow4 = '|- ' + topRowAttrib + '\n| ', tableRow5 = '', tableRow6 = '|- ' + bottomRowAttrib + '\n| ', tableRow7 = '', tableRow8 = ' ', tableRow9 = '[http://www.minecraftwiki.net/wiki/User:Kanegasi/editcounter.js ', timeStamp = dateTransclude + bracesLeft + 'subst:#time:F j, Y' + bracesRight + transcludeEnd, totalEdits = countTransclude + bracesLeft + 'subst:formatnum:'; // Check if user autoconfirmed if (/autoconfirmed/.exec(autoConfirmed)) { captcha = false; } else { $('#t-editcounter').html('Error!'); alert("Sorry, you're not autoconfirmed yet, so captcha is required to make new pages.\nGo to the page shown below and create it by putting anything into the page and saving.\n\n\tUser:" + userName + "/" + pageName + "\t"); return; } // Initialize namespace, logevent, upload, 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] = 0; logUploads[siResponse.query.namespaces[ns].id] = 0; edits[siResponse.query.namespaces[ns].id] = 0; }               }            },            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) + '&leprop=title&lelimit=max&ledir=newer' + 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); }           });        } // Fill upload array        next = '';        while (next != 'stop') {            $.ajax({ url: '/api.php?action=query&list=logevents&letype=upload&leuser=' + escape(userName) + '&leprop=title&lelimit=max&ledir=newer' + next + '&format=json', dataType: 'json', async: false, success: function (uploadResponse) { for (var event in uploadResponse.query.logevents) { logUploads[uploadResponse.query.logevents[event].ns] += 1; }                   if (uploadResponse["query-continue"]) { next = '&lestart=' + uploadResponse["query-continue"].logevents.lestart; } else { next = 'stop'; }               },                error: function (xhr, textStatus, error) { $('#t-editcounter').html('Error!'); alert(xhr.statusText); alert(textStatus); alert(error); }           });        } // Fill edit array, subtract logevents, and add uploads        next = '';        while (next != 'stop') {            $.ajax({ url: '/api.php?action=query&list=usercontribs&ucuser=' + escape(userName) + '&ucprop=title&uclimit=max&ucdir=newer' + 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];            edits[i] += logUploads[i];            if (edits[i] > 0) {                tableRow5 += nameSpaces[i] + ' || ';                tableRow7 += 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) { tableRow7 += 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        tableRow5 += 'Total\n';        tableRow9 += timeStamp + '] \n ';        tableAll = tableRow1 + tableRow2 + tableRow3 + tableRow4 + tableRow5 + tableRow6 + tableRow7 + tableRow8 + tableRow9; // 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(tableAll) + '&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.\nGo to the page shown below and create it by putting anything into the page and saving.\n\n\tUser:" + userName + "/" + pageName + "\t");            return;        }    }); });