/************************************************************************************************/
/* Edit counter script based on strategywiki.org/wiki/User:Najzere/edit_counter.js */
/* author: [[User:Technical 13]] */
/* source: [[User:Technical 13/Scripts/edit counter.js]] */
/* documentation: [[User:Technical 13/Scripts/edit counter]] */
/* contact: [[User talk:Technical 13]] */
/* license: CC-BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.0/) */
/* version: 1.0.0 */
/************************************************************************************************/
// Only add edit count button on user pages
if (mw.config.get('wgTitle') == mw.config.get('wgUserName') && mw.config.get('wgNamespaceNumber') == 2) {
$(document).ready(function() {
// Add a new link after "User contributions" in the toolbox
if ($('#t-contributions').length === 0) {
alert('Missing "User contributions" link in the side menu! Contact script creator for assistance.');
return;
}
var $editCountVLink = $('<a>Edit counter</a>').attr( { id: 't-editcounter', href: '#', title: 'Run edit counter script' } );
$('#t-contributions').after($('<li></li>').html($editCountVLink));
// start process
$('#t-editcounter').click( function() {
// Set up variables
$(this).html('Initializing<br />variables...');
if (document.location.href.match(/:\/\/en.wikipedia.org\/w/)) {
var editSummaryV = 'Edited with [[User:Technical_13/Scripts/Edit_counter|edit counter]]';
var tableVTopLink = '[[User:Technical_13/Scripts/Edit_counter|Edit count]]';
} else {
var editSummaryV = 'Edited with [[en:User:Technical_13/Scripts/Edit_counter|edit counter]]';
var tableVTopLink = '[[en:User:Technical_13/Scripts/Edit_counter|Edit count]]';
}
if ( typeof(editPageV) == 'undefined' || editPageV === '' ) { editPageV = "User:" + mw.config.get('wgUserName') + "/Edit count"; }
else { editPageV = "User:" + mw.config.get('wgUserName') + "/" + editPageV; }
if ( typeof(tableVClass) == 'undefined' ) { tableVClass = ''; }
if ( typeof(tableVStyle) == 'undefined' ) { tableVStyle = ''; }
else { tableVStyle = ' style="' + tableVStyle + '"'; }
if ( typeof(tableVTopStyle) == 'undefined' ) { tableVTopStyle = ''; }
else { tableVTopStyle = ' style="' + tableVTopStyle + '"'; }
var VskipCAPTCHA = false;
var oldPageV = false;
var mwEditCountV = 0;
var editsV = 0;
var editCountV = 0;
var nsnumsV = [];
var nsnumsV2 = [];
var namespacesV = [];
var movelogV = [];
var protectlogV = [];
var nextV;
var editTokenV;
var succeededV = false;
var substV = '{SUBST:';
var tableV = '';
var projectLinkV = mw.config.get('wgServer') + mw.config.get('wgScriptPath');
var apiLinkV = projectLinkV + '/api.php?';
var tableVTop = '{| class="wikitable ' + tableVClass + '"' + tableVStyle + '\n|- \n! colspan="4"' + tableVTopStyle + '| ' + tableVTopLink + ':';
var tableVBottom = '\n|- \n| colspan="4" align="center"| <small>Last run: {' + '{Plain link|url=' + projectLinkV.replace(/\s/g, "_") + "iki/" + editPageV.replace(/\s/g, "_") + '?diff=curr|name=' + '{' + substV + '#time:F j, Y}}}}</small>\n|}';
// Check if the user is autoconfirmed
$(this).html('CAPTCHA test...');
for (var right in mw.config.get('wgUserGroups'))
if (mw.config.get('wgUserGroups')[right] == 'confirmed' || mw.config.get('wgUserGroups')[right] == 'autoconfirmed') {
VskipCAPTCHA = true;
}
// Get full edit count, as tracked by MW, add it to the tableVTop
$(this).html('Gathering total edits...');
mwEditCountV = mw.config.get( 'wgUserEditCount' );
tableVTop += ' {' + substV + 'formatnum:' + mwEditCountV + "}}";
// Initialize namespace, move log and patrol log arrays
$(this).html('Gathering<br />namespaces');
$.ajax({
url: apiLinkV + 'action=query&meta=siteinfo&siprop=namespaces&format=json',
dataType: 'json',
success: function(siResponse) {
var i = 1;
for (var ns in siResponse.query.namespaces) {
if (siResponse.query.namespaces[ns].id > -1) {
if (siResponse.query.namespaces[ns].id === 0) {
namespacesV[i] = 'Main';
}
else if (siResponse.query.namespaces[ns].id == 4) {
namespacesV[i] = mw.config.get('wgSiteName');
}
else if (siResponse.query.namespaces[ns].id == 5) {
namespacesV[i] = mw.config.get('wgSiteName') + ' talk';
}
else {
namespacesV[i] = siResponse.query.namespaces[ns].canonical;
}
nsnumsV[siResponse.query.namespaces[ns].id] = i;
nsnumsV2[i] = siResponse.query.namespaces[ns].id;
movelogV[i] = 0;
protectlogV[i] = 0;
i++;
}
}
}
});
// Fill move log array
$(this).html('Counting page<br />move logs');
nextV = '';
while (nextV != 'stop') {
$.ajax({
url: apiLinkV + 'action=query&list=logevents&letype=move&leuser=' + mw.config.get( 'wgUserName' ) + '&lelimit=max&leprop=title&rawcontinue=&format=json' + nextV,
dataType: 'json',
success: function(moveResponse) {
for (var event in moveResponse.query.logevents) {
movelogV[nsnumsV[moveResponse.query.logevents[event].ns]] += 1;
}
if (moveResponse["query-continue"]) {
nextV = '&lestart=' + moveResponse["query-continue"].logevents.lestart;
} else {
nextV = 'stop';
}
}
});
}
// Fill protect log array
$(this).html('Counting page<br />protection logs');
nextV = '';
while (nextV != 'stop') {
$.ajax({
url: apiLinkV + 'action=query&list=logevents&letype=protect&leuser=' + mw.config.get('wgUserName') + '&lelimit=max&leprop=title&rawcontinue=&format=json' + nextV,
dataType: 'json',
success: function(protectResponse) {
for (var event in protectResponse.query.logevents) {
protectlogV[nsnumsV[protectResponse.query.logevents[event].ns]] += 1;
}
if (protectResponse["query-continue"]) {
nextV = '&lestart=' + protectResponse["query-continue"].logevents.lestart;
} else {
nextV = 'stop';
}
}
});
}
// Loop through namespaces counting edits and subtracting page moves and patrols
$(this).html('Counting<br />edits in<br />' + namespacesV[1]);
for (var i = 1; i < namespacesV.length; i++) {
nextV = '';
editsV = 0;
while (nextV != 'stop') {
$.ajax({
url: apiLinkV + 'action=query&list=usercontribs&ucuser=' + mw.config.get('wgUserName') + '&uclimit=max&ucdir=newer&rawcontinue=&format=json&ucnamespace=' + nsnumsV2[i] + nextV,
dataType: 'json',
success: function(ucResponse) {
editsV += ucResponse.query.usercontribs.length;
if (ucResponse["query-continue"]) {
nextV = '&uccontinue=' + ucResponse["query-continue"].usercontribs.uccontinue;
} else {
nextV = 'stop';
}
}
});
}
editsV = editsV - movelogV[i] - protectlogV[i];
editCountV = editCountV + editsV;
if (editsV > 0) {
tableV += '\n|- \n| style="border-right: 0px;"| ' + namespacesV[i]+' || style="border-left: 0px; text-align: center;"| NS:' + nsnumsV2[i] + ' || style="text-align: center;"| {' + substV + 'formatnum:' + editsV + '}} || style="text-align: right;"| ' + ( Math.round( ( editsV / mwEditCountV ) * 100 ) ) + "%";
}
if (i+1 < namespacesV.length){
$(this).html('Counting<br />edits in<br />' + namespacesV[i+1]);
} else {
$(this).html('Tallying all<br />edits from<br />the API...');
}
}
tableVTop += " (" + ( 100 - ( Math.round( ( editCountV / mwEditCountV ) * 100 ) ) ) + "%)";
tableV = tableVTop + tableV + tableVBottom;
// Get edit token and check if we'll be creating a new page (in the case of non-autoconfirmed users)
$.ajax({
url: apiLinkV + 'action=query&prop=info|revisions&intoken=edit&format=json&titles=' + editPageV,
dataType: 'json',
success: function(propResponse) {
for (var page in propResponse.query.pages) {
editTokenV = propResponse.query.pages[page].edittoken.replace(/\+\\$/g, '%2B%5C');
if (propResponse.query.pages[page].revisions) {
oldPageV = true;
}
}
}
});
// If the user is not autoconfirmed and we're creating a new page, kick out of the script
if (!VskipCAPTCHA && !oldPageV) {
alert("Sorry, you're not autoconfirmed yet, so captcha is required to make new pages. Go create the page at: " + editPageV + ", then try again.");
$(this).html('Error!<br /> Error!<br />Error!');
return;
}
// Edit the target page with the table
$.ajax({
url: apiLinkV + 'action=edit&title=' + editPageV + '&summary=' + editSummaryV + '&minor=1&recreate=1&bot=1&text=' + escape(tableV) + '&token=' + editTokenV,
type: 'POST',
success: function() {
succeededV = true;
}
});
// change edit count link to reflect the status of the page save
if (succeededV) {
$(this).html('Done!').attr('href', document.location.href);
$('#t-editcounter').click(function(){$(this).html('Reloading!')});
} else {
$(this).html('Error!<br /> Error!<br />Error!');
}
});
});
}