Jump to content

User:Ahecht/Scripts/refconsolidate.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 script is designed to run from the browser console ([[HELP:CONSOLE]]) */
/* on a page with multiple similar references. The below code is targeted     */
/* towards Al Jazeera live blogs, but it can be easily modified for other     */
/* sources by changing titRE. The first time it is run it will standardize  */
/* the names based on the URL, so all references with the same URL can be     */
/* detected as identical when the page is run through [[WP:REFILL]], and      */
/* and outputs the new wikitext to the console.                               */
/*                                                                            */
/* After ReFill, the page should contain named references called "auto",      */
/* "auto2", "auto3", etc. Running the script again will rename those refs in  */
/* the form "NameOfWorkYYMMDDFirstTitleWords", as long as it finds the "work",*/
/* "title" and "date" fields, and output new wikitext to the console.         */
/******************************************************************************/

var wikitext;
const smallWords = new Set(["a", "an", "the", "and", "but", "or", "nor", "for", "yet",
	"so", "as", "in", "of", "on", "to", "from", "into", "over", "with",	"upon", "amid"]);
const titRE = /url\s*=\s*[^\|\}]*aljazeera\.com[^\|\}]*\/live-([^\|\}]*)/i;

new mw.Api().get({
	"action": "query",
	"format": "json",
	"prop": "revisions",
	"titles": mw.config.get("wgRelevantPageName"),
	"formatversion": "2",
	"rvprop": "content",
	"rvslots": "main",
	"rvlimit": "1"
}).done( (data) => {
	wikitext = data.query.pages[0].revisions[0].slots.main.content;
	for (const ref of wikitext.matchAll(/<ref\b(?![^>]*\/>)([^>]*)>(.*?)<\/\s*ref>/gi)) {
		var text = ref[0];
		var newTitle = text.match(titRE)?.[1];
		if (newTitle) {
			newTitle = newTitle.trim().split(/[-_ \+]/).map( (word, i, arr) => 
				(i === 0 || i === arr.length - 1 || !smallWords.has(word.toLowerCase())) 
				? word.charAt(0).toUpperCase() + word.slice(1) 
				: word)
			.join(' ').trim();
			var oldTitle = text.match(/title\s*=\s*([^\|\}]*)/i)?.[0];
			text = text.replace(oldTitle, "title = "+newTitle+" ").replace(/\|\s*quote\s*=\s*[^\|\}]*/i, '');
		} else {
			text = text.replace(/\|\s*quote\s*=\s*[^\|\}]*/i, '');
		}
		if (ref[0] != text) wikitext = wikitext.replace(ref[0], text);
		
		var name = text.match(/<ref\s*name\s*=\s*['"](.*?)['"]\s*>/i)?.[1];
		if (name && name.match(/^(auto|:)\d*$/)) {
			var work = text.match(/(?:work|website|newspaper|via)\s*=\s*([^\|\}]*)/i)?.[1].replace(/[^a-z]/ig, '');
			var date = text.match(/date\s*=\s*([^\|\}]*)/i)?.[1];
			if (new Date(date).getTime()) {
				date = new Date(date);
				date = date.getFullYear()+String(date.getMonth() + 1).padStart(2, '0')+
					String(date.getDate()).padStart(2, '0');
			}
			var abbrTitle = text.match(/title\s*=\s*([^\|\}]*)/i)?.[1].split(' ').slice(0, 3).join('');
			if (work && date && abbrTitle) {
				var re = new RegExp('<ref\\s*name\\s*=\\s*[\'"]'+name+'[\'"]', 'gis');
				var newRef = '<ref name="'+(work+date+abbrTitle).replace(/[^\w]/ig,"")+'"';
				wikitext = wikitext.replace(re, newRef);
			}
		}
	}
	console.log(wikitext);
});