„Modul:WikibaseLink“ – Versionsunterschied
Erscheinungsbild
KKeine Bearbeitungszusammenfassung |
KKeine Bearbeitungszusammenfassung |
||
| Zeile 2: | Zeile 2: | ||
------------------------------------------------------------------ | ------------------------------------------------------------------ | ||
-- Modul:WikibaseLink – | -- Modul:WikibaseLink – überarbeitete Fassung | ||
-- Ziel: | -- Ziel: | ||
-- 1) Links zu Q / P / L ausgeben (Special:EntityPage/...) | -- 1) Links zu Q / P / L ausgeben (Special:EntityPage/...) | ||
| Zeile 11: | Zeile 11: | ||
-- so dass sie als Such-Untertitel verwendet werden kann. | -- so dass sie als Such-Untertitel verwendet werden kann. | ||
-- | -- | ||
-- Nutzung ( | -- Nutzung (direkt per #invoke): | ||
-- {{#invoke:WikibaseLink|link|entity=Q123}} | -- {{#invoke:WikibaseLink|link|entity=Q123}} | ||
-- {{#invoke:WikibaseLink|link|P=P31}} | -- {{#invoke:WikibaseLink|link|P=P31}} | ||
| Zeile 17: | Zeile 17: | ||
-- {{#invoke:WikibaseLink|shortdesc|P=P31|mode=P}} | -- {{#invoke:WikibaseLink|shortdesc|P=P31|mode=P}} | ||
-- {{#invoke:WikibaseLink|shortdesc|L=L1|mode=L}} | -- {{#invoke:WikibaseLink|shortdesc|L=L1|mode=L}} | ||
-- {{#invoke:WikibaseLink|linkAndShortdesc| | -- {{#invoke:WikibaseLink|linkAndShortdesc|L=L1|mode=L}} | ||
-- | |||
-- Empfohlene Vorlage:WikibaseLink (siehe Chat), die intern | |||
-- {{#invoke:WikibaseLink|linkAndShortdesc|entity={{{1|}}}|mode={{{mode|auto}}}|shortdesc={{{shortdesc|ja}}}|label={{{label|}}}}} | |||
-- aufruft. | |||
-- | -- | ||
-- Parameter (alle Funktionen): | -- Parameter (alle Funktionen): | ||
| Zeile 27: | Zeile 31: | ||
-- bestimmt, ob SHORTDESC erzeugt wird und wie. | -- bestimmt, ob SHORTDESC erzeugt wird und wie. | ||
-- |label= optionaler Linktext statt Wikibase-Label | -- |label= optionaler Linktext statt Wikibase-Label | ||
-- |shortdesc= ja/nein/1/0 (nur für linkAndShortdesc) | |||
-- Standard (nil) = ja (SHORTDESC ausgeben, wenn vorhanden) | |||
-- | -- | ||
-- Fallback: | -- Fallback: | ||
| Zeile 41: | Zeile 47: | ||
end | end | ||
-- Hilfsfunktion: Argumente aus frame | -- Hilfsfunktion: Argumente nur aus dem aktuellen frame holen | ||
-- (keine Parent-Args, damit Aufrufe wie L=L2 zuverlässig wirken) | |||
local function getArgs(frame) | local function getArgs(frame) | ||
local args = {} | local args = {} | ||
| Zeile 48: | Zeile 55: | ||
end | end | ||
for k, v in pairs(frame.args or {}) do | |||
for k, v in pairs(frame.args) do | |||
if v ~= '' and v ~= nil then | if v ~= '' and v ~= nil then | ||
args[k] = v | args[k] = v | ||
| Zeile 83: | Zeile 81: | ||
-- Entity-ID aus den Argumenten (oder Kontext) ermitteln | -- Entity-ID aus den Argumenten (oder Kontext) ermitteln | ||
local function detectEntityId(args) | local function detectEntityId(args) | ||
local | -- Kandidaten aus diesem Aufruf; Reihenfolge: | ||
-- 1) explizite Typen (L, P, Q) | |||
-- 2) generische entity/id | |||
-- 3) Positionsparameter (1) | |||
local rawL = args.L or args.lexeme or args.l | |||
local rawP = args.P or args.p | |||
local rawQ = args.Q or args.q | |||
local rawEntity = args.entity or args.Entity | |||
local rawId = args.id or args.ID | |||
local rawPos = args[1] | |||
for _, v in ipairs( | local ordered = { rawL, rawP, rawQ, rawEntity, rawId, rawPos } | ||
for _, v in ipairs(ordered) do | |||
if v ~= nil and v ~= '' then | if v ~= nil and v ~= '' then | ||
local s = trim(tostring(v)) | local s = trim(tostring(v)) | ||
| Zeile 101: | Zeile 103: | ||
end | end | ||
-- Fallback: Entity zur aktuellen Seite (z. B. Q bei Sitelink | -- Fallback: Entity zur aktuellen Seite (z. B. Q bei Sitelink) | ||
local ok, currentId = pcall(mw.wikibase.getEntityIdForCurrentPage) | local ok, currentId = pcall(mw.wikibase.getEntityIdForCurrentPage) | ||
if ok and type(currentId) == 'string' and currentId:match('^[QPL]%d+$') then | if ok and type(currentId) == 'string' and currentId:match('^[QPL]%d+$') then | ||
| Zeile 242: | Zeile 244: | ||
local label = args.label or args.text | local label = args.label or args.text | ||
local short = buildShortdescText(entityId, mode) | local wantShort = args.shortdesc | ||
local emitShort = true | |||
if wantShort ~= nil and wantShort ~= '' then | |||
local s = tostring(wantShort):lower() | |||
if s == 'nein' or s == 'no' or s == '0' or s == 'false' then | |||
emitShort = false | |||
end | |||
end | |||
local short = emitShort and buildShortdescText(entityId, mode) or nil | |||
local link = buildEntityLink(entityId, label) | local link = buildEntityLink(entityId, label) | ||
Version vom 11. Dezember 2025, 17:40 Uhr
| Vorlagenprogrammierung | Diskussionen | Lua | Unterseiten | ||
| Modul | Deutsch
|
Modul: | Dokumentation | ||
Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus
local p = {}
------------------------------------------------------------------
-- Modul:WikibaseLink – überarbeitete 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 (direkt per #invoke):
-- {{#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|L=L1|mode=L}}
--
-- Empfohlene Vorlage:WikibaseLink (siehe Chat), die intern
-- {{#invoke:WikibaseLink|linkAndShortdesc|entity={{{1|}}}|mode={{{mode|auto}}}|shortdesc={{{shortdesc|ja}}}|label={{{label|}}}}}
-- aufruft.
--
-- 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
-- |shortdesc= ja/nein/1/0 (nur für linkAndShortdesc)
-- Standard (nil) = ja (SHORTDESC ausgeben, wenn vorhanden)
--
-- 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 nur aus dem aktuellen frame holen
-- (keine Parent-Args, damit Aufrufe wie L=L2 zuverlässig wirken)
local function getArgs(frame)
local args = {}
if not frame then
return args
end
for k, v in pairs(frame.args or {}) 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)
-- Kandidaten aus diesem Aufruf; Reihenfolge:
-- 1) explizite Typen (L, P, Q)
-- 2) generische entity/id
-- 3) Positionsparameter (1)
local rawL = args.L or args.lexeme or args.l
local rawP = args.P or args.p
local rawQ = args.Q or args.q
local rawEntity = args.entity or args.Entity
local rawId = args.id or args.ID
local rawPos = args[1]
local ordered = { rawL, rawP, rawQ, rawEntity, rawId, rawPos }
for _, v in ipairs(ordered) 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 wantShort = args.shortdesc
local emitShort = true
if wantShort ~= nil and wantShort ~= '' then
local s = tostring(wantShort):lower()
if s == 'nein' or s == 'no' or s == '0' or s == 'false' then
emitShort = false
end
end
local short = emitShort and buildShortdescText(entityId, mode) or nil
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