User:007a83/editcounter.js

/***********************************************************************************/ /*                                                                                */ /*                            MEDIAWIKI EDITCOUNTER v69                            */ /*                                                                                */ /*     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/en.wikipedia.org                  */ /*  source:        http://en.wikipedia.org/wiki/User:Kanegasi/editcounter.js      */ /*  documentation: http://en.wikipedia.org/wiki/User:Kanegasi/editcounter         */ /*  contacts:      http://minecraftwiki.net/wiki/User_talk:Kanegasi  -OR-         */ /*                 http://en.wikipedia.org/wiki/User_talk:Kanegasi                */ /***********************************************************************************/

$(document).ready(function { // Add new link labeled "Edit counter v69" 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 v69').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 (/wikipedia/.exec(wgServer)) { interwikiLink = 'en'; } else { interwikiLink = 'wikipedia'; }       if (typeof(pageName) == 'undefined') { pageName = 'edit count'; } if (typeof(summaryLink) == 'undefined') { summaryLink = interwikiLink + ':User:Kanegasi/editcounter'; } 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 = ''; } if (summaryLink == 'wikipedia:User:Kanegasi/editcounter' && interwikiLink == 'en') { summaryLink = interwikiLink + ':User:Kanegasi/editcounter'; }       var contribLink = '[' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=Special%3AContributions&target=' + wgUserName + '&namespace=', countTransclude = '', dateTransclude = '', transcludeEnd = '', headerPipe = '! ',           headerPipes = ' !! ';       if (transcludeAll == false) { countTransclude = ' {{#ifeq:{{{transcludesection|count}}}|count|'; dateTransclude = ' {{#ifeq:{{{transcludesection|date}}}|date|'; transcludeEnd = '}} '; }       if (tableHeaders == false) { headerPipe = '| '; headerPipes = ' || '; }       var bracesLeft = '', edits = [], editToken = '', logEvents = [], logUploads = [], nameSpaces = [], next = '', noCaptcha = false, oldPage = false, tableAll = '', tableRow1 = '\n', tableRow2 = '{| ' + mainTableAttrib + '\n', tableRow3 = '|- ' + topRowAttrib + '\n' + headerPipe, tableRow4 = '', tableRow5 = '|- class="plainlinks" ' + bottomRowAttrib + '\n| ', tableRow6 = '', tableRow7 = ' ', tableRow8 = '[[' + interwikiLink + ':User:Kanegasi/editcounter|Last counted on ',           timeStamp = dateTransclude + bracesLeft + 'subst:#time:F j, Y' + bracesRight + transcludeEnd,            totalEdits = countTransclude + bracesLeft + 'subst:formatnum:'; // Retrieve edit token and check if page exists        $.ajax({            url: mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?action=query&prop=info&intoken=edit&titles=User%3A' + escape(wgUserName) + '%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"]) {                        oldPage = true;                    }                }            },            error: function (xhr, textStatus, error){                $('#t-editcounter').html('Error!');                alert(xhr.statusText);                alert(textStatus);                alert(error);                return;            }        }); // Check if user autoconfirmed and stop script if false and no page exists        if (/autoconfirmed/.exec(wgUserGroups)) {            noCaptcha = true;        }        if (!oldPage && !noCaptcha) {            $('#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:" + wgUserName + "/" + pageName + "\t");            return;        } // Initialize namespace, logevent, upload, and edit arrays        $.ajax({            url: mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/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 === 0) {                            nameSpaces[siResponse.query.namespaces[ns].id] = 'Main';                        } else if (siResponse.query.namespaces[ns].id == 4) {                            nameSpaces[siResponse.query.namespaces[ns].id] = wgSiteName;                        } else if (siResponse.query.namespaces[ns].id == 5) {                            nameSpaces[siResponse.query.namespaces[ns].id] = wgSiteName + ' 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);                return;            }        }); // Fill logevent array        next = ;        while (next != 'stop') {            $.ajax({                url: mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?action=query&rawcontinue=&list=logevents&leuser=' + escape(wgUserName) + '&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);                    return;                }            });        } // Fill upload array        next = ;        while (next != 'stop') {            $.ajax({                url: mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?action=query&rawcontinue=&list=logevents&letype=upload&leuser=' + escape(wgUserName) + '&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);                    return;                }            });        } // Fill edit array, subtract logevents, and add uploads        next = ;        while (next != 'stop') {            $.ajax({                url: mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?action=query&rawcontinue=&list=usercontribs&ucuser=' + escape(wgUserName) + '&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);                    return;                }            });        }        for (var i = 0; i < nameSpaces.length; i++) {            edits[i] -= logEvents[i];            edits[i] += logUploads[i];            if (edits[i] > 0) {                tableRow4 += nameSpaces[i] + headerPipes;                tableRow6 += contribLink + i + ' ' + bracesLeft + 'subst:formatnum:' + edits[i] + bracesRight + ']' + ' || ';            }        } // Retrieve total edit count as seen on Preferences page        $.ajax({            url: mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/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);                return;            }        }); // Arrange data into formatted table        tableRow4 += 'Total\n';        tableRow8 += timeStamp + ']] \n '; tableAll = tableRow1 + tableRow2 + tableRow3 + tableRow4 + tableRow5 + tableRow6 + tableRow7 + tableRow8; // Submit post request to page $.ajax({           url: mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?action=edit&title=User%3A' + escape(wgUserName) + '%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);                return;            }        }); }); });