Jump to content

User:Alexis Jazz/EverybodyLikesPie.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.
/* EverybodyLikesPie(charts..)
Creates a pie chart of the size of current default gadgets to help find low-hanging fruit
This script is public domain, irrevocably released as WTFPL Version 2[www.wtfpl.net/about/] by its author, Alexis Jazz.
As of 2023-09-12, this script isn't thoroughly tested or polished and it's mostly a proof of concept. Maybe you find its output helpful, maybe you won't.
Todo:
* get {{pie chart}} template name from Wikidata Q6121704
* make second chart of all gadgets (not just the default ones)
* provide the absolute sizes
* provide gzipped sizes
* make loop to cover multiple projects at once
* some sort of interface
* sort by size
* boo synchronous http requests boo
* many other things I guess
*/
/*globals mw:false*/
var fGS = {};
fGS.pageTitle = mw.config.get('wgFormattedNamespaces')[2]+':'+mw.config.get('wgUserName')+'/Sandbox';
mw.loader.using(['mediawiki.api']).then(function(){
var api = new mw.Api();
fGS.getWikitextFromExport = function(text) {
	if ( text.match(/[^]*<text bytes[^>]*>([^]*)<\/text>[^]*/) ) {
		return text.replace(/[^]*<text bytes[^>]*>([^]*)<\/text>[^]*/, '$1').replace(/\&lt\;/g, '<').replace(/\&gt\;/g, '>').replace(/\&amp\;/g, '&');
	} else {
		return '';
	}
};
fGS.gadgetSizeObj = {};
fGS.checkedGadgets = 0;
fGS.gadgetTotalSize = 0;
fGS.saveGadgetSize = function(gadgetname,int) {
	var xmlHttp = new XMLHttpRequest();
	xmlHttp.open( "GET", '/w/load.php?lang=en&modules=ext.gadget.'+gadgetname+'&skin=vector-2022', false );
	xmlHttp.send( null );
	fGS.gadgetSizeObj[gadgetname] = xmlHttp.response.length;
	fGS.gadgetTotalSize = fGS.gadgetTotalSize + xmlHttp.response.length;
	fGS.checkedGadgets++;
	if ( fGS.checkedGadgets == fGS.defaultGadgetArr.length ) {
		fGS.gadgetPercent = fGS.gadgetTotalSize / 100;
		fGS.pieChart = '{{Pie chart|';
		fGS.chartVal = 1;
		for ( int=0;int<Object.keys(fGS.gadgetSizeObj).length;int++) {
			fGS.gadgetSizeVal = fGS.gadgetSizeObj[Object.keys(fGS.gadgetSizeObj)[int]] / fGS.gadgetPercent;
			fGS.pieChart = fGS.pieChart + '\n|value'+[int + 1]+'='+fGS.gadgetSizeVal.toFixed(2)+'\n|label'+[int + 1]+'='+Object.keys(fGS.gadgetSizeObj)[int];
		}
		fGS.pieChart = fGS.pieChart + '\n}}';
		console.log(fGS.pieChart);
		api.post({action:'parse',text:fGS.pieChart,format:'json'} ).then( function ( data ) {
			OO.ui.alert(new OO.ui.HtmlSnippet(data.parse.text['*']+'<div style="width:100%"><textarea rows="10">'+fGS.pieChart+'</textarea></div>'),{size:'full'});
		}, function ( code, data ) {
				//fail
		});
		/*
		api.postWithEditToken( {action:'edit', title:fGS.pageTitle, summary:'Gadget stats', watchlist:'nochange', text:fGS.pieChart,format:'json'} ).then( function ( data ) {
		}, function ( code, data ) {
				//fail
		});
		*/
	}
};
fGS.defaultGadgetArr = [];
fGS.getDef = function(int) {
	api.get( {action: 'query', export: 'true', format: 'json', titles: 'MediaWiki:Gadgets-definition'} ).then( function ( data ) {
		fGS.wikiText = fGS.getWikitextFromExport(data.query.export["*"]);
		fGS.default = fGS.wikiText.match(/\n.*[\[\|]default[\]\|].*/g);
		for (int=0;int<fGS.default.length;int++) {
			if ( ! fGS.default[int].match(/(rights|actions)=/) ) { //exclude gadgets that require rights or only load for specific actions
				fGS.defaultGadgetArr.push(fGS.default[int].replace(/\n\* ([^\[]*).*/,'$1'));
			}
		}
		for (int=0;int<fGS.defaultGadgetArr.length;int++) {
			fGS.saveGadgetSize(fGS.defaultGadgetArr[int]);
		}
	}
);};
fGS.getDef();
});