Module:BioboxPersonalPages

Revision as of 00:07, 19 March 2025 by PeaceDeadC (talk | contribs) (Created page with "local p = {} local platformsModule = require('Module:BioboxPersonalPages/platforms') local platforms = platformsModule.platforms local platformOrder = platformsModule.platformOrder 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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}

local platformsModule = require('Module:BioboxPersonalPages/platforms')
local platforms = platformsModule.platforms
local platformOrder = platformsModule.platformOrder

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

local function formatFullWidthField(state, value, rowIndex)
    if isEmpty(value) then
        return ''
    end
    
    local rowStyle = state:getRowStyle(rowIndex)
    
    return string.format('|-\n| colspan="2" style="%s text-align:center;" | %s', 
        rowStyle,
        value)
end

function formatArchiveInfo(archiveUrl, archiveDate)
    if isEmpty(archiveUrl) then return "" end
    
    archiveUrl = archiveUrl:match("^%s*(.-)%s*$") or archiveUrl
    
    local hasProtocol = archiveUrl:match("^https?://")
    
    if not hasProtocol then
        archiveUrl = "https://archive.is/" .. archiveUrl
    end
    
    local result = string.format('<br><small>([%s Archive copy])', archiveUrl)
    if not isEmpty(archiveDate) then
        result = result .. string.format('<br>%s', archiveDate)
    end
    result = result .. '</small>'
    return result
end

function collectParametersWithSuffixes(args, paramBase, archiveBase, maxCount, altParams, altArchiveBases)
    local values = {}
    maxCount = maxCount or 4

    local mainParamUsed = false
    for i = 1, maxCount do
        local suffix = i > 1 and i or ""
        local paramName = paramBase .. suffix
        if not isEmpty(args[paramName]) then
            mainParamUsed = true
            break
        end
    end

    if mainParamUsed then
        for i = 1, maxCount do
            local suffix = i > 1 and i or ""
            local paramName = paramBase .. suffix
            local archiveParamName = archiveBase .. suffix
            if not isEmpty(args[paramName]) then
                local entry = {username = args[paramName]}
                if not isEmpty(args[archiveParamName]) then
                    entry.archive = args[archiveParamName]
                end
                table.insert(values, entry)
            end
        end
    else
        for j, altParam in ipairs(altParams or {}) do
            local altArchiveBase = altArchiveBases and altArchiveBases[j] or archiveBase
            for i = 1, maxCount do
                local suffix = i > 1 and i or ""
                local paramName = altParam .. suffix
                local archiveParamName = altArchiveBase .. suffix
                if not isEmpty(args[paramName]) then
                    local entry = {username = args[paramName]}
                    if not isEmpty(args[archiveParamName]) then
                        entry.archive = args[archiveParamName]
                    end
                    table.insert(values, entry)
                end
            end
        end
    end

    return values
end

function p.formatPlatform(state, args, platformConfig, rowIndex)
    local section = {}
    local values = {}

    if platformConfig.paramBase then
        values = collectParametersWithSuffixes(
            args,
            platformConfig.paramBase,
            platformConfig.archiveBase,
            platformConfig.maxCount,
            platformConfig.altParams,
            platformConfig.altArchiveBases
        )
    elseif platformConfig.types then
        for _, typeConfig in ipairs(platformConfig.types) do
            local typeValues = collectParametersWithSuffixes(
                args,
                typeConfig.paramBase,
                typeConfig.archiveBase,
                typeConfig.maxCount,
                typeConfig.altParams,
                typeConfig.altArchiveBases
            )
            for _, val in ipairs(typeValues) do
                val.typeConfig = typeConfig
                table.insert(values, val)
            end
        end
    end

    if #values == 0 and args.qid then
        local entity = mw.wikibase.getEntity(args.qid)
        if platformConfig.pbdProp and entity and entity.claims and entity.claims[platformConfig.pbdProp] then
            for _, statement in ipairs(entity.claims[platformConfig.pbdProp]) do
                if statement.mainsnak.snaktype == "value" and 
                   statement.mainsnak.datavalue and 
                   statement.mainsnak.datavalue.type == "string" then
                    local username = statement.mainsnak.datavalue.value
                    local entry = {username = username, statement = statement}
                    if statement.qualifiers and statement.qualifiers.P208 then
                        entry.archive = statement.qualifiers.P208[1].datavalue.value
                    end
                    if statement.qualifiers and statement.qualifiers.P227 then
                        local timeValue = statement.qualifiers.P227[1].datavalue.value
                        if timeValue and timeValue.time then
                            local year, month, day = timeValue.time:match("^[+%-](%d%d%d%d)-(%d%d)-(%d%d)")
                            if year and month and day then
                                entry.archiveDate = day .. " " .. os.date("%B", os.time({year=2000, month=tonumber(month), day=1})) .. " " .. year
                            end
                        end
                    end
                    table.insert(values, entry)
                end
            end
        elseif platformConfig.types then
            for _, typeConfig in ipairs(platformConfig.types) do
                if typeConfig.pbdProp and entity and entity.claims and entity.claims[typeConfig.pbdProp] then
                    for _, statement in ipairs(entity.claims[typeConfig.pbdProp]) do
                        if statement.mainsnak.snaktype == "value" and 
                           statement.mainsnak.datavalue and 
                           statement.mainsnak.datavalue.type == "string" then
                            local username = statement.mainsnak.datavalue.value
                            local entry = {username = username, statement = statement, typeConfig = typeConfig}
                            if statement.qualifiers and statement.qualifiers.P208 then
                                entry.archive = statement.qualifiers.P208[1].datavalue.value
                            end
                            if statement.qualifiers and statement.qualifiers.P227 then
                                local timeValue = statement.qualifiers.P227[1].datavalue.value
                                if timeValue and timeValue.time then
                                    local year, month, day = timeValue.time:match("^[+%-](%d%d%d%d)-(%d%d)-(%d%d)")
                                    if year and month and day then
                                        entry.archiveDate = day .. " " .. os.date("%B", os.time({year=2000, month=tonumber(month), day=1})) .. " " .. year
                                    end
                                end
                            end
                            table.insert(values, entry)
                        end
                    end
                end
            end
        end
    end

    if #values > 0 then
        local displayValues = {}
        for _, entry in ipairs(values) do
            local urlFormat = platformConfig.urlFormat or (entry.typeConfig and entry.typeConfig.urlFormat)
            local displayValue
            if platformConfig.isFullWidth then
                displayValue = string.format('[%s %s]', urlFormat:format(entry.username), platformConfig.label)
            else
                displayValue = string.format('[%s %s]', urlFormat:format(entry.username), entry.username)
            end
            if platformConfig.extraFormatFunc then
                displayValue = platformConfig.extraFormatFunc(displayValue, entry)
            end
            if entry.archive then
                displayValue = displayValue .. formatArchiveInfo(entry.archive, entry.archiveDate)
            end
            table.insert(displayValues, displayValue)
        end
        local combinedDisplay = table.concat(displayValues, '<br>')

        if platformConfig.category then
            local isMainNamespace = mw.title.getCurrentTitle().namespace == 0
            if isMainNamespace then
                combinedDisplay = combinedDisplay .. '[[Category:' .. platformConfig.category .. '|' .. mw.title.getCurrentTitle().text .. ']]'
            end
        end

        if platformConfig.isFullWidth then
            section[#section + 1] = formatFullWidthField(state, combinedDisplay, rowIndex)
        else
            local label
            if platformConfig.labelFunc then
                label = platformConfig.labelFunc()
            else
                label = platformConfig.label
                if platformConfig.icon then
                    label = '[[File:' .. platformConfig.icon .. '|16px|alt=' .. platformConfig.label .. '|link=]] ' .. label .. ':'
                end
            end
            section[#section + 1] = formatField(state, label, combinedDisplay, platformConfig.name, rowIndex)
        end
        rowIndex = rowIndex + 1
    end

    return section, rowIndex
end

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

    for _, platformName in ipairs(platformOrder) do
        local config = platforms[platformName]
        if config then
            local platformSection, newRowIndex = p.formatPlatform(state, args, config, rowIndex)
            if #platformSection > 0 then
                hasAny = true
                for _, line in ipairs(platformSection) do
                    platformSections[#platformSections + 1] = line
                end
                rowIndex = newRowIndex
            end
        end
    end

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

    return section, rowIndex
end

return p