Modul:Infobox television season name: Perbedaan antara revisi

Konten dihapus Konten ditambahkan
k Cleanup.
RaFaDa20631 (bicara | kontrib)
Tidak ada ringkasan suntingan
 
(3 revisi perantara oleh pengguna yang sama tidak ditampilkan)
Baris 3:
local p = {}
 
--- Returns a formatted link to the list of episodes article.
--[[
--- @param listOfEpisodesArticle string
Local function which is used to create an pipped article link.
--- @return string
--]]
local function createArticleTitleWithPipedLinkgetListOfEpisodesLink(article, pipedLinklistOfEpisodesArticle)
local listOfEpisodesPage = mw.title.new(listOfEpisodesArticle, 0)
if (pipedLink == nil or pipedLink == "") then
if listOfEpisodesPage and listOfEpisodesPage.exists and listOfEpisodesPage.redirectTarget ~= mw.title.getCurrentTitle() then
return string.format("[[%s|Daftar episode]]", listOfEpisodesArticle)
end
end
 
--- Returns an article link.
--- @param article string The article's title.
--- @param pipedLink string The piped link.
--- @return string
local function getArticleLink(article, pipedLink)
if not pipedLink or pipedLink == "" then
return "[[" .. article .. "]]"
else
return "[[" .. article .. "|" .. pipedLink .. "]]"
end
return "[[" .. article .. "|" .. pipedLink .. "]]"
end
 
--- Returns the show name and season number from a title.
--[[
--- @param showName string The show's title.
Local helper function which is used to get the current season number and modified show name
--- @return nil | number | string, nil | number | string
from the show name.
local function getShowNameAndSeasonNumberFromShowName(showName)
--]]
local _, _, showNameModified, seasonNumber = string.find(showName, "(.*)%s+(%d+)$")
local function getModifiedShowNameAndCurrentSeasonNumberFromShowName(showName)
local _, _, showNameModified, seasonNumber = string.find(showName, "(.*)%s+(%d+)")
return showNameModified, seasonNumber
end
 
--- Returns the current season number from the disambiguation.
--[[
Local--- helper@param functiondisambiguation whichstring isThe used to get the current season number from thearticle's disambiguation.
--- @return string
--]]
local function getCurrentSeasonNumberFromDisambiguation(shortDisambiguationdisambiguation)
return match(shortDisambiguationdisambiguation , "%d+", 1, -1, false, "")
end
 
--- Returns the type of word used for "Musim" in the disambiguation.
--[[
---
Local helper function which is used to get the type of word used for "season"
--- The returned value can be one of three options: "Musim", "Seri", "story arc" or "spesials".
in the disambiguation.
--- @param disambiguation string The article's disambiguation.
 
--- @return string
Returns one of three options: "season", "series" or "story arc".
local function getSeasonType(disambiguation)
--]]
for _, seasonType in pairs({"musim", "seri", "story arc", "spesials"}) do
local function getSeasonType(shortDisambiguation)
if (string.find(shortDisambiguation disambiguation, "arc cerita")seasonType) then
return "arc cerita"seasonType
end
end
 
return "season"
if (string.find(shortDisambiguation , "seri")) then
return "seri"
end
 
return "musim"
end
 
--- Returns the disambiguation without the "(year) TV series," part.
--[[
Local--- helper@param function which is used todisambiguation getstring theThe shortarticle's disambiguation,.
--- @return string
without the "(year) TV series," part, which can cause issues later on.
--]]
local function getShortDisambiguation(disambiguation)
returnlocal shortDisambiguation, _ = string.gsub(disambiguation, "%d+ seri TV series, ", "")
return shortDisambiguation
end
 
--- Returns the disambiguation from the title.
--[[
--- @param title string The article's title.
Local helper function which is used to get the disambiguation from the title.
--- @return string | nil
--]]
local function getDisambiguation(title)
local disambiguation = match(title, "%s%((.-)%)", 1, -1, false, "")
if (disambiguation == "") then
return nil
else
return disambiguation
end
return disambiguation
end
 
--- Returns the TV program's disambiguation.
--[[
Local--- helper@param functiondisambiguation whichstring isThe disambiguation used to getin the showseason's name from thearticle title.
--- @return string
--]]
local function getShowNamegetTVProgramDisambiguation(titledisambiguation)
if not disambiguation then
return mw.ustring.gsub(title, "%s+%b()$", "")
return ""
end
end
 
