Jump to content

User:Kanegasi/editcounter.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/***********************************************************************************/
/*                                                                                 */
/*                           MEDIAWIKI EDITCOUNTER  v111                           */
/*                                                                                 */
/*     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:        Matt (Majr) @ minecraft.gamepedia.com                          */
/*   source:        http://minecraft.gamepedia.com/User:Majr/editcounter.js        */
/* Current version credit:                                                         */
/*   author:        Kanegasi @ minecraft.gamepedia.com/en.wikipedia.org            */
/*   source:        http://en.wikipedia.org/wiki/User:Kanegasi/editcounter.js      */
/*   documentation: http://en.wikipedia.org/wiki/User:Kanegasi/editcounter         */
/*   contacts:      http://minecraft.gamepedia.com/User_talk:Kanegasi  -OR-        */
/*                  http://en.wikipedia.org/wiki/User_talk:Kanegasi                */
/***********************************************************************************/
 
$(document).ready(function () {
// Add new link labeled "Edit counter v111" after "User contributions" in side menu
if (!$('#t-contributions').length) {
alert('Missing "User contributions" link in the side menu');
return;
}
var $editCountLink = $('<a>Edit counter v111</a>').attr({
id: 't-editcounter', href: '#', title: 'Run edit counter script'
});
$('#t-contributions').after($('<li></li>').html($editCountLink));
$('#t-editcounter').click(function () {
$('#t-editcounter').text('Processing…');
setTimeout(function () {
// Set up variables
if (/wikipedia.org/.test(mw.config.get('wgServer'))) {
interwikiLink = 'en';
} else {
interwikiLink = 'wikipedia';
}
if (typeof (pageName) == 'undefined') { pageName = 'edit count'; }
if (typeof (subPages) == 'undefined') { subPages = false; }
if (typeof (datePageName) == 'undefined') { datePageName = 'date'; }
if (typeof (totalPageName) == 'undefined') { totalPageName = 'total'; }
if (typeof (summaryLink) == 'undefined') {
summaryLink = interwikiLink + ':User:Kanegasi/editcounter';
}
if (typeof (tableCaption) == 'undefined') { tableCaption = ''; }
if (typeof (tableHeaders) == 'undefined' || tableHeaders === true) {
headerPipe = '! ';
} else {
headerPipe = '| ';
}
if (typeof (nameSpaces) == 'undefined') { nameSpaces = []; }
if (typeof (mainTableAttrib) == 'undefined') {
mainTableAttrib = 'class="wikitable" style="margin: ' +
'0 auto 1em; text-align: center"';
}
if (typeof (topRowAttrib) == 'undefined') { topRowAttrib = ''; }
if (typeof (bottomRowAttrib) == 'undefined') { bottomRowAttrib = ''; }
if (typeof (dateFormat) == 'undefined') { dateFormat = 'F j, Y'; }
if (typeof (noInclude1) == 'undefined') { noInclude1 = ''; } else {
noInclude1 = '<noinclude>' + noInclude1 + '</noinclude>';
}
if (typeof (noInclude2) == 'undefined') { noInclude2 = ''; } else {
noInclude2 = '<noinclude>' + noInclude2 + '</noinclude>';
}
if (typeof (noInclude3) == 'undefined') { noInclude3 = ''; } else {
noInclude3 = '<noinclude>' + noInclude3 + '</noinclude>';
}
if (typeof (noInclude) == 'undefined') { noInclude = ''; } else {
noInclude1 = '<noinclude>' + noInclude + '</noinclude>';
noInclude2 = '<noinclude>' + noInclude + '</noinclude>';
noInclude3 = '<noinclude>' + noInclude + '</noinclude>';
}
if (!(/\|/.test(summaryLink))) { summaryLink += '|updated'; }
if (/wikipedia:User:Kanegasi\/editcounter/i.test(summaryLink) &&
interwikiLink == 'en') {
summaryLink = summaryLink.replace(/wikipedia/i,'en');
}
var userName = mw.config.get('wgUserName'),
contribLink = '[' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') +
'/index.php?title=Special%3AContributions&target=' +
encodeURIComponent(userName) + '&namespace=',
bracesLeft = '{{',
bracesRight = '}}',
deletedEdits = 0,
edits = [],
editToken,
fullPageName = 'User:' + userName + '/' + pageName,
fullDatePageName = fullPageName + '/' + datePageName,
fullTotalPageName = fullPageName + '/' + totalPageName,
commonPage = 'User:' + userName + '/' + 'common.js',
vectorPage = 'User:' + userName + '/' + 'vector.js',
scriptPages = commonPage + '|' + vectorPage,
next,
noCaptcha = false,
oldPage = false,
oldPage1 = true,
oldPage2 = true,
oldPage3 = true,
tableAll,
timeStamp = bracesLeft + 'subst:#time:' + dateFormat + bracesRight,
tablePart1 = '{| ' + mainTableAttrib + '\n',
tablePart2 = '|+ style="caption-side: bottom; ' +
'font-size: x-small; font-weight: normal" | ',
tablePart3 = '[[' + interwikiLink + ':User:Kanegasi/editcounter|' +
'Last update: ' + timeStamp + ']]\n',
tablePart5 = '|- ' + topRowAttrib + '\n',
tablePart6 = '',
tablePart7 = '|- class="plainlinks" ' + bottomRowAttrib + '\n',
tablePart8 = '',
totalCount,
totalEdits = bracesLeft + 'subst:formatnum:',
touchedDate,
currentDate,
coolDown,
touchedDate2,
currentDate2,
coolDown2,
ajaxvars;
if (tableCaption !== '') {
tablePart4 = '|-\n! colspan=99 style="align: center; ' +
'border-left: hidden; border-right: hidden; ' +
'border-top: hidden" | ' + tableCaption + '\n';
} else {
tablePart4 = '';
}
// Retrieve edit token and check for existing page(s)
$.ajax({
type: "GET",
url: mw.util.wikiScript('api'),
data: {
format: 'json',
action: 'query',
prop: 'info',
intoken: 'edit',
titles: fullPageName + '|' + fullDatePageName +
'|' + fullTotalPageName + '|' + scriptPages
},
dataType: 'json',
async: false,
success: function (propResponse) {
for (var page in propResponse.query.pages) {
editToken = propResponse.query.pages[page].edittoken;
if (propResponse.query.pages[page].title == fullPageName) {
if (propResponse.query.pages[page].missing) {
oldPage1 = false;
} else {
oldPage = true;
}
if (propResponse.query.pages[page].touched) {
touchedDate = propResponse.query.pages[page].touched;
currentDate = propResponse.query.pages[page].starttimestamp;
coolDown = new Date(currentDate.substr(0,10).replace(/-/ig,',')) -
new Date(touchedDate.substr(0,10).replace(/-/ig,','));
}
}
if (propResponse.query.pages[page].title == vectorPage) {
if (!propResponse.query.pages[page].missing) {
if (propResponse.query.pages[page].touched) {
touchedDate2 = propResponse.query.pages[page].touched;
currentDate2 = propResponse.query.pages[page].starttimestamp;
coolDown2 = new Date(currentDate2.substr(0,10).replace(/-/ig,',')) -
new Date(touchedDate2.substr(0,10).replace(/-/ig,','));
}
}
}
if (propResponse.query.pages[page].title == commonPage) {
if (!propResponse.query.pages[page].missing && !coolDown2) {
if (propResponse.query.pages[page].touched) {
touchedDate2 = propResponse.query.pages[page].touched;
currentDate2 = propResponse.query.pages[page].starttimestamp;
coolDown2 = new Date(currentDate2.substr(0,10).replace(/-/ig,',')) -
new Date(touchedDate2.substr(0,10).replace(/-/ig,','));
}
}
}
if (propResponse.query.pages[page].title == fullDatePageName) {
if (propResponse.query.pages[page].missing && subPages) {
oldPage2 = false;
} else {
oldPage = true;
}
}
if (propResponse.query.pages[page].title == fullTotalPageName) {
if (propResponse.query.pages[page].missing && subPages) {
oldPage3 = false;
} else {
oldPage = true;
}
}
}
},
error: function (xhr, textStatus, error){
$('#t-editcounter').text('Error!');
alert(xhr.statusText);
alert(textStatus);
alert(error);
return;
}
});
// Check if user autoconfirmed and stop script if false and no page exists.
// Also check for last count and stop if same day.
if (/confirmed/i.test(mw.config.get('wgUserGroups'))) {
noCaptcha = true;
}
if (!oldPage && !noCaptcha) {
$('#t-editcounter').text('Error!');
if (!oldPage1) {
oldPage1 = '\n\t' + fullPageName;
} else {
oldPage1 = '';
}
if (!oldPage2 && subPages) {
oldPage2 = '\n\t' + fullDatePageName;
} else {
oldPage2 = '';
}
if (!oldPage3 && subPages) {
oldPage3 = '\n\t' + fullTotalPageName;
} else {
oldPage3 = '';
}
alert('Your account requires a captcha to make new pages.\n' +
'Make sure the following page(s) exist:\n' +
oldPage1 + oldPage2 + oldPage3);
return;
}
if (coolDown === 0 && coolDown2 !== 0) {
$('#t-editcounter').text('Error!');
alert('You have already counted your edits today.');
return;
}
// Initialize namespace and edit arrays
$.ajax({
type: "GET",
url: mw.util.wikiScript('api'),
data: {
format: 'json',
action: 'query',
meta: 'siteinfo',
siprop: 'namespaces'
},
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 &&
typeof (nameSpaces[0]) == 'undefined') {
nameSpaces[siResponse.query.namespaces[ns].id] = 'Main';
} else if (siResponse.query.namespaces[ns].id == 4 &&
typeof (nameSpaces[4]) == 'undefined') {
nameSpaces[siResponse.query.namespaces[ns].id] = mw.config.get('wgSiteName');
} else if (siResponse.query.namespaces[ns].id == 5 &&
typeof (nameSpaces[5]) == 'undefined') {
nameSpaces[siResponse.query.namespaces[ns].id] = mw.config.get('wgSiteName') +
' talk';
} else {
if (typeof (nameSpaces[siResponse.query.namespaces[ns].id]) == 'undefined') {
nameSpaces[siResponse.query.namespaces[ns].id] =
siResponse.query.namespaces[ns].canonical;
}
}
edits[siResponse.query.namespaces[ns].id] = 0;
}
}
},
error: function (xhr, textStatus, error) {
$('#t-editcounter').text('Error!');
alert(xhr.statusText);
alert(textStatus);
alert(error);
return;
}
});
// Fill edit array with moves (negative numbers)
next = '1';
while (next != 'stop') {
ajaxvars = {
type: "GET",
url: mw.util.wikiScript('api'),
data: {
format: 'json',
action: 'query',
rawcontinue: '',
list: 'logevents',
leuser: userName,
letype: 'move',
leprop: 'title',
lelimit: 'max',
ledir: 'newer'
},
dataType: 'json',
async: false,
success: function (logResponse) {
for (var event in logResponse.query.logevents) {
edits[logResponse.query.logevents[event].ns] -= 1;
deletedEdits -= 1;
}
if (logResponse['query-continue']) {
next = logResponse['query-continue'].logevents.lecontinue;
} else {
next = 'stop';
}
},
error: function (xhr, textStatus, error) {
$('#t-editcounter').text('Error!');
alert(xhr.statusText);
alert(textStatus);
alert(error);
return;
}
};
if (next != '1') {
ajaxvars.data = {
format: 'json',
action: 'query',
rawcontinue: '',
list: 'logevents',
leuser: userName,
letype: 'move',
leprop: 'title',
lelimit: 'max',
ledir: 'newer',
lecontinue: next
};
}
$.ajax(ajaxvars);
}
// Fill edit array with protects (negative numbers)
next = '1';
while (next != 'stop') {
ajaxvars = {
type: "GET",
url: mw.util.wikiScript('api'),
data: {
format: 'json',
action: 'query',
rawcontinue: '',
list: 'logevents',
leuser: userName,
letype: 'protect',
leprop: 'title',
lelimit: 'max',
ledir: 'newer'
},
dataType: 'json',
async: false,
success: function (logResponse) {
for (var event in logResponse.query.logevents) {
edits[logResponse.query.logevents[event].ns] -= 1;
deletedEdits -= 1;
}
if (logResponse['query-continue']) {
next = logResponse['query-continue'].logevents.lecontinue;
} else {
next = 'stop';
}
},
error: function (xhr, textStatus, error) {
$('#t-editcounter').text('Error!');
alert(xhr.statusText);
alert(textStatus);
alert(error);
return;
}
};
if (next != '1') {
ajaxvars.data = {
format: 'json',
action: 'query',
rawcontinue: '',
list: 'logevents',
leuser: userName,
letype: 'protect',
leprop: 'title',
lelimit: 'max',
ledir: 'newer',
lecontinue: next
};
}
$.ajax(ajaxvars);
}
// Fill edit array with file overwrites (negative numbers)
next = '1';
while (next != 'stop') {
ajaxvars = {
type: "GET",
url: mw.util.wikiScript('api'),
data: {
format: 'json',
action: 'query',
rawcontinue: '',
list: 'logevents',
leuser: userName,
leaction: 'upload/overwrite',
leprop: 'title',
lelimit: 'max',
ledir: 'newer'
},
dataType: 'json',
async: false,
success: function (logResponse) {
for (var event in logResponse.query.logevents) {
edits[logResponse.query.logevents[event].ns] -= 1;
deletedEdits -= 1;
}
if (logResponse['query-continue']) {
next = logResponse['query-continue'].logevents.lecontinue;
} else {
next = 'stop';
}
},
error: function (xhr, textStatus, error) {
$('#t-editcounter').text('Error!');
alert(xhr.statusText);
alert(textStatus);
alert(error);
return;
}
};
if (next != '1') {
ajaxvars.data = {
format: 'json',
action: 'query',
list: 'logevents',
rawcontinue: '',
leuser: userName,
leaction: 'upload/overwrite',
leprop: 'title',
lelimit: 'max',
ledir: 'newer',
lecontinue: next
};
}
$.ajax(ajaxvars);
}
// Fill edit array with normal edits (positive numbers)
next = '1';
while (next != 'stop') {
ajaxvars = {
type: "GET",
url: mw.util.wikiScript('api'),
data: {
format: 'json',
action: 'query',
rawcontinue: '',
list: 'usercontribs',
ucuser: userName,
ucprop: 'title',
uclimit: 'max',
ucdir: 'newer'
},
dataType: 'json',
async: false,
success: function (ucResponse) {
for (var event in ucResponse.query.usercontribs) {
edits[ucResponse.query.usercontribs[event].ns] += 1;
deletedEdits += 1;
}
if (ucResponse['query-continue']) {
next = ucResponse['query-continue'].usercontribs.uccontinue;
} else {
next = 'stop';
}
},
error: function (xhr, textStatus, error) {
$('#t-editcounter').text('Error!');
alert(xhr.statusText);
alert(textStatus);
alert(error);
return;
}
};
if (next != '1') {
ajaxvars.data = {
format: 'json',
action: 'query',
rawcontinue: '',
list: 'usercontribs',
ucuser: userName,
ucprop: 'title',
uclimit: 'max',
ucdir: 'newer',
uccontinue: next
};
}
$.ajax(ajaxvars);
}
for (var i = 0; i < nameSpaces.length; i++) {
if (edits[i] > 0) {
if (i == 2) {
if (subPages) {
edits[i] += 3;
} else {
edits[i] += 1;
}
}
tablePart6 += headerPipe + nameSpaces[i] + '\n';
tablePart8 += '| ' + contribLink + i + ' ' + bracesLeft +
'subst:formatnum:' + edits[i] + bracesRight + ']' + '\n';
}
}
tablePart6 += headerPipe + 'Total\n';
tablePart8 += '| [[Special:Contributions/' +
userName + '|<span title="Deleted edits: ';
// Retrieve total edit count and calculate estimated deleted edits
$.ajax({
type: "GET",
url: mw.util.wikiScript('api'),
data: {
format: 'json',
action: 'query',
meta: 'userinfo',
uiprop: 'editcount'
},
dataType: 'json',
async: false,
success: function (totalResponse) {
deletedEdits -= totalResponse.query.userinfo.editcount;
if (subPages) {
totalCount = totalResponse.query.userinfo.editcount + 3;
} else {
totalCount = totalResponse.query.userinfo.editcount + 1;
}
tablePart8 += Math.abs(deletedEdits) + '">' + totalEdits +
totalCount + bracesRight + '</span>]]\n|}';
},
error: function (xhr, textStatus, error) {
$('#t-editcounter').text('Error!');
alert(xhr.statusText);
alert(textStatus);
alert(error);
return;
}
});
tableAll = tablePart1 + tablePart2 + tablePart3 + tablePart4 +
tablePart5 + tablePart6 + tablePart7 + tablePart8;
// Submit post request to main page
$.ajax({
type: 'POST',
url: mw.util.wikiScript('api'),
data: {
format: 'json',
action: 'edit',
title: fullPageName,
text: tableAll + noInclude1,
summary: '[[' + summaryLink + ']]',
bot: '1',
minor: '1',
recreate: '1',
token: editToken
},
dataType: 'json',
async: false,
success: function () {
$('#t-editcounter').text('Success!');
},
error: function (xhr, textStatus, error) {
$('#t-editcounter').text('Error!');
alert(xhr.statusText);
alert(textStatus);
alert(error);
return;
}
});
// Submit post request to date page
if (subPages) {
$.ajax({
type: 'POST',
url: mw.util.wikiScript('api'),
data: {
format: 'json',
action: 'edit',
title: fullDatePageName,
text: timeStamp + noInclude2,
summary: '[[' + summaryLink + ']]',
bot: '1',
minor: '1',
recreate: '1',
token: editToken
},
dataType: 'json',
async: false,
success: function () {
$('#t-editcounter').text('Success!');
},
error: function (xhr, textStatus, error) {
$('#t-editcounter').text('Error!');
alert(xhr.statusText);
alert(textStatus);
alert(error);
return;
}
});
}
// Submit post request to total page
if (subPages) {
$.ajax({
type: 'POST',
url: mw.util.wikiScript('api'),
data: {
format: 'json',
action: 'edit',
title: fullTotalPageName,
text: totalCount + noInclude3,
summary: '[[' + summaryLink + ']]',
bot: '1',
minor: '1',
recreate: '1',
token: editToken
},
dataType: 'json',
async: false,
success: function () {
$('#t-editcounter').text('Success!');
},
error: function (xhr, textStatus, error) {
$('#t-editcounter').text('Error!');
alert(xhr.statusText);
alert(textStatus);
alert(error);
return;
}
});
}
},1000);
});
});