User:Dr pda/strikethrough.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/strikethrough. |
//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', '', '');
}
});