-- Check if the disambiguation is normal 'season #' or 'series #'.
--[[
-- If so, remove disambiguation.
Local function which is used to check if the given article exists.
if string.match(disambiguation, "^season %d*$") or string.match(disambiguation, "^series %d*$") then
The function returns "true" in the following cases:
return ""
-- A season article exists.
end
-- A redirect exists to a season section.
 
local disambiguationStyle = " (%s)"
The function returns nil in the following cases:
-- Check if the disambiguation is extended and has 'TV series' and isn't just season #.
-- A season article or redirect do not exist.
-- AOnly redirectleave exists,the butTV itseries is a general redirect anddisambiguation, not for anyincluding specificthe season section#.
-- Example: Teenage Mutant Ninja Turtles (1987 TV series, season 5) will return '1987 TV series'.
]]--
if string.find(disambiguation, "TV series") then
local function checkArticle(articleTitle)
local shortDisambiguation, _ = disambiguation:match("^(.*),")
local article = mw.title.new(articleTitle)
if shortDisambiguation then
if (article ~= nil and article.exists) then
return string.format(disambiguationStyle, shortDisambiguation)
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
 
-- Check if the disambiguation is extended with country adjective.
-- Example: The Office (American season 2) will return "American season 2".
-- Keep only country adjective.
local countryDisambiguation = disambiguation:match("^(.*) season %d*") or disambiguation:match("^(.*) series %d*")
local data = mw.loadData("Module:Country adjective")
local valid_result = data.getCountryFromAdj[countryDisambiguation]
-- Check if the country adjective is valid.
if valid_result then
-- Add 'TV series' suffix
return string.format(disambiguationStyle, countryDisambiguation .. " TV series")
end
 
-- Not a known disambiguation style. Use whatever was used in the title or manually added.
-- Note: might not be a valid style link.
return string.format(disambiguationStyle, disambiguation)
end
 
--- Returns the show's name from the title.
--[[
Local--- function@param whichtitle returnsstring a seasonThe article's title and a piped link.
--- @return string
local function getShowName(title)
local name, _ = mw.ustring.gsub(title, "%s+%b()$", "")
return name
end
 
--- Returns "true" if the given link is valid; nil otherwise.
The following are the supported season naming styles:
--- A link is valid in the following cases:
-- <showName> (<seasonType> <seasonNumber>)
--- Example:-- LostA (season 2)article exists.
--- -- A redirect exists to a season section.
-- <showName> (<country> <seasonType> <seasonNumber>)
---
Example: The Office (American season 2).
--- A link is invalid in the following cases:
Example: X Factor (British series 2).
--- -- A season article or redirect do not exist.
-- <showName> (<country> <seasonType>)
--- -- A redirect exists, but it is a general redirect and not for any specific season section.
Example: Big Brother 2 (American season).
---
-- <showName> (<year> TV series, <seasonType> <seasonNumber>)
--- Note: Return values are not booleans as the returned value is used in template space.
Example: Teenage Mutant Ninja Turtles (1987 TV series, season 2)
--- @param title string The article's title.
-- <showName> (<country> TV series, <seasonType> <seasonNumber>)
--- @return string | nil
Example: Love Island (British TV series, series 2)
local function isLinkValid(title)
--]]
local functionarticle getArticleTitle= mw.title.new(title, prevOrNextSeasonNumber)
 
local showName = getShowName(title)
-- Article or redirect do not exist; Not a valid link.
if not article or not article.exists then
return nil
end
 
local redirectTarget = article.redirectTarget
 
-- Article exists and is not a redirect; Valid link.
if not redirectTarget then
return "true"
end
 
local fullLink = redirectTarget.fullText
local isSection = fullLink:find("#")
 
-- Article is a section redirect; Valid link.
if isSection then
return "true"
end
 
-- Article is a general redirect; Not a valid link.
return nil
end
 
--- Returns a season article title and a piped link.
---
--- The following are the supported season naming styles:
--- -- Style: <showName> (<seasonType> <seasonNumber>)
--- Example: Lost (season 2).
--- Example: Doctor Who (series 2).
--- -- Style: <showName> (<country> <seasonType> <seasonNumber>)
--- Example: The Office (American season 2).
--- Example: X Factor (British series 2).
--- -- Style: <showName> (<country> <seasonType>)
--- Example: Big Brother 2 (American season).
--- -- Style: <showName> (<year> TV series, <seasonType> <seasonNumber>)
--- Example: Teenage Mutant Ninja Turtles (1987 TV series, season 2)
--- -- Style: <showName> (<country> TV series, <seasonType> <seasonNumber>)
--- Example: Love Island (British TV series, series 2)
--- @param title string The article's title.
--- @param seasonNumberDiff number The number difference between the current season and the other season.
--- @return string, string
local function getArticleTitleAndPipedLink(title, seasonNumberDiff)
local disambiguation = getDisambiguation(title)
local shortDisambiguation
local seasonType
local seasonNumber = ""
local pipedLink = ""
 
