Minecraft Wiki
m (success! finished product, usable by anyone on this wiki, straight from this script page with no alterations, and on other wikis, assuming same api urls on other mediawiki sites)
mNo edit summary
Line 1: Line 1:
 
/***********************************************************************************/
 
/***********************************************************************************/
 
/* */
 
/* */
/* MEDIAWIKI EDITCOUNTER v39 */
+
/* MEDIAWIKI EDITCOUNTER v40 */
 
/* */
 
/* */
 
/* Original credit: */
 
/* Original credit: */
Line 18: Line 18:
   
 
$(document).ready(function () {
 
$(document).ready(function () {
// Add new link labeled "Edit counter v39" after "User contributions" link in side menu
+
// Add new link labeled "Edit counter v40" after "User contributions" link in side menu
 
if (!$('#t-contributions').length) {
 
if (!$('#t-contributions').length) {
 
alert('Missing "User contributions" link in the side menu');
 
alert('Missing "User contributions" link in the side menu');
 
return;
 
return;
 
}
 
}
var $editCountLink = $('<a>Edit counter v39</a>').attr( { id: 't-editcounter', href: '#', title: 'Run edit counter script' } );
+
var $editCountLink = $('<a>Edit counter v40</a>').attr( { id: 't-editcounter', href: '#', title: 'Run edit counter script' } );
 
$('#t-contributions').after($('<li></li>').html($editCountLink));
 
$('#t-contributions').after($('<li></li>').html($editCountLink));
 
// Change link to read "Processing..." when clicked
 
// Change link to read "Processing..." when clicked
Line 65: Line 65:
 
timestamp = '<onlyinclude>{{#ifeq:{{{transcludesection|date}}}|date|{{subst:#time:F j, Y',
 
timestamp = '<onlyinclude>{{#ifeq:{{{transcludesection|date}}}|date|{{subst:#time:F j, Y',
 
totaledits = '<onlyinclude>{{#ifeq:{{{transcludesection|count}}}|count|{{subst:formatnum:',
 
totaledits = '<onlyinclude>{{#ifeq:{{{transcludesection|count}}}|count|{{subst:formatnum:',
scriptpage = mw.config.get('wgTitle'),
 
 
usergroup = mw.config.get('wgUserGroups'),
 
usergroup = mw.config.get('wgUserGroups'),
 
username = mw.config.get('wgUserName'),
 
username = mw.config.get('wgUserName'),
Line 206: Line 205:
 
if (!captcha && !newpage) {
 
if (!captcha && !newpage) {
 
$.ajax({
 
$.ajax({
url: '/api.php?action=edit&title=User%3A' + escape(username) + '%2F' + escape(pagename) + '&summary=%5B%5BUser%3A' + escape(scriptpage) + '%7Cupdated%5D%5D&bot=1&minor=1&recreate=1&text=' + escape(table) + '&token=' + edittoken,
+
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',
 
type: 'POST',
 
async: false,
 
async: false,

Revision as of 00:47, 7 January 2013

/***********************************************************************************/
/*                                                                                 */
/*                            MEDIAWIKI EDITCOUNTER v40                            */
/*                                                                                 */
/* 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 v40" after "User contributions" link in side menu
    if (!$('#t-contributions').length) {
        alert('Missing "User contributions" link in the side menu');
        return;
    }
    var $editCountLink = $('<a>Edit counter v40</a>').attr( { id: 't-editcounter', href: '#', title: 'Run edit counter script' } );
    $('#t-contributions').after($('<li></li>').html($editCountLink));
// Change link to read "Processing..." when clicked
    $('#t-editcounter').click(function () {
        $('#t-editcounter').html('Processing...');
// Set up variables
        if (typeof pagename == 'undefined' || pagename == '') { var pagename = 'edit count'; }
        if (typeof tableclass == 'undefined') { var tableclass = ''; }
        if (typeof tablecolortop == 'undefined' || tablecolortop == '') {
            var tablecolortop = '729FCF';
        } else {
            tablecolortop = tablecolortop.replace(/#/g, '');
        }
        if (typeof tablecolorbottom == 'undefined' || tablecolorbottom == '') {
            var tablecolorbottom = 'FFFFFF';
        } else {
            tablecolorbottom = tablecolorbottom.replace(/#/g, '');
        }
        var bracesleft = '{{',
            bracesright = '}}',
            captcha = true,
            edits = [],
            edittoken = '',
            logevents = [],
            namespaces = [],
            newpage = true,
            next = '',
            oi = '}}}}</onlyinclude>',
            table = '',
            table1 = '{| class="' + tableclass + '" cellpadding=5 cellspacing=1 style="margin: 0 auto; text-align: center"\n',
            table2 = '|- style="background-color: #' + tablecolortop + '; font-size: small"\n| ',
            table3 = '',
            table4 = '|- style="background-color: #' + tablecolorbottom + '; font-size: large"\n| ',
            table5 = '',
            table6 = '|- style="line-height: 1em"\n',
            table7 = '| colspan=20 |\n',
            table8 = '{| style="font-size: xx-small" width=100%\n',
            table9 = '| align=left width=30% | ',
            table10 = '| align=center | [http://www.minecraftwiki.net/wiki/User:Kanegasi/editcounter.js editcounter.js]\n',
            table11 = '| align=right width=30% | [http://strategywiki.org/wiki/User_talk:Najzere/edit_counter.js#Bugs count discrepancy]\n|}\n|}',
            timestamp = '<onlyinclude>{{#ifeq:{{{transcludesection|date}}}|date|{{subst:#time:F j, Y',
            totaledits = '<onlyinclude>{{#ifeq:{{{transcludesection|count}}}|count|{{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 + oi + '\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';
        table9 += timestamp + oi + '\n';
        table = table1 + table2 + table3 + table4 + table5 + table6 + table7 + table8 + table9 + table10 + table11;
// 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;
        }
    });
});