Jump to content

User:Dr pda/templatecheck.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Dr pda (talk | contribs) at 01:40, 19 October 2007 (more testing). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
 //This function adds a link to the toolbox which, when clicked on a Category page, prompts for the name of a template,
 //checks if the pages listed contain that template, and changes the bullet to a tick mark if so.
 //To use this function add <nowiki>{{subst:js|User:Dr pda/templatecheck.js}}</nowiki> to your monobook.js
 //
 function loadXMLDoc(url,handler,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 getTemplateList(req,template) {
     // 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;
          alert(req.responseXML);
          var pages = response.getElementsByTagName('page');
          if(pages.length > 0){
           var pagesList = new Object();
	    for(var i=0;i<pages.length; i++){
	     var tl=pages[i].getElementsByTagName('tl');
	     if(tl.length > 0){
 	     for(var j=0;j<tl.length;j++){
	       if(tl[j].getAttribute('title')==template){
 	         pagesList[pages[i].getAttribute('title')] = 'true';
	       }
	      }
             }
 	   }
            var mwPagesDiv=document.getElementById('mw-pages');
            var liList = mwPagesDiv.getElementsByTagName('li');
 
            if(liList.length>0){
              for(var i=0; i<liList.length; i++){
               var pageName = liList[i].firstChild.title;
                if(pagesList[pageName]){
                  liList[i].style.cssText = "list-style-image:url('/media/wikipedia/commons/thumb/5/52/%E2%98%91.svg/11px-%E2%98%91.svg.png')";
                } 
                else{
                  liList[i].style.cssText = '';
                }
              }
            }
          }
 
         } else {
             alert("There was a problem retrieving the XML data:\n" +
                 req.statusText);
         }
     }
 } 
  
 function checkTemplates(){

  var template=prompt("Enter the template you want to check for\n (Don't include Template:)","");
  template = "Template:"+template.toUpperCase().substr(0,1)+template.substr(1);
  //queryURL = '/w/api.php?action=query&generator=categorymembers&gcmcategory=' + wgPageName.substr(9) + '&gcmlimit=200&prop=templates&format=xml';
  //queryURL = '/w/api.php?action=query&generator=categorymembers&gcmcategory=Presidents_of_the_United_States&gcmlimit=200&prop=templates&format=xml';
  queryURL = '/w/query.php?what=revisions&titles=Category:Presidents_of_the_United_States&rvlimit=200&rvcomments&format=xml';
  alert(queryURL);
  loadXMLDoc(queryURL,getTemplateList,template);

 } 

 addOnloadHook(function () {
   if(wgCanonicalNamespace == 'Category'){
     addPortletLink('p-tb', 'javascript:checkTemplates()', 'Check for Template', 't-check-template', 'Check pages in category for use of a template', '', '');
   }
 });