User:Kanegasi/editcounter.js

/*************************************************************************************/ /*                                                                                  */ /*                             MEDIAWIKI EDITCOUNTER v47                             */ /*                                                                                  */ /*      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 v47" 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 v47').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 = ' '; }       if ((typeof(pageName) == 'undefined') || (pageName == '')) { pageName = 'edit count'; } if (typeof(mainDivAttrib) == 'undefined') { mainDivCont = ' '; } else if (mainDivAttrib == '') { mainDivCont = ' '; } else { mainDivCont = ''; } var bracesLeft = '', captcha = true, edits = [], editToken = '', logEvents = [], logUploads = [], nameSpaces = [], newPage = true, next = '', table00 = '', table01 = transcludeAll1 + mainDivCont + '\n', table02 = '{| ' + mainTableAttrib + '\n', table03 = '|+ ' + tableCaption + '\n', table04 = '|- ' + topRowAttrib + '\n| ', table05 = '', table06 = '|- ' + bottomRowAttrib + '\n| ', table07 = '', table08 = ' ', table09 = '[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; logUploads[siResponse.query.namespaces[ns].id] = 6; 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) + '&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; logUploads[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 edit array and add/subtract logevents        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) {                table05 += nameSpaces[i] + ' || ';                table07 += 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) { table07 += 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        table05 += 'Total\n';        table09 += timeStamp + '] \n ' + transcludeAll2;        table00 = table01 + table02 + table03 + table04 + table05 + table06 + table07 + table08 + table09; // 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(table00) + '&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;        }    }); });