User:Elliottmobile/monobook.js: Difference between revisions

From Hydrogenaudio Knowledgebase
No edit summary
No edit summary
Line 1: Line 1:
// Modified version of [http://en.wikipedia.org/wiki/User:Ais523/editcount.js] by Elliott Hird/Elliottmobila
// Modded version of http://en.wikipedia.org/wiki/User:Interiot/Tool2/code.js by Elliott.


//Please leave this link: [[User:ais523/editcount.js]]
// see http://paperlined.org/apps/wikipedia/Tool2/ for instructions on adding this to your monobook.js
//<pre><nowiki>


//JavaScript edit counter. By [[User:ais523]].
// To run this tool on other servers:
//To install this, you can copy it into your monobook.js or use a script-inclusion
// 1. copy this script to the target server (this is required because of javascript cross-site security restrictions)
//method (see WikiProject User Scripts). It produces a 'count' tab on contribs pages
//that can be used to count a user's edits. It won't count more than 5000 edits in any
//namespace, to prevent excessive server load.


//Add LI Link and Add Tab, renamed to prevent conflicts. To make installation easier
// 2. update the following URL
//for people who haven't used User Scripts before.
// for example: "User:Interiot/Tool2/code.js"
var tool2_url = "User:Interiot/Tool2/code.js";


function ecAddLILink(tabs, url, name, id, title, key){
// 3. update this namespace list, extracted from something like http://en.wikiquote.org/wiki/Special:Export//
    var na = document.createElement('a');
// These *should not* have colons after them.
    na.href = url;
var namespaces = [
    na.appendChild(document.createTextNode(name));
"Talk",
    var li = document.createElement('li');
"User",
    if(id) li.id = id;
"User talk",
    li.appendChild(na);
"Wikiquote",
    tabs.appendChild(li);
"Wikiquote talk",
    if(id)
"Image",
     {
"Image talk",
        if(key && title)
"MediaWiki",
        {
"MediaWiki talk",
             ta[id] = [key, title];
"Template",
        }
"Template talk",
        else if(key)
"Help",
        {
"Help talk",
            ta[id] = [key, ''];
"Category",
        }
"Category talk",
        else if(title)
// 3b. these two project project entries are not added by Special:Export, and might or might not need to be updated
        {
"Hydrogenaudio Knowledgebase",
            ta[id] = ['', title];
"Hydrogenaudio Knowledgebase talk"
        }
];
 
namespaces[100] = "Portal";
namespaces[101] = "Portal talk";
 
// 4. update this date-parser to match the format and language of your specific wiki.  Feel free to contact Interiot regarding this, if you can't find another
// copy of this script that uses the same language.
// input: a text string from Special:Contributions.    output: a javascript Date object
// documentation:  http://www.quirksmode.org/js/introdate.html#parse, http://www.elated.com/tutorials/programming/javascript/dates/
function date_parse(text) {
var matches = text.match(/^([0-9:]+), +([0-9]+) +([a-z]+) +([0-9]+)$/i);
if (!matches) {
//dump_text("XXX"); // for debugging
return matches;
}
 
parseme = matches[3] + ", " + matches[2] + " "  + matches[4] + " " + matches[1] + ":00";
 
//dump_text(parseme); // for debugging
 
var dt = new Date();
dt.setTime( Date.parse(parseme));
 
//dump_text(dt.toLocaleString()); // for debugging
 
return dt;
}
 
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ end of server-specific configuration ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 
 
// TODO:
// - the current document.location method doesn't work when the page is accessed sans-mod_rewrite
// - test with non-ASCII characters
// - non-ascii usernames
// - ??
 
 
 
var prefix = "";
var params = parse_params();
 
addOnloadFunction(function() {
  var path_len = document.location.pathname.length;
  // trigger once we view the right page
  if (document.location.pathname.substring(path_len - tool2_url.length, path_len) == tool2_url) {
     // get the prefix (needs to be fixed to work sans-mod_rewrite
    prefix = document.location.protocol + "//" + document.location.host + "/"
             + document.location.pathname.substring(1, path_len - tool2_url.length);
 
    // blank the inner contents of the page
    var bodyContent = document.getElementById("bodyContent");
    while (bodyContent.childNodes.length > 0) bodyContent.removeChild(bodyContent.lastChild);
 
    if (document.location.search.length == 0) {
      generate_input_form(bodyContent);
    } else {
      generate_main_report(bodyContent);
     }
     }
    // re-render the title and accesskeys from existing code in wikibits.js
  }
    akeytt();
});
    return li;
 
 
function generate_input_form(bodyContent) {
  if (navigator.userAgent.toLowerCase().indexOf('msie')+1)
  {
  bodyContent.innerHTML = "This counter does not currently work in Internet Explorer.  Please <a href='http://www.getfirefox.com'>get Firefox</a>.";
  }
  else
  {
  bodyContent.innerHTML =
            "<form><table><tr><td>Username <td><input maxlength=128 name=username value='' id=username title='username'>" +
            "            <tr><td>        <td><input type=submit value='Submit'>" +
            "</table></form>";
 
  var form = bodyContent.getElementsByTagName("form")[0];
  form.method = "get";
  form.action = document.location;
 
  document.getElementById("username").focus();
  }
}
 
function generate_main_report() {
  fetch_data(params["username"].replace(/\+/g, " "),
"", output_main_report, 0, []);
}
}


