User:Ahecht/Scripts/refconsolidate.js
Appearance
< User:Ahecht | Scripts
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:Ahecht/Scripts/refconsolidate. |
/******************************************************************************/
/* 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);
});