User:Kanegasi/editcounter.js

/*************************************************************************************/ /*                                                                                  */ /*                             MEDIAWIKI EDITCOUNTER v60                             */ /*                                                                                  */ /*      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 v60" 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 v60').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'), siteURL = mw.config.get('wgServer'), contribLink = '[' + siteURL + '/index.php?title=Special%3AContributions&target=' + userName + '&namespace=', wikiName = mw.config.get('wgSiteName'); if (typeof(pageName) == 'undefined') { pageName = 'edit count'; } if (typeof(summaryLink) == 'undefined') { summaryLink = 'User%3A' + userName + '%2F' + pageName; } if (typeof(transcludeAll) == 'undefined') { transcludeAll = true; } if (typeof(tableHeaders) == 'undefined') { tableHeaders = true; } if (typeof(mainDivAttrib) == 'undefined') { mainDivAttrib = 'style="margin-bottom: 1em; text-align: center"'; } if (typeof(mainTableAttrib) == 'undefined') { mainTableAttrib = 'class="wikitable" style="margin: 0 auto"'; } if (typeof(topRowAttrib) == 'undefined') { topRowAttrib = ''; } if (typeof(bottomRowAttrib) == 'undefined') { bottomRowAttrib = ''; } var countTransclude = '', dateTransclude = '', transcludeEnd = '', headerBars = ' !! ',           mainDivCont = ''; if (transcludeAll == false) { countTransclude = ' {{#ifeq:{{{transcludesection|count}}}|count|'; dateTransclude = ' {{#ifeq:{{{transcludesection|date}}}|date|'; transcludeEnd = '}} '; }       if (tableHeaders == false) { headerBars = ' || '; }       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 = '|- ' + topRowAttrib + '\n| ', tableRow4 = '', tableRow5 = '|- class="plainlinks" ' + bottomRowAttrib + '\n| ', tableRow6 = '', tableRow7 = ' ', tableRow8 = '[http://www.minecraftwiki.net/wiki/User_talk: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) {                tableRow4 += nameSpaces[i] + headerBars;                tableRow6 += contribLink + i + ' ' + 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) { tableRow6 +=  + totalEdits + totalResponse.query.userinfo.editcount + bracesRight + transcludeEnd +  + '\n|}'; },           error: function (xhr, textStatus, error){ $('#t-editcounter').html('Error!'); alert(xhr.statusText); alert(textStatus); alert(error); }       }); // Arrange data into formatted table        tableRow4 += 'Total\n';        tableRow8 += timeStamp + '] \n ';        tableAll = tableRow1 + tableRow2 + tableRow3 + tableRow4 + tableRow5 + tableRow6 + tableRow7 + tableRow8; // 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(summaryLink) + '%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;        }    }); });