Module:GayPornographicCareerList

From Porn Base Central, the free encyclopedia of gay porn
Jump to navigation Jump to search

Overview

Module:GayPornographicCareerList is a Lua script designed to handle the rendering of gay pornographic career lists in a flexible, efficient, and centralized way. The module was developed to enhance the performance of template processing, eliminating the need for parser function templates like {{#if:}}, {{#ifeq:}}, {{#switch:}} and {{#expr:}}. Instead, all operations are performed within the module, using a language that was truly designed as a programming language, rather than a templating system that has had various extensions bolted on over time to try to make it into a programming language. Furthermore, the use of Lua eliminates the need for template transclusion into subsequent templates, which can result in hitting the transclusion depth limit. A fully Lua-using template never needs to include other templates.

The module reads data from companion modules, which are named after the first letter of the sites they contain (for example, 'Module:GayPornographicCareerList/A' for sites beginning with 'A').

Usage

This module is intended to be invoked from the Template:GayPornographicCareerList. For usage information, see that template's documentation.

Functions

p.site(frame)

This is the main function that Template:GayPornographicCareerList calls. It receives a 'frame' object from the template call, which contains arguments that specify the site to render in the list. These arguments can include an 'id', 'sceneid', 'alias', and 'notes'.

This function attempts to load the site data from the appropriate companion module. If the site data is loaded successfully, the function formats a table row string that represents the site in the list. The table row includes the site's label, a link to the site (or to its archive if it's no longer available), the performer's alias on the site, and any additional notes.

table.contains(table, element)

This is a helper function that checks if a specific 'element' is in the 'table'. It is used in 'getSite()' to check if the site's name matches an alias.

getSite(name)

This function receives a 'name' argument and tries to find the corresponding site in the appropriate companion module. If the site is found, the function returns the site's data as a table. If multiple matching sites are found, an error message is returned instead.

See also


local p = {}
local arg = ...
local i18n

local function loadI18n(aliasesP, frame)
    local title

    if frame then
        title = frame:getTitle()
    else
        title = arg
    end

    if not i18n then
        i18n = require(title .. "/i18n").init(aliasesP)
    end
end

function p.site(frame)
    loadI18n(nil, frame)

    local args = require('Module:Arguments').getArgs(frame, {wrappers = 'Template:GayPornographicCareerList'})

    local id = args.id2 or args.id
    local sceneid = args.sceneid2 or args.sceneid
    
    if not args.site or args.site == '' then
        return string.format('|-\n| colspan="4" style="text-align:center;vertical-align:middle;color:red;font-weight:bold;" | %s \n| style="width: 0px; padding: 0px; border: none;" |\n', i18n.errors.siteIsEmpty)
    end

    local site, error = getSite(args.site)

    if error then
        return string.format('|-\n| colspan="4" style="text-align:center;vertical-align:middle;color:red;font-weight:bold;" | %s \n| style="width: 0px; padding: 0px; border: none;" |\n', error)
    elseif site then
        local url = site.url or ''
        local isArchived = site.archived and (site.archived == 'yes' or site.archived == 'y')
        local archiveNotice = isArchived and " <small>(''Website archived'')</small>" or ''
        if isArchived and (not site.archiveURL or site.archiveURL == '') then
            return string.format('|-\n| colspan="4" style="text-align:center;vertical-align:middle;color:red;font-weight:bold;" | %s \n| style="width: 0px; padding: 0px; border: none;" |\n', i18n.errors.archiveURLMissing)
        end
        local transsexualIcon = ''
        
        if site.transsexual and (site.transsexual == 'yes' or site.transsexual == 'y') then
            transsexualIcon = '[[File:TranssexualIconPNG.png|13px|Transsexual content|link=]] '
        end
        
        if isArchived and site.archiveURL and site.archiveURL ~= '' then
            url = site.archiveURL
        elseif isArchived then
            url = ''
        else
            if sceneid then
                if args.sceneid2 and (site.sceneUrl2 or '') ~= '' then
                    url = site.sceneUrl2 .. sceneid .. (site.sceneUrl2suffix or '')
                elseif site.sceneUrl or '' ~= '' then
                    url = site.sceneUrl .. sceneid .. (site.suffix or '')
                end
            elseif id then
                if args.id2 and (site.modelUrl2 or '') ~= '' then
                    url = site.modelUrl2 .. id .. (site.modelUrl2suffix or '')
                elseif (site.modelUrl or '') ~= '' then
                    url = site.modelUrl .. id .. (site.suffixmodel or site.suffix or '')
                end
            end
        end

        local alias = args.alias or '<div style="font-size: 1em;">' .. mw.title.getCurrentTitle().text .. '</div>'
        local notes = args.notes and '<div style="font-size: 1em;">' .. args.notes .. '</div>' or '<div style="font-size: 1em;">N/A</div>'

        local networks = site.networks or {}
        local networkLinks = {}

        for _, network in pairs(networks) do
            local networkName = network.name
            local networkText = network.text and network.text ~= "" and network.text or networkName
            table.insert(networkLinks, string.format("[[:Category:%s|'''%s''']]", networkName, networkText))
        end

        local categoryLink = #networkLinks > 0 and string.format(" <small>(%s)</small>", table.concat(networkLinks, " / ")) or ""

        local siteType = site.sitetype or args[1]
        local style

        if siteType == nil or siteType == '' then
            siteType = 'Invalid type'
            style = 'background-color:#E7E7E7;text-align:center;color:red;font-weight:bold'
        elseif siteType:lower() == 'studio' then
            style = 'background-color:#DADADA;text-align:center;color:black;font-weight:bold'
        else
            style = 'background-color:#E7E7E7;text-align:center;color:black;font-weight:bold'
        end

        local linkOrLabel
        if url ~= '' then
            if site.idtonum and (site.idtonum == 'yes' or site.idtonum == 'y') and args.id then
                linkOrLabel = string.format('<span data-sort-value="%s">%s[%s %s #%s]%s</span>', site.label, transsexualIcon, url, site.label, args.id, archiveNotice)
            else
                linkOrLabel = string.format('<span data-sort-value="%s">%s[%s %s]%s</span>', site.label, transsexualIcon, url, site.label, archiveNotice)
            end
        else
            linkOrLabel = string.format('<span data-sort-value="%s">%s%s</span>', site.label, transsexualIcon, site.label .. archiveNotice)
        end

        if site.list and site.list ~= '' then
            linkOrLabel = linkOrLabel .. string.format(" ([[%s|'''LIST''']])", site.list)
        end

        local result = string.format(
            '|-\n| style="%s" | %s\n| style="text-align:center;vertical-align:middle;" | %s%s\n| style="text-align:center;vertical-align:middle;" | %s\n| style="text-align:center;vertical-align:middle;" | %s\n| style="width: 0px; padding: 0px; border: none;" |\n',
            style,
            siteType and siteType:gsub("^%l", string.upper) or 'Website',
            linkOrLabel,
            categoryLink,
            alias,
            notes
        )

        local ns = mw.title.getCurrentTitle().namespace
        if ns == 0 then
            if type(site.category) == "table" then
                for _, cat in pairs(site.category) do
                    if cat ~= '' then
                        result = result .. '[[Category:' .. cat .. ']]'
                    end
		        end
		    elseif site.category ~= '' then
		        result = result .. '[[Category:' .. site.category .. ']]'
		    end
		    result = result .. '[[Category:Articles in the mainspace using Template:GayPornographicCareerList]]'
		elseif ns == 2 then
		    result = result .. '[[Category:Articles in the userspace using Template:GayPornographicCareerList]]'
		elseif ns == 118 then
		    result = result .. '[[Category:Drafts using Template:GayPornographicCareerList]]'
		end

        return result
    else
        return string.format('|-\n| colspan="4" style="text-align:center;vertical-align:middle;color:red;font-weight:bold;" | %s \n| style="width: 0px; padding: 0px; border: none;" |\n', i18n.errors.siteIsInvalid)
    end
end

function table.contains(table, element)
  for _, value in pairs(table) do
    if value == element then
      return true
    end
  end
  return false
end

function getSite(name)
    local modulesToTry = {}

    local nameWithoutThe = name:lower():gsub("^the%s*", ""):gsub("%s*", "")
    if nameWithoutThe ~= name:lower() then
        table.insert(modulesToTry, nameWithoutThe:sub(1, 1):upper())
    end

    table.insert(modulesToTry, name:sub(1, 1):upper())

    for _, module in ipairs(modulesToTry) do
        local success, siteDictModule = pcall(require, 'Module:GayPornographicCareerList/' .. module)

        if success then
            local matchedSites = {}

            if siteDictModule.redirects and siteDictModule.redirects[name:lower()] then
                return getSite(siteDictModule.redirects[name:lower()])
            end

            for _, site in pairs(siteDictModule.sites) do
                if site.label:lower() == name:lower() or table.contains(site.aliases, name:lower()) then
                    table.insert(matchedSites, site)
                end
            end

            if #matchedSites > 1 then
                return nil, i18n.errors.duplicateSites
            elseif #matchedSites == 1 then
                return matchedSites[1]
            end
        else
            mw.log('Could not load module: ' .. 'Module:GayPornographicCareerList/' .. module)
        end
    end

    return nil, i18n.errors.invalidSite:format(name)
end

return p