User:Frietjes/queryoldtransclusions.js

/// Query Old Transclusions /// Adapted from User:Timotheus_Canens/massedit.js /// Once installed, go to Special:QueryOldTransclusions and provide a list of pages

function getTemplateRegexp(tname) { tname = tname.replace(/[\s][\s]*/g, '[_ ]'); return new RegExp ('\\{\\{[_\\s]*(?:' + (tname.charAt(0).toUpperCase + tname.slice(1)) + '|' + (tname.charAt(0).toLowerCase + tname.slice(1)) + ')'		+ '[\\s_]*(?:\\||\\}\\})', 'g'); } function getTimestampstr(tstr) { tstr = tstr.replace(/^([0-9][0-9][0-9][0-9]\-)([1-9]\-)/g, '$10$2'); tstr = tstr.replace(/^([0-9][0-9][0-9][0-9]\-[0-9][0-9]\-)([1-9]Z)/g, '$10$2'); tstr = tstr.replace(/^([0-9][0-9][0-9][0-9]\-[0-9][0-9]\-)([1-9])$/g, '$10$2'); if (tstr.search(/^[0-9][0-9][0-9][0-9]\-[0-9][0-9]\-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]Z/g) >= 0) { // Good } else if (tstr.search(/^[0-9][0-9][0-9][0-9]\-[0-9][0-9]\-[0-9][0-9]$/g) >= 0) { tstr = tstr + 'T00:00:00Z'; } else if (tstr.search(/^[^\t ]*$/g) >= 0) { alert('Ignoring malformed timestamp'); tstr = ""; }	return tstr; }

function doQueryOldTransclusions { document.getElementById("wpQueryOldTransclusionsSubmit").disabled = true; var templates = document.getElementById("wpQueryOldTransclusions").value.split("\n");

if(templates.length > 0) { if(templates[0].search(/[^\s]/g) < 0) { templates.shift; }	}	if(templates.length > 0) { var articlelist = []; for(var j=0;j 1) { cstr = "&eicontinue=" + ckey; }				req.open("GET", mw.config.get('wgScriptPath') + "/api.php?format=json" +					"&action=query&continue=" + cstr + 					"&list=embeddedin&eilimit=500&eititle=" + 					encodeURIComponent(templates[j]), false); req.send; var response = JSON.parse(req.responseText).query.embeddedin; for(var index in response) { var info = response[index]; articlelist.push(info.title); }				if(JSON.parse(req.responseText).continue) { loopflag = true; ckey = JSON.parse(req.responseText).continue.eicontinue; } else { loopflag = false; }			}		}		articlelist.sort; articlelist = articlelist.filter( function( item, index, inputArray ) {			return inputArray.indexOf(item) == index;		}); document.getElementById("wpQueryOldPages").value = document.getElementById("wpQueryOldPages").value + '\n' + articlelist.join('\n'); }	document.getElementById("wpQueryOldPages").value = document.getElementById("wpQueryOldPages").value.replace(/[\r\n][\t ]*[\r\n]/, '\n'); document.getElementById("wpQueryOldPages").value = document.getElementById("wpQueryOldPages").value.replace(/^[\t ]*[\r\n]/, ''); var articles = document.getElementById("wpQueryOldPages").value.split("\n"); articles.sort; var maxrev = document.getElementById("wpQueryMaxrev").value.trim; var searchexp = getTemplateRegexp(document.getElementById("wpQueryTemplate").value.trim); var parametername = document.getElementById("wpQueryParameter").value.trim; var ts = getTimestampstr(document.getElementById("wpQueryTimestamp").value.trim);

var rmblankparam = new RegExp("\\|[\\s]*" + parametername + "[\\s]*=[\\s ]*(\\||\\}\\})", "g"); var matchparam = new RegExp("\\|[\\s]*" + parametername + "[\\s]*=", "g");

var failedtxt = ""; var foundtxt = ""; var incompletetxt = ""; var notfoundtxt = ""; for(var i=0; i< articles.length; i++) { document.getElementById("wpQueryOldTransclusionsSubmit").value = "(" + (i+1) + " / " + articles.length + ")"; if(0 === ((i+1) % 10)) { document.getElementById("wpQueryOldTransclusionsProcessing").innerHTML = "Processing " + articles[i]; }		if (articles[i].search(/[^\s]/g) >= 0) { var result = getparammatches(articles[i], getpriortimestamp(articles[i], ts), maxrev, searchexp, rmblankparam, matchparam); var foundparameter = result[0]; var foundtemplate = result[1]; var incompletesearch = result[2]; if(incompletesearch > 0) { incompletetxt = incompletetxt + '' + articles[i] + '\n'; }			if(foundparameter > 0) { foundtxt = foundtxt + '' + articles[i] + '\n'; } else if (foundtemplate > 0) { notfoundtxt = notfoundtxt + '' + articles[i] + '\n'; } else { failedtxt = failedtxt + '' + articles[i] + '\n'; }		}		if(0 === ((i+1) % 100)) { document.getElementById("wpQueryOldTransclusionsIncomplete").innerHTML = ' Too many revisions to process: \n\n' + incompletetxt + '\n'; document.getElementById("wpQueryOldTransclusionsFailed").innerHTML = ' Failed to find revision: \n\n' + failedtxt + '\n'; document.getElementById("wpQueryOldTransclusionsFound").innerHTML = ' Found text: \n\n' + foundtxt + '\n'; document.getElementById("wpQueryOldTransclusionsNotFound").innerHTML = ' Did not find text: \n\n' + notfoundtxt + '\n</ul>'; }	}	document.getElementById("wpQueryOldTransclusionsIncomplete").innerHTML = ' Too many revisions to process: \n<ul>\n' + incompletetxt + '\n</ul>'; document.getElementById("wpQueryOldTransclusionsFailed").innerHTML = ' Failed to find revision: \n<ul>\n' + failedtxt + '\n</ul>'; document.getElementById("wpQueryOldTransclusionsFound").innerHTML = ' Found text: \n<ul>\n' + foundtxt + '\n</ul>'; document.getElementById("wpQueryOldTransclusionsNotFound").innerHTML = ' Did not find text: \n<ul>\n' + notfoundtxt + '\n</ul>'; }

