User:Splarka/genaverage.js
/* Article average generator (median and mean), version [0.0.1]
Originally from: http://en.wikipedia.org/wiki/User:Splarka/genaverage.js
If you go to: http://en.wikipedia.org/wiki/MediaWiki:Generatestats (generic placeholder for page to generate input)
A textarea is generated where you can enter page names and submit for median/mean stats. Each usage appends a <pre> to the page.
Test version using API.
*/
(function() {
mw.log.warn('User:Splarka/genaverage.js script disabled due to [[phab:T72470]]. Please fix!');
if(wgPageName == 'MediaWiki:Generatestats' && wgAction == 'view') addOnloadHook(genstatsInit)
function genstatsInit() {
var gen = (document.getElementById('bodyContent')) ? document.getElementById('bodyContent') : document.getElementById('content')
if(!gen) return;
var info = document.createElement('pre');
info.style.padding = '.5em';
info.style.backgroundColor = '#ddffdd';
info.style.border = '1px solid black';
info.appendChild(document.createTextNode('Enter a list of pages you wish to take the arithmatic mean and median of.\nPlease have only one page title per newline.\nNOTES:\n * The following characters are stripped and ignored: # < > [ ] | { }\n * Pages unedited in several years have no size information available, so are skipped.'));
gen.appendChild(info);
var ta = document.createElement('textarea');
ta.setAttribute('id','genstatbox');
ta.style.height = '30em';
ta.style.width = '100%';
gen.appendChild(ta);
var bt = document.createElement('input');
bt.setAttribute('type','button');
bt.setAttribute('value','Generate');
bt.setAttribute('id','genstatsbutton');
addClickHandler(bt,genstatsStart);
gen.appendChild(bt);
}
function genstatsStart() {
var bt = document.getElementById('genstatsbutton');
bt.setAttribute('disabled','disabled');
bt.setAttribute('value','fetching...');
var ta = document.getElementById('genstatbox');
ta.value = ta.value.replace(/(\#|\<|\>|\[|\]|\||\{|\})/g,'');
ta.value = ta.value.replace(/[ ]{2,}/g,' ');
tav = ta.value.split('\n');
var titles = '';
for(var i=0;i<tav.length;i++) {
var ttl = tav[i].replace(/[ ]/g,'_');
if (ttl != '_' && ttl != '') titles += encodeURIComponent(ttl) + '|'
}
titles = titles.replace(/\|$/g,'')
var url = wgScriptPath + '/api.php?action=query&prop=revisions&rvprop=size&format=json&callback=genstats&titles=' + titles;
if(url.length > 4000) {
alert('Too many pages entered');
} else {
var scriptElem = document.createElement('script');
scriptElem.setAttribute('src',url);
scriptElem.setAttribute('type','text/javascript');
document.getElementsByTagName('head')[0].appendChild(scriptElem);
}
}
function genstats(obj) {
var bt = document.getElementById('genstatsbutton');
bt.removeAttribute('disabled');
bt.setAttribute('value','Generate');
if(!obj['query'] || !obj['query']['pages']) return
var pages = obj['query']['pages'];
var num = 0; var tsize = 0;
var med = []; var txt = '';
var goodtitles = '';
var badtitles = '';
for(var i in pages) {
if(pages[i].missing == undefined) {
var sz = pages[i].revisions[0].size;
if(sz != undefined) {
num++;
tsize += parseInt(sz);
med[med.length] = sz;
goodtitles += ' * [[' + pages[i].title + ']] (' + sz + ' bytes)\n';
} else {
badtitles += ' * [[' + pages[i].title + ']] (too old)\n';
}
} else {
badtitles += ' * [[' + pages[i].title + ']] (no such page)\n';
}
}
if(num > 0) {
var mean = Math.round(tsize / num * 100) / 100;
txt += 'Number of counted pages: ' + num + '\n';
txt += ' mean = ' + mean + ' bytes\n';
med = med.sort(sortbynum);
if(num % 2 == 1) {
var median = med[num / 2 - .5];
} else {
var median = (med[num / 2 - 1] + med[num / 2]) / 2;
}
txt += ' median = ' + median + ' bytes\n\n';
txt += 'Counted pages: \n' + goodtitles + '\n';
txt += 'Uncounted pages: \n' + badtitles + '\n';
} else {
txt += 'No valid page titles found\n';
}
var pre = document.createElement('pre');
pre.appendChild(document.createTextNode(txt));
bt.parentNode.appendChild(pre);
}
function sortbynum(a, b) {
x = parseInt(a);
y = parseInt(b);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
});
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.