if (disambiguation) then
if disambiguation then
shortDisambiguation = getShortDisambiguation(disambiguation)
seasonType = getSeasonType(shortDisambiguation)
Baris 136 ⟶ 193:
end
 
local showName = getShowName(title)
local showNameModified
if (not seasonNumber or seasonNumber == "") then
-- Not a valid next/prev season link.
if (string.match(showName , "%s+(%d+)")) then
if not string.match(showName, "%s+(%d+)$") then
showNameModified, seasonNumber = getModifiedShowNameAndCurrentSeasonNumberFromShowName(showName)
return "", nil
else
return "" -- Not a valid next/prev season link
end
showNameModified, seasonNumber = getShowNameAndSeasonNumberFromShowName(showName)
end
if (tonumber(seasonNumber) == nil) then
return ""
else
seasonNumber = seasonNumber + prevOrNextSeasonNumber
pipedLink = pipedLink .. seasonNumber
-- Titles such as "Big Brother 1 (American season)""
if (showNameModified and disambiguation) then
return showNameModified .. " " .. seasonNumber .. " (" .. disambiguation .. ")", pipedLink
 
if tonumber(seasonNumber) == nil then
-- Titles such as "Big Brother Brasil 1"
return "", nil
elseif (showNameModified) then
end
return showNameModified .. " " .. seasonNumber, nil
 
seasonNumber = seasonNumber + seasonNumberDiff
-- Standard titles such as "Lost (season 1)"
pipedLink = pipedLink .. seasonNumber
else
 
disambiguation = string.gsub(disambiguation, "%d+$", seasonNumber)
-- Titles such as "Big Brother 1 (American season)".
return showName .. " (" .. disambiguation .. ")", pipedLink
if showNameModified and disambiguation then
end
return showNameModified .. " " .. seasonNumber .. " (" .. disambiguation .. ")", pipedLink
 
-- Titles such as "Big Brother Brasil 1".
elseif showNameModified then
return showNameModified .. " " .. seasonNumber, nil
 
-- Invalid usages of TV series articles with the television season infobox.
elseif string.find(disambiguation, "TV series") and not (string.find(disambiguation, ", season") or string.find(disambiguation, ", series")) then
return "", nil
-- Standard titles such as "Lost (season 1)".
else
local newDisambiguation, _ = string.gsub(disambiguation, "%d+$", seasonNumber)
return showName .. " (" .. newDisambiguation .. ")", pipedLink
end
end
 
