Module:BioboxPersonal

Revision as of 00:05, 19 March 2025 by PeaceDeadC (talk | contribs) (Created page with "local p = {} local PBD = require('Module:PBD') local PBDIB = require('Module:PornBaseDataIB') local GetBirthDate = require('Module:GetBirthDate') local BirthDateAndAge = require('Module:Birth date and age') local Pornbasedata = require('Module:Pornbasedata') local CategoryMapping = require('Module:BioboxCategoryMapping') local function isEmpty(value) return value == nil or value == '' end local function isPositive(value) if isEmpty(value) then return false end...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}

local PBD = require('Module:PBD')
local PBDIB = require('Module:PornBaseDataIB')
local GetBirthDate = require('Module:GetBirthDate')
local BirthDateAndAge = require('Module:Birth date and age')
local Pornbasedata = require('Module:Pornbasedata')
local CategoryMapping = require('Module:BioboxCategoryMapping')

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

local function isPositive(value)
    if isEmpty(value) then return false end
    value = tostring(value):lower()
    local positiveValues = {
        ['yes'] = true,
        ['y'] = true,
        ['ya'] = true,
        ['yep'] = true,
        ['true'] = true,
        ['1'] = true
    }
    return positiveValues[value] or false
end

local function getPBDValue(propertyId, args, directValue)
    if isEmpty(directValue) then
        return PBD._property{propertyId, 'raw', 'multilanguage', ['eid'] = args.qid}
    end
    return directValue
end

local function processCountryValue(value)
    if not value then return nil end
    
    value = mw.text.trim(value)
    
    if value == '' then return nil end
    
    value = value:gsub('^{{', ''):gsub('}}$', '')
    value = mw.text.trim(value)
    
    return string.format('{{flagcountry|%s}}', value)
end

local function calculateAge(birthYear, birthMonth, birthDay, deathYear, deathMonth, deathDay)
    birthYear = tonumber(birthYear)
    deathYear = tonumber(deathYear)
    birthMonth = tonumber(birthMonth)
    deathMonth = tonumber(deathMonth)
    birthDay = tonumber(birthDay)
    deathDay = tonumber(deathDay)
    
    if birthYear and deathYear and birthYear > 0 and deathYear > 0 then
        local age = deathYear - birthYear
        if birthMonth and deathMonth then
            if deathMonth < birthMonth or 
               (deathMonth == birthMonth and birthDay and deathDay and deathDay < birthDay) then
                age = age - 1
            end
        end
        if age >= 0 then
            return string.format(" (aged %d)", age)
        end
    end
    return ""
end

local function getImage(args)
    if not isEmpty(args.image) then
        return string.format('[[File:%s|256px]]', args.image)
    end

    local qid = args.qid
    if isEmpty(qid) then
        qid = mw.wikibase.getEntityIdForCurrentPage()
    end
    
    if qid then
        local image = PBDIB._getValue({
            [1] = "P470",
            qid = qid,
            rank = "best",
            name = "image",
            fpbd = "ALL",
            osd = "no",
            noicon = "yes",
            maxvals = 1
        })
        
        if not isEmpty(image) then
            return string.format([[%s
            <div style="text-align: center;">%s</div>]], 
            mw.ustring.format(
                '[[File:%s|256px]]',
                image
            ),
            PBDIB._getValue({
                qid = qid,
                fpbd = "ALL",
                osd = "no",
                noicon = "y",
                qual = "P523",
                qualsonly = "yes",
                maxvals = 1
            }) or ''
        )
        end
    end

    return '[[File:NoImageAvailable.png|256px|link=PBC:Images]]'
end

function p.formatBirthDate(state, args, rowIndex)
    local section = {}
    
    local birthDay = args.birth_day ~= "" and args.birth_day or nil
    local birthMonth = args.birth_month ~= "" and args.birth_month or nil
    local birthYear = args.birth_year ~= "" and args.birth_year or nil
    local birthRef = args.birth_ref
    
    local hasDeathDate = false
    if (args.death_year and args.death_year ~= "")
       or (args.death_month and args.death_month ~= "")
       or (args.death_day and args.death_day ~= "")
    then
        hasDeathDate = true
    elseif args.qid then
        local deathDate = PBD._property{
            [1] = 'P247',
            raw = true,
            multilanguage = true,
            ['eid'] = args.qid
        }
        hasDeathDate = deathDate and deathDate ~= ""
    end

    if not (birthDay or birthMonth or birthYear) and args.qid then
        local birthDate = PBD._property{
            [1] = 'P9',
            raw = true,
            multilanguage = true,
            ['eid'] = args.qid
        }
        
        if birthDate and birthDate ~= "" then
            local frame = mw.getCurrentFrame()
            
            birthDay = GetBirthDate.property(frame:newChild{ args = {
                [1] = 'P9',
                [2] = 'day'  
            }})
            
            birthMonth = GetBirthDate.property(frame:newChild{ args = {
                [1] = 'P9', 
                [2] = 'month'
            }})
            
            birthYear = GetBirthDate.property(frame:newChild{ args = {
                [1] = 'P9',
                [2] = 'year'
            }})
            
            birthDay = birthDay ~= "" and tonumber(birthDay) or nil
            birthMonth = birthMonth ~= "" and tonumber(birthMonth) or nil  
            birthYear = birthYear ~= "" and tonumber(birthYear) or nil
        end
    end

    if not (birthDay or birthMonth or birthYear) then
        return {}, rowIndex
    end

    local birthDateDisplay = ""
    local categories = {}
    local isMainNamespace = mw.title.getCurrentTitle().namespace == 0

    if birthMonth and birthDay and birthYear then
        local frame = mw.getCurrentFrame()
        local success, result = pcall(function()
            local monthTime = {year = 2000, month = tonumber(birthMonth), day = 1}
            local monthName = os.date('%B', os.time(monthTime))
            
            if isMainNamespace then
                categories[#categories + 1] = string.format("[[Category:%s_%s_birthdays]]", monthName, birthDay)
                categories[#categories + 1] = string.format("[[Category:%s_births]]", birthYear)
            end

            if hasDeathDate then
                local basicDate = string.format("[[:Category:%s %s birthdays|%s %s]] [[:Category:%s births|%s]]",
                    monthName, birthDay,
                    birthDay, monthName,
                    birthYear, birthYear
                )
                
                local childFrame = frame:newChild{
                    args = { birthYear, birthMonth, birthDay }
                }
                local zodiacResult = BirthDateAndAge.main(childFrame)
                local zodiacPart = zodiacResult:match("<br>Astrology:.*$")
                
                return basicDate .. (zodiacPart or "")
            else
                local childFrame = frame:newChild{
                    args = { birthYear, birthMonth, birthDay }
                }
                return BirthDateAndAge.main(childFrame)
            end
        end)
        
        if success and result ~= "" then
            birthDateDisplay = result .. table.concat(categories, "")
            
            section[#section + 1] = string.format('|-\n! style="%s %s" | \'\'\'Birth:\'\'\'\n| style="text-align:left;" | %s',
                state:getRowStyle(rowIndex),
                state.STYLES.LABEL,
                birthDateDisplay
            )
            rowIndex = rowIndex + 1
            return section, rowIndex
        end
        
        local monthTime = {year = 2000, month = tonumber(birthMonth), day = 1}
        local monthName = os.date('%B', os.time(monthTime))
        birthDateDisplay = string.format("[[:Category:%s %s birthdays|%s %s]] [[:Category:%s births|%s]]",
            monthName, birthDay,
            birthDay, monthName,
            birthYear, birthYear
        )
        
        if isMainNamespace then
            categories[#categories + 1] = string.format("[[Category:%s_%s_birthdays]]", monthName, birthDay)
            categories[#categories + 1] = string.format("[[Category:%s_births]]", birthYear)
        end
        
        local zodiacSuccess, zodiacResult = pcall(function()
            return BirthDateAndAge.main{[1]=birthYear, [2]=birthMonth, [3]=birthDay}
        end)
        
        if zodiacSuccess then
            local zodiacPart = zodiacResult:match("<br>Astrology:.*$")
            if zodiacPart then
                birthDateDisplay = birthDateDisplay .. zodiacPart
            end
        end
    
    elseif birthMonth and birthYear then
        local ok, monthName
        local monthTime = {year = 2000, month = tonumber(birthMonth), day = 1}
        ok, monthName = pcall(os.date, '%B', os.time(monthTime))
        
        if ok and monthName then
            birthDateDisplay = string.format("[[:Category:%s birthdays|%s]] [[:Category:%s births|%s]]%s", 
                monthName,
                monthName,
                birthYear,
                birthYear,
                not hasDeathDate and string.format(" (≈%d years)", os.date('%Y') - birthYear) or ""
            )
            if isMainNamespace then
                categories[#categories + 1] = string.format("[[Category:%s births]]", birthYear)
                categories[#categories + 1] = string.format("[[Category:%s birthdays]]", monthName)
            end
        end
    
    elseif birthYear then
        local appendAge = not hasDeathDate and string.format(" (≈%d years)", os.date('%Y') - birthYear) or ""
        
        birthDateDisplay = string.format("[[:Category:%s births|%s]]%s",
            birthYear,
            birthYear,
            appendAge
        )
        if isMainNamespace then
            categories[#categories + 1] = string.format("[[Category:%s births]]", birthYear)
        end
    
    elseif birthMonth and birthDay then
        local monthTime = {year = 2000, month = birthMonth, day = 1}
        local ok, monthName = pcall(os.date, '%B', os.time(monthTime))
        
        if ok and monthName then
            birthDateDisplay = string.format("[[:Category:%s %s birthdays|%s %s]]", 
                monthName, birthDay,
                monthName, birthDay
            )
            if isMainNamespace then
                categories[#categories + 1] = "[[Category:Birth year needed]]"
                categories[#categories + 1] = string.format("[[Category:%s_%s_birthdays]]", monthName, birthDay)
            end
        end
    
        do
            local frame = mw.getCurrentFrame()
            local childFrame = frame:newChild{
                args = {
                    [1] = "",
                    [2] = birthMonth,
                    [3] = birthDay
                }
            }
            local success, zodiacResult = pcall(BirthDateAndAge.main, childFrame)
            if success and type(zodiacResult) == "string" and zodiacResult ~= "" then
                local zodiacPart = zodiacResult:match("<br>Astrology:.*$")
                if zodiacPart then
                    birthDateDisplay = birthDateDisplay .. zodiacPart
                end
            end
        end
    end

    if birthDateDisplay ~= "" then
        if birthRef then
            birthDateDisplay = birthDateDisplay .. birthRef
        end
        
        section[#section + 1] = string.format('|-\n! style="%s %s" | \'\'\'Birth:\'\'\'\n| style="text-align:left;" | %s',
            state:getRowStyle(rowIndex),
            state.STYLES.LABEL,
            birthDateDisplay .. table.concat(categories, '')
        )
        rowIndex = rowIndex + 1
    end
    
    return section, rowIndex
end

local function formatPlaceString(placeString)
    if not placeString or placeString == '' then
        return placeString
    end
    
    return placeString:gsub(',%s*', ', ')
end

function p.formatPlaceOfBirth(state, args, rowIndex)
    local section = {}
    local categories = {}
    
    local placeOfBirth = args.place_of_birth
    
    if isEmpty(placeOfBirth) and args.qid then
        local birthplaces = {}
        local entity = mw.wikibase.getEntity(args.qid)
        if entity and entity.claims and entity.claims.P20 then
            for _, statement in pairs(entity.claims.P20) do
                if statement.mainsnak.datavalue and 
                   statement.mainsnak.datavalue.value and 
                   statement.mainsnak.datavalue.value.id then
                    local placeId = statement.mainsnak.datavalue.value.id
                    local sitelink = mw.wikibase.getSitelink(placeId)
                    if sitelink and mw.ustring.match(sitelink, '^' .. mw.site.namespaces[14].name .. ':') then
                        table.insert(categories, '[[' .. sitelink .. ']]')
                    end
                end
            end
        end

        local frame = mw.getCurrentFrame()
        local newFrame = frame:newChild{
            args = {
                property = 'P20',
                entityId = args.qid,
                references = 'yes',
                separator = ', '
            }
        }
        
        local formatResult = Pornbasedata.formatProperty(newFrame)
        if not isEmpty(formatResult) then
            placeOfBirth = formatResult
        end
    end
    
    if not isEmpty(placeOfBirth) then
        placeOfBirth = formatPlaceString(placeOfBirth)
        
        local rowStyle = state:getRowStyle(rowIndex)
        section[#section + 1] = string.format(
            '|-\n! style="%s %s" | \'\'\'Place of birth:\'\'\'\n| style="%s text-align:left;" | %s%s',
            rowStyle,
            state.STYLES.LABEL,
            rowStyle,
            placeOfBirth,
            table.concat(categories, '')
        )
        rowIndex = rowIndex + 1
    end
    
    return section, rowIndex
end

function p.formatBirthCountry(state, args, rowIndex)
    local section = {}
    local birthCountry = args.birth_country
    
    if isEmpty(birthCountry) and args.qid then
        birthCountry = PBD._property{
            [1] = 'P144',
            raw = true,
            multilanguage = true,
            ['eid'] = args.qid
        }
        
        if not isEmpty(birthCountry) then
            birthCountry = birthCountry:match("^[^,\n]+")
        end
    end
    
    local displayValue = processCountryValue(birthCountry)
    
    if displayValue then
        local frame = mw.getCurrentFrame()
        local expandedDisplayValue = frame:preprocess(displayValue)

        section[#section + 1] = state:formatField(
            "Country of birth:",
            expandedDisplayValue,
            "birth_country",
            rowIndex
        )
        rowIndex = rowIndex + 1
    end
    
    return section, rowIndex
end

function p.formatCitizenship(state, args, rowIndex)
    local section = {}
    
    local citizenshipValues = {}
    if not isEmpty(args.citizenship) then
        table.insert(citizenshipValues, args.citizenship)
    end
    
    for i = 2, 4 do
        local param = "citizenship" .. i
        if not isEmpty(args[param]) then
            table.insert(citizenshipValues, args[param])
        end
    end
    
    if #citizenshipValues == 0 and args.qid then
        local pbdValues = PBD._properties{
            [1] = 'P133',
            raw = true,
            multilanguage = true,
            sep = '\n',
            ['eid'] = args.qid
        }
        
        if not isEmpty(pbdValues) then
            for value in pbdValues:gmatch("[^,\n]+") do
                table.insert(citizenshipValues, mw.text.trim(value))
            end
        end
    end
    
    local displayValues = {}
    for _, value in ipairs(citizenshipValues) do
        local processed = processCountryValue(value)
        if processed then
            table.insert(displayValues, processed)
        end
    end
    
    if #displayValues > 0 then
        local concatenated = table.concat(displayValues, "<br>")
        
        local frame = mw.getCurrentFrame()
        local expandedDisplayValue = frame:preprocess(concatenated)
        
        section[#section + 1] = state:formatField(
            "Citizenship:",
            expandedDisplayValue,
            "citizenship",
            rowIndex
        )
        rowIndex = rowIndex + 1
    end
    
    return section, rowIndex
end

function p.formatDeathDate(state, args, rowIndex)
    local section = {}
    
    local deathDay = args.death_day ~= "" and args.death_day or nil
    local deathMonth = args.death_month ~= "" and args.death_month or nil
    local deathYear = args.death_year ~= "" and args.death_year or nil
    local deathRef = args.death_ref
    
    local birthYear = args.birth_year ~= "" and args.birth_year or nil
    local birthMonth = args.birth_month ~= "" and args.birth_month or nil
    local birthDay = args.birth_day ~= "" and args.birth_day or nil

    if deathYear and tonumber(deathYear) and tonumber(deathYear) <= 0 then
        deathYear = nil
    end

    local hasPBDDeathDate = false
    if not (deathDay or deathMonth or deathYear) and args.qid then
        local deathDate = PBD._property{
            [1] = 'P247',
            raw = true,
            multilanguage = true,
            ['eid'] = args.qid
        }
        
        if deathDate and deathDate ~= "" then
            hasPBDDeathDate = true
            local frame = mw.getCurrentFrame()
            
            local pdbDeathDay = GetBirthDate.property(frame:newChild{ args = {
                [1] = 'P247',
                [2] = 'day'
            }})
            
            local pdbDeathMonth = GetBirthDate.property(frame:newChild{ args = {
                [1] = 'P247',
                [2] = 'month'
            }})
            
            local pdbDeathYear = GetBirthDate.property(frame:newChild{ args = {
                [1] = 'P247',
                [2] = 'year'
            }})
            
            if pdbDeathDay ~= "" then deathDay = tonumber(pdbDeathDay) end
            if pdbDeathMonth ~= "" then deathMonth = tonumber(pdbDeathMonth) end
            if pdbDeathYear ~= "" then deathYear = tonumber(pdbDeathYear) end
            
            if not birthYear or not birthMonth or not birthDay then
                local birthDate = PBD._property{
                    [1] = 'P9',
                    raw = true,
                    multilanguage = true,
                    ['eid'] = args.qid
                }
                
                if birthDate and birthDate ~= "" then
                    if not birthYear then
                        local pdbBirthYear = GetBirthDate.property(frame:newChild{ args = {
                            [1] = 'P9',
                            [2] = 'year'
                        }})
                        if pdbBirthYear ~= "" then birthYear = tonumber(pdbBirthYear) end
                    end
                    
                    if not birthMonth then
                        local pdbBirthMonth = GetBirthDate.property(frame:newChild{ args = {
                            [1] = 'P9',
                            [2] = 'month'
                        }})
                        if pdbBirthMonth ~= "" then birthMonth = tonumber(pdbBirthMonth) end
                    end
                    
                    if not birthDay then
                        local pdbBirthDay = GetBirthDate.property(frame:newChild{ args = {
                            [1] = 'P9',
                            [2] = 'day'
                        }})
                        if pdbBirthDay ~= "" then birthDay = tonumber(pdbBirthDay) end
                    end
                end
            end
        end
    end

    if not (deathDay or deathMonth or deathYear) and not hasPBDDeathDate then
        return {}, rowIndex
    end

    local deathDateDisplay = ""
    local categories = {}
    local isMainNamespace = mw.title.getCurrentTitle().namespace == 0

    if isMainNamespace then
        categories[#categories + 1] = "[[Category:Dead people]]"
    end
    
    local ageAtDeath = calculateAge(birthYear, birthMonth, birthDay, deathYear, deathMonth, deathDay)
    
    if deathMonth and deathDay and deathYear then
        local ok, monthName = pcall(os.date, '%B', os.time({year = 2000, month = tonumber(deathMonth), day = 1}))
        if ok and monthName then
            deathDateDisplay = string.format("[[:Category:%s %s deaths|%s %s]] [[:Category:%s deaths|%s]]%s",
                monthName, deathDay,
                deathDay, monthName,
                deathYear, deathYear,
                ageAtDeath
            )
            if isMainNamespace then
                categories[#categories + 1] = string.format("[[Category:%s deaths]]", deathYear)
                categories[#categories + 1] = string.format("[[Category:%s_%s_deaths]]", monthName, deathDay)
            end
        end
    
    elseif deathMonth and deathYear then
        local ok, monthName = pcall(os.date, '%B', os.time({year = 2000, month = tonumber(deathMonth), day = 1}))
        if ok and monthName then
            deathDateDisplay = string.format("[[:Category:%s deaths|%s]] [[:Category:%s deaths|%s]]%s",
                monthName,
                monthName,
                deathYear, deathYear,
                ageAtDeath
            )
            if isMainNamespace then
                categories[#categories + 1] = string.format("[[Category:%s deaths]]", deathYear)
                categories[#categories + 1] = string.format("[[Category:%s deaths]]", monthName)
            end
        end
    
    elseif deathYear then
        deathDateDisplay = string.format("[[:Category:%s deaths|%s]]%s",
            deathYear, deathYear,
            ageAtDeath
        )
        if isMainNamespace then
            categories[#categories + 1] = string.format("[[Category:%s deaths]]", deathYear)
        end
    
    elseif deathMonth and deathDay then
        local ok, monthName = pcall(os.date, '%B', os.time({year = 2000, month = tonumber(deathMonth), day = 1}))
        if ok and monthName then
            deathDateDisplay = string.format("[[:Category:%s %s deaths|%s %s]]",
                monthName, deathDay,
                monthName, deathDay
            )
            if isMainNamespace then
                categories[#categories + 1] = string.format("[[Category:%s_%s_deaths]]", monthName, deathDay)
            end
        end
    end

    if deathDateDisplay ~= "" then
        if deathRef then
            deathDateDisplay = deathDateDisplay .. deathRef
        end
        
        section[#section + 1] = string.format('|-\n! style="%s %s" | \'\'\'Died:\'\'\'\n| style="text-align:left;" | %s',
            state:getRowStyle(rowIndex),
            state.STYLES.LABEL,
            deathDateDisplay .. table.concat(categories, '')
        )
        rowIndex = rowIndex + 1

        local deathLocation = nil
        if args.death_location and args.death_location ~= "" then
            deathLocation = args.death_location
        elseif args.qid then
            deathLocation = PBD._properties{[1] = 'P23', raw = true, multilanguage = true, ['eid'] = args.qid}
        end
        
        if not isEmpty(deathLocation) then
            section[#section + 1] = string.format('|-\n! style="%s %s" | \'\'\'Place of death:\'\'\'\n| style="text-align:left;" | %s',
                state:getRowStyle(rowIndex),
                state.STYLES.LABEL,
                deathLocation
            )
            rowIndex = rowIndex + 1
        end
    end
    
    return section, rowIndex
end

function p.formatEthnicity(state, args, rowIndex)
    local section = {}
    
    local ethnicityValues = state:collectNumberedParams(args, "ethnicity")
    
    if #ethnicityValues == 0 and args.qid then
        local pbdValue = PBD._properties{
            [1] = 'P136',
            raw = true,
            normal = true,
            ['eid'] = args.qid
        }
        if not isEmpty(pbdValue) then
            table.insert(ethnicityValues, pbdValue)
        end
    end
    
    if #ethnicityValues > 0 then
        local displayValue = {}
        
        for _, eth in ipairs(ethnicityValues) do
            local ethnicities = state:splitByComma(eth)
            for _, e in ipairs(ethnicities) do
                if #displayValue > 0 then
                    table.insert(displayValue, ", ")
                end
                
                e = mw.text.trim(e):gsub("^%l", string.upper)
                local result = string.format("[[:Category:%s|%s]]", e, e)
                if mw.title.getCurrentTitle().namespace == 0 then
                    result = result .. string.format("[[Category:%s]]", e)
                end
                table.insert(displayValue, result)
            end
        end
        
        section[#section + 1] = state:formatField("Ethnicity:",
            table.concat(displayValue, ""),
            "ethnicity",
            rowIndex)
        rowIndex = rowIndex + 1
    end
    
    return section, rowIndex
end

function p.formatNationality(state, args, rowIndex)
    local section = {}
    
    local nationalityValues = state:collectNumberedParams(args, "nationality")
    
    if #nationalityValues == 0 and args.qid then
        local pbdValue = PBD._properties{
            [1] = 'P131',
            raw = true,
            normal = true,
            ['eid'] = args.qid
        }
        if not isEmpty(pbdValue) then
            table.insert(nationalityValues, pbdValue)
        end
    end
    
    if #nationalityValues > 0 then
        local displayValue = {}
        
        for _, nat in ipairs(nationalityValues) do
            local nationalities = state:splitByComma(nat)
            for _, n in ipairs(nationalities) do
                if #displayValue > 0 then
                    table.insert(displayValue, ", ")
                end
                
                n = mw.text.trim(n):gsub("^%l", string.upper)
                local result = string.format("[[:Category:%s|%s]]", n, n)
                if mw.title.getCurrentTitle().namespace == 0 then
                    result = result .. string.format("[[Category:%s]]", n)
                end
                table.insert(displayValue, result)
            end
        end
        
        section[#section + 1] = state:formatField("Nationality:",
            table.concat(displayValue, ""),
            "nationality",
            rowIndex)
        rowIndex = rowIndex + 1
    end
    
    return section, rowIndex
end

function p.formatLanguages(state, args, rowIndex)
    local section = {}
    
    local languageValues = {}
    for i = 1, 4 do
        local param = i == 1 and "language" or "language" .. i
        if not isEmpty(args[param]) then
            table.insert(languageValues, args[param])
        end
    end
    
    if #languageValues == 0 and args.qid then
        local pbdValues = PBD._properties{
            [1] = 'P634',
            raw = true,
            multilanguage = true,
            sep = '\n',
            ['eid'] = args.qid
        }
        
        if not isEmpty(pbdValues) then
            for value in pbdValues:gmatch("[^,\n]+") do
                table.insert(languageValues, mw.text.trim(value))
            end
        end
    end
    
    if #languageValues > 0 then
        local displayValues = {}
        for _, lang in ipairs(languageValues) do
            lang = mw.text.trim(lang):gsub("^%l", string.upper)
            local result = string.format("[[:Category:%s language spoken|%s]]", lang, lang)
            if mw.title.getCurrentTitle().namespace == 0 then
                result = result .. string.format("[[Category:%s language spoken]]", lang)
            end
            table.insert(displayValues, result)
        end
        
        local frame = mw.getCurrentFrame()
        local abbrTemplate = frame:expandTemplate{
            title = 'abbr',
            args = {
                "Languages",
                "Language(s) that a person speaks, including the native language(s)"
            }
        }
        
        section[#section + 1] = state:formatField(
            abbrTemplate .. ":",
            table.concat(displayValues, ", "),
            "language",
            rowIndex
        )
        rowIndex = rowIndex + 1
    end
    
    return section, rowIndex
end

function p.formatBrother(state, args, rowIndex)
    local section = {}
    
    local brotherValue = args.brother
    
    if isEmpty(brotherValue) and args.qid then
        brotherValue = PBD._properties{
            [1] = 'P358',
            linked = true,
            multilanguage = true,
            ['eid'] = args.qid
        }
    end
    
    if not isEmpty(brotherValue) then
        section[#section + 1] = state:formatField(
            "Brother of:",
            brotherValue,
            "brother",
            rowIndex
        )
        rowIndex = rowIndex + 1
    end
    
    return section, rowIndex
end

function p.formatSpouse(state, args, rowIndex)
    local section = {}
    
    if not isEmpty(args.spouse) then
        section[#section + 1] = state:formatField(
            "Spouse:",
            args.spouse,
            "spouse",
            rowIndex
        )
        rowIndex = rowIndex + 1
    end
    
    return section, rowIndex
end

function p.formatAlmaMater(state, args, rowIndex)
    local section = {}
    
    local almaMaterValue = args["alma mater"] or args["alma_mater"]
    
    if isEmpty(almaMaterValue) and args.qid then
        local formattedValues = {}
        local entity = mw.wikibase.getEntity(args.qid)
        
        if entity and entity.claims and entity.claims.P151 then
            local statements = {}
            for _, statement in pairs(entity.claims.P151) do
                table.insert(statements, statement)
            end
            
            table.sort(statements, function(a, b)
                local rankOrder = {preferred = 1, normal = 2, deprecated = 3}
                return (rankOrder[a.rank] or 99) < (rankOrder[b.rank] or 99)
            end)
            
            for _, statement in ipairs(statements) do
                if statement.mainsnak and 
                   statement.mainsnak.snaktype == "value" and 
                   statement.mainsnak.datavalue then
                    
                    local schoolValue
                    
                    if statement.mainsnak.datavalue.type == "wikibase-entityid" then
                        local schoolId = statement.mainsnak.datavalue.value.id
                        schoolValue = mw.wikibase.getLabel(schoolId) or schoolId
                    elseif statement.mainsnak.datavalue.type == "string" then
                        schoolValue = statement.mainsnak.datavalue.value
                    end
                    
                    if schoolValue and schoolValue ~= "" then
                        local formattedValue = schoolValue
                        
                        if statement.qualifiers then
                            local qualifiers = {}
                            for qualifierId, qualifierSnaks in pairs(statement.qualifiers) do
                                for _, qualifierSnak in ipairs(qualifierSnaks) do
                                    if qualifierSnak.datavalue then
                                        local qualValue
                                        
                                        if qualifierSnak.datavalue.type == "wikibase-entityid" then
                                            local qId = qualifierSnak.datavalue.value.id
                                            qualValue = mw.wikibase.getLabel(qId) or qId
                                        elseif qualifierSnak.datavalue.type == "string" then
                                            qualValue = qualifierSnak.datavalue.value
                                        end
                                        
                                        if qualValue and qualValue ~= "" then
                                            table.insert(qualifiers, qualValue)
                                        end
                                    end
                                end
                            end
                            
                            if #qualifiers > 0 then
                                formattedValue = formattedValue .. ' <span style="font-size:85%">(' .. table.concat(qualifiers, ", ") .. ')</span>'
                            end
                        end
                        
                        if statement.references then
                            local refCount = 0
                            for _, ref in ipairs(statement.references) do
                                refCount = refCount + 1
                                local refId = "pbdref-" .. statement.id .. "-" .. refCount
                                
                                local refValues = {}
                                if ref.snaks then
                                    for refPropId, refSnaks in pairs(ref.snaks) do
                                        for _, refSnak in ipairs(refSnaks) do
                                            local refLabel = mw.wikibase.getLabel(refPropId) or refPropId
                                            local refValue
                                            
                                            if refSnak.datavalue then
                                                if refSnak.datavalue.type == "wikibase-entityid" then
                                                    local valId = refSnak.datavalue.value.id
                                                    refValue = mw.wikibase.getLabel(valId) or valId
                                                elseif refSnak.datavalue.type == "string" then
                                                    refValue = refSnak.datavalue.value
                                                elseif refSnak.datavalue.type == "time" then
                                                    refValue = refSnak.datavalue.value.time
                                                elseif refSnak.datavalue.type == "url" then
                                                    refValue = refSnak.datavalue.value
                                                end
                                                
                                                if refValue and refValue ~= "" then
                                                    table.insert(refValues, refLabel .. ": " .. refValue)
                                                end
                                            end
                                        end
                                    end
                                end
                                
                                if #refValues > 0 then
                                    formattedValue = formattedValue .. 
                                        mw.getCurrentFrame():extensionTag("ref", 
                                            table.concat(refValues, "; "), 
                                            {name = refId})
                                end
                            end
                        end
                        
                        table.insert(formattedValues, formattedValue)
                    end
                end
            end
            
            if #formattedValues > 0 then
                almaMaterValue = table.concat(formattedValues, ", ")
            end
        else
            local baseValue = PBD._properties{
                [1] = 'P151',
                multilanguage = true,
                linked = true,
                ['eid'] = args.qid
            }
            
            if not isEmpty(baseValue) then
                almaMaterValue = baseValue
            end
        end
    end
    
    if not isEmpty(almaMaterValue) then
        section[#section + 1] = state:formatField(
            "Alma mater:",
            almaMaterValue,
            "alma_mater",
            rowIndex
        )
        rowIndex = rowIndex + 1
    end
    
    return section, rowIndex
end

function p.formatOrientation(state, args, rowIndex)
    local section = {}
    
    local orientationValue = args.orientation
    
    if isEmpty(orientationValue) and args.qid then
        orientationValue = PBD._property{
            [1] = 'P347',
            raw = true,
            multilanguage = true,
            ['eid'] = args.qid
        }
    end
    
    if not isEmpty(orientationValue) then
        orientationValue = tostring(orientationValue):lower()
        local orientationDisplay = nil
        
        for _, mapping in ipairs(CategoryMapping.MAPPINGS.orientation) do
            for _, alias in ipairs(mapping.aliases) do
                if orientationValue == alias:lower() then
                    local result = string.format('<span class="%s">[[:Category:%s|%s]]</span>',
                        mapping.class,
                        mapping.category,
                        mapping.display
                    )
                    
                    if mw.title.getCurrentTitle().namespace == 0 then
                        result = result .. string.format("[[Category:%s]]",
                            mapping.category
                        )
                    end
                    
                    orientationDisplay = result
                    break
                end
            end
            if orientationDisplay then break end
        end
        
        if not orientationDisplay then
            orientationValue = mw.text.trim(orientationValue):gsub("^%l", string.upper)
            orientationDisplay = string.format("[[:Category:%s|%s]]", orientationValue, orientationValue)
            
            if mw.title.getCurrentTitle().namespace == 0 then
                orientationDisplay = orientationDisplay .. string.format("[[Category:%s]]", orientationValue)
            end
        end
        
        local frame = mw.getCurrentFrame()
        local abbrTemplate = frame:expandTemplate{
            title = 'abbr',
            args = {
                "Orientation",
                "Sexual orientation"
            }
        }
        
        section[#section + 1] = state:formatField(
            abbrTemplate .. ":",
            orientationDisplay,
            "orientation",
            rowIndex
        )
        rowIndex = rowIndex + 1
    end
    
    return section, rowIndex
end

function p.formatCareer(state, args, rowIndex)
    local section = {}
    local isMainNamespace = mw.title.getCurrentTitle().namespace == 0
    
    local careerstart = args.careerstart
    if isEmpty(careerstart) and args.qid then
        careerstart = PBD._property{
            [1] = 'P153',
            raw = true,
            multilanguage = true,
            ['eid'] = args.qid
        }
    end
    
    if not isEmpty(careerstart) then
        local result = string.format("[[:Category:%s career start|%s]]", careerstart, careerstart)
        if isMainNamespace then
            result = result .. string.format("[[Category:%s career start]]", careerstart)
        end
        
        section[#section + 1] = state:formatField("Career Start:",
            result,
            "careerstart",
            rowIndex)
        rowIndex = rowIndex + 1
    end
    
    local career = args.career
    if isEmpty(career) and args.qid then
        career = PBD._property{
            [1] = 'P154',
            raw = true,
            multilanguage = true,
            ['eid'] = args.qid
        }
    end
    
    local isActive = false
    if not isEmpty(career) then
        local statusMap = {
            ["A"] = {text = "Active", category = "Currently Active Pornstars", class = "careerstatusactive"},
            ["AS"] = {text = "Active (Straight Only)", category = "Currently Active Pornstars (Straight Only)", class = "careerstatusactive"},
            ["R"] = {text = "Retired", category = "Retired Pornstars", class = "careerstatusretired"},
            ["ACTIVE"] = {text = "Active", category = "Currently Active Pornstars", class = "careerstatusactive"},
            ["RETIRED"] = {text = "Retired", category = "Retired Pornstars", class = "careerstatusretired"}
        }
        
        career = career:upper()
        local status = statusMap[career]
        if status then
            isActive = (career == "A" or career == "AS" or career == "ACTIVE")
            
            local displayValue = string.format('<span class="%s">[[:Category:%s|%s]]</span>',
                status.class,
                status.category,
                status.text
            )
            
            if isMainNamespace then
                displayValue = displayValue .. string.format("[[Category:%s]]",
                    status.category
                )
            end
            
            section[#section + 1] = state:formatField("Career Status:",
                displayValue,
                "career",
                rowIndex)
            rowIndex = rowIndex + 1
        end
    end
    
    if not isActive then
        local careerend = args.careerend
        if isEmpty(careerend) and args.qid then
            careerend = PBD._property{
                [1] = 'P155',
                raw = true,
                multilanguage = true,
                ['eid'] = args.qid
            }
        end
        
        if not isEmpty(careerend) then
            local result = string.format("[[:Category:%s career end|%s]]", careerend, careerend)
            if isMainNamespace then
                result = result .. string.format("[[Category:%s career end]]", careerend)
            end
            
            section[#section + 1] = state:formatField("Career End:",
                result,
                "careerend", 
                rowIndex)
            rowIndex = rowIndex + 1
        end
    end
    
    return section, rowIndex
end

function p.getPBDCategories(args)
    if mw.title.getCurrentTitle().namespace ~= 0 then
        return {}
    end

    local categories = {}
    
    local qid = args.qid
    if isEmpty(qid) then
        qid = mw.wikibase.getEntityIdForCurrentPage()
    end
    if isEmpty(qid) then
        return {}
    end

    local birthplaceProps = mw.wikibase.getBestStatements(qid, "P20")
    if birthplaceProps and #birthplaceProps > 0 then
        for _, statement in pairs(birthplaceProps) do
            if statement.mainsnak and 
               statement.mainsnak.datavalue and 
               statement.mainsnak.datavalue.value and 
               statement.mainsnak.datavalue.value.id then
                local placeId = statement.mainsnak.datavalue.value.id
                local sitelink = mw.wikibase.getSitelink(placeId)
                if sitelink then
                    sitelink = sitelink:gsub("^" .. mw.site.namespaces[14].name .. ":", "")
                    table.insert(categories, sitelink)
                end
            end
        end
    end

    local birthCountryProps = mw.wikibase.getBestStatements(qid, "P144")
    if birthCountryProps and #birthCountryProps > 0 then
        for _, statement in pairs(birthCountryProps) do
            if statement.mainsnak and 
               statement.mainsnak.datavalue and 
               statement.mainsnak.datavalue.value and 
               statement.mainsnak.datavalue.value.id then
                local countryId = statement.mainsnak.datavalue.value.id
                local sitelink = mw.wikibase.getSitelink(countryId)
                if sitelink then
                    sitelink = sitelink:gsub("^" .. mw.site.namespaces[14].name .. ":", "")
                    table.insert(categories, sitelink)
                end
            end
        end
    end

    local occupationProps = mw.wikibase.getBestStatements(qid, "P124")
    if occupationProps and #occupationProps > 0 then
        for _, statement in pairs(occupationProps) do
            if statement.mainsnak and 
               statement.mainsnak.datavalue and 
               statement.mainsnak.datavalue.value and 
               statement.mainsnak.datavalue.value.id then
                local occupationId = statement.mainsnak.datavalue.value.id
                local sitelink = mw.wikibase.getSitelink(occupationId)
                if sitelink then
                    sitelink = sitelink:gsub("^" .. mw.site.namespaces[14].name .. ":", "")
                    table.insert(categories, sitelink)
                end
            end
        end
    end

    return categories
end

function p.formatPersonalSection(state, args, title)
    local section = {}
    local rowIndex = 1

    if not args.qid or args.qid == "" then
        args.qid = mw.wikibase.getEntityIdForCurrentPage()
    end

    section[#section + 1] = string.format('{| %s', state.STYLES.INFOBOX)

    local name = args.name
    if isEmpty(name) then
        name = getPBDValue('P7', args)
        if isEmpty(name) then
            name = title
        end 
    end

    section[#section + 1] = string.format('|-\n! colspan="2" %s | \'\'\'%s\'\'\'', state.STYLES.HEADER, name)

    local imageHtml = getImage(args)
    local imageCaption = not isEmpty(args.imagecaption) and 
        string.format('<br>\'\'%s\'\'', args.imagecaption) or ''
    section[#section + 1] = string.format('|-\n| colspan="2" style="text-align:center;" | %s%s', 
        imageHtml, imageCaption)

    section[#section + 1] = string.format('|-\n! colspan="2" %s | \'\'\'Personal\'\'\'', state.STYLES.SECTION_HEADER)

    local realName = args.real_name
    local marriedName = args.married_name
    
    if isEmpty(realName) then
        realName = getPBDValue('P5', args)
    end
    
    if isEmpty(marriedName) then
        marriedName = getPBDValue('P648', args)
    end
    
    if not isEmpty(marriedName) then
        if not isEmpty(realName) then
            local fieldLine = state:formatField('Real name:', 
                marriedName .. '<br>\'\'\'né\'\'\' ' .. realName, 
                'real_name', 
                rowIndex)
            if fieldLine ~= '' then
                section[#section + 1] = fieldLine
                rowIndex = rowIndex + 1
            end
        else
            local fieldLine = state:formatField('Real name:', 
                marriedName, 
                'married_name', 
                rowIndex)
            if fieldLine ~= '' then
                section[#section + 1] = fieldLine
                rowIndex = rowIndex + 1
            end
        end
    elseif not isEmpty(realName) then
        local fieldLine = state:formatField('Birth name:', 
            realName, 
            'real_name', 
            rowIndex)
        if fieldLine ~= '' then
            section[#section + 1] = fieldLine
            rowIndex = rowIndex + 1
        end
    end

    local personalFields = {
        {label = 'Legal name:', prop = 'P753', param = 'legal_name'},
        {label = 'Native name:', prop = 'P13', param = 'native_name'},
        {label = 'Name in kana:', prop = 'P480', param = 'name_in_kana'},
        {label = 'Aliases:', prop = 'P140', param = 'alias'},
        {label = 'Nicknames:', prop = 'P363', param = 'nicknames'}
    }

    for _, field in ipairs(personalFields) do
        local value
        
        if field.param == 'alias' or field.param == 'nicknames' then
            value = args[field.param]
            if isEmpty(value) and args.qid then
                value = PBD._properties{
                    [1] = field.prop,
                    raw = true,
                    multilanguage = true,
                    ['eid'] = args.qid
                }
            end
        else
            value = getPBDValue(field.prop, args, args[field.param])
        end
        
        local fieldLine = state:formatField(field.label, value, field.param, rowIndex)
        if fieldLine ~= '' then
            section[#section + 1] = fieldLine
            rowIndex = rowIndex + 1
        end
    end

    local birthSection, newRowIndex = p.formatBirthDate(state, args, rowIndex)
    for _, line in ipairs(birthSection) do
        section[#section + 1] = line
    end
    rowIndex = newRowIndex
    
    local placeOfBirthSection, newRowIndex = p.formatPlaceOfBirth(state, args, rowIndex) 
    for _, line in ipairs(placeOfBirthSection) do
        section[#section + 1] = line
    end
    rowIndex = newRowIndex
    
    local birthCountrySection, newRowIndex = p.formatBirthCountry(state, args, rowIndex)
    for _, line in ipairs(birthCountrySection) do
        section[#section + 1] = line
    end
    rowIndex = newRowIndex

    local citizenshipSection, newRowIndex = p.formatCitizenship(state, args, rowIndex)
    for _, line in ipairs(citizenshipSection) do
        section[#section + 1] = line
    end
    rowIndex = newRowIndex

    local deathSection, newRowIndex = p.formatDeathDate(state, args, rowIndex)
    for _, line in ipairs(deathSection) do
        section[#section + 1] = line
    end
    rowIndex = newRowIndex

    local ethnicitySection, newRowIndex = p.formatEthnicity(state, args, rowIndex)
    for _, line in ipairs(ethnicitySection) do
        section[#section + 1] = line
    end
    rowIndex = newRowIndex

    local nationalitySection, newRowIndex = p.formatNationality(state, args, rowIndex)
    for _, line in ipairs(nationalitySection) do
        section[#section + 1] = line
    end
    rowIndex = newRowIndex
    
    local languagesSection, newRowIndex = p.formatLanguages(state, args, rowIndex)
    for _, line in ipairs(languagesSection) do
        section[#section + 1] = line
    end
    rowIndex = newRowIndex
    
    local brotherSection, newRowIndex = p.formatBrother(state, args, rowIndex)
    for _, line in ipairs(brotherSection) do
        section[#section + 1] = line
    end
    rowIndex = newRowIndex
    
    local spouseSection, newRowIndex = p.formatSpouse(state, args, rowIndex)
    for _, line in ipairs(spouseSection) do
        section[#section + 1] = line
    end
    rowIndex = newRowIndex
    
    local almaMaterSection, newRowIndex = p.formatAlmaMater(state, args, rowIndex)
    for _, line in ipairs(almaMaterSection) do
        section[#section + 1] = line
    end
    rowIndex = newRowIndex
    
	local orientationSection, newRowIndex = p.formatOrientation(state, args, rowIndex)
	for _, line in ipairs(orientationSection) do
	    section[#section + 1] = line
	end
	rowIndex = newRowIndex

    local careerSection, newRowIndex = p.formatCareer(state, args, rowIndex)
    for _, line in ipairs(careerSection) do
        section[#section + 1] = line
    end
    rowIndex = newRowIndex

    return table.concat(section, '\n'), rowIndex
end

return p