MediaWiki

Gadget-statistics.js

From Dogcraft Wiki

No edit summary
(Added comments)
Line 1: Line 1:
/*
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() {
$(document).ready(function() {


/*function uecGetEditCount() {
/**
* Sends an API request to get the usernames and editcounts of the first 200 accounts, and then
var params2 = {
* and then combines these into forms usable by wiki templates. Then it takes all the different formats, and
        action: 'query',
* creates one template (using #switch) out of them.
        format: 'json',
*
        list: 'allusers',
* wec = wiki edit count
        aulimit: 10,
*/
        auprop: 'editcount'
function wecGetUserEditCounts() {
},
uecData = "";
//var api2 = new mw.Api();
    uecData += "ab";
   
    api.get( params2 ).done( function uecCallAPI ( data )
{
var users2 = data.query.allusers,
        u2;
       
    //if (uecMode == "table")
    //{
    for ( u2 in users2 ) {
        uecData += '{{!}}-\n{{!}}' + users2[u2].name + '{{!!}}' + users2[u2].editcount + '\n{{!}}-\n';
    }
    //}
    //else if (uecMode == "single")
    //{
    // for ( u in users ) {
    //    uecData += '|users[u].name = users[u].editcount \n';
    // }
    //}
    //else if (uecMode == "raw")
    //{
    // for ( u in users ) {
    //    uecData += 'users[u].name  users[u].editcount\n';
    // }
    //}
   
});
return uecData;
}*/
 
function testAPIEdit() {
/* Creates the mediawiki api object */
api = new mw.Api();
api = new mw.Api();
var params2 = {
/* Creates parameters for the api call */
var queryParams = {
         action: 'query',
         action: 'query',
         format: 'json',
         format: 'json',
         list: 'allusers',
         list: 'allusers', /* https://www.mediawiki.org/wiki/API:Allusers */
         aulimit: 200,
         aulimit: 200,     /* If the number of accounts reaches 200, bump this up. Max: 500 */
         auprop: 'editcount'
         auprop: 'editcount'
},
},
uecTableData = "",
/* Variablse to fill with the data from the api call */
uecSingleData = "",
wecTableData = "",  
uecRawData = "";
wecSingleData = "",
wecRawData = "";
api.get( params2 ).done( function ( uecdata )  
/* 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 = uecdata.query.allusers,
var users = wecdata.query.allusers,
         u;
         u;
       
       
        /* Fills the wecTableData variable with the usernames and editcounts, formated like the body of a wikitable */
         for ( u in users ) {
         for ( u in users ) {
         uecTableData += '{{!}}-\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 */
     for ( u in users ) {
     for ( u in users ) {
         uecSingleData += '|' + 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
    * 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 ) {
         uecRawData += '{{{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.
    *  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]].}}{{' + 'EditCountMain/doc}}</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]].}}{{' + 'EditCountMain/doc}}</noinclude>\n',
templateSection1 = '<includeonly>{{#switch: {{{mode|}}}\n|table = <div class=\"phonefullscreen uecTable2\">\n{{{!}} class=\"wikitable sortable\"\n!Username\n!Edit count\n',
templateSection1 = '<includeonly>{{#switch: {{{mode|}}}\n|table = <div class={{{tableBoxClass}}} style={{{tableBoxStyle}}}>\n{{{!}} class=\"wikitable sortable\"\n!Username\n!Edit count\n',
templateSection1data = uecTableData,
templateSection1data = wecTableData,
templateSection2 = '{{!}}}</div>\n|single = \n{{#switch: {{{username}}} \n',
templateSection2 = '{{!}}}</div>\n|single = \n{{#switch: {{{username}}} \n',
templateSection2data = uecSingleData,
templateSection2data = wecSingleData,
templateSection3 = '|There is no data on this user\n}}\n|raw =\n',
templateSection3 = '|There is no editcount found for this user\n}}\n|raw =\n',
templateSection3data = uecRawData,
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;
var params = {
/* Creates paramters for the second api call.
* The second api call edits the template page, replacing the page with the "pagecontent" variable.
* The edit will appear in page history as performed by the account that calls the function.
*/
var editParams = {
action: 'edit',
action: 'edit',
format: 'json',
format: 'json',
title: 'Template:EditCountMain',
title: 'Template:EditCountMain', /* Title of the template page that gets replaced with new content */
text: pagecontent,
text: pagecontent, /* The pagecontent variable that was assembled above */
summary: 'Edit added with the API',
summary: 'Edit added with the API', /* Description of the edit that appears in the page history */
bot : 1,
bot : true, /* Tags the edit as a bot edit, which should hide it from the default Recent Changes page */
};
};
api.postWithToken( 'csrf', params );
/* This is the api call that edits the page */
api.postWithToken( 'csrf', editParams );
        
        
});
});
}  
}  
 
/* Creates the updat button. (Button is only visable to people with this gadget enabled, aka admins.)
document.getElementById('gadgetSandboxButton').innerHTML = "<div id='gadgetSandboxButtoninner'><button type='button' style='background:blue;'>Press this to get editcounts</button></div>";
* Line 1: Creates the button (the wiki by defualt does not allow the <button> html tag, so it has to be created from here)
document.getElementById('gadgetSandboxButton').onclick = function() {testAPIEdit()};
* Line 2: Adds the function "wecGetUserEditCounts()" to be executed when the button is clicked
*/
document.getElementById('gadgetSandboxButton').innerHTML = "<div id='wecUpdateButton'><button type='button' class='wecUpdateButton' style='background:blue;'>Press this to update the data on the Template:EditCountMain page</button></div>";
document.getElementById('gadgetSandboxButton').onclick = function() {wecGetUserEditCounts()};
});
});

Revision as of 01:50, 13 August 2020

/*
	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() {

/**
 * 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 the mediawiki api object */
	api = new mw.Api();
	
	/* Creates parameters for the api call */
	var queryParams = {
        action: 'query',
        format: 'json',
        list: 'allusers', /* https://www.mediawiki.org/wiki/API:Allusers */
        aulimit: 200,     /* If the number of accounts reaches 200, bump this up. Max: 500 */
        auprop: 'editcount'
	},
	/* 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]].}}{{' + 'EditCountMain/doc}}</noinclude>\n',
			templateSection1 = '<includeonly>{{#switch: {{{mode|}}}\n|table = <div class={{{tableBoxClass}}} style={{{tableBoxStyle}}}>\n{{{!}} class=\"wikitable sortable\"\n!Username\n!Edit count\n',
			templateSection1data = wecTableData,
			templateSection2 = '{{!}}}</div>\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;
		
		
		/* Creates paramters for the second api call.
		 * The second api call edits the template page, replacing the page with the "pagecontent" variable.
		 * The edit will appear in page history as performed by the account that calls the function.
		 */
		var editParams = {
			action: 'edit',
			format: 'json',
			title: 'Template:EditCountMain', /* Title of the template page that gets replaced with new content */
			text: pagecontent, /* The pagecontent variable that was assembled above */
			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 */
			
		};
		
		/* This is the api call that edits the page */
		api.postWithToken( 'csrf', editParams );
        	
	});
	
} 
	/* 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
	 */
	document.getElementById('gadgetSandboxButton').innerHTML = "<div id='wecUpdateButton'><button type='button' class='wecUpdateButton' style='background:blue;'>Press this to update the data on the Template:EditCountMain page</button></div>";
	document.getElementById('gadgetSandboxButton').onclick = function() {wecGetUserEditCounts()};
});
This page was last modified on 13 August 2020, at 01:50. (5 months ago)
Background Takeshi by Edo