Module:BioboxDatabases

From Porn Base Central
Jump to navigation Jump to search

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

local p = {}

local databasesModule = require('Module:BioboxDatabases/databases')
local databases = databasesModule.databases
local databaseOrder = databasesModule.databaseOrder

local PBD = require('Module:PBD')
local PBDIB = require('Module:PornBaseDataIB')

local function isEmpty(value)
    return value == nil or value == ''
end

local function formatField(state, label, value, fieldName, rowIndex)
    if isEmpty(value) then
        return ''
    end
    
    local rowStyle = state:getRowStyle(rowIndex)
    
    return string.format('|-\n! style="%s %s" | \'\'\'%s\'\'\'\n| style="%s text-align:left; max-width: 100px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;" | %s', 
        rowStyle,
        state.STYLES.LABEL,
        label,
        rowStyle,
        value)
end

function collectParametersWithSuffixes(args, paramBase, maxCount)
    local values = {}
    maxCount = tonumber(maxCount) or 4

    for i = 1, maxCount do
        local suffix = i > 1 and i or ""
        local paramName = paramBase .. suffix
        if not isEmpty(args[paramName]) then
            table.insert(values, args[paramName])
        end
    end

    return values
end

function p.formatDatabase(state, args, databaseConfig, rowIndex)
    local section = {}
    local values = {}

    if databaseConfig.paramBase then
        values = collectParametersWithSuffixes(
            args,
            databaseConfig.paramBase,
            databaseConfig.maxCount
        )
    end

    if #values == 0 and args.qid then
        local entity = mw.wikibase.getEntity(args.qid)
        if databaseConfig.pbdProps then
            for _, pbdProp in ipairs(databaseConfig.pbdProps) do
                if entity and entity.claims and entity.claims[pbdProp] then
                    if databaseConfig.name == "iafd" and #values > 0 and pbdProp == "P169" then
                        break
                    end
                    
                    for _, statement in ipairs(entity.claims[pbdProp]) do
                        if statement.mainsnak.snaktype == "value" and 
                           statement.mainsnak.datavalue then
                            local id
                            if statement.mainsnak.datavalue.type == "string" then
                                id = statement.mainsnak.datavalue.value
                            elseif statement.mainsnak.datavalue.type == "wikibase-entityid" then
                                id = statement.mainsnak.datavalue.value.id
                            end
                            
                            if id then
                                if pbdProp == "P16" and not id:match("^nm") then
                                    id = "nm" .. id
                                end
                                
                                table.insert(values, {id = id, pbdProp = pbdProp})
                            end
                        end
                    end
                end
            end
        end
    end

    if #values > 0 then
        local displayValues = {}
        
        for _, entry in ipairs(values) do
            local id, pbdProp
            if type(entry) == "table" then
                id = entry.id
                pbdProp = entry.pbdProp
            else
                id = entry
            end
            
            local urlFormat = databaseConfig.urlFormat
            
            if databaseConfig.name == "iafd" then
                if id:match("^%x+%-%x+%-%x+%-%x+%-%x+$") then
                    urlFormat = "https://www.iafd.com/person.rme/id=%s"
                else
                    urlFormat = "https://www.iafd.com/person.rme/perfid=%s/gender=m"
                end
            end
            
            if databaseConfig.name == "imdb" then
                if not id:match("^nm%d+$") then
                    id = "nm" .. id
                end
            end
            
            local displayValue = string.format('[%s %s]', urlFormat:format(id), id:gsub("^nm", ""))
            table.insert(displayValues, displayValue)
        end
        
        local combinedDisplay = table.concat(displayValues, '<br>')

        local label
        if databaseConfig.labelFunc then
            label = databaseConfig.labelFunc()
        else
            local frame = mw.getCurrentFrame()
            label = frame:expandTemplate{ title = 'abbr', args = { databaseConfig.abbr, databaseConfig.label } }
        end
        
        section[#section + 1] = formatField(state, label, combinedDisplay, databaseConfig.name, rowIndex)
        rowIndex = rowIndex + 1
    end

    return section, rowIndex
end

function p.formatDatabasesSection(state, args, rowIndex)
    local section = {}
    local databaseSections = {}
    local hasAny = false

    for _, databaseName in ipairs(databaseOrder) do
        local config = databases[databaseName]
        if config then
            local databaseSection, newRowIndex = p.formatDatabase(state, args, config, rowIndex)
            if #databaseSection > 0 then
                hasAny = true
                for _, line in ipairs(databaseSection) do
                    databaseSections[#databaseSections + 1] = line
                end
                rowIndex = newRowIndex
            end
        end
    end

    if hasAny then
        section[#section + 1] = string.format('|-\n! colspan="2" %s | \'\'\'Databases\'\'\'', state.STYLES.SECTION_HEADER)
        for _, line in ipairs(databaseSections) do
            section[#section + 1] = line
        end
    end

    return section, rowIndex
end

return p