User:Dr pda/generatelist.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Dr pda/generatelist. |
//<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>