User:Alex 21/script-formatdates.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. |
This user script seems to have a documentation page at User:Alex 21/script-formatdates. |
$(function($) {
setTimeout(function() {
$.when( mw.loader.using( ['mediawiki.util']), $.ready ).then(function() {
var portletlink = mw.util.addPortletLink('p-tv', '#', 'Format airdates');
$(portletlink).click( function(e) {
e.preventDefault();
// Textbox value
var wpTextbox1 = document.getElementById('wpTextbox1');
var wpTextbox1_V = wpTextbox1.value.split("\n");
// Df parameter, update parameters, months and regex used throughout
var df = false;
var AltDate_U = false;
var OriginalAirDate_U = false;
var MDY = /(\|\s*(OriginalAirDate)_?\d?\s*=\s*)(([A-Z][a-z]*)\s*)?((\d{1,2}),\s*)?(\d{4})/;
var DMY = /(\|\s*(OriginalAirDate)_?\d?\s*=\s*)((\d{1,2})\s*)?(([A-Z][a-z]*)\s*)?(\d{4})/;
var ad_reg = /(\|\s*(AltDate|FirstEngAirDate)_?\d?\s*=\s*)\{\{[Ss]tart\sdate\|(\d+)\|0?(\d+)\|0?(\d+)([^\}]*)\}\}/;
var months = ['','January','February','March','April','May','June','July','August','September','October','November','December'];
var months_short = ['','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
// Iterate through each line of the textbox
for (var i = 0; i < wpTextbox1_V.length; i++) {
var line = wpTextbox1_V[i];
// Check for df and to see if the line actually needs changing at all
if (line.match(/\|\s*df\s*\=\s*([^\s]+)/)) df = true;
if (line.toLowerCase().indexOf('start date') > -1 && line.indexOf('OriginalAirDate') > -1) continue;
if (line.toLowerCase().indexOf('start date') == -1 && (line.indexOf('AltDate') > -1 || line.indexOf('FirstEngAirDate') > -1)) continue;
if ((line.indexOf('OriginalAirDate') == -1 && line.indexOf('AltDate') == -1 && line.indexOf('FirstEngAirDate') == -1) || line.indexOf('TableTBA') > -1) continue;
// Replace OriginalAirDate
var line_old = line;
if (line.match(MDY)) {
line = line.replace(MDY, "$1{{Start date|$7|$4|$6"+(df?"|df=y":"")+"}}");
} else {
line = line.replace(DMY, "$1{{Start date|$7|$6|$4|df=y}}");
}
if (line_old == line && line.indexOf("{{Start date") == -1) {
line = line.replace(/(\|\s*(OriginalAirDate)\s*=\s*)(.*)/, "$1{{Start date|$3}}");
} else {
OriginalAirDate_U = true;
}
// Months to numbers
for (var j = 1; j < months.length; j++) {
var re = new RegExp("\\|"+months[j]+"\\|","g");
line = line.replace(re, "|"+j+"|");
re = new RegExp("\\|"+months_short[j]+"\\|","g");
line = line.replace(re, "|"+j+"|");
}
// Replace AltDate and FirstEngAirDate
line_old = line;
if (df) {
line = line.replace(ad_reg, "$1$5 <<$4>> $3");
} else {
line = line.replace(ad_reg, "$1<<$4>> $5, $3");
}
if (line_old != line) {
AltDate_U = true;
}
// Numbers to months
for (j = 1; j < months.length; j++) {
line = line.replace("<<"+j+">>", months[j]);
}
// Replace any occurences of {{Start date|}} and similar
line = line.replace(/\|\|+/g,'|').replace("|}}","}}").replace("{{Start date}}","");
wpTextbox1_V[i] = line;
}
// Save content
var wpTextbox1NewValue = '';
for (i = 0; i < wpTextbox1_V.length; i++) wpTextbox1NewValue += wpTextbox1_V[i]+"\n";
wpTextbox1.value = wpTextbox1NewValue;
document.getElementById('wpSummary').value += "Update OriginalAirDate and/or AltDate to use correct formats per template documentation via [[User:Alex 21/script-formatdates|script]].";
});
});
},200);
});