Jump to content

User:Dr pda/generatelist.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.
 //<pre>
 //This script generates a list of the articles in a specified WikiProject,
 //using the categories such as FA-Class Biography articles, A-Class Biography articles etc.
 //The lists are presented in chunks of 10000 articles.
 //To use this function add {{subst:js|User:Dr pda/generatelist.js}} to your monobook.js
 //then go to http://en.wikipedia.org/w/index.php?title=User:Dr_pda/generatelist&action=edit
 //See the talk page for documentation.
 
 
 function loadXMLDocPassingTemplate(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)};
   req.open("GET", url, true);
   req.send("");
  }
 }
 
 function getMembersFromAPI(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;
         var members = response.getElementsByTagName('cm');
 
         if(members.length > 0){
 
           for(var i=0;i<members.length; i++){
             var title = members[i].getAttribute('title');
 	     title=title.replace(/^Talk:/,'');
 	     index++;
 	     totalIndex++;
 	     //If reached maximum per page
 	     if(index>10000){
 	       listString = workingString;
               workingString = '';
               index = 1;
               newPage = true;
 	     }	
 	     if ((index%100) == 1) workingString += '\n==' + index + ' to ' + (index+99) + '==\n';
 	     workingString += '# [[' +title + ']]\n';
           }
             document.getElementById('wpTextbox1').value = 'Retrieved ' + totalIndex + ' articles.\n To abort click the back button in your browser.';
 
 	   //Check for more pages
 	   var querycontinue = response.getElementsByTagName('query-continue');
 	   var categorymembers = response.getElementsByTagName('categorymembers');
 	   if(querycontinue.length > 0){
 	     cmcontinue = '&cmcontinue=' + categorymembers[0].getAttribute('cmcontinue');
 	     if(newPage){
 	       document.getElementById('wpTextbox1').value = listString;
               newPage = false;
 	       alert('10000 articles retrieved. Copy list from edit window and click the continue button');
 	     }
 	     else{
               loadXMLDocPassingTemplate(queryURL+cmcontinue,getMembersFromAPI,wikiproj);
 	     }
           }
 	   //If last page in class retrieved move on to next class
           else{
 	     if(queryURLIndex < queryURLIndexMax){
 	       queryURL = queryURLArray[++queryURLIndex];
 
 	       if(newPage){
 	         document.getElementById('wpTextbox1').value = listString;
                 newPage = false;
 		 cmcontinue='';
 	         alert('10000 articles retrieved. Copy list from edit window and click the continue button');
 	       }
 	      else{
                loadXMLDocPassingTemplate(queryURL,getMembersFromAPI,wikiproj);
 	      }
 	     } 
 	     else{
 	       document.getElementById('wpTextbox1').value = workingString;
 	       alert('List finished. '+totalIndex+' articles in WikiProject '+wikiproj);
 	     }
           } //end else
 	 } //end members.length>0
       } else {
             alert("There was a problem retrieving the XML data:\n" +
                 req.statusText);
       }
    }
 } 
 
 function continueWrapper(){
   loadXMLDocPassingTemplate(queryURL+cmcontinue,getMembersFromAPI,wikiproj);  
 } 
  
 function generateList(){
 
  wikiproj=prompt("Enter the name of the WikiProject you want to make a list for. (Case sensitive)\nShould be the same as in Category:FA-Class XXX articles","");
  queryURLBase = '/w/api.php?action=query&rawcontinue=&list=categorymembers&cmlimit=500&cmnamespace=1&format=xml&cmcategory=';
  queryURLArray = new Array();
  queryURLArray[0] = queryURLBase + 'FA-Class_' + wikiproj + '_articles';
  queryURLArray[1] = queryURLBase + 'A-Class_' + wikiproj + '_articles';
  queryURLArray[2] = queryURLBase + 'GA-Class_' + wikiproj + '_articles';
  queryURLArray[3] = queryURLBase + 'B-Class_' + wikiproj + '_articles';
  queryURLArray[4] = queryURLBase + 'Start-Class_' + wikiproj + '_articles';
  queryURLArray[5] = queryURLBase + 'Stub-Class_' + wikiproj + '_articles';
  queryURLArray[6] = queryURLBase + 'Unassessed_' + wikiproj + '_articles';
  queryURLArray[7] = queryURLBase + 'List-Class_' + wikiproj + '_articles';
  queryURLIndex = 0;
  queryURLIndexMax = confirm("Include List-Class articles?") ? 7 : 6 ;
  queryURL = queryURLArray[queryURLIndex];
  index = 0;
  totalIndex = 0;
  listString = '';
  workingString = '';
  newPage = false;
  cmcontinue = '';
 
  continueButton = document.createElement("input");
  continueButton.id = 'continue-button';
  continueButton.value = 'Continue';
  continueButton.type = 'button';
  continueButton.onclick = continueWrapper;
 
  var textbox = document.getElementById('wpTextbox1');
  textbox.parentNode.insertBefore(continueButton, textbox.nextSibling); 
 
  loadXMLDocPassingTemplate(queryURL,getMembersFromAPI,wikiproj);
 
 
 } 
 
 addOnloadHook(function () {
   if(document.location.href.indexOf('User:Dr_pda/generatelist&action=edit') != -1){
     generateList();
   }
 });
 
 //</pre>