User:CharlotteWebb/history filter

To-do edit

  • Add block button (and more?) if viewing user haz admin.
  • Add paging feature (for users with >500 edits to one page).
    • Make it default to 50 and properly respect the &limit=, &offset= url parameters.
  • Parse wiki-text of edit summaries.
  • Improve code readability, documentation
  • Profit!

Script edit

qs = function(){
  qdo = {}, s = location.href.split("?")[1];
  if(s){
    p = s.split("&");
    for(var i = 0; i < p.length; i++){
      lv = p[i].split("=");
      qdo[decodeURIComponent(lv[0])] = (lv[1] ? decodeURIComponent(lv[1]) : "");
      }
    }
  return(qdo);
  }();

months = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"];
function history_filter(){
  if(wgAction != "history") return;
  ul = document.getElementById("pagehistory");
  if(!ul) return;
  if(qs["myeditsonly"]) user = wgUserName;
  else if(qs["user"]) user = qs["user"];
  else user = null;
  if(!user)
    return(document.getElementById("contentSub").innerHTML += ' | <a href="' +
      location.href + "&myeditsonly=1" + '">show my edits only</a>');
  ul.innerHTML="";
  document.getElementById("contentSub").innerHTML += ' | <a href="' +
    document.getElementById("ca-history").getElementsByTagName("a")[0].href +
    '">show all edits</a>';
  x = new XMLHttpRequest();
  u = wgServer + "/w/api.php?action=query&prop=revisions&rvlimit=500&titles=" + 
    encodeURIComponent(mw.config.get('wgPageName')) + "&rvprop=ids|flags|timestamp|size|comment&rvuser=" +
    encodeURIComponent(user) + "&format=xml";
  x.open("GET", u, true);
  x.onreadystatechange = function() {
    if(x.readyState != 4) return;
    s = "", ul = document.getElementById("pagehistory");
    rev = new DOMParser().parseFromString(x.responseText, "text/xml").getElementsByTagName("rev");
    if(!rev.length) return(ul.innerHTML = '<li class="error">' + (user == wgUserName ?
      "you have" : user + " has") + ' never edited this page, or else there is ' +
      'a bug somewhere\u2026</li>');
    difflink = function(r1, r2, a) {
      return('<a href="' + wgServer + '?title=' + encodeURIComponent(mw.config.get('wgPageName')) +
        '&diff=' + r1 + '&oldid=' + r2 + '" title="' + wgPageName + '">' + a + '</a>');
      }
    oldidlink = function(r, a) {
      return('<a href="' + wgServer + '?title=' + encodeURIComponent(mw.config.get('wgPageName')) +
        '&oldid=' + r + '" title="' + wgPageName + '">' + a + '</a>');
      }
    radio = function(r, n) {
      return(' <input type="radio" value="' + r + '" name="' + n + '" />');
      }
    userlinks = function(u) {
      return('<a href="' + wgServer + wgArticlePath.replace("$1", "User:" + u) + '">' +
        u + '</a> (<a href="' + wgServer + wgArticlePath.replace("$1", "User talk:" + 
        u) + '">' + "talk" + '</a> | <a href="' + wgServer +
        wgArticlePath.replace("$1", "Special:Contributions/" + u) +  '">' +
        "contribs" + '</a>) ');
      }
    for(i = 0; i < rev.length; i++){
      oldid = rev[i].getAttribute("revid"), bytes = rev[i].getAttribute("size");
      minor = rev[i].getAttribute("minor"), sum = rev[i].getAttribute("comment");        
      ts = rev[i].getAttribute("timestamp").match(/(\d{4})\-(\d\d)\-(\d\d)T([\d\:]+)\:\d\dZ/);
      s += '<li>(' + (oldid == wgCurRevisionId ? "cur" : 
        difflink(wgCurRevisionId, oldid, "cur")) + ") (" +
        difflink("prev", oldid, "last") + ")" + radio(oldid, "oldid") +
        radio(oldid, "diff") + " " + oldidlink(oldid, ts[4] + ", " +
        parseInt(ts[3].replace("0", "")) + " " + months[parseInt(ts[2].replace("0",
        ""))-1] +" " + ts[1]) + " " + '<span class="history-user">' +
        userlinks(user) + "</span>" +
        (minor ? ' <span class="minor">m</span>' : "") + 
        (bytes ? ' <span class="history-size">(' + bytes.replace(/(\d)(\d\d\d)$/, "$1,$2") + ' bytes)</span>' : "") +
        (sum ? ' <span class="comment">(' +  sum + ')</span>' : "") +'</li>';
      }
    ul.innerHTML = s;
    }
  ul.innerHTML = "<li>scraping from api.php, please wait\u2026</li>";
  x.send("");
  }
$(history_filter);