function getparammatches(title, ts, rvlimit, searchexp, rmblankparam, matchparam) { var req = new XMLHttpRequest; req.open("GET", mw.config.get('wgScriptPath') 		+ "/api.php?format=json&action=query&prop=revisions&rvlimit=" + rvlimit 		+ "&rvdir=newer&rvslots=*&rvprop=timestamp|content&rvstart=" + ts 		+ "&titles="+encodeURIComponent(title), false); req.send; var response = JSON.parse(req.responseText); var result = [0, 0, 0]; if("continue" in response) { result[2] = 1; }	for(var pageid in response.query.pages) { if ("revisions" in response.query.pages[pageid]) { var revs = response.query.pages[pageid].revisions; for(var k=0; k<revs.length; k++) { var t = revs[k].timestamp; if ('*' in revs[k].slots.main) { var pagetext = revs[k].slots.main['*']; pagetext = pagetext.replace(/[\t\r\n]/g, ' '); pagetext = pagetext.replace(//g, ''); pagetext = pagetext.replace(/[ ]+/g, ' '); if (pagetext.search(searchexp) >= 0) { result[1] = result[1] + 1; pagetext = pagetext.replace(rmblankparam, '$1'); if (pagetext.search(matchparam) >= 0) { result[0] = result[0] + 1; }					}				}			}		}	}	return result; }

function getpriortimestamp(title, ts) { var req = new XMLHttpRequest; req.open("GET", mw.config.get('wgScriptPath') 		+ "/api.php?format=json&action=query&prop=revisions&rvdir=older&rvlimit=2"		+ "&rvslots=*&rvprop=timestamp&rvstart=" + ts		+ "&titles="+encodeURIComponent(title), false); req.send; var response = JSON.parse(req.responseText); for(var pageid in response.query.pages) { if ("revisions" in response.query.pages[pageid]) { var revs = response.query.pages[pageid].revisions; if (revs.length > 1) { return revs[1].timestamp; } else if (revs.length == 1) { return revs[0].timestamp; }		}	}	return ts; }

function queryoldtransclusionsform { var bodyContent = 'bodyContent'; switch (mw.config.get('skin')) { case 'modern': bodyContent = 'mw_contentholder'; break; case 'cologneblue': bodyContent = 'article'; break; case 'vector': case 'monobook': case 'vector': default: bodyContent = 'bodyContent'; break; }	document.getElementsByTagName("h1")[0].textContent = "Query old transclusions"; document.title = "Query old transclusions tool - Wikipedia, the free encyclopedia"; document.getElementById(bodyContent).innerHTML = '<h3 id="siteSub">From Wikipedia, the free encyclopedia

'		+ '<form id="wpQueryOldTransclusions1" name="wpQueryOldTransclusions1">' + 'If you abuse this tool, it\'s your fault, not mine.' + '<p id="wpQueryOldTransclusionsProcessing"> ' + 'Pages with transclusions to query (one on each line, with namespace prefix): '			+ '<textarea tabindex="1" name="wpQueryOldTransclusions" id="wpQueryOldTransclusions" rows="2" cols="80"> ' + ' <form id="wpQueryOldTransclusions2" name="wpQueryOldTransclusions2">' + 'Pages to query (one on each line, please): '			+ '<textarea tabindex="1" name="wpQueryOldPages" id="wpQueryOldPages" rows="10" cols="80"> ' + '<input type="button" id="wpQueryOldTransclusionsSubmit" name="wpQueryOldTransclusionsSubmit" value="Query" onclick="doQueryOldTransclusions" />' + ' <form id="wpQueryOldTransclusions3" name="wpQueryOldTransclusions3">Template name: ' + '<textarea tabindex="1" name="wpQueryTemplate" id="wpQueryTemplate" rows="1" cols="11">Geobox ' + ' <form id="wpQueryOldTransclusions4" name="wpQueryOldTransclusions4">Parameter name: ' + '<textarea tabindex="1" name="wpQueryParameter" id="wpQueryParameter" rows="1" cols="11">source[0-9]?_length ' + ' <form id="wpQueryOldTransclusions5" name="wpQueryOldTransclusions5">Date of earliest revision (YYYY-MM-DD): ' + '<textarea tabindex="1" name="wpQueryTimestamp" id="wpQueryTimestamp" rows="1" cols="11">2018-11-11 ' + ' <form id="wpQueryOldTransclusions6" name="wpQueryOldTransclusions6">Maximum number of revisions: ' + '<textarea tabindex="1" name="wpQueryMaxrev" id="wpQueryMaxrev" rows="1" cols="11">50 ' + ' '		+ ' '		+ ' '		+ ' '		+ ' '; } if(mw.config.get('wgNamespaceNumber') === -1 && (mw.config.get('wgPageName') === "Special:Queryoldtransclusions" || mw.config.get('wgPageName') === "Special:QueryOldTransclusions") ) { $.when( $.ready, mw.loader.using(['mediawiki.util'])).done( queryoldtransclusionsform ); }