„Modul:WikibaseL“ – Versionsunterschied
Erscheinungsbild
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: | local util = require('Module:Wikibase') | ||
local cfg = require('Module:Wikibase/config') | |||
local p = {} | local p = {} | ||
local function | local function isNo(value) | ||
value = tostring(value or ''):lower() | |||
return value == 'nein' or value == '0' or value == 'no' or value == 'false' | |||
end | end | ||
local function | 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 | end | ||
local function | 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 | end | ||
local function | 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 | end | ||
function p.link(frame) | 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 | end | ||
function p.render(frame) | 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 | end | ||
function p.shortdesc(frame) | 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 | 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