Content deleted Content added
Created page with "local p = {} local arg = ... local i18n function p.channel(frame) local args = require('Module:Arguments').getArgs(frame, {wrappers = 'Template:TVChannelList'}) if not args.channel or args.channel == '' then return string.format('|-\n| colspan="6" style="text-align:center;vertical-align:middle;color:red;font-weight:bold;" | Channel name is empty\n') end local channel, error = getChannel(args.channel) if error then return string..."
 
No edit summary
Line 1:
-- This module handles the display of television network and streaming service information
local p = {}
local arg = ...
local i18n
 
-- Initialize i18n data for error messages
local function loadI18n()
if not i18n then
i18n = {
errors = {
channelIsEmpty = "Channel name is empty",
channelNotFound = "Channel '%s' not found",
duplicateChannels = "Multiple channels found with this name",
moduleLoadError = "Could not load channel data module"
}
}
end
end
 
-- Helper function to check if a table contains a value
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
 
-- Main function to handle channel information display
function p.channel(frame)
loadI18n()
-- Get template arguments
local args = require('Module:Arguments').getArgs(frame, {wrappers = 'Template:TVChannelList'})
-- Check if channel parameter is provided
if not args.channel or args.channel == '' then
return string.format('|-\n| colspan="6" style="text-align:center;vertical-align:middle;color:red;font-weight:bold;" | Channel name is empty%s\n'),
i18n.errors.channelIsEmpty)
end
 
-- Get channel data
local channel, error = getChannel(args.channel)
 
if error then
return string.format('|-\n| colspan="6" style="text-align:center;vertical-align:middle;color:red;font-weight:bold;" | %s\n', error)
error)
elseif channel then
-- Format channel data, prioritizing template arguments over module data
local channelType = channelargs.type or argschannel.type or 'Channel'
local genre = channelargs.genre or argschannel.genre or 'N/A'
local alias = args.alias or channel.alias or 'N/A'
local notes = args.notes or channel.notes or 'N/A'
Line 35 ⟶ 68:
local platformsStr = #platforms > 0 and table.concat(platforms, ' & ') or 'N/A'
 
-- Format the row with gathered data
local result = string.format(
'|-\n| style="text-align:center;vertical-align:middle;" | %s\n| style="text-align:center;vertical-align:middle;" | %s\n| style="text-align:center;vertical-align:middle;" | %s\n| style="text-align:center;vertical-align:middle;" | %s\n| style="text-align:center;vertical-align:middle;" | %s\n| style="text-align:center;vertical-align:middle;" | %s\n',
Line 49 ⟶ 82:
local ns = mw.title.getCurrentTitle().namespace
if ns == 0 then
-- Handle multiple categories
if type(channel.category) == "table" then
for _, cat in pairs(channel.category) do
Line 55 ⟶ 89:
end
end
-- Handle single category
elseif channel.category and channel.category ~= '' then
result = result .. '[[Category:' .. channel.category .. ']]'
end
-- Add default category
result = result .. '[[Category:Television Networks]]'
elseif ns == 2 then
result = result .. '[[Category:User pages using TVChannelList]]'
elseif ns == 118 then
result = result .. '[[Category:Draft pages using TVChannelList]]'
end
 
return result
else
return string.format('|-\n| colspan="6" style="text-align:center;vertical-align:middle;color:red;font-weight:bold;" | Invalid channel: %s\n', args.channel)
string.format(i18n.errors.channelNotFound, args.channel))
end
end
 
-- Function to find and retrieve channel data from appropriate module
function getChannel(name)
local modulesToTry = {}
-- Handle "The" prefix in channel names
local nameWithoutThe = name:lower():gsub("^the%s*", ""):gsub("%s*", "")
if nameWithoutThe ~= name:lower() then
table.insert(modulesToTry, nameWithoutThe:sub(1, 1):upper())
end
-- Add original first letter to modules to try
table.insert(modulesToTry, name:sub(1, 1):upper())
for _, module in ipairs(modulesToTry) do
local success, channelModule = pcall(require, 'Module:TVChannelList/' .. module)
if success then
local matchedChannels = {}
-- Check redirects first
if channelModule.redirects and channelModule.redirects[name:lower()] then
return getChannel(channelModule.redirects[name:lower()])
end
-- Look for matches in channel names and aliases
for _, channel in pairs(channelModule.channels) do
if channel.name:lower() == name:lower() or
(channel.aliases and table.contains(channel.aliases, name:lower())) then
table.insert(matchedChannels, channel)
end
end
-- Handle multiple matches
if #matchedChannels > 1 then
return nil, i18n.errors.duplicateChannels
elseif #matchedChannels == 1 then
return matchedChannels[1]
end
else
mw.log('Module load failed: Module:TVChannelList/' .. module)
end
end
return nil, string.format(i18n.errors.channelNotFound, name)
end
 
return p