Jump to content

User:Dr pda/strikethrough.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 subpage of
 //[[Wikipedia:WikiProject Persondata/List of biographies]], strikes through all those articles
 //which transclude the template <nowiki>{{Persondata}}</nowiki>
 //To use this function add <nowiki>{{subst:js|User:Dr pda/strikethrough.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 getStrikethroughTemplateList(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
            jobsLeft--;
            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');  
                jobsLeft++;
                loadXMLDocPassingTemplateAndURL(url+'&tlcontinue='+tlcontinue,getStrikethroughTemplateList,template);
              }
            }   
            //Do processing once all tlcontinues have been followed  
            if(tlcontinue=='' && jobsLeft==0){
 
              for(x in list){
                var match = list[x].match(/# \[\[(.*)\]\]/);
                if(match && match.length==2){
                  var pageName = match[1];
                  if(pagesList[pageName]){
                    list[x] = "# <s>[[" + pageName + "]]</s>";
                  } 
                }
              }
              document.getElementById('wpTextbox1').value = list.join('\n');
              var check = document.getElementById('t-strike-persondata');
              if(check) removeSpinner('check');
 
            }
          }
 
         } else {
             alert("There was a problem retrieving the XML data:\n" +
                 req.statusText);
         }
     }
 } 
  
 function strikePersondata(){
 
  var template=prompt("Enter the template you want to check for\n (Don't include Template:)","");
  var check = document.getElementById('t-strike-persondata');
  if(check) injectSpinner(check,'check');
  template = "Template:"+template.toUpperCase().substr(0,1)+template.substr(1);
 
  pagesList = new Object();
  text = document.getElementById('wpTextbox1').value;
  list = text.split('\n');
  var titleString = '';
  jobsLeft = 0;
  queryURL = '/w/api.php?action=query&tllimit=500&prop=templates&format=xml&titles=';
 
  for(i in list){
    var match = list[i].match(/# \[\[(.*)\]\]/);
    if(match && match.length==2){
      //API limited to 50 titles per query
      if( i%50 == 0 && i>0){
        titleString = titleString.substr(1);
        jobsLeft++;
        loadXMLDocPassingTemplateAndURL(queryURL+titleString,getStrikethroughTemplateList,template);
        titleString='';
      }
      titleString += '|' + encodeURIComponent(match[1]);
    }
  }
  //Process remainder
  titleString = titleString.substr(1);
  jobsLeft++;
  loadXMLDocPassingTemplateAndURL(queryURL+titleString,getStrikethroughTemplateList,template);  
  
 } 
 
 addOnloadHook(function () {
   if(document.location.href.indexOf('List_of_biographies/') != -1){
     mw.util.addPortletLink('p-tb', 'javascript:strikePersondata()', 'Strike items with persondata', 't-strike-persondata', 'Strikethrough pages in list which already have persondata', '', '');
   }
 
 });