User:Alexis Jazz/EverybodyLikesPie.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Alexis Jazz/EverybodyLikesPie. |
/* 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(/\<\;/g, '<').replace(/\>\;/g, '>').replace(/\&\;/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();
});