//<nowiki>
/************************************************************************************************/
/* Edit counter script based on strategywiki.org/wiki/User:Najzere/edit_counter.js */
/* author: [[User:Technical 13]] */
/* source: https://en.wikipedia.org/wiki/User:Technical_13/edit_counter.js */
/* documentation: https://en.wikipedia.org/wiki/User:Technical_13/edit_counter */
/* contact: https://en.wikipedia.org/wiki/User_talk:Technical_13 */
/* license: CC-BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.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 in the toolbox */
var $editCountLink = mw.util.addPortletLink(
'p-cactions',
'#',
'Edit Counter',
'pt-editcounter',
'Run edit counter script',
null,
null
);
/* start process */
$('#pt-editcounter').click( function() {
/* Set up variables */
$(this).html('Initializing<br />variables...');
if (document.location.href.match(/:\/\/en.wikipedia.org\/w/)) {
var editSummary = 'Edited with [[User:Technical_13/Scripts/Edit_counter|edit counter]]';
var tableTopLink = '[[User:Technical_13/Scripts/Edit_counter|Edit count]]';
} else {
var editSummary = 'Edited with [[en:User:Technical_13/Scripts/Edit_counter|edit counter]]';
var tableTopLink = '[[en:User:Technical_13/Scripts/Edit_counter|Edit count]]';tableTopLink);
}
if ( typeof(editPage) == 'undefined' || editPage === '' ) {
var editPage = "User:" + mw.config.get('wgUserName') + "/Edit count";
} else {
editPage = "User:" + mw.config.get('wgUserName') + "/" + editPage;
}
if ( typeof(tableClass) == 'undefined' || tableClass === '' ) {
var tableClass = '';
}
if ( typeof(tableStyle) == 'undefined' || tableStyle === '' ) {
var tableStyle = '';
} else {
tableStyle = ' style="' + tableStyle + '"';
}
if ( typeof(tableTopStyle) == 'undefined' ) {
var tableTopStyle = '';
} else {
tableTopStyle = ' style="' + tableTopStyle + '"';
}
var skipCAPTCHA = false;
var oldPage = false;
var mwEditCount = 0;
var edits = 0;
var editCount = 0;
var nsnums = [];
var nsnums2 = [];
var namespaces = [];
var movelog = [];
var protectlog = [];
var next;
var editToken;
var succeeded = false;
var table = '';
var tableTop = '{| class="wikitable ' + tableClass + '"' + tableStyle + '\n|- \n! colspan="4"' + tableTopStyle + '| ' + tableTopLink + ':';
var tableBottom = '\n|- \n| colspan="4" align="center"| <small>Last run: {' + '{Plain link|url=' + projectLink.replace(/\s/g, "_") + "iki/" + editPage.replace(/\s/g, "_") + '?diff=curr|name=' + '{{SUBST:#time:F j, Y}}}}</small>\n|}';
/* Check if the user is autoconfirmed */
$(this).html('CAPTCHA test...');
if ($.inArray('confirmed', mw.config.get('wgUserGroups')) || $.inArray('autoconfirmed', mw.config.get('wgUserGroups'))){
var skipCAPTCHA = true;
}
/* Get full edit count, as tracked by MW, add it to the tableTop */
$(this).html('Gathering total edits...');
mwEditCount = mw.config.get( 'wgUserEditCount' );
tableTop += ' {{SUBST:formatnum:' + mwEditCount + '}}';
/* Initialize namespace, move log and patrol log arrays */
$(this).html('Gathering<br />namespaces');
var siRequest = {
action: 'query',
meta: 'siteinfo',
siprop: 'namespaces',
format: 'json',
continue: ''
};
$.get(mw.config.get( 'wgScriptPath' ) + "/api.php", siRequest, 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) {
namespaces[i] = 'Main';
}
else if (siResponse.query.namespaces[ns].id == 4) {
namespaces[i] = mw.config.get('wgSiteName');
}
else if (siResponse.query.namespaces[ns].id == 5) {
namespaces[i] = mw.config.get('wgSiteName') + ' talk';
}
else {
namespaces[i] = siResponse.query.namespaces[ns].canonical;
}
nsnums[siResponse.query.namespaces[ns].id] = i;
console.info("namespace %d added to list as %s.", siResponse.query.namespaces[ns].id, namespaces[i]);
nsnums2[i] = siResponse.query.namespaces[ns].id;
movelog[i] = 0;
protectlog[i] = 0;
i++;
}
}
}).done(
/* Fill move log array */
$(this).html('Counting page<br />move logs');
next = '';
while (next != 'stop') {
var moveRequest = {
action: 'query',
rawcontinue: '',
list: 'logevents',
letype: 'move',
leuser: mw.config.get( 'wgUserName' ),
lelimit: 'max',
leprop: 'title',
format: 'json',
lestart: next,
continue: ''
};
$.get(mw.config.get( 'wgScriptPath' ) + "/api.php", moveRequest, function(moveResponse) {
for (var event in moveResponse.query.logevents) {
movelog[nsnums[moveResponse.query.logevents[event].ns]] += 1;
}
if (moveResponse["query-continue"]) {
next = moveResponse["query-continue"].logevents.lestart;
} else {
next = 'stop';
}
});
}
/* Fill protect log array */
$(this).html('Counting page<br />protection logs');
next = '';
while (next != 'stop') {
var protectRequest = {
action: 'query',
rawcontinue: '',
list: 'logevents',
letype: 'protect',
leuser: mw.config.get( 'wgUserName' ),
lelimit: 'max',
leprop: 'title',
format: 'json',
lestart: next,
continue: ''
};
$.get(mw.config.get( 'wgScriptPath' ) + "/api.php", protectRequest, function(protectResponse) {
for (var event in protectResponse.query.logevents) {
protectlog[nsnums[protectResponse.query.logevents[event].ns]] += 1;
}
if (protectResponse["query-continue"]) {
next = protectResponse["query-continue"].logevents.lestart;
} else {
next = 'stop';
}
});
}
).done(
/* Loop through namespaces counting edits and subtracting page moves and patrols */
$(this).html('Counting<br />edits in<br />' + namespaces[1]);
for (var i = 1; i < namespaces.length; i++) {
next = '';
edits = 0;
while (next != 'stop') {
var ucRequest = {
action: 'query',
rawcontinue: '',
list: 'usercontribs',
leuser: mw.config.get( 'wgUserName' ),
lelimit: 'max',
ucdir: 'newer',
leprop: 'title',
format: 'json',
ucnamespace: nsnums2[i],
lestart: next,
continue: ''
};
$.get(mw.config.get( 'wgScriptPath' ) + "/api.php", ucRequest, function(ucResponse) {
edits += ucResponse.query.usercontribs.length;
if (ucResponse["query-continue"]) {
next = ucResponse["query-continue"].usercontribs.ucstart;
} else {
next = 'stop';
}
});
}
edits = edits - movelog[i] - protectlog[i];
editCount = editCount + edits;
if (edits > 0) {
table += '\n|- \n| style="border-right: 0px;"| '+namespaces[i]+' || style="border-left: 0px; text-align: center;"| NS:' + nsnums2[i] + ' || style="text-align: center;"| {{SUBST:formatnum:' + edits + '}} || style="text-align: right;"| ' + ( Math.round( ( edits / mwEditCount ) * 100 ) ) + '%';
}
if (i+1 < namespaces.length){
$(this).html('Counting<br />edits in<br />' + namespaces[i+1]);
} else {
$(this).html('Tallying all<br />edits from<br />the API...');
}
}
tableTop += ' (' + ( 100 - ( Math.round( ( editCount / mwEditCount ) * 100 ) ) ) + '%)';
table = tableTop + table + tableBottom;
).done(
/* Get edit token and check if we'll be creating a new page (in the case of non-autoconfirmed users) */
$.ajax({
url: apiLink + 'action=query&prop=info|revisions&intoken=edit&format=json&titles=' + editPage,
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].revisions) {
oldPage = true;
}
}
}
});
).done(
/* If the user is not autoconfirmed and we're creating a new page, kick out of the script */
if (!skipCAPTCHA && !oldPage) {
alert("Sorry, you're not autoconfirmed yet, so captcha is required to make new pages. Go create the page at: " + editPage + ", then try again.");
$(this).html('Error!<br /> Error!<br />Error!');
return;
}
/* Edit the target page with the table */
var saveResults = {
action:"edit",
title: editPage,
text: escape(table),
summary: editSummary,
minor: 1,
recreate: 1,
bot: 1,
token: mw.user.tokens.get( 'editToken' )
};
$.post(mw.config.get( 'wgScriptPath' )+"/api.php", saveResults, function(savedResults){ succeeded = true; });
).done(
/* change edit count link to reflect the status of the page save */
if (succeeded) {
$(this).html('Done!');
} else {
$(this).html('Error!<br /> Error!<br />Error!');
}
);
});
});
//</nowiki>