User:Jerome Frank Disciple/url-status fixerBETA.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:Jerome Frank Disciple/url-status fixerBETA. |
// This module removes many "bad" url-status parameters (i.e. citation templates with url-status parameters that are defined but archive-url parameters are not). See [[User:Jerome Frank Disciple/url-status fixer]] for a fuller explanation.
function autoEDurlStatusFixerBETA (str) {
let citestart, citeend, Param, ParamEnd, targetparamindex;
//Deleting most empty archive-associated parameters. This saves bytes and makes things easier down the line.
str = str.replace(/archive-url=\|/g,"").replace(/archive-url= \|/g,"").replace(/\|archive-url=\}\}/g,"}}").replace(/\|archive-url= \}\}/g,"}}").replace(/archiveurl=\|/g,"").replace(/archiveurl= \|/g,"").replace(/\|archiveurl=\}\}/g,"}}").replace(/\|archiveurl= \}\}/g,"}}").replace(/archive-date=\|/g,"").replace(/archive-date= \|/g,"").replace(/\|archive-date=\}\}/g,"}}").replace(/\|archive-date= \}\} /g, "}}");
//Checking if url-status exists somewhere on the page
targetparamindex = str.indexOf("url-status");
//This while statement checks (and rechecks) if there's an un-reviewed instance of "url-status"
while (targetparamindex != -1) {
//Isolating the citation template with the url-status param (omitting curly brackets). I use brackets in lieu of <ref> tags because of [[Template:multiref]]
citestart = str.lastIndexOf("{{", targetparamindex)+2;
citeend = str.indexOf("}}", targetparamindex);
let justtheref=str.substring(citestart, citeend);
/* If there are internal templates, justtheref will be off. (This comes up with the use of thigns like {{ProQuest}} or {{!}} )
If the internal template precedes url-status, justtheref will start at that internal template, and it'll contain a }}
If the internal template follows url-status, justtheref will end at the end of that internal template, and it will contain a {{
This portion of the code makes the necessary adjustments.
*/
let internaltemplatedetector1 = justtheref.indexOf("{{");
let internaltemplatedetector2 = justtheref.indexOf("}}");
let internaltester;
while (internaltemplatedetector1 !=-1) {
citeend = str.indexOf("}}", citeend+2);
justtheref=str.substring(citestart, citeend);
internaltemplatedetector1 = justtheref.indexOf("{{", citeend);
}
while (internaltemplatedetector2 !=-1) {
let citestartold = citestart-2;
citestart = str.lastIndexOf("{{", citestart-4)+2;
justtheref=str.substring(citestart, citeend);
internaltester =str.substring(citestart, citestartold);
internaltemplatedetector2 = internaltester.lastIndexOf("}}");
}
//Having deleting most of the empty archive-url parameters, this checks if an archive-url or archiveurl parameter is present.
let archivethere1 = justtheref.indexOf("archive-url");
let archivethere2 = justtheref.indexOf("archiveurl");
/*
The key conditional: If there's no archive-url OR archiveurl paramater ... delete the url-status param.
*/
if (archivethere1 == -1 && archivethere2 == -1) {
//Isolating the parameter and the | before it (in case there isn't one after it). The if/else statement here handles url-status parameters that are and are not the last paramters in a citation template.
let ParamStart = justtheref.lastIndexOf("|", justtheref.indexOf("url-status"));
let ParamEnd = justtheref.indexOf("|", ParamStart+1);
if (ParamEnd != -1 ){
Param = justtheref.substring(ParamStart, ParamEnd);
}
else {
Param = justtheref.substring(ParamStart);
}
// Fix the ref by removing the url-status param, replaces the old ref with the new
let fixedref = justtheref.replace(Param,"");
str=str.replace(justtheref, fixedref);
citeend = str.indexOf("}}", citestart); // Adjusts the end of the citation position (probably not needed but makes sure nothing is skipped)
}
// Look to see if there's another url-status param after the citation just worked on->back to the "while" statement
targetparamindex=str.indexOf("url-status", citeend);
}
return str;
}