Zum Inhalt springen

„Modul:WikibaseL“ – Versionsunterschied

aus AufEinWort, der freien Forschung
Die Seite wurde neu angelegt: „local util = require('Module:WikibaseUtil') local p = {} local function getLexicalCategoryLabel(entityId) local ok, entity = pcall(mw.wikibase.getEntity, entityId) if not ok or type(entity) ~= 'table' then return nil end local lc = entity.lexicalCategory if type(lc) == 'table' then lc = lc.id or lc.value end if type(lc) ~= 'string' or lc == '' then return nil end local ok2, label = pcall(mw.wikibase.getLabel, lc) if ok2 and type(label) == 'string'…“
 
KKeine Bearbeitungszusammenfassung
 
(2 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
local util = require('Module:WikibaseUtil')
local util = require('Module:Wikibase')
local cfg = require('Module:Wikibase/config')
local p = {}
local p = {}


local function getLexicalCategoryLabel(entityId)
local function isNo(value)
local ok, entity = pcall(mw.wikibase.getEntity, entityId)
    value = tostring(value or ''):lower()
if not ok or type(entity) ~= 'table' then return nil end
    return value == 'nein' or value == '0' or value == 'no' or value == 'false'
local lc = entity.lexicalCategory
if type(lc) == 'table' then lc = lc.id or lc.value end
if type(lc) ~= 'string' or lc == '' then return nil end
local ok2, label = pcall(mw.wikibase.getLabel, lc)
if ok2 and type(label) == 'string' and label ~= '' then return label end
return lc
end
end


local function getSenseGloss(entityId, senseParam)
local function entityReference(value)
-- identisch zur Logik aus dem Alt-Modul, hier nur als Platzhalter: wird beim Übernehmen 1:1 portiert
    if type(value) == 'table' then value = value.id or value.value end
return nil
    if type(value) == 'string' and value:match('^Q[1-9]%d*$') then
        return value
    end
    return nil
end
end


local function computeSubtitle(args, entityId)
local function getId(args)
local s = args.subtitle
    -- Lexeme haben keinen Sitelink: L-ID ausdrücklich übergeben.
if s and s ~= '' then
    local id = util.trim(tostring(args.L or args[1] or args.entity or args.id or ''))
s = tostring(s)
    if id:match('^L[1-9]%d*$') then return id end
local low = s:lower()
    return nil
if low == 'nein' or low == '0' then return nil end
if low == 'sense' then
local sp = args.S or '1'
return getSenseGloss(entityId, sp)
end
-- "S1" / "1" als Direktangabe
if low:match('^s%d+$') or low:match('^%d+$') then
return getSenseGloss(entityId, low)
end
if low ~= 'ja' and low ~= 'auto' then
return util.trim(s) -- eigener Untertitel
end
end
-- Default: Wortart
return getLexicalCategoryLabel(entityId)
end
end


local function shouldSetShortdesc(args)
local function subtitle(args, category)
local set = args.set
    local value = util.trim(tostring(args.subtitle or 'auto'))
if not set or set == '' then return true end
    local low = value:lower()
set = tostring(set):lower()
    if isNo(value) then return nil end
if set == 'nein' or set == '0' then return false end
    if low == 'sense' or low:match('^s%d+$') or low:match('^%d+$') then
return true
        -- Vorhandene Parameter beibehalten; Bedeutungen weiterhin nicht implementiert.
        return nil
    end
    if value == '' or low == 'auto' or low == 'ja' then return category end
    return value
end
 
-- Gemeinsame Daten für Einzelanzeige und kombinierte Q-/L-Tabelle.
-- Keine Speicherung und kein SHORTDESC als Nebeneffekt dieses Datenabrufs.
function p.collect(args)
    local id = getId(args)
    if not id then return nil end
    local entity = mw.wikibase.getEntity(id)
    if type(entity) ~= 'table' then return nil end
    local c = (cfg.smw and cfg.smw.L) or {}
    local lemma = args.label or util.getEntityLabelOrLemma(id) or id
    local categoryId = entityReference(entity.lexicalCategory)
    local languageId = entityReference(entity.language)
    local category = categoryId and util.getClassificationName(categoryId) or nil
    local language = languageId and util.getClassificationName(languageId) or nil
    local sub = subtitle(args, category)
    local rows, notices = {}, {}
    local function add(setting, value, display)
        local property = util.resolveSmwPropertyName(setting)
        value = util.trim(tostring(value or '')):gsub('[\r\n]+', ' ')
        if property ~= '' and value ~= '' then
            rows[#rows + 1] = {
                property = property, value = value,
                display = display or util.smwLiteral(value),
            }
        end
    end
    local target = util.getEntityPageTarget(id, cfg.entityNamespaces)
    add(c.propArt, id, '[[:' .. target .. '|' .. id .. ']]')
    add(c.propLabel, lemma)
    add(c.propDesc, sub)
    add(c.propLanguage, language)
    -- Optionale Aussagen am Lexem, wie bei Q mit sechs Präfix-Slots.
    local seen = {}
    for slot = 1, 6 do
        local property = util.trim(tostring(c['prop' .. slot] or ''))
        if property ~= '' and not seen[property] then
            if not property:match('^P[1-9]%d*$') then
                error('Wikibase/config: L.prop' .. slot .. ' enthält keine gültige P-ID')
            end
            seen[property] = true
            local statements = mw.wikibase.getBestStatements(id, property)
            local seenValues = {}
            for _, statement in ipairs(statements or {}) do
                local value, warning = util.classificationValue(statement.mainsnak)
                if warning then notices[#notices + 1] = property .. ': ' .. warning end
                if value and value ~= '' and not seenValues[value] then
                    seenValues[value] = true
                    add(property, tostring(c['prefix' .. slot] or '') .. value)
                end
            end
        end
    end
    return { id = id, lemma = lemma, subtitle = sub, rows = rows, notices = notices }
end
 
function p.appendRows(tableNode, data, section)
    if section then
        tableNode:tag('tr'):tag('th'):attr('colspan', 2):wikitext(util.smwLiteral(section))
    end
    for _, entry in ipairs(data.rows) do
        local tr = tableNode:tag('tr')
        tr:tag('th'):wikitext(util.smwLiteral(entry.property))
        tr:tag('td'):wikitext(entry.display)
    end
end
 
function p.annotate(frame, args, data)
    local result = util.setSmwRows(frame, data.rows)
    for _, notice in ipairs(data.notices) do
        result = result .. '<span class="error">' .. util.smwLiteral(notice) .. '</span>'
    end
    if data.subtitle and data.subtitle ~= '' and not isNo(args.set) then
        result = result .. (util.setShortdesc(frame, data.subtitle) or '')
    end
    return result
end
 
local function lexemeLink(id, label, target)
    if tostring(target or ''):lower() == 'ns' then
        local page = util.getEntityPageTarget(id, cfg.entityNamespaces)
        return '[[:' .. page .. '|' .. util.smwLiteral(label) .. ']]'
    end
    return util.buildEntityLink(id, label, target)
end
end


function p.link(frame)
function p.link(frame)
local args = util.getArgs(frame)
    local args = util.getArgs(frame)
local entityId = util.detectEntityId(args)
    local id = getId(args)
if not entityId then return '' end
    if not id then return '' end
local lemma = args.label or util.getEntityLabelOrLemma(entityId) or entityId
    local lemma = args.label or util.getEntityLabelOrLemma(id) or id
local line1 = string.format('%s %s', entityId, lemma)
    return lexemeLink(id, id .. ' ' .. lemma, args.target)
return util.buildEntityLink(entityId, line1, args.target)
end
end


function p.render(frame)
function p.render(frame)
local args = util.getArgs(frame)
    local args = util.getArgs(frame)
local entityId = util.detectEntityId(args)
    local data = p.collect(args)
if not entityId then return '' end
    if not data then return '' end
 
    local result = p.annotate(frame, args, data)
local lemma = args.label or util.getEntityLabelOrLemma(entityId) or entityId
    if not isNo(args.show) and #data.rows > 0 then
local line1text = string.format('%s %s', entityId, lemma)
        local t = mw.html.create('table'):addClass('aew-wb-smwtable')
local link = util.buildEntityLink(entityId, line1text, args.target)
        p.appendRows(t, data)
 
        return result .. tostring(t)
local sub = computeSubtitle(args, entityId)
    end
if sub and sub ~= '' then
    local link = lexemeLink(data.id, data.id .. ' ' .. data.lemma, args.target)
if shouldSetShortdesc(args) then
    if data.subtitle and data.subtitle ~= '' then
util.setShortdesc(frame, sub)
        return result .. link .. '<br />' .. util.smwLiteral(data.subtitle)
end
    end
return link .. '<br />' .. util.trim(sub)
    return result .. link
end
return link
end
end


function p.shortdesc(frame)
function p.shortdesc(frame)
local args = util.getArgs(frame)
    local args = util.getArgs(frame)
local entityId = util.detectEntityId(args)
    local data = p.collect(args)
if not entityId then return '' end
    if not data or not data.subtitle or data.subtitle == '' then return '' end
local sub = computeSubtitle(args, entityId)
    return util.setShortdesc(frame, data.subtitle)
if not sub or sub == '' then return '' end
return util.setShortdesc(frame, sub)
end
end


return p
return p

Aktuelle Version vom 20. Oktober 2026, 12:16 Uhr

Die Dokumentation für dieses Modul kann unter Modul:WikibaseL/Doku erstellt werden

local util = require('Module:Wikibase')
local cfg = require('Module:Wikibase/config')
local p = {}

local function isNo(value)
    value = tostring(value or ''):lower()
    return value == 'nein' or value == '0' or value == 'no' or value == 'false'
end

local function entityReference(value)
    if type(value) == 'table' then value = value.id or value.value end
    if type(value) == 'string' and value:match('^Q[1-9]%d*$') then
        return value
    end
    return nil
end

local function getId(args)
    -- Lexeme haben keinen Sitelink: L-ID ausdrücklich übergeben.
    local id = util.trim(tostring(args.L or args[1] or args.entity or args.id or ''))
    if id:match('^L[1-9]%d*$') then return id end
    return nil
end

local function subtitle(args, category)
    local value = util.trim(tostring(args.subtitle or 'auto'))
    local low = value:lower()
    if isNo(value) then return nil end
    if low == 'sense' or low:match('^s%d+$') or low:match('^%d+$') then
        -- Vorhandene Parameter beibehalten; Bedeutungen weiterhin nicht implementiert.
        return nil
    end
    if value == '' or low == 'auto' or low == 'ja' then return category end
    return value
end

-- Gemeinsame Daten für Einzelanzeige und kombinierte Q-/L-Tabelle.
-- Keine Speicherung und kein SHORTDESC als Nebeneffekt dieses Datenabrufs.
function p.collect(args)
    local id = getId(args)
    if not id then return nil end
    local entity = mw.wikibase.getEntity(id)
    if type(entity) ~= 'table' then return nil end
    local c = (cfg.smw and cfg.smw.L) or {}
    local lemma = args.label or util.getEntityLabelOrLemma(id) or id
    local categoryId = entityReference(entity.lexicalCategory)
    local languageId = entityReference(entity.language)
    local category = categoryId and util.getClassificationName(categoryId) or nil
    local language = languageId and util.getClassificationName(languageId) or nil
    local sub = subtitle(args, category)
    local rows, notices = {}, {}
    local function add(setting, value, display)
        local property = util.resolveSmwPropertyName(setting)
        value = util.trim(tostring(value or '')):gsub('[\r\n]+', ' ')
        if property ~= '' and value ~= '' then
            rows[#rows + 1] = {
                property = property, value = value,
                display = display or util.smwLiteral(value),
            }
        end
    end
    local target = util.getEntityPageTarget(id, cfg.entityNamespaces)
    add(c.propArt, id, '[[:' .. target .. '|' .. id .. ']]')
    add(c.propLabel, lemma)
    add(c.propDesc, sub)
    add(c.propLanguage, language)
    -- Optionale Aussagen am Lexem, wie bei Q mit sechs Präfix-Slots.
    local seen = {}
    for slot = 1, 6 do
        local property = util.trim(tostring(c['prop' .. slot] or ''))
        if property ~= '' and not seen[property] then
            if not property:match('^P[1-9]%d*$') then
                error('Wikibase/config: L.prop' .. slot .. ' enthält keine gültige P-ID')
            end
            seen[property] = true
            local statements = mw.wikibase.getBestStatements(id, property)
            local seenValues = {}
            for _, statement in ipairs(statements or {}) do
                local value, warning = util.classificationValue(statement.mainsnak)
                if warning then notices[#notices + 1] = property .. ': ' .. warning end
                if value and value ~= '' and not seenValues[value] then
                    seenValues[value] = true
                    add(property, tostring(c['prefix' .. slot] or '') .. value)
                end
            end
        end
    end
    return { id = id, lemma = lemma, subtitle = sub, rows = rows, notices = notices }
end

function p.appendRows(tableNode, data, section)
    if section then
        tableNode:tag('tr'):tag('th'):attr('colspan', 2):wikitext(util.smwLiteral(section))
    end
    for _, entry in ipairs(data.rows) do
        local tr = tableNode:tag('tr')
        tr:tag('th'):wikitext(util.smwLiteral(entry.property))
        tr:tag('td'):wikitext(entry.display)
    end
end

function p.annotate(frame, args, data)
    local result = util.setSmwRows(frame, data.rows)
    for _, notice in ipairs(data.notices) do
        result = result .. '<span class="error">' .. util.smwLiteral(notice) .. '</span>'
    end
    if data.subtitle and data.subtitle ~= '' and not isNo(args.set) then
        result = result .. (util.setShortdesc(frame, data.subtitle) or '')
    end
    return result
end

local function lexemeLink(id, label, target)
    if tostring(target or ''):lower() == 'ns' then
        local page = util.getEntityPageTarget(id, cfg.entityNamespaces)
        return '[[:' .. page .. '|' .. util.smwLiteral(label) .. ']]'
    end
    return util.buildEntityLink(id, label, target)
end

function p.link(frame)
    local args = util.getArgs(frame)
    local id = getId(args)
    if not id then return '' end
    local lemma = args.label or util.getEntityLabelOrLemma(id) or id
    return lexemeLink(id, id .. ' ' .. lemma, args.target)
end

function p.render(frame)
    local args = util.getArgs(frame)
    local data = p.collect(args)
    if not data then return '' end
    local result = p.annotate(frame, args, data)
    if not isNo(args.show) and #data.rows > 0 then
        local t = mw.html.create('table'):addClass('aew-wb-smwtable')
        p.appendRows(t, data)
        return result .. tostring(t)
    end
    local link = lexemeLink(data.id, data.id .. ' ' .. data.lemma, args.target)
    if data.subtitle and data.subtitle ~= '' then
        return result .. link .. '<br />' .. util.smwLiteral(data.subtitle)
    end
    return result .. link
end

function p.shortdesc(frame)
    local args = util.getArgs(frame)
    local data = p.collect(args)
    if not data or not data.subtitle or data.subtitle == '' then return '' end
    return util.setShortdesc(frame, data.subtitle)
end

return p