$(function($) {
setTimeout(function() {
$.when( mw.loader.using( ), $.ready ).then(function() {
var portletlink = mw.util.addPortletLink('p-tv', '#', 'Update episodes');
$(portletlink).click( function(e) {
e.preventDefault();
// Textbox contents
var NumEpisodesM;
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;
// Remove references param set, and on to the next episode row
if (RmvRefs && (line.toLowerCase().indexOf('episode list') >= 0 || line.trim() == '}}')) RmvRefs = false;
// Update num_episodes in Template:Infobox_television, if it exists
if (!NumEpisodesM) {
NumEpisodesM = line.match(/(\s*\|\s*num_episodes\s*=\s*(<onlyinclude>)?)(\d+)(.*)/);
if (NumEpisodesM) NumEpisodes = parseInt(NumEpisodesM)+1;
}
// Check if first appearance of RTitle is found
if (!RTitle) {
// If line contains RTitle, set to <<RTitle>> if only a ref (will be removed later), else set to contents of RTitle is more than a ref (e.g. raw episode title).
var RTitleM = line.match(/(\s*\|\s*RTitle\s*=\s*)(*)<ref(.*)/);
if (RTitleM) {
if (RTitleM === "") line = '<<RTitle>>';
else line = RTitleM+RTitleM;
RTitle = true;
RmvRefs = true;
}
// Catch episode number to add to summary
var EpisodeNumberM = line.match(/(\s*\|\s*EpisodeNumber\s*=\s*)(.*)/);
if (EpisodeNumberM) EpisodeNumber = EpisodeNumberM;
} else if (!OriginalAirDate) {
// If RTitle is found but OriginalAirDate not set, find the next occurrence
// of OriginalAirDate to update Template:Aired_episodes, if it exists
var OriginalAirDateM = line.match(/(\s*\|\s*OriginalAirDate\s*=\s*)\{\{tart date\|(*)\}\}/);
if (OriginalAirDateM) OriginalAirDate = OriginalAirDateM.replace("|df=y","");
}
// Remove director/writer/etc. references
if (RmvRefs) line = line.replace(/<ref(.*)\/(ref)?\>/g,"");
// Update line
wpTextbox1_V = line;
}
EpisodeNumber = (EpisodeNumber?EpisodeNumber:NumEpisodes);
// 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 (localStorage.getItem('OriginalAirDate') || localStorage.getItem('EpisodeNumber')) {
// 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
var update = true;
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 = false;
}
// Update summary
var summary = document.getElementById('wpSummary');
if (summary.value.indexOf('Update]]; aired') >= 0) summary.value += " ";
else if (update) summary.value += "]; aired ";
// Concatenate textbox values again, but without <<RTitle>>
var wpTextbox1NewValue = '';
for (i = 0; i < wpTextbox1_V.length; i++) if (wpTextbox1_V != '<<RTitle>>') wpTextbox1NewValue += wpTextbox1_V+"\n";
// Update |num_episodes=
if (NumEpisodesM) {
if (RTitle) wpTextbox1NewValue = wpTextbox1NewValue.replace(NumEpisodesM+NumEpisodesM, NumEpisodesM+NumEpisodes); // Season page
else wpTextbox1NewValue = wpTextbox1NewValue.replace(NumEpisodesM+NumEpisodesM, NumEpisodesM+EpisodeNumber); // Main page
}
// Update Template:Aired_episodes if it exists
if (OriginalAirDate) wpTextbox1NewValue = wpTextbox1NewValue.replace(/(\{\{Aired episodes\|)((\d*)\|(\d*)\|(\d*))/, "$1"+OriginalAirDate);
if (EpisodeNumber) wpTextbox1NewValue = wpTextbox1NewValue.replace(/((\{\{Aired episodes\|)(.*)(\|\s*num\s*=))(\d+)/, "$1"+EpisodeNumber);
wpTextbox1NewValue = wpTextbox1NewValue.replace(/((\{\{Aired episodes\|)(.*)(\|\s*finished\s*=))(+)/, "$2$3");
// Reset textbox with new version
wpTextbox1.value = wpTextbox1NewValue;
});
});
},900);
});