--- Returns the article's title either from args (usually from /testcases) or from the page itself.
--[[
--- @param frame table The frame invoking the module.
Local helper function which is used to get the title,
--- @return string
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
if not title then
title = mw.title.getCurrentTitle().text
end
 
return title
end
 
--- Returns "true" if the given season link is valid; nil otherwise.
--[[
--- @param frame table The frame invoking the module.
Local helper function which is called to create a TV season title for the next or previous season.
Passes--- the@param valueseasonNumberDiff "1"number orThe -1"number todifference increment or decrementbetween the current season numberand the other season.
--- @return string | nil
--]]
local function createArticleTitleHelperisSeasonLinkValid(frame, numberseasonNumberDiff)
local title = getTitle(frame)
local articleTitle, _ = getArticleTitleAndPipedLink(title, seasonNumberDiff)
return getArticleTitle(title, number)
return isLinkValid(articleTitle)
end
 
--- Returns a season article link.
--[[
--- @param frame table The frame invoking the module.
Local helper function which is used to check if a season article exists.
--- @param seasonNumberDiff number The number difference between the current season and the other season.
--]]
--- @return string
local function checkSeason(frame, number)
local articleTitlefunction = createArticleTitleHelpergetSeasonArticleLink(frame, numberseasonNumberDiff)
local title = getTitle(frame)
return checkArticle(articleTitle)
local articleTitle, pipedLink = getArticleTitleAndPipedLink(title, seasonNumberDiff)
return getArticleLink(articleTitle, pipedLink)
end
 
--- Returns "true" if the season link for the next season is valid; nil otherwise.
--[[
--- @param frame table The frame invoking the module.
Local helper function which is used to create a season article link.
--- @return string | nil
--]]
local function getSeasonArticleLinkp.isNextSeasonLinkValid(frame, number)
return isSeasonLinkValid(frame, 1)
local articleTitle, pipedLink = createArticleTitleHelper(frame, number)
return createArticleTitleWithPipedLink(articleTitle, pipedLink)
end
 
--- Returns "true" if the season link for the previous season is valid; nil otherwise.
--[[
--- @param frame table The frame invoking the module.
Public function which is used to check if the next season has
--- @return string | nil
a valid created article or redirect.
function p.isPrevSeasonLinkValid(frame)
--]]
function return p.checkNextSeasonisSeasonLinkValid(frame, -1)
return checkSeason(frame, 1)
end
 
--- Returns "true" if the season link for the previous or next season is valid; nil otherwise.
--[[
--- @param frame table The frame invoking the module.
Public function which is used to check if the previous season has
--- @return string | nil
a valid article or redirect.
function p.isPrevOrNextSeasonLinkValid(frame)
--]]
if p.isPrevSeasonLinkValid(frame) == "true" then
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
return p.isNextSeasonLinkValid(frame)
end
 
--- Returns the next season article title.
--[[
--- @param frame table The frame invoking the module.
Public function which is used to get the next season article title.
--- @return string
--]]
function p.getNextSeasonArticle(frame)
return getSeasonArticleLink(frame, 1)
end
 
--- Returns the previous season article title.
 
--- @param frame table The frame invoking the module.
--[[
--- @return string
Public function which is used to get the previous season article title.
--]]
function p.getPrevSeasonArticle(frame)
return getSeasonArticleLink(frame, -1)
end
 
--- Returns the type of season word used - "season" or "Seri".
--[[
--- @param frame table The frame invoking the module.
Public function which is used to get the type of season word used - "season" or "series".
--- @return string
--]]
function p.getSeasonWord(frame)
local title = getTitle(frame)
local disambiguation = getDisambiguation(title)
if (not disambiguation) then
local shortDisambiguation = getShortDisambiguation(disambiguation)
return getSeasonType(shortDisambiguation)
else
return ""
end
 
local shortDisambiguation = getShortDisambiguation(disambiguation)
local seasonType = getSeasonType(shortDisambiguation)
if seasonType == "spesials" then
seasonType = "season"
end
 
return seasonType
end
 
--- Returns the relevant text for the sub-header field.
--[[
---
Public function which is used to return the relevant text for the sub-header field.
--- The text is returned in the format of <code>"Season #</code>" or <code>Series"Seri #</code>",
--- depending on either what the article disambiguation uses, or on the manually entered parameters of the infobox.
--- @param frame table The frame invoking the module.
--]]
--- @return string | nil
function p.getInfoboxSubHeader(frame)
local getArgs = require('"Module:Arguments'").getArgs
local args = getArgs(frame)
 
local seasonType
local seasonNumber
if (args.season_number) then
seasonType = "Musim"
seasonNumber = args.season_number
elseif (args.series_number) then
seasonType = "SeriSeries"
seasonNumber = args.series_number
end
 
local title = getTitle(frame)
local showName = getShowName(title)
local disambiguation = getDisambiguation(title)
if (not seasonNumber and disambiguation) then
local shortDisambiguation = getShortDisambiguation(disambiguation)
 
seasonType = getSeasonType(shortDisambiguation)
if seasonType == "spesials" then
return shortDisambiguation
end
seasonType = seasonType:sub(1, 1):upper() .. seasonType:sub(2)
seasonNumber = getCurrentSeasonNumberFromDisambiguation(shortDisambiguation)
end
 
if (seasonNumber and seasonNumber ~= "") then
return seasonType .. " " .. seasonNumber
end
 
return nil
end
 
--- Returns a formatted link to the list of episodes article.
---
--- The returned link is in the style of:
--- [List of <series name> <disambiguation, if present> episodes <range, if present>|List of episodes]
---
--- The link will only return if the page exists.
--- @param frame table The frame invoking the module.
--- @return string | nil
function p.getListOfEpisodes(frame)
local getArgs = require("Module:Arguments").getArgs
local args = getArgs(frame)
 
if args.link then
-- Parameter should be unformatted.
if string.find(args.link, "%[") then
local delink = require("Module:Delink")._delink
args.link = delink({args.link, wikilinks = "target"})
end
 
return getListOfEpisodesLink(args.link)
end
 
local title = getTitle(frame)
local showName = getShowName(title)
 
if showName then
local disambiguation = getDisambiguation(title)
local TVProgramDisambiguation = getTVProgramDisambiguation(disambiguation)
local listOfEpisodesArticle = string.format("List of %s%s episodes", showName, TVProgramDisambiguation)
return getListOfEpisodesLink(listOfEpisodesArticle)
end
end