Jump to content

User:Jerome Frank Disciple/url-status fixerBETA.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.
// 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;
}