Jump to content

User:Alex 21/script-updateepisodes.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Alex 21 (talk | contribs) at 08:33, 6 October 2016. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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($) {
	var portletlink = mw.util.addPortletLink('p-tv', '#', 'Update episodes');
	$(portletlink).click( function(e) {
		e.preventDefault();
		// Textbox contents
		var wpTextbox1 = document.getElementById('wpTextbox1');
		var wpTextbox1_V = wpTextbox1.value.split("\n");
		
		// Declare variables
		var NumEpisodes = false; var RTitle = false; var line;
		var OriginalAirDate = false; var EpisodeNumber = false; var RmvRefs = false;
		
		// Go though contents one line at a time
		for (var i = 0; i < wpTextbox1_V.length; i++) {	
			// Retreive particular line
			line = wpTextbox1_V[i];
			
			// Remove references param set, and on to the next episode row
			if (RmvRefs && line.toLowerCase().indexOf('episode list') >= 0) RmvRefs = false;
			
			// Update num_episodes in {{Infobox television}}, if it exists
			var NumEpisodesM = line.match(/(\s*\|\s*num_episodes\s*=\s*(<onlyinclude>)?)(\d+)(.*)/);
			if (NumEpisodesM) {
				NumEpisodes = parseInt(NumEpisodesM[3])+1;
				line = line.replace(NumEpisodesM[1]+NumEpisodesM[3], NumEpisodesM[1]+NumEpisodes);
			}
			
			// Check if first appearance of RTitle is found
			if (!RTitle) {
				// If line contains RTitle, set to <<RTitle>> (will be removed later).
				var RTitleM = line.match(/(\s*\|\s*RTitle\s*=\s*)(.*)/);
				if (RTitleM) {
					line = '<<RTitle>>';
					RTitle = true;
					RmvRefs = true;
				}
				
				// Catch episode number to add to summary
				var EpisodeNumberM = line.match(/(\s*\|\s*EpisodeNumber\s*=\s*)(.*)/);
				if (EpisodeNumberM) EpisodeNumber = EpisodeNumberM[2];
			} else if (!OriginalAirDate) {
				// If RTitle is found but OriginalAirDate not set, find the next occurrence
				// of OriginalAirDate to update {{Aired episodes}}, if it exists
				var OriginalAirDateM = line.match(/(\s*\|\s*OriginalAirDate\s*=\s*)\{\{[s|S]tart date\|([^\}]*)\}\}/);
				if (OriginalAirDateM) OriginalAirDate = OriginalAirDateM[2];
			}
			
			// Remove director/writer/etc. references
			if (RmvRefs) line = line.replace(/<ref(.*)\/(ref)?\>/g,"");
			
			// Update line
			wpTextbox1_V[i] = line;
		}
		
		// Save values or get saved values
		if (OriginalAirDate) {
			// If EpisodeNumber/OriginalAirDate was found, saved it from transcluded season page for LoE page update
			localStorage.setItem('EpisodeNumber', EpisodeNumber);
			localStorage.setItem('OriginalAirDate', OriginalAirDate);
		} else {
			// If EpisodeNumber/OriginalAirDate was not found, obtain it from transcluded season page save for LoE page update
			OriginalAirDate = localStorage.getItem('OriginalAirDate');
			localStorage.removeItem('OriginalAirDate');
			EpisodeNumber = localStorage.getItem('EpisodeNumber');
			localStorage.removeItem('EpisodeNumber');
		}
		
		// Alert of no existance of OriginalAirDate/EpisodeNumber
		if (!OriginalAirDate || !EpisodeNumber)
			alert('Be sure to update the individual transcluded season page first, then the List of Episode page, to save the episode number and air date.');
		
		// Update summary
		var summary = document.getElementById('wpSummary');
		var EpNumber = (EpisodeNumber?EpisodeNumber:NumEpisodes);
		if (summary.value.indexOf('Update]]; aired') >= 0) summary.value += " ["+EpNumber+"]";
		else summary.value += "[[User:AlexTheWhovian/script-updateepisodes|Update]]; aired ["+EpNumber+"]";
		
		// Concatenate textbox values again, but without <<RTitle>>
		var wpTextbox1NewValue = '';
		for (i = 0; i < wpTextbox1_V.length; i++) if (wpTextbox1_V[i] != '<<RTitle>>') wpTextbox1NewValue += wpTextbox1_V[i]+"\n";
		
		// Update {{Aired episodes}}, if it exists
		if (OriginalAirDate) wpTextbox1NewValue = wpTextbox1NewValue.replace(/(\{\{Aired episodes\|)((\d*)\|(\d*)\|(\d*))/, "$1"+OriginalAirDate);
		if (EpNumber) wpTextbox1NewValue = wpTextbox1NewValue.replace(/((\{\{Aired episodes\|)(.*)(\|\s*num\s*=))(\d+)/, "$1"+EpNumber);
		wpTextbox1NewValue = wpTextbox1NewValue.replace(/((\{\{Aired episodes\|)(.*)(\|\s*finished\s*=))(\d+)/, "$2$3");
		
		// Reset textbox with new version
		wpTextbox1.value = wpTextbox1NewValue;
	});
});