Jump to content

User:Alex 21/script-formatdates.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.
$(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);
});