User:Dr pda/articlehistory.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.

//<pre>
//This function adds a link to the toolbox which, when clicked, searches the talk page history
//to find events which might be relevant to the {{ArticleHistory}} template. See the talk page for more details.
//
//To use this function add {{subst:js|User:Dr pda/articlehistory.js}} to your monobook.js
//
function loadXMLDocAppendingId(url,handler,id)
{
    url=url+id;
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        var req = new XMLHttpRequest();
    }
    // branch for IE/Windows ActiveX version
    else if (window.ActiveXObject) {
        var req = new ActiveXObject("Microsoft.XMLHTTP");
   }
   if (req) {
     req.onreadystatechange = function () {handler(req,id)};
     req.open("GET", url, true);
     req.send("");
   }
}
 
function getArticleOldid(req,id) {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here...
	 var response = req.responseXML.documentElement;
	 var rv = response.getElementsByTagName('rv');
	 var spannode = document.getElementById('oldid-'+id);
	 spannode.innerHTML = rv[0].getAttribute('revid');
	 var linode = document.getElementById(id);
	 linode.onclick='';
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}
 
function getArticleOldidManual(req,id) {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here...
	 var response = req.responseXML.documentElement;
	 var rv = response.getElementsByTagName('rv');
	 var spannode = document.getElementById('oldid-'+id);
 
	 var timestamp = rv[0].getAttribute('timestamp');
	 timestamp = timestamp.replace(/Z/g,'');
	 var prettytimestamp = timestamp.replace(/T/,', ');
	 timestamp = timestamp.replace(/[-T:]/g,'');
	 var idtime = id.substr(0,4)+'-'+id.substr(4,2)+'-'+id.substr(6,2)+', '+id.substr(8,2)+':'+id.substr(10,2)+':'+id.substr(12,2);
 
	 var revid = rv[0].getAttribute('revid');
 
	 spannode.innerHTML = 'The last version before '+idtime+' was at '+prettytimestamp+', with an oldid of '+revid+'. Click on the links to jump to the <a href="/w/index.php?title='+articleName+'&action=history&offset='+id+'">article history</a> or <a href="/w/index.php?title='+talkName+'&action=history&offset='+id+'">talk page history</a> at this point.';
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}
 
function getHistoryMilestones(req) {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here...
	 var response = req.responseXML.documentElement;
	 var revList = response.getElementsByTagName('rv');
	 var prettylastcomment = '';
	 var lasttimestamp = '';
	 var prettylasttimestamp = '';
 
         if(revList.length > 0){
	   for(var i=0; i<revList.length; i++){
	     var comment = revList[i].getAttribute('comment');
	     //strip out headers i.e. /*...*/
	     prettycomment=comment.replace(/\/\*.*\*\//,'');
	     comment=prettycomment.toLowerCase();
	     var timestamp = revList[i].getAttribute('timestamp');
	     timestamp = timestamp.replace(/Z/g,'');
	     var prettytimestamp = timestamp.replace(/T/,', ');
	     timestamp = timestamp.replace(/[-T:]/g,'');
 
             var output = document.getElementById("article-milestones");
	     if(comment.match('{{') || comment.match('featured') || comment.match('fac.?failed') || comment.match('review')|| comment.match(/\bfa\b/) || comment.match(/\bfac\b/) || comment.match(/\bga\b/) || comment.match(/\bgac\b/) || comment.match(/\bgafailed\b/) || comment.match(/\bga.?nominee\b/)|| comment.match('good article') || comment.match(/\bfar\b/) || comment.match(/\bfarc\b/) || comment.match(/\bfl\b/) || comment.match(/\bflrc\b/) || comment.match(/main.?page/) || comment.match('dyk') || comment.match('delist')){
	       var milestone = document.createElement("li");
	       spanid='oldid-'+timestamp;
	       milestone.innerHTML='<b>'+prettytimestamp+'</b> '+prettycomment+' <span id='+spanid+'>(oldid)</span>';
	       milestone.id=timestamp;
	       milestone.onclick=getArticleOldidWrapper;
	       output.appendChild(milestone);
             }
	     prettylastcomment = prettycomment;
	     lasttimestamp = timestamp;
	     prettylasttimestamp = prettytimestamp;
	   }
	   var getmore = document.getElementById("getmore");
	   getmore.innerHTML='(last entry read: <b>'+prettylasttimestamp+'</b> '+prettylastcomment+')';
           //Get more milestones, if any (if not, the program won't get here again)
           loadXMLDocAppendingId(talkQueryURL,getHistoryMilestones,'&rvend='+(parseInt(lasttimestamp)-1))
          }
        }
	 else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}
 
