Module:TVChannelList

From Porn Base Central
Revision as of 01:58, 17 February 2025 by PeaceDeadC (talk | contribs)
Jump to navigation Jump to search

Documentation for this module may be created at Module:TVChannelList/doc

-- 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;" | %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)
    elseif channel then
        -- Format channel data, prioritizing template arguments over module data
        local channelType = args.type or channel.type or 'Channel'
        local genre = args.genre or channel.genre or 'N/A'
        local alias = args.alias or channel.alias or 'N/A'
        local notes = args.notes or channel.notes or 'N/A'
        
        -- Handle multiple platforms/websites
        local platforms = {}
        if channel.website and channel.website ~= '' then
            table.insert(platforms, string.format('[%s Website]', channel.website))
        end
        if channel.xvideos and channel.xvideos ~= '' then
            table.insert(platforms, string.format('[%s Xvideos Channel]', channel.xvideos))
        end
        if channel.streaming and channel.streaming ~= '' then
            table.insert(platforms, string.format('[%s Streaming]', channel.streaming))
        end
        
        local platformsStr = #platforms > 0 and table.concat(platforms, ' & ') or 'N/A'

        -- Format 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',
            channel.name,
            channelType,
            platformsStr,
            genre,
            alias,
            notes
        )

        -- Add categories if in main namespace
        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
                    if cat ~= '' then
                        result = result .. '[[Category:' .. cat .. ']]'
                    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;" | %s\n', 
            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