User:Alexis Jazz/EverybodyLikesPie.js

/* 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: /*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>[^]*/) ) {		return text.replace(/[^]*]*>([^]*)<\/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 = ; 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['*']+' '+fGS.pieChart+' '),{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; });
 * get 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