Jump to content

User:Splarka/genaverage.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.
/* 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));
}

});