function ecAddTab(url, name, id, title, key){
 
    var tabs = document.getElementById('p-cactions').getElementsByTagName('ul')[0];
function add_stats_row(left_col, right_col) {
    return ecAddLILink(tabs, url, name, id, title, key)
var row = document.createElement("tr");
var left = document.createElement("td");
var right = document.createElement("td");
document.getElementById("basic_stats").appendChild(row);
row.appendChild(left);
row.appendChild(right);
//left.innerHTML = left_col;
left.appendChild( document.createTextNode(left_col) );
right.appendChild( document.createTextNode(right_col) );
return row;
}
 
function output_main_report(history) {
// -- generate summary statistics
var unique_articles = new Array();
var namespace_numedits = new Array();
for (var i=0; i<namespaces.length; i++) {
namespace_numedits[ namespaces[i] ] = 0;
}
namespace_numedits[""] = 0;
for (var i=0; i<history.length; i++) {
var h = history[i];
unique_articles[  h["title"] ]++;
namespace_numedits[ h["namespace"] ]++;
}
var unique_articles_keys = keys(unique_articles);
 
// -- output report
var table = document.createElement("table");
table.id = "basic_stats";
document.getElementById("bodyContent").appendChild(table);
 
add_stats_row("Username", params["username"].replace(/\+/g, " "));
add_stats_row("Total edits", history.length);
add_stats_row("Distinct pages edited", unique_articles_keys.length);
add_stats_row("Average edits/page", new Number(history.length / unique_articles_keys.length).toFixed(3));
add_stats_row("First edit", history[ history.length-1 ]["date_text"] );
 
// add a blank row
add_stats_row("", "").childNodes[0].style.height = "1em";
 
add_stats_row("(main)", namespace_numedits[""]);
for (var i=0; i<namespaces.length; i++) {
var nmspc = namespaces[i];
if (namespace_numedits[nmspc]) {
add_stats_row(nmspc, namespace_numedits[nmspc]);
}
}
}
}


addOnloadHook(function() {
  if(location.href.indexOf("Special")!=-1&&location.href.indexOf("Contributions")!=-1)
    ecAddTab("javascript:ais523contrib()","count","ca-ais523count","Count","");
});


//This function was orignally taken from [[User:Lupin/autoedit.js]]. I've renamed it
//because I know many users use popups, and I don't want to cause a naming conflict.
//Edited to decode + to space as well, and to use a decoding function that handles
//a broader range of characters.
function ecGetParamValue(paramName) {
  var cmdRe=RegExp('[&?]'+paramName+'=([^&]*)');
  var h=document.location;
  var m;
  if (m=cmdRe.exec(h)) {
    try {
      while(m[1].indexOf('+')!=-1)
      {
        m[1]=m[1].substr(0,m[1].indexOf('+'))+" "+m[1].substr(m[1].indexOf('+')+1);
      }
      return decodeURIComponent(m[1]);
    } catch (someError) {}
  }
  return null;
};


// ===================================== HTML-scraping backend =========================================


function ais523contrib()
function add_loading_notice() {
{
if (document.getElementById("loading_notice"))
  var u;
return;
  if(location.href.indexOf("?")!=-1) u=ecGetParamValue("target");
var loading = document.createElement("div");
  else u=location.href.substr(location.href.lastIndexOf("/")+1);
loading.id = "loading_notice";
  location.href="http://wiki.hydrogenaudio.org/index.php?title=Special:Contributions&limit=5000&target="+u+"&ais523count=1&namespace=0";
loading.innerHTML = "<br><br>Retrieving data<blink>...</blink>";
document.getElementById("bodyContent").appendChild(loading);
}
function remove_loading_notice() {
var loading = document.getElementById("loading_notice");
if (!loading) return;
loading.parentNode.removeChild(loading);
}
}


