Modul:WikibaseLink
Erscheinungsbild
| Vorlagenprogrammierung | Diskussionen | Lua | Unterseiten | ||
| Modul | Deutsch
|
Modul: | Dokumentation | ||
Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus
local p = {}
------------------------------------------------------------------
-- Modul:WikibaseLink – neue Fassung
-- Ziel:
-- 1) Links zu Q / P / L ausgeben (Special:EntityPage/...)
-- 2) Für P und L passende Kurzbeschreibung ermitteln
-- - P: Wikibase-Beschreibung
-- - L: lexikalische Kategorie (Label)
-- 3) OPTIONAL: Diese Kurzbeschreibung als SHORTDESC ausgeben,
-- so dass sie als Such-Untertitel verwendet werden kann.
--
-- Nutzung (Vorschlag für Vorlage "WikibaseLink"):
-- {{#invoke:WikibaseLink|link|entity=Q123}}
-- {{#invoke:WikibaseLink|link|P=P31}}
-- {{#invoke:WikibaseLink|link|L=L1}}
-- {{#invoke:WikibaseLink|shortdesc|P=P31|mode=P}}
-- {{#invoke:WikibaseLink|shortdesc|L=L1|mode=L}}
-- {{#invoke:WikibaseLink|linkAndShortdesc|P=P31|mode=P}}
--
-- Parameter (alle Funktionen):
-- |entity= Q123 / P31 / L1 (allgemein)
-- |Q= explizit Item
-- |P= explizit Property
-- |L= explizit Lexem
-- |mode= 'auto' (Standard) / 'Q' / 'P' / 'L'
-- bestimmt, ob SHORTDESC erzeugt wird und wie.
-- |label= optionaler Linktext statt Wikibase-Label
--
-- Fallback:
-- Wenn keine Entity per Parameter angegeben ist, versucht
-- das Modul mw.wikibase.getEntityIdForCurrentPage().
------------------------------------------------------------------
-- Hilfsfunktion: Trim
local function trim(s)
if type(s) ~= 'string' then
return s
end
return (s:gsub('^%s+', ''):gsub('%s+$', ''))
end
-- Hilfsfunktion: Argumente aus frame + Parent zusammenführen
local function getArgs(frame)
local args = {}
if not frame then
return args
end
local parent = frame:getParent()
if parent and parent.args then
for k, v in pairs(parent.args) do
if v ~= '' and v ~= nil then
args[k] = v
end
end
end
for k, v in pairs(frame.args) do
if v ~= '' and v ~= nil then
args[k] = v
end
end
return args
end
-- Entity-Typ bestimmen
local function getEntityType(id)
if type(id) ~= 'string' then
return nil
end
if id:match('^Q%d+$') then
return 'item' -- Q
elseif id:match('^P%d+$') then
return 'property' -- P
elseif id:match('^L%d+$') then
return 'lexeme' -- L
end
return nil
end
-- Entity-ID aus den Argumenten (oder Kontext) ermitteln
local function detectEntityId(args)
local candidates = {
args.entity or args.Entity,
args.id or args.ID,
args.Q or args.q,
args.P or args.p,
args.L or args.lexeme or args.l,
args[1],
}
for _, v in ipairs(candidates) do
if v ~= nil and v ~= '' then
local s = trim(tostring(v))
if s ~= '' and s:match('^[QPL]%d+$') then
return s
end
end
end
-- Fallback: Entity zur aktuellen Seite (z. B. Q bei Sitelink
local ok, currentId = pcall(mw.wikibase.getEntityIdForCurrentPage)
if ok and type(currentId) == 'string' and currentId:match('^[QPL]%d+$') then
return currentId
end
return nil
end
-- Link auf eine Entity bauen (mit optionalem Label)
local function buildEntityLink(entityId, labelOverride)
if not entityId then
return ''
end
local label = labelOverride
if not label or label == '' then
local ok, fetched = pcall(mw.wikibase.getLabel, entityId)
if ok and fetched and fetched ~= '' then
label = fetched
else
label = entityId
end
end
return string.format('[[Special:EntityPage/%s|%s]]', entityId, label)
end
-- Kurzbeschreibung für eine Entity bestimmen
-- mode:
-- 'auto' / nil ->
-- Q (item) → keine SHORTDESC (Q-Seiten haben eigene Brücke)
-- P (property) → Wikibase-Beschreibung
-- L (lexeme) → Label der lexikalischen Kategorie
-- 'Q' / 'item' → keine SHORTDESC
-- 'P' / 'property' → Wikibase-Beschreibung
-- 'L' / 'lexeme' → Label der lexikalischen Kategorie
local function buildShortdescText(entityId, mode)
if not entityId then
return nil
end
local etype = getEntityType(entityId)
if not etype then
return nil
end
mode = mode and tostring(mode):lower() or 'auto'
if mode == 'auto' then
if etype == 'item' then
-- Q-Entities: keine SHORTDESC, Beschreibung kommt über Wikibase-Sitelink
return nil
elseif etype == 'property' then
mode = 'p'
elseif etype == 'lexeme' then
mode = 'l'
end
end
if mode == 'q' or mode == 'item' then
-- bewusst leer: Q-Beschreibungen kommen zentral
return nil
elseif mode == 'p' or mode == 'property' then
local ok, desc = pcall(mw.wikibase.getDescription, entityId)
if ok and type(desc) == 'string' and desc ~= '' then
return desc
end
return nil
elseif mode == 'l' or mode == 'lexeme' then
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 okLabel, label = pcall(mw.wikibase.getLabel, lc)
if okLabel and type(label) == 'string' and label ~= '' then
return label
else
return lc
end
end
return nil
end
------------------------------------------------------------------
-- Öffentliche Funktionen
------------------------------------------------------------------
-- 1) Nur Link ausgeben
function p.link(frame)
local args = getArgs(frame)
local entityId = detectEntityId(args)
if not entityId then
return ''
end
local label = args.label or args.text
return buildEntityLink(entityId, label)
end
-- 2) Nur SHORTDESC ausgeben (falls sinnvoll)
function p.shortdesc(frame)
local args = getArgs(frame)
local entityId = detectEntityId(args)
if not entityId then
return ''
end
local mode = args.mode or args.typ or args.type
local text = buildShortdescText(entityId, mode)
if not text or text == '' then
return ''
end
text = trim(text):gsub('\n', ' ')
return string.format('{{SHORTDESC:%s}}', text)
end
-- 3) Link + SHORTDESC kombinieren
function p.linkAndShortdesc(frame)
local args = getArgs(frame)
local entityId = detectEntityId(args)
if not entityId then
return ''
end
local mode = args.mode or args.typ or args.type
local label = args.label or args.text
local short = buildShortdescText(entityId, mode)
local link = buildEntityLink(entityId, label)
local out = {}
if short and short ~= '' then
short = trim(short):gsub('\n', ' ')
table.insert(out, string.format('{{SHORTDESC:%s}}', short))
end
if link and link ~= '' then
table.insert(out, link)
end
return table.concat(out, '\n')
end
return p