Module:Infobox television season name

Jump to navigation Jump to search

Module:Infobox television season name is used to validate or create a formatted season link for use in Template:Infobox television season.

Usage[edit source]

  • {{#invoke:Infobox television season name|checkAll|show_name= |pre_season_qualifier= |season_qualifier= |season_type= |season_number= }}
  • {{#invoke:Infobox television season name|checkPrevSeason|show_name= |pre_season_qualifier= |season_qualifier= |season_type= |season_number= }}
  • {{#invoke:Infobox television season name|checkNextSeason|show_name= |pre_season_qualifier= |season_qualifier= |season_type= |season_number= }}
  • {{#invoke:Infobox television season name|getPrevSeasonArticle|show_name= |pre_season_qualifier= |season_qualifier= |season_type= |season_number= }}
  • {{#invoke:Infobox television season name|getNextSeasonArticle|show_name= |pre_season_qualifier= |season_qualifier= |season_type= |season_number= }}

Parameter list[edit source]

The following parameters can be used as named parameters or nameless positional parameters.

Parameter Explanation Status
show_name The title of TV program. required
pre_season_qualifier For cases where disambiguation before the season is required (e.g. the pre-season qualifier for Dallas (1978 TV series) (season 7) is (1978 TV series)). optional
season_qualifier For cases where disambiguation (typically of the show's origin country) before the season type is required (e.g. the season qualifier for The Apprentice (U.S. season 2) is U.S.). optional
season_type To determine the usage of the word "season" for American-based shows, or "series" for shows using British-English. optional
season_number Season number. The N season of this particular show. optional

Function list[edit source]

Function Explanation
checkAll Checks if the next or previous season have a created article or redirect.
checkPrevSeason Checks if the previous season has a created article or redirect.
checkNextSeason Checks if the next season has a created article or redirect.
getPrevSeasonArticle Retrieves the previous season article title.
getNextSeasonArticle Retrieves the next season article title.

Examples[edit source]

checkAll[edit source]

  • Code: {{#invoke:Infobox television season name|checkAll|show_name=Dallas |pre_season_qualifier=(1978 TV series) |season_qualifier= |season_type=season |season_number=7 }}
  • Produces:

getPrevSeasonArticle[edit source]

  • Code: {{#invoke:Infobox television season name|getPrevSeasonArticle|show_name=Dallas |pre_season_qualifier=(1978 TV series) |season_qualifier= |season_type=season |season_number=7 }}
  • Produces:

getNextSeasonArticle[edit source]

  • Code: {{#invoke:Infobox television season name|getNextSeasonArticle|show_name=Lost |pre_season_qualifier= |season_qualifier= |season_type=season |season_number=3 }}
  • Produces:

External links[edit source]

 

local match = require("Module:String")._match
	
local pipedLink = ""

local p = {}

--[[
Local function which is used to create an pipped article link.
--]]
local function createArticleTitleWithPipedLink(article)
	if (pipedLink == nil or pipedLink == "") then
		return ""
	else
		return "[[" .. article .. "|" .. pipedLink .. "]]"
	end
end

--[[
Local helper function which is used to get the type of word used for "season"
in the disambiguation.
--]]
local function getSeasonType(shortDisambiguation)
	local seasonType  = string.find(shortDisambiguation , "series")
	if (seasonType) then
		seasonType = "series"
	else
		seasonType = "season"
	end
	return seasonType
end

--[[
Local helper function which is used to get the short disambiguation,
without the "(year) TV series," part, which can cause issues later on.
--]]
local function getShortDisambiguation(disambiguation)
	return string.gsub(disambiguation, "%d+ TV series, ", "")
end

--[[
Local helper function which is used to get the disambiguation from the title.
--]]
local function getDisambiguation(title)
	return match(title, "%s%((.-)%)", 1, -1, false, "")
end

--[[
Local helper function which is used to get the show name from the title.
--]]
local function getShowName(title)
	return mw.ustring.gsub(title, "%s+%b()$", "")
end

--[[
Local function which is used to check if the given article exists.
The function returns "true" in the following cases:
	-- A season article exists.
	-- A redirect exists to a season section.

The function returns nil in the following cases:
	-- A season article or redirect do not exist.
	-- A redirect exists, but it is a general redirect and not for any specific season section.
]]--
local function checkArticle(articleTitle)
	local article = mw.title.new(articleTitle)
	if (article ~= nil and article.exists) then
		local redirectTarget = article.redirectTarget
		if (redirectTarget) then
			local fullLink = redirectTarget.fullText
			local isSection = fullLink:find("#")
			if (isSection) then
				return "true"								-- Article is a section redirect; Valid link.
			else
				return nil									-- Article is a general redirect; Not a valid link.
			end
		else
			return "true"									-- Article exists and is not a redirect; Valid link.
		end
	else
		return nil											-- Article or redirect do not exist; Not a valid link.
	end
end

--[[
Local function which is used to create a TV season link.

The following are the supported season naming styles:
	-- <showName> (<seasonType> <seasonNumber>)
		Example: Lost (season 2).
	-- <showName> (<country> <seasonType> <seasonNumber>)
		Example: The Office (American season 2).
		Example: X Factor (British series 2).
	-- <showName> (<country> <seasonType>)
		Example: Big Brother 2 (American season).
	-- <showName> (<year> TV series, <seasonType> <seasonNumber>)
		Example: Teenage Mutant Ninja Turtles (1987 TV series, season 2)
		-- <showName> (<country> TV series, <seasonType> <seasonNumber>)
		Example: Love Island (British TV series, series 2)
--]]
local function getArticleTitle(title, prevOrNextSeasonNumber)
	local showName = getShowName(title)
	local disambiguation = getDisambiguation(title)
	local shortDisambiguation = getShortDisambiguation(disambiguation)
	local seasonType = getSeasonType(shortDisambiguation)
	local seasonNumber = match(shortDisambiguation , "%d+", 1, -1, false, "")

	local realitySeriesStyle = false
	local showNameModified
	local seasonNumber = match(shortDisambiguation , "%d+", 1, -1, false, "")
	if (seasonNumber == "") then
		if (string.match(showName , "%s+(%d+)")) then
			_, _, showNameModified, seasonNumber = string.find(showName, "(.*)%s+(%d+)")
			realitySeriesStyle = true
		else
			return "" -- Not a valid next/prev season link
		end
	end
	
	if (tonumber(seasonNumber) == nil) then
		return ""
	else
		seasonNumber = seasonNumber + prevOrNextSeasonNumber
		pipedLink = seasonType:gsub("^%l", string.upper) .. " " .. seasonNumber
		
		if (realitySeriesStyle) then
			return showNameModified .. " " .. seasonNumber .. " (" .. disambiguation  .. ")"
		else
			disambiguation = string.gsub(disambiguation, "%d+$", seasonNumber)
			return showName .. " (" .. disambiguation .. ")"
		end
		
		return 
	end
end

--[[
Local helper function which is used to get the title,
either from args (usually from /testcases) or from the page itself.
--]]
local function getTitle(frame)
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame)
	local title = args.title
	if (not title) then
		title = mw.title.getCurrentTitle().text
	end

	return title
end

--[[
Local helper function which is called to create a TV season title for the next or previous season.
Passes the value "1" or -1" to increment or decrement the current season number.
--]]
local function createArticleTitleHelper(frame, number)
	local title = getTitle(frame)
	return getArticleTitle(title, number)
end

--[[
Local helper function which is used to check if a season article exists.
--]]
local function checkSeason(frame, number)
	local articleTitle = createArticleTitleHelper(frame, number)
	return checkArticle(articleTitle)
end

--[[
Local helper function which is used to create a season article link.
--]]
local function getSeasonArticleLink(frame, number)
	local articleTitle = createArticleTitleHelper(frame, number)
	return createArticleTitleWithPipedLink(articleTitle)
end

--[[
Public function which is used to check if the next season has
a valid created article or redirect.
--]]
function p.checkNextSeason(frame)
	return checkSeason(frame, 1)
end

--[[
Public function which is used to check if the previous season has
a valid article or redirect.
--]]
function p.checkPrevSeason(frame)
	return checkSeason(frame, -1)
end

--[[
Public function which is used to check if the next or previous season have
a valid article or redirect.

Parameters: 
--]]
function p.checkAll(frame)
	if (p.checkPrevSeason(frame) == "true") then
		return "true"
	else
		return p.checkNextSeason(frame)
	end
end

--[[
Public function which is used to get the next season article title.
--]]
function p.getNextSeasonArticle(frame)
	return getSeasonArticleLink(frame, 1)
end


--[[
Public function which is used to get the previous season article title.
--]]
function p.getPrevSeasonArticle(frame)
	return getSeasonArticleLink(frame, -1)
end

--[[
Public function which is used to get the type of season word used - "season" or "series".
--]]
function p.getSeasonWord(frame)
	local title = getTitle(frame)
	local disambiguation = getDisambiguation(title)
	local shortDisambiguation = getShortDisambiguation(disambiguation)
	return getSeasonType(shortDisambiguation)
end

--[[
Public function which is used to get the infobox header text.
Currently set to retrieve it as follows: title (season/series #).
--]]
function p.getInfoboxHeader(frame)
	local showName = getShowName(title)
	local disambiguation = getDisambiguation(title)
	local shortDisambiguation = getShortDisambiguation(disambiguation)
	local seasonType = getSeasonType(shortDisambiguation)
	local seasonNumber = match(shortDisambiguation , "%d+", 1, -1, false, "")
	return showName .. " (" .. seasonType .. " " .. seasonNumber .. ")"
end

return p