//Analyses an edit summary and returns a two-letter code indicating what the edit seems
var offset_regexp = /href="[^"]+:Contributions[^"]+offset=(\d+)/gi;
//to be doing. The edit summary is passed with parens round it, written in HTML. This
function fetch_data(username, end_date, handler, offset, page_list) {
//doesn't yet work for section edits, which will have to be parsed out in the main
add_loading_notice();
//function.
var url = prefix + "Special:Contributions/" + username + "?offset=" + offset + "&limit=5000";
function ecAnalyseSummary(edsum)
loadXMLDoc(url,  
{
function (request) {
  edsum=edsum.toLowerCase();
var next_offset = 0;
  if(edsum.indexOf("?")!=-1) return 'se'; //section edit, can't say any more than that
if (request.readyState != 4)   return;
  if(edsum==")") return 'se'; //section edit, no summary
if (request.status == 200) {
  if(edsum.indexOf(" ")==0) edsum="("+edsum.substr(1); //came from section
page_list.push(request.responseText);
//dump_text(request.responseText);
 
// see if there's another pageful to get
var matches = map( function(p){
return p.match( /(\d+)$/ )[0];
}, request.responseText.match( offset_regexp ) );
for (var i=0; i<matches.length; i++) {
var v = matches[i] * 1;
if (v != 0 && (offset == 0 || v < offset)) {
next_offset = v;
break;
}
}
}


  if(edsum.indexOf("(rvv")==0) return 'rv'; //vandalism revert
//next_offset = 0; // for testing only, retrieve just the first page of results
  if(edsum.indexOf("(rv vand")==0) return 'rv'; //vandalism revert
  if(edsum.indexOf("(revv")==0) return 'rv'; //vandalism revert
  if(edsum.indexOf("(rev vand")==0) return 'rv'; //vandalism revert
  if(edsum.indexOf("(revert vand")==0) return 'rv'; //vandalism revert


  if(edsum.indexOf("(rv ")==0&&edsum.indexOf("vandal")!=-1) return 'rv';
if (next_offset == 0) {
  if(edsum.indexOf("(rev ")==0&&edsum.indexOf("vandal")!=-1) return 'rv';
parse_data(page_list, handler);
} else {
// tail recurse
fetch_data(username, end_date, handler, next_offset, page_list);
}
});
}


  if(edsum.indexOf("(rv ")==0) return 'ro'; //other manual revert
  if(edsum.indexOf("(rev ")==0) return 'ro'; //other manual revert


  if(edsum.indexOf("(reverted ")==0) return 'ra'; //automatic revert
// input: a list of strings, each string containing the HTML from a single page
  if(edsum.indexOf("(revert to ")==0) return 'ra'; //automatic revert
// output: a list, where each individual entry is a specific edit from history
function parse_data(page_list, handler) {
//var total_len = 0;
//for (var i=0; i<page_list.length; i++) total_len += page_list[i].length;
//alert("parsing " + page_list.length + " pages comprising " + total_len + " total bytes");


  if(edsum.indexOf("(revert")==0) return 'ro'; //guess manual for time being;
var last_history_ent = [];
                                              //I need more examples of this sort of rv
last_history_ent["title"] = "";
last_history_ent["oldid"] = "";


  if(edsum.indexOf("(rm ")==0) return 'rm'; //removal
var edit_history = new Array();
  if(edsum.indexOf("(rem ")==0) return 'rm'; //removal
for (var pagecnt=0; pagecnt<page_list.length; pagecnt++) {
  if(edsum.indexOf("(remove ")==0) return 'rm'; //removal
var matches = page_list[pagecnt].match( /^<li>[^(]+\(<a href="[^"]+action=history.*/gim );
//dump_lines(matches);
for (var matchcnt=0; matchcnt<matches.length; matchcnt++) {
var history_text = matches[matchcnt];


  if(edsum.indexOf("(redir")==0) return 'rd'; //redirect, including redir auto-summary
var history_entry = new Array();
  if(edsum.indexOf("(#redir")==0) return 'rd'; //redirect, including redir auto-summary
history_entry["date_text"] = history_text.match( /^<li>([^(<]+)/i )[1]
.replace( / +$/, "");
history_entry["date"] = date_parse( history_entry["date_text"] );
history_entry["title"] = history_text.match( /title="([^"]+)"/i )[1]
.replace( /&quot;/g, "\"")
.replace( /&amp;/g, "&");
var find_comment = history_text.replace(/<span class="autocomment">.*?<\/span> ?/, "");
history_entry["comment"] = ifmatch(find_comment.match( /<span class='comment'>(.*?)<\/span>/ ))
.replace(/^\((.*)\)$/, "$1");
history_entry["minor"] = /<span class="minor"/.test(history_text);
history_entry["oldid"] = ifmatch(history_text.match(/oldid=([0-9]+)/i));


  if(edsum.indexOf('(<a href="/w')==0) return 'li'; //edit summary was a link
history_entry["namespace"] = "";
  if(edsum.indexOf("(<a href='/w")==0) return 'li'; //edit summary was a link
for (var nmspc_ctr=0; nmspc_ctr<namespaces.length; nmspc_ctr++) {
  if(edsum.indexOf('(<a href=/w')==0) return 'li'; //edit summary was a link
var nmspc = namespaces[nmspc_ctr] + ":";
if (history_entry["title"].substring(0, nmspc.length) == nmspc) {
history_entry["namespace"] = namespaces[nmspc_ctr];
break;
}
}


  if(edsum.indexOf('{{welcome')!=-1) return 'we'; //welcome
//dump_object(history_entry);
  if(edsum.indexOf('welcome}}')!=-1) return 'we'; //welcome
  if(edsum.indexOf('(welcome')!=-1) return 'we'; //welcome
  if(edsum.indexOf('welcome)')!=-1) return 'we'; //welcome


  //User warnings are sorted by level. Other warnings and edit summaries are used;
if (history_entry["title"] != last_history_ent["title"] || history_entry["oldid"] != last_history_ent["oldid"])
  //this is just a small beginning for now.
edit_history.push(history_entry);
  if(edsum.indexOf('test0')!=-1) return 'w0'; //warning 1
last_history_ent = history_entry;
  if(edsum.indexOf('test1')!=-1) return 'w1'; //warning 1
}
  if(edsum.indexOf('test2')!=-1) return 'w2'; //warning 2
}
  if(edsum.indexOf('test3')!=-1) return 'w3'; //warning 3
  if(edsum.indexOf('test4')!=-1) return 'w4'; //warning 4
  if(edsum.indexOf('test5')!=-1) return 'w5'; //warning 5
  if(edsum.indexOf('test6')!=-1) return 'w6'; //warning 6


  //Prodding
remove_loading_notice();
  if(edsum.indexOf('{'+'{prod')!=-1) return 'pr'; //prod
  if(edsum.indexOf('(prod')!=-1) return 'pr'; //prod


  //Some XfD-tagging summaries. So far I've only included the deletion-debates
handler(edit_history);
  //I'm familiar with.
}
  if(edsum.indexOf('{'+'{afd}}')!=-1) return 'xf'; //XfD tagging
  if(edsum.indexOf('{'+'{afd1')!=-1) return 'xf'; //XfD tagging
  if(edsum.indexOf('(afd)')!=-1) return 'xf'; //XfD tagging
  if(edsum.indexOf('{'+'{tfd}}')!=-1) return 'xf'; //XfD tagging
  if(edsum.indexOf('(tfd)')!=-1) return 'xf'; //XfD tagging
  if(edsum.indexOf('{'+'{md}}')!=-1) return 'xf'; //XfD tagging
  if(edsum.indexOf('{'+'{md1')!=-1) return 'xf'; //XfD tagging
  if(edsum.indexOf('(mfd)')!=-1) return 'xf'; //XfD tagging
  if(edsum.indexOf('{'+'{rfd}}')!=-1) return 'xf'; //XfD tagging
  if(edsum.indexOf('(rfd)')!=-1) return 'xf'; //XfD tagging
  if(edsum.indexOf('for deletion')!=-1) return 'xf'; //XfD tagging


  //Speedy deletions
  if(edsum.indexOf('db-')!=-1) return 'sp'; //Speedy
  if(edsum.indexOf('db|')!=-1) return 'sp'; //Speedy
  if(edsum.indexOf('speedy')!=-1) return 'sp'; //Speedy (probably)
  if(edsum.indexOf('{{delete}}')!=-1) return 'sp'; //override group de below


  //Other deletion-related (removal of text, delete votes, etc.
  if(edsum.indexOf('(del')!=-1) return 'de';
  if(edsum.indexOf('delete')!=-1) return 'de';
  if(edsum.indexOf('(d)')!=-1) return 'de';
  if(edsum.indexOf('(d ')!=-1) return 'de';
  if(edsum.indexOf('(-')==0) return 'de'; // as in -link


  //Marked as additions
  if(edsum.indexOf('(add ')!=-1) return 'ad';
  if(edsum.indexOf(' add ')!=-1) return 'ad';
  if(edsum.indexOf('(add)')!=-1) return 'ad';
  if(edsum.indexOf(' add)')!=-1) return 'ad';
  if(edsum.indexOf('(+')==0) return 'ad'; // as in +1


  //Probable XfD votes to keep; reasonably useless at the moment because section edits
// ===================================== test/debug functions =========================================
  //can't be parsed
  if(edsum.indexOf('(k)')!=-1) return 'ke';
  if(edsum.indexOf('(keep')!=-1) return 'ke';
  if(edsum.indexOf("'keep'")!=-1) return 'ke'; //for when the user just copies their
                                              //vote to the summary; also produced by
                                              //WikiVoter


  //Votes somewhere to support
function dump_text(text) {
  if(edsum.indexOf('(support')!=-1) return 'su';
   //alert("dump_text, with text of size " + text.length);
   //to oppose
  if(edsum.indexOf('(oppose')!=-1) return 'op';


   if(edsum.indexOf("{"+"{")!=-1) return 'ta'; //unknown, marked as a tag
   var pre = document.createElement("pre");


   if(edsum.length<=6) return 'ab'; //unknown abbreviation <=4 chars + parens
   var div = document.createElement("div");
  div.style.width = "60em";
  div.style.maxHeight = "40em";
  div.style.overflow = "auto";


   return 'uk'; //unknown
   pre.appendChild(document.createTextNode(text));
  div.appendChild(pre);
  document.getElementById("bodyContent").appendChild(div);
}
}


//The main function; this actually counts the edits. The section at the end displays
function dump_lines(ary) {
//the results.
   dump_text("--> " + ary.join("\n--> "));
addOnloadHook(function() {
}
   if(location.href.indexOf("ais523count")!=-1&&location.href.indexOf("ais523countres")==-1)
 
  {
function dump_object(obj) {
    //Counting edits. Relies on the fact that <LI> with no arguments only appears
var toString = "";
    //at the start of a contrib line.
for (var prop in obj) {
    var contribs=0;
toString += prop + ": " + obj[prop] + "\n";
    var nosum=0,oldnosum;
}
    var sumloc;
dump_text(toString);
    var sortcount=new Array();
}
    var bodyh=document.body.innerHTML;
 
    while(bodyh.indexOf("<li>")!=-1)
 
    {
// ===================================== utility functions =========================================
      contribs++;
 
      oldnosum=nosum;
function addOnloadFunction(f) {
      bodyh=bodyh.substr(bodyh.indexOf("<li>")+4);
  if (window.addEventListener) window.addEventListener("load",f,false);
      sumloc=9999999;
  else if (window.attachEvent) window.attachEvent("onload",f);
      if(bodyh.indexOf('<span class="comment">')!=-1)
  else {
        sumloc=bodyh.indexOf('<span class="comment">');
    var oldOnload='_old_onload_'+addOnloadFunction.uid;
      if(bodyh.indexOf("<li>")<sumloc)
    addOnloadFunction[oldOnload] = window.onload ? window.onload : function () {};
        nosum++;
     window.onload = function() { addOnloadFunction[oldOnload]();  f(); }
      if(bodyh.indexOf("<li>")==-1&&sumloc!=9999999) nosum--; //last edit on page
     ++addOnloadFunction.uid;
      if(nosum==oldnosum)
      { //Parse edit summary
        var edsum=bodyh.indexOf('>',sumloc);
        bodyh=bodyh.substr(edsum+1);
        sumloc=bodyh.indexOf("</span>");
        edsum=bodyh.substr(0,sumloc);
        edsum=ecAnalyseSummary(edsum);
        if(edsum=='se')
        {
          //jump to next </span>
          bodyh=bodyh.substr(sumloc+7);
          sumloc=bodyh.indexOf("</span>");
          edsum=bodyh.substr(0,sumloc);
          edsum=ecAnalyseSummary(edsum);         
        }
        if(sortcount[edsum]==undefined) sortcount[edsum]=0;
        sortcount[edsum]++;
      }
    }
    bodyh=document.body.innerHTML;
    //This is the way IE counts it.
    while(bodyh.indexOf("<LI>")!=-1)
    {
      contribs++;
      oldnosum=nosum;
      bodyh=bodyh.substr(bodyh.indexOf("<LI>")+4);
      sumloc=9999999;
      if(bodyh.indexOf('<SPAN CLASS="comment">')!=-1) //a plausible format
        sumloc=bodyh.indexOf('<SPAN CLASS="comment">');
      if(bodyh.indexOf('<SPAN class=comment>')!=-1) //The IE method
        sumloc=bodyh.indexOf('<SPAN class=comment>');
      if(bodyh.indexOf("<LI>")<sumloc)
        nosum++;
      if(bodyh.indexOf("<LI>")==-1&&sumloc!=9999999) nosum--; //last edit on page
      if(nosum==oldnosum)
      { //Parse edit summary
        var edsum=bodyh.indexOf('>',sumloc);
        bodyh=bodyh.substr(edsum+1);
        sumloc=bodyh.indexOf("</SPAN>");
        edsum=bodyh.substr(0,sumloc);
        edsum=ecAnalyseSummary(edsum);
        if(edsum=='se')
        {
          //jump to next </SPAN>
          bodyh=bodyh.substr(sumloc+7);
          sumloc=bodyh.indexOf("</SPAN>");
          edsum=bodyh.substr(0,sumloc);
          edsum=ecAnalyseSummary(edsum);         
        }
        if(sortcount[edsum]==undefined) sortcount[edsum]=0;
        sortcount[edsum]++;
      }
    }
    var namespace=ecGetParamValue("namespace");
     var scres="";
    var scit;
    for (scit in sortcount)
    {
      scres+="&cns"+namespace+scit+"="+sortcount[scit];
    }
    if(namespace!="101") //Portal talk
      location.href=location.href.substr(0,location.href.lastIndexOf("namespace="))+
        "countns"+namespace+"="+contribs+scres+"&countnosum"+namespace+"="+nosum+"&namespace="+(namespace=="15"?"100":1+new Number(namespace));
     else
    {
      var lh=location.href;
      location.href="http://wiki.hydrogenaudio.org/User:ais523/results?ais523countres="+lh+"&countns101="+contribs+"&countnosum101="+nosum+scres;
      //You can use a page other than [[User:ais523/results]] as long as it's in the
      //correct format.
    }
   }
   }
  else if(location.href.indexOf("ais523countres=")!=-1)
}
  { //This relies on the template page [[User:ais523/results]] being in the
 
    //correct format.
 
    document.getElementById("ais523echead").style.display="none";
function parse_params() {
    var h=document.getElementById("ais523ecbody").innerHTML;
  var pairs = document.location.search.substring(1).split("&");
    while(h.indexOf("((")!=-1)
  var ret = [];
    {
  for (var i=0; i < pairs.length; i++) {
      var i=h.indexOf("((");
    var values = pairs[i].split("=");
      var f=h.substr(0,i);
    ret[values[0]] = unescape(values[1]);
      var g=h.substr(i+2,h.indexOf("))")-i-2);
      if(g.indexOf('#d')==0)
      { //delete unwanted lines to remove clutter
        var j=h.indexOf("((/#d))");
        var v=false;
        j=h.substr(i+6,j-i-2);
        while(j.indexOf("((")!=-1)
        {
          var ii=j.indexOf("((");
          var gg=j.substr(ii+2,j.indexOf("))")-ii-2);
          j=j.substr(ii+2);
          gg=ecGetParamValue(gg);
          if(gg!=null&&gg!=0&&gg!='0') v=true;
        }
        if(v) g="";
        else {h=h.substr(h.indexOf("((/#d")); g="";}
      }
      else if(g.indexOf("/#d")==0)
        g="";
      else if(g.indexOf("total")==0)
      {
        g=new Number(ecGetParamValue('countns0'));
        g+=new Number(ecGetParamValue('countns1'));
        g+=new Number(ecGetParamValue('countns2'));
        g+=new Number(ecGetParamValue('countns3'));
        g+=new Number(ecGetParamValue('countns4'));
        g+=new Number(ecGetParamValue('countns5'));
        g+=new Number(ecGetParamValue('countns6'));
        g+=new Number(ecGetParamValue('countns7'));
        g+=new Number(ecGetParamValue('countns8'));
        g+=new Number(ecGetParamValue('countns9'));
        g+=new Number(ecGetParamValue('countns10'));
        g+=new Number(ecGetParamValue('countns11'));
        g+=new Number(ecGetParamValue('countns12'));
        g+=new Number(ecGetParamValue('countns13'));
        g+=new Number(ecGetParamValue('countns14'));
        g+=new Number(ecGetParamValue('countns15'));
        g+=new Number(ecGetParamValue('countns100'));
        g+=new Number(ecGetParamValue('countns101'));
      }
      else
        g=ecGetParamValue(g);
      if(g==null) g='0';
      f+=g+h.substr(h.indexOf("))")+2);
      h=f;
    }
    document.getElementById("ais523ecbody").innerHTML=h;
   }
   }
});
  return ret;
}


//JavaScript diff finder. By [[User:ais523]]
addOnloadHook(function() {
  if(location.href.indexOf("Special")!=-1&&location.href.indexOf("Contributions")!=-1)
    ecAddTab("javascript:ais523l1000()","last 1000","ca-ais523sort","Last 1000","");
  if(location.href.indexOf("&ais523sort=last1000")!=-1)
    window.setTimeout("ais523sort();",500); //work around IE bug
});


function ais523l1000()
function loadXMLDoc(url, handler)
{
{
  var trg;
    // branch for native XMLHttpRequest object
  trg=ecGetParamValue('target');
    if (window.XMLHttpRequest) {
  if(trg==null) trg=location.href.substr(location.href.lastIndexOf("/")+1);
        req = new XMLHttpRequest();
  location.href="http://wiki.hydrogenaudio.org/index.php?title=Special:Contributions"+
req.onreadystatechange = function () {handler(req)};
    "&limit=1000&target="+trg+"&ais523sort=last1000";
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function () {handler(req)};
            req.open("GET", url, true);
            req.send();
        }
    }
}
}


function ais523sort()
 
{
// see http://search.cpan.org/dist/perl/pod/perlfunc.pod#map
  var s=document.body.innerHTML;
function map (handler, list) {
  var re=/href="(\/w\/index\.php\?title=([^"/]*)((\/[^"]*)?)&amp;diff=prev&amp;oldid=[0-9]*)"/i;
   var ret = new Array();
  var a=new Array();
   for (var i=0; i<list.length; i++) {
  var b=new Array();
     ret[i] = handler( list[i] );
   var c=new Array();
     // ret.push( handler( list[i] ) );
   var nc=new Array();
  var d=new Array();
  while(s.search(re)!=-1)
  {
     var m=s.match(re);
    var m2="";
    var q;
    if(m[3]!='') m2=" subpages";
    m[4]=decodeURIComponent(m[2])+m2;
     m[5]=m2;
    if(c[m[4]]==undefined) c[m[4]]=0;
    if(c[m[4]]<10) q=c[m[4]];
    else if(Math.random()<10/(c[m[4]]+1)) q=Math.floor(Math.random()*10);
    else q=-1;
    c[m[4]]++;
    if(d[m[4]]==undefined) d[m[4]]=new Array();
    if(q>-1) d[m[4]][q]=m;
    s=s.substr(s.search(re)+2);
   }
   }
   var i;
   return ret;
  var j;
}
  for(i in c)
 
  {
// see http://search.cpan.org/dist/perl/pod/perlfunc.pod#keys
    if(c[i]<5)
function keys (obj) {
    {
var ret = new Array();
      for(j in d[i])
for (var key in obj) {
      {
ret.push(key);
        var ns="(main)";
}
        var q;
return ret;
        if(d[i][j][4].indexOf(":")!=-1) ns=d[i][j][4].substr(0,d[i][j][4].indexOf(":"));
        m=d[i][j];
        m[2]="Others in namespace "+ns;
        m[3]="";
        m[4]=m[2];
        m[5]="";
        if(nc[m[4]]==undefined) nc[m[4]]=0;
        if(nc[m[4]]<10) q=nc[m[4]];
        else if(Math.random()<10/(nc[m[4]]+1)) q=Math.floor(Math.random()*10);
        else q=-1;
        nc[m[4]]++;
        if(d[m[4]]==undefined) d[m[4]]=new Array();
        if(q>-1) d[m[4]][q]=m;
      }
    }
  }
  for(i in d)
  {
    if(nc[i]!=undefined||c[i]>=5)
    for(j in d[i])
    {
      var m=d[i][j];
      m[2]=decodeURIComponent(m[2]);
      if(a[m[4]]==undefined) a[m[4]]="*[[:"+m[2].split("_").join(" ")+"]]"+m[5]+":";
      if(b[m[4]]==undefined) b[m[4]]="<LI><A HREF='http://wiki.hydrogenaudio.org/"+
        m[2]+"'>"+m[2].split("_").join(" ")+"</A>"+m[5]+":";
      a[m[4]]+=" [http://wiki.hydrogenaudio.org"+m[1]+"]";
      b[m[4]]+=" <A HREF='http://wiki.hydrogenaudio.org"+m[1]+"'>["+(new Number(j)+1)+"]</A>";
    }
  }
  var e=0;
  for(i in c)
  {
    if(c[i]>=5)
    {
      a[i]+=(c[i]>10?"...":"")+" ("+c[i]+" edit(s))\n";
      b[i]+=(c[i]>10?"...":"")+" ("+c[i]+" edit(s))\n";
      if(c[i]>e) e=c[i]+1;
    }
  }
  for(i in nc)
  {
    if(nc[i]>=5)
    {
      a[i]+=(nc[i]>10?"...":"")+" ("+nc[i]+" edit(s))\n";
      b[i]+=(nc[i]>10?"...":"")+" ("+nc[i]+" edit(s))\n";
    }
  }
  var trg=ecGetParamValue('target');
  var h="<H1>Contribution breakdown for <A HREF='http://wiki.hydrogenaudio.org/index.php?title=User:"+trg;
  h+="'>User:"+trg+"</A></H1>\n";
  h+="<H2>HTML output</H2><UL>";
  var j=e;
  while(--j>=5)
  {
    for(i in c)
    {
      if(c[i]==j) h+=b[i];
    }
  }
  for(i in nc) if(nc[i]>=5) h+=b[i];
  j=e;
  h+="</UL>\n<H2>Wikimarkup output</H2><pr"+"e>";
  while(--j>=5)
  {
    for(i in c)
    {
      if(c[i]==j) h+=a[i];
    }
  }
  for(i in nc) if(nc[i]>=5) h+=a[i];
  h+="</p"+"re>";
  document.body.innerHTML=h;
}
}


//</nowiki></pre>
 
function ifmatch(ary) {
if (ary && ary.length >= 2) {
return ary[1];
} else {
return "";
}
}

Revision as of 21:04, 21 September 2006

// Modded version of http://en.wikipedia.org/wiki/User:Interiot/Tool2/code.js by Elliott.

// see http://paperlined.org/apps/wikipedia/Tool2/ for instructions on adding this to your monobook.js

// To run this tool on other servers:
//	1. copy this script to the target server (this is required because of javascript cross-site security restrictions)

//	2. update the following URL
//		for example: "User:Interiot/Tool2/code.js"
var tool2_url = "User:Interiot/Tool2/code.js";

//	3. update this namespace list, extracted from something like http://en.wikiquote.org/wiki/Special:Export//
//			These *should not* have colons after them.
var namespaces = [
"Talk",
"User",
"User talk",
"Wikiquote",
"Wikiquote talk",
"Image",
"Image talk",
"MediaWiki",
"MediaWiki talk",
"Template",
"Template talk",
"Help",
"Help talk",
"Category",
"Category talk",
		// 3b. these two project project entries are not added by Special:Export, and might or might not need to be updated
"Hydrogenaudio Knowledgebase",
"Hydrogenaudio Knowledgebase talk"
];

namespaces[100] = "Portal";
namespaces[101] = "Portal talk";

//	4. update this date-parser to match the format and language of your specific wiki.  Feel free to contact Interiot regarding this, if you can't find another
//		copy of this script that uses the same language.
// input: a text string from Special:Contributions.    output: a javascript Date object
// documentation:  http://www.quirksmode.org/js/introdate.html#parse, http://www.elated.com/tutorials/programming/javascript/dates/
function date_parse(text) {
	var matches = text.match(/^([0-9:]+), +([0-9]+) +([a-z]+) +([0-9]+)$/i);
	if (!matches) {
		//dump_text("XXX");			// for debugging
		return matches;
	}

	parseme = matches[3] + ", " + matches[2] + " "  + matches[4] + " " + matches[1] + ":00";

	//dump_text(parseme);				// for debugging

	var dt = new Date();
	dt.setTime( Date.parse(parseme));

	//dump_text(dt.toLocaleString());		// for debugging

	return dt;
}

// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ end of server-specific configuration ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^



// TODO:
//	- the current document.location method doesn't work when the page is accessed sans-mod_rewrite
//	- test with non-ASCII characters
//		- non-ascii usernames
//		- ??



var prefix = "";
var params = parse_params();

addOnloadFunction(function() {
  var path_len = document.location.pathname.length;
  // trigger once we view the right page
  if (document.location.pathname.substring(path_len - tool2_url.length, path_len) == tool2_url) {
    // get the prefix (needs to be fixed to work sans-mod_rewrite
    prefix = document.location.protocol + "//" + document.location.host + "/"
            + document.location.pathname.substring(1, path_len - tool2_url.length);

    // blank the inner contents of the page
    var bodyContent = document.getElementById("bodyContent");
    while (bodyContent.childNodes.length > 0) bodyContent.removeChild(bodyContent.lastChild);

    if (document.location.search.length == 0) {
      generate_input_form(bodyContent);
    } else {
      generate_main_report(bodyContent);
    }
  }
});


function generate_input_form(bodyContent) {
  if (navigator.userAgent.toLowerCase().indexOf('msie')+1)
  {
  bodyContent.innerHTML = "This counter does not currently work in Internet Explorer.  Please <a href='http://www.getfirefox.com'>get Firefox</a>.";
  }
  else
  {
  bodyContent.innerHTML =
            "<form><table><tr><td>Username <td><input maxlength=128 name=username value='' id=username title='username'>" +
            "             <tr><td>         <td><input type=submit value='Submit'>" +
            "</table></form>";

  var form = bodyContent.getElementsByTagName("form")[0];
  form.method = "get";
  form.action = document.location;

  document.getElementById("username").focus();
  }
}

function generate_main_report() {
  fetch_data(params["username"].replace(/\+/g, " "),
		"", output_main_report, 0, []);
}


	function add_stats_row(left_col, right_col) {
		var row = document.createElement("tr");
		var left = document.createElement("td");
		var right = document.createElement("td");
	
		document.getElementById("basic_stats").appendChild(row);
		row.appendChild(left);
		row.appendChild(right);
		//left.innerHTML = left_col;
		left.appendChild( document.createTextNode(left_col) );
		right.appendChild( document.createTextNode(right_col) );
		return row;
	}

function output_main_report(history) {
	// -- generate summary statistics
	var unique_articles = new Array();
	var namespace_numedits = new Array();
	for (var i=0; i<namespaces.length; i++) {
		namespace_numedits[ namespaces[i] ] = 0;
	}
	namespace_numedits[""] = 0;
	for (var i=0; i<history.length; i++) {
		var h = history[i];
		unique_articles[  h["title"] ]++;
		namespace_numedits[  h["namespace"] ]++;
	}
	var unique_articles_keys = keys(unique_articles);

	// -- output report
	var table = document.createElement("table");
	table.id = "basic_stats";
	document.getElementById("bodyContent").appendChild(table);

	add_stats_row("Username", params["username"].replace(/\+/g, " "));
	add_stats_row("Total edits", history.length);
	add_stats_row("Distinct pages edited", unique_articles_keys.length);
	add_stats_row("Average edits/page", new Number(history.length / unique_articles_keys.length).toFixed(3));
	add_stats_row("First edit", history[ history.length-1 ]["date_text"] );

	// add a blank row
	add_stats_row("", "").childNodes[0].style.height = "1em";

	add_stats_row("(main)", namespace_numedits[""]);
	for (var i=0; i<namespaces.length; i++) {
		var nmspc = namespaces[i];
		if (namespace_numedits[nmspc]) {
			add_stats_row(nmspc, namespace_numedits[nmspc]);
		}
	}
}



// ===================================== HTML-scraping backend =========================================

function add_loading_notice() {
	if (document.getElementById("loading_notice"))
		return;
	var loading = document.createElement("div");
	loading.id = "loading_notice";
	loading.innerHTML = "<br><br>Retrieving data<blink>...</blink>";
	document.getElementById("bodyContent").appendChild(loading);
}
function remove_loading_notice() {
	var loading = document.getElementById("loading_notice");
	if (!loading) return;
	loading.parentNode.removeChild(loading);
}

var offset_regexp = /href="[^"]+:Contributions[^"]+offset=(\d+)/gi;
function fetch_data(username, end_date, handler, offset, page_list) {
	add_loading_notice();
	var url = prefix + "Special:Contributions/" + username + "?offset=" + offset + "&limit=5000";
	loadXMLDoc(url, 
		function (request) {
			var next_offset = 0;
			if (request.readyState != 4)   return;
			if (request.status == 200) {
				page_list.push(request.responseText);
				//dump_text(request.responseText);

				// see if there's another pageful to get
				var matches = map( function(p){
						return p.match( /(\d+)$/ )[0];
					}, request.responseText.match( offset_regexp ) );
				for (var i=0; i<matches.length; i++) {
					var v = matches[i] * 1;
					if (v != 0 && (offset == 0 || v < offset)) {
						next_offset = v;
						break;
					}
				}
			}

			//next_offset = 0;			// for testing only, retrieve just the first page of results

			if (next_offset == 0) {
				parse_data(page_list, handler);
			} else {
				// tail recurse
				fetch_data(username, end_date, handler, next_offset, page_list);
			}
		});
}


// input: a list of strings, each string containing the HTML from a single page
// output: a list, where each individual entry is a specific edit from history
function parse_data(page_list, handler) {
	//var total_len = 0;
	//for (var i=0; i<page_list.length; i++) total_len += page_list[i].length;
	//alert("parsing " + page_list.length + " pages comprising " + total_len + " total bytes");

	var last_history_ent = [];
	last_history_ent["title"] = "";
	last_history_ent["oldid"] = "";

	var edit_history = new Array();
	for (var pagecnt=0; pagecnt<page_list.length; pagecnt++) {
		var matches = page_list[pagecnt].match( /^<li>[^(]+\(<a href="[^"]+action=history.*/gim );
		//dump_lines(matches);
		for (var matchcnt=0; matchcnt<matches.length; matchcnt++) {
			var history_text = matches[matchcnt];

			var history_entry = new Array();
			history_entry["date_text"] = history_text.match( /^<li>([^(<]+)/i )[1]
					.replace( / +$/, "");
			history_entry["date"] = date_parse( history_entry["date_text"] );
			history_entry["title"] = history_text.match( /title="([^"]+)"/i )[1]
					.replace( /&quot;/g, "\"")
					.replace( /&amp;/g, "&");
			var find_comment = history_text.replace(/<span class="autocomment">.*?<\/span> ?/, "");
			history_entry["comment"] = ifmatch(find_comment.match( /<span class='comment'>(.*?)<\/span>/ ))
					.replace(/^\((.*)\)$/, "$1");
			history_entry["minor"] = /<span class="minor"/.test(history_text);
			history_entry["oldid"] = ifmatch(history_text.match(/oldid=([0-9]+)/i));

			history_entry["namespace"] = "";
			for (var nmspc_ctr=0; nmspc_ctr<namespaces.length; nmspc_ctr++) {
				var nmspc = namespaces[nmspc_ctr] + ":";
				if (history_entry["title"].substring(0, nmspc.length) == nmspc) {
					history_entry["namespace"] = namespaces[nmspc_ctr];
					break;
				}
			}

			//dump_object(history_entry);

			if (history_entry["title"] != last_history_ent["title"] || history_entry["oldid"] != last_history_ent["oldid"])
				edit_history.push(history_entry);
			last_history_ent = history_entry;
		}
	}

	remove_loading_notice();

	handler(edit_history);
}




// ===================================== test/debug functions =========================================

function dump_text(text) {
  //alert("dump_text, with text of size " + text.length);

  var pre = document.createElement("pre");

  var div = document.createElement("div");
  div.style.width = "60em";
  div.style.maxHeight = "40em";
  div.style.overflow = "auto";

  pre.appendChild(document.createTextNode(text));
  div.appendChild(pre);
  document.getElementById("bodyContent").appendChild(div);
}

function dump_lines(ary) {
  dump_text("--> " + ary.join("\n--> "));
}

function dump_object(obj) {
	var toString = "";
	for (var prop in obj) {
		toString += prop + ": " + obj[prop] + "\n";
	}
	dump_text(toString);
}


// ===================================== utility functions =========================================

function addOnloadFunction(f) {
  if (window.addEventListener) window.addEventListener("load",f,false);
  else if (window.attachEvent) window.attachEvent("onload",f);
  else {
    var oldOnload='_old_onload_'+addOnloadFunction.uid;
    addOnloadFunction[oldOnload] = window.onload ? window.onload : function () {};
    window.onload = function() { addOnloadFunction[oldOnload]();  f(); }
    ++addOnloadFunction.uid;
  }
}


function parse_params() {
  var pairs = document.location.search.substring(1).split("&");
  var ret = [];
  for (var i=0; i < pairs.length; i++) {
    var values = pairs[i].split("=");
    ret[values[0]] = unescape(values[1]);
  }
  return ret; 
}


function loadXMLDoc(url, handler)
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
	req.onreadystatechange = function () {handler(req)};
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function () {handler(req)};
            req.open("GET", url, true);
            req.send();
        }
    }
}


// see http://search.cpan.org/dist/perl/pod/perlfunc.pod#map
function map (handler, list) {
  var ret = new Array();
  for (var i=0; i<list.length; i++) {
    ret[i] = handler( list[i] );
    // ret.push( handler( list[i] ) );
  }
  return ret;
}

// see http://search.cpan.org/dist/perl/pod/perlfunc.pod#keys
function keys (obj) {
	var ret = new Array();
	for (var key in obj) {
		ret.push(key);
	}
	return ret;
}


function ifmatch(ary) {
	if (ary && ary.length >= 2) {
		return ary[1];
	} else {
		return "";
	}
}