function getArticleOldidWrapper(event) {
  loadXMLDocAppendingId(articleQueryURL,getArticleOldid,this.id)
}
 
function manualDate(event)
  {
  var mydate=prompt("Enter the date for which you want the oldid","")
  if (mydate!=null && mydate!=""){
    var utc = Date.parse(mydate+' UTC');
    var d = new Date();
    d.setTime(utc);
    if((d.getUTCHours()+d.getUTCMinutes()) == 0){
      d.setUTCHours(23);
      d.setUTCMinutes(59);
      d.setUTCSeconds(59);
    }
    var timestamp ='';
    timestamp += d.getUTCFullYear();
    timestamp += (d.getUTCMonth()<10) ? '0'+(d.getUTCMonth()+1) : (d.getUTCMonth()+1);
    timestamp += (d.getUTCDate()<10) ? '0'+ d.getUTCDate() : d.getUTCDate();
    timestamp += (d.getUTCHours()<10) ? '0'+ d.getUTCHours() : d.getUTCHours();
    timestamp += (d.getUTCMinutes()<10) ? '0'+ d.getUTCMinutes() : d.getUTCMinutes();
    timestamp += (d.getUTCSeconds()<10) ? '0'+ d.getUTCSeconds() : d.getUTCSeconds();
 
    var dateoldid = document.createElement("p");
    dateoldid.id = 'oldid-'+timestamp;
    output.parentNode.insertBefore(dateoldid,enterdate.nextSibling); 
    loadXMLDocAppendingId(articleQueryURL,getArticleOldidManual,timestamp)
    }
  }
 
//Javascript URL encode function from http://cass-hacks.com/articles/code/js_url_encode_decode/
//(CC-BY-NC 2.5 Licence)
function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}
 
function getArticleHistory(){
 
 output = document.createElement("ul");
 output.id = "article-milestones";
 
 var dummy = document.getElementById("siteSub");
 dummy.parentNode.insertBefore(output, dummy.nextSibling);
 
 var getmore = document.createElement("p");
 getmore.id = 'getmore';
 output.parentNode.insertBefore(getmore,output.nextSibling); 
 
 enterdate = document.createElement("p");
 enterdate.id = 'enterdate';
 enterdate.innerHTML='<span style="background-color:lightblue;font-weight:bold;">Click here to enter a date manually</span>';
 enterdate.onclick = manualDate;
 output.parentNode.insertBefore(enterdate,getmore.nextSibling); 
 
 var pageName = wgPageName;
 articleName = (wgNamespaceNumber == 1) ? pageName.substr(5) : pageName;
 //Handle encoded characters, i.e. \x26 in wgPageName = %26 in URL
 articleName = URLEncode(articleName);
 talkName = 'Talk:'+articleName;
 //correctly handle article name when on a Talk page archive
 var archiveIndex = articleName.toLowerCase().indexOf("/archive");
 articleName = (archiveIndex == -1) ? articleName : articleName.substring(0,archiveIndex);
 
 //Use query.php instead of api.php since it has a limit of 200 instead of 50
 //var apiHistoryQuery = '/w/api.php?action=query&prop=revisions&titles='+talkName+'&rvlimit=50&rvprop=timestamp|comment&format=xml';
 
 talkQueryURL = '/w/query.php?what=revisions&titles='+talkName+'&rvlimit=200&rvcomments&format=xml';
 articleQueryURL = '/w/query.php?what=revisions&titles='+articleName+'&rvlimit=200&rvcomments&format=xml&rvend=';
 loadXMLDocAppendingId(talkQueryURL,getHistoryMilestones,'');
}
 
addOnloadHook(function () {
    addPortletLink('p-tb', 'javascript:getArticleHistory()', 'Article history', 't-article-history', 'Search Talk page history for events relevant to ArticleHistory template', '', '');
});
 
//</pre>