MediaWiki
Gadget-statistics.js
From Dogcraft Wiki
(Added site statistics) |
m (Replacing {{!!}} with {{!}}{{!}}) |
||
(14 intermediate revisions by 2 users not shown) | |||
Line 6: | Line 6: | ||
$(document).ready(function() { | $(document).ready(function() { | ||
/* Creates the mediawiki api object */ | |||
var api = new mw.Api(); | |||
/* | /* | ||
* Sends an API request to get the usernames and editcounts of the first 200 accounts, and then | * Sends an API request to get the usernames and editcounts of the first 200 accounts, and then | ||
Line 15: | Line 18: | ||
*/ | */ | ||
function wecGetUserEditCounts() { | function wecGetUserEditCounts() { | ||
/* Creates parameters for the api call */ | /* Creates parameters for the api call */ | ||
Line 24: | Line 24: | ||
format: 'json', | format: 'json', | ||
list: 'allusers', /* https://www.mediawiki.org/wiki/API:Allusers */ | list: 'allusers', /* https://www.mediawiki.org/wiki/API:Allusers */ | ||
aulimit: | aulimit: 5000, | ||
auprop: 'editcount' | auprop: 'editcount', | ||
auwitheditsonly: true | |||
}, | }, | ||
/* Variablse to fill with the data from the api call */ | /* Variablse to fill with the data from the api call */ | ||
wecTableData = "", | wecTableData = ""/*, | ||
wecSingleData = "", | wecSingleData = "", | ||
wecRawData = ""; | wecRawData = ""*/; | ||
/* This is the actual api call */ | /* This is the actual api call */ | ||
Line 40: | Line 41: | ||
/* Fills the wecTableData variable with the usernames and editcounts, formated like the body of a wikitable */ | /* Fills the wecTableData variable with the usernames and editcounts, formated like the body of a wikitable */ | ||
for ( u in users ) { | for ( u in users ) { | ||
wecTableData += '{{!}}-\n{{!}}' + users[u].name + '{{!!}}' + users[u].editcount + '\n{{!}}-\n'; | wecTableData += '{{!}}-\n{{!}}' + users[u].name + '{{!}}{{!}}' + users[u].editcount + '\n{{!}}-\n'; | ||
} | } | ||
/* Fills the wecSingleData variable with the usernames and editcounts, formated like the body of a #switch parser function */ | /* Fills the wecSingleData variable with the usernames and editcounts, formated like the body of a #switch parser function */ | ||
for ( u in users ) { | /*for ( u in users ) { | ||
wecSingleData += '|' + users[u].name + ' = ' + users[u].editcount + '\n'; | wecSingleData += '|' + users[u].name + ' = ' + users[u].editcount + '\n'; | ||
} | }*/ | ||
/* Fills the wecTableData variable with the usernames and editcounts, formated like plain wikitext, with the option to add | /* Fills the wecTableData variable with the usernames and editcounts, formated like plain wikitext, with the option to add | ||
* text (this can also be HTML tags like <li>) at the front, middle and end using template variables. | * text (this can also be HTML tags like <li>) at the front, middle and end using template variables. | ||
*/ | */ | ||
for ( u in users ) { | /*for ( u in users ) { | ||
wecRawData += '{{{rawRowFront}}}' + users[u].name + '{{{rawRowMid}}}' + users[u].editcount + '{{{rawRowBack}}}'; | wecRawData += '{{{rawRowFront}}}' + users[u].name + '{{{rawRowMid}}}' + users[u].editcount + '{{{rawRowBack}}}'; | ||
} | }*/ | ||
/* Creates the variables that when combined, create the template page, and fills them up. | /* Creates the variables that when combined, create the template page, and fills them up. | ||
Line 67: | Line 68: | ||
*/ | */ | ||
var templateNoInclude = '<noinclude>{{Notice|title=Automated edit warning|message=This template is edited by an automated process via the EditCount gadget, manual edits may get overwritten. To modify the template code please head to [[MediaWiki:Gadget-sandbox.js]]. To modify the documentation appearing here, please head to [[Template:EditCountMain/doc]].}}{{' + 'documentation}}</noinclude>\n', | var templateNoInclude = '<noinclude>{{Notice|title=Automated edit warning|message=This template is edited by an automated process via the EditCount gadget, manual edits may get overwritten. To modify the template code please head to [[MediaWiki:Gadget-sandbox.js]]. To modify the documentation appearing here, please head to [[Template:EditCountMain/doc]].}}{{' + 'documentation}}</noinclude>\n', | ||
templateSection1 = '< | templateSection1 = '<onlyinclude>' + /*{{#switch: {{{mode|}}}\n|table = */ '<div class={{{tableBoxClass}}} style={{{tableBoxStyle}}}>\n{{{!}} class=\"wikitable sortable\"\n!Username\n!Edit count\n', | ||
templateSection1data = wecTableData, | templateSection1data = wecTableData, | ||
templateSection2 = '{{!}}}</div>\n|single = \n{{#switch: {{{username}}} \n', | templateSection2 = '{{!}}}</div>}}</onlyinclude>'/*\n|single = \n{{#switch: {{{username}}} \n', | ||
templateSection2data = wecSingleData, | templateSection2data = wecSingleData, | ||
templateSection3 = '|There is no editcount found for this user\n}}\n|raw =\n', | templateSection3 = '|There is no editcount found for this user\n}}\n|raw =\n', | ||
templateSection3data = wecRawData, | templateSection3data = wecRawData, | ||
templateSectionEnd = '|That mode does not exist\n}}\n', | templateSectionEnd = '|That mode does not exist\n}}\n'*/, | ||
pagecontent = templateNoInclude + templateSection1 + templateSection1data + templateSection2 + templateSection2data + templateSection3 + templateSection3data + templateSectionEnd; | pagecontent = templateNoInclude + templateSection1 + templateSection1data + templateSection2 /*+ templateSection2data + templateSection3 + templateSection3data + templateSectionEnd*/; | ||
/* Calls the "wecUpdateTemplatePage" function with the "pagecontet" variable that was assembled above. | /* Calls the "wecUpdateTemplatePage" function with the "pagecontet" variable that was assembled above. | ||
Line 80: | Line 81: | ||
*/ | */ | ||
wecUpdateTemplatePage(pagecontent, 'Template:EditCountMain', false); | wecUpdateTemplatePage(pagecontent, 'Template:EditCountMain', false); | ||
wecGetWikiStatistics(); | |||
Line 89: | Line 91: | ||
*/ | */ | ||
function wecUpdateTemplatePage(content, title, appendtext) { | function wecUpdateTemplatePage(content, title, appendtext) { | ||
/* Creates paramters for the edit api call. | /* Creates paramters for the edit api call. | ||
Line 105: | Line 104: | ||
title: title, /* Title of the template page that gets replaced with new content */ | title: title, /* Title of the template page that gets replaced with new content */ | ||
appendtext: content, | appendtext: content, | ||
summary: 'Edit added with the API' | summary: 'Edit added with the API' /* Description of the edit that appears in the page history */ | ||
bot : true | //bot : true /* Tags the edit as a bot edit, which should hide it from the default Recent Changes page (does not work) */ | ||
}; | }; | ||
} | } | ||
Line 116: | Line 115: | ||
title: title, /* Title of the template page that gets replaced with new content */ | title: title, /* Title of the template page that gets replaced with new content */ | ||
text: content, | text: content, | ||
summary: 'Edit added with the API' | summary: 'Edit added with the API' /* Description of the edit that appears in the page history */ | ||
bot : true | //bot : true /* Tags the edit as a bot edit, which should hide it from the default Recent Changes page (does not work) */ | ||
}; | }; | ||
} | } | ||
Line 126: | Line 125: | ||
} | } | ||
function wecGetWikiStatistics( | function wecGetWikiStatistics() { | ||
/ | /* Creates paramters for the query api call.*/ | ||
var statsParams = { | var statsParams = { | ||
action: 'query', | action: 'query', | ||
Line 148: | Line 142: | ||
articlenumber = wikidata.query.statistics.articles, | articlenumber = wikidata.query.statistics.articles, | ||
editnumber = wikidata.query.statistics.edits, | editnumber = wikidata.query.statistics.edits, | ||
imagenumber = wikidata.query.statistics.images; | imagenumber = wikidata.query.statistics.images, | ||
statcontent = usernumber + ' ' + pagenumber + ' ' + articlenumber + ' ' + editnumber + ' ' + imagenumber; | statdate = new Date(); | ||
statdate = statdate.toUTCString(), | |||
statcontent = 'Users: ' + usernumber + ' Pages: ' + pagenumber + ' Articles: ' + articlenumber + ' Edits: ' + editnumber + ' Images: ' + imagenumber + ' Date: ' + statdate + '<br />'; | |||
wecUpdateTemplatePage(statcontent, 'Dogcraft_Wiki:Statistics/History', true); | wecUpdateTemplatePage(statcontent, 'Dogcraft_Wiki:Statistics/History', true); | ||
Line 158: | Line 154: | ||
/*if (mw.config.get("wgPageName") === "Dogcraft_Wiki:Statistics") | |||
{ | |||
wecGetUserEditCounts(); | |||
}*/ | |||
/* Creates the updat button. (Button is only visable to people with this gadget enabled, aka admins.) | /* Creates the updat button. (Button is only visable to people with this gadget enabled, aka admins.) | ||
* Line 1: Creates the button (the wiki by defualt does not allow the <button> html tag, so it has to be created from here) | * Line 1: Creates the button (the wiki by defualt does not allow the <button> html tag, so it has to be created from here) | ||
* Line 2: Adds the function "wecGetUserEditCounts()" to be executed when the button is clicked | * Line 2: Adds the function "wecGetUserEditCounts()" to be executed when the button is clicked | ||
*/ | */ | ||
document.getElementById( | var wecUpdateButton = document.getElementById("wecUpdateButton"); | ||
if (wecUpdateButton !== null) { | |||
wecUpdateButton.innerHTML = "<div id='wecUpdateButtonBox'><button type='button' class='wecUpdateButton' style='background:blue;'>Press this to update the data on the Template:EditCountMain page</button></div>"; | |||
wecUpdateButton.onclick = function() {wecGetUserEditCounts()}; | |||
} | |||
}); | }); |
Latest revision as of 00:24, 4 December 2023
/*
This gadget uses bits of code from https://www.mediawiki.org/wiki/API:Allusers
and https://www.mediawiki.org/wiki/API:Edit.
*/
$(document).ready(function() {
/* Creates the mediawiki api object */
var api = new mw.Api();
/*
* Sends an API request to get the usernames and editcounts of the first 200 accounts, and then
* and then combines these into forms usable by wiki templates. Then it takes all the different formats, and
* creates one template (using #switch) out of them.
*
* wec = wiki edit count
*/
function wecGetUserEditCounts() {
/* Creates parameters for the api call */
var queryParams = {
action: 'query',
format: 'json',
list: 'allusers', /* https://www.mediawiki.org/wiki/API:Allusers */
aulimit: 5000,
auprop: 'editcount',
auwitheditsonly: true
},
/* Variablse to fill with the data from the api call */
wecTableData = ""/*,
wecSingleData = "",
wecRawData = ""*/;
/* This is the actual api call */
api.get( queryParams ).done( function ( wecdata ) /* This is the function that uses the data from the api call */
{
var users = wecdata.query.allusers,
u;
/* Fills the wecTableData variable with the usernames and editcounts, formated like the body of a wikitable */
for ( u in users ) {
wecTableData += '{{!}}-\n{{!}}' + users[u].name + '{{!}}{{!}}' + users[u].editcount + '\n{{!}}-\n';
}
/* Fills the wecSingleData variable with the usernames and editcounts, formated like the body of a #switch parser function */
/*for ( u in users ) {
wecSingleData += '|' + users[u].name + ' = ' + users[u].editcount + '\n';
}*/
/* Fills the wecTableData variable with the usernames and editcounts, formated like plain wikitext, with the option to add
* text (this can also be HTML tags like <li>) at the front, middle and end using template variables.
*/
/*for ( u in users ) {
wecRawData += '{{{rawRowFront}}}' + users[u].name + '{{{rawRowMid}}}' + users[u].editcount + '{{{rawRowBack}}}';
}*/
/* Creates the variables that when combined, create the template page, and fills them up.
* var: templateNoInclude - Contains the template documentation, and a notice letting editors know that the template page should not be edited.
* var: templateSection1 - Contains the start of the #switch parser function, and the start of the "table" option with the head section of the table.
* var: templateSection1data - Contains the "wecTableData" variable
* var: templateSection2 - Contains the end of the table, and the start of the "single" option, with the start of its own #switch parser function.
* var: templateSection2data - Contains the "wecSingleData" variable
* var: templateSection3 - Contains the end of the "single" option and its #switch parser, and the start of the "raw" option.
* var: templateSection3data - Contains the "wecRawData" variable
* var: templateSectionEnd - Contains the fallback value for the inital #switch parser function, and then closes it.
* var: pagecontent - Combines the above variables to create the text of a template page
*/
var templateNoInclude = '<noinclude>{{Notice|title=Automated edit warning|message=This template is edited by an automated process via the EditCount gadget, manual edits may get overwritten. To modify the template code please head to [[MediaWiki:Gadget-sandbox.js]]. To modify the documentation appearing here, please head to [[Template:EditCountMain/doc]].}}{{' + 'documentation}}</noinclude>\n',
templateSection1 = '<onlyinclude>' + /*{{#switch: {{{mode|}}}\n|table = */ '<div class={{{tableBoxClass}}} style={{{tableBoxStyle}}}>\n{{{!}} class=\"wikitable sortable\"\n!Username\n!Edit count\n',
templateSection1data = wecTableData,
templateSection2 = '{{!}}}</div>}}</onlyinclude>'/*\n|single = \n{{#switch: {{{username}}} \n',
templateSection2data = wecSingleData,
templateSection3 = '|There is no editcount found for this user\n}}\n|raw =\n',
templateSection3data = wecRawData,
templateSectionEnd = '|That mode does not exist\n}}\n'*/,
pagecontent = templateNoInclude + templateSection1 + templateSection1data + templateSection2 /*+ templateSection2data + templateSection3 + templateSection3data + templateSectionEnd*/;
/* Calls the "wecUpdateTemplatePage" function with the "pagecontet" variable that was assembled above.
* This is what adds the content to a Template page.
*/
wecUpdateTemplatePage(pagecontent, 'Template:EditCountMain', false);
wecGetWikiStatistics();
});
}
/*
* Takes a variable as an input, and replaces the content of a specified page with it.
*/
function wecUpdateTemplatePage(content, title, appendtext) {
/* Creates paramters for the edit api call.
* The edit api call edits the template page, replacing the previous page content with the "content" variable.
* The edit will appear in page history as performed by the account that calls the function.
*/
var editParams = {};
if (appendtext === true)
{
editParams = {
action: 'edit',
format: 'json',
title: title, /* Title of the template page that gets replaced with new content */
appendtext: content,
summary: 'Edit added with the API' /* Description of the edit that appears in the page history */
//bot : true /* Tags the edit as a bot edit, which should hide it from the default Recent Changes page (does not work) */
};
}
else
{
editParams = {
action: 'edit',
format: 'json',
title: title, /* Title of the template page that gets replaced with new content */
text: content,
summary: 'Edit added with the API' /* Description of the edit that appears in the page history */
//bot : true /* Tags the edit as a bot edit, which should hide it from the default Recent Changes page (does not work) */
};
}
/* This is the api call that edits the page specified with "title" */
api.postWithToken( 'csrf', editParams );
}
function wecGetWikiStatistics() {
/* Creates paramters for the query api call.*/
var statsParams = {
action: 'query',
meta: 'siteinfo',
siprop: 'statistics',
format: 'json',
};
/* This is the api call that gets the statistics about the wiki */
api.get( statsParams ).done( function ( wikidata )
{
var usernumber = wikidata.query.statistics.users,
pagenumber = wikidata.query.statistics.pages,
articlenumber = wikidata.query.statistics.articles,
editnumber = wikidata.query.statistics.edits,
imagenumber = wikidata.query.statistics.images,
statdate = new Date();
statdate = statdate.toUTCString(),
statcontent = 'Users: ' + usernumber + ' Pages: ' + pagenumber + ' Articles: ' + articlenumber + ' Edits: ' + editnumber + ' Images: ' + imagenumber + ' Date: ' + statdate + '<br />';
wecUpdateTemplatePage(statcontent, 'Dogcraft_Wiki:Statistics/History', true);
});
}
/*if (mw.config.get("wgPageName") === "Dogcraft_Wiki:Statistics")
{
wecGetUserEditCounts();
}*/
/* Creates the updat button. (Button is only visable to people with this gadget enabled, aka admins.)
* Line 1: Creates the button (the wiki by defualt does not allow the <button> html tag, so it has to be created from here)
* Line 2: Adds the function "wecGetUserEditCounts()" to be executed when the button is clicked
*/
var wecUpdateButton = document.getElementById("wecUpdateButton");
if (wecUpdateButton !== null) {
wecUpdateButton.innerHTML = "<div id='wecUpdateButtonBox'><button type='button' class='wecUpdateButton' style='background:blue;'>Press this to update the data on the Template:EditCountMain page</button></div>";
wecUpdateButton.onclick = function() {wecGetUserEditCounts()};
}
});