„Modul:Wikibase“ – Versionsunterschied
Erscheinungsbild
Die Seite wurde neu angelegt: „local p = {} -- ersetzt z.B. mw.wikibase.getBestLabel function p.getBestLabel(frame) -- Rückgabe eines Fallbacks oder der übergebenen ID local id = frame.args[1] or "" return id -- oder leer: return "" end -- weitere erwartete Funktionen als No-Op oder mit defensiver Logik function p.getEntity(id) return nil -- keine echte Entity, nur Platzhalter end return p“ |
KKeine Bearbeitungszusammenfassung |
||
| Zeile 1: | Zeile 1: | ||
local | local util = {} | ||
local function trim(s) | |||
function | if type(s) ~= 'string' then return s end | ||
return (s:gsub('^%s+', ''):gsub('%s+$', '')) | |||
end | end | ||
- | util.trim = trim | ||
function | |||
function util.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 | |||
function util.getEntityType(id) | |||
if type(id) ~= 'string' then return nil end | |||
if id:match('^Q%d+$') then return 'item' end | |||
if id:match('^P%d+$') then return 'property' end | |||
if id:match('^L%d+$') then return 'lexeme' end | |||
return nil | |||
end | |||
function util.detectEntityId(args) | |||
local ordered = {} | |||
local function push(v) | |||
if v ~= nil and v ~= '' then table.insert(ordered, v) end | |||
end | |||
push(args.L or args.lexeme or args.l) | |||
push(args.P or args.p) | |||
push(args.Q or args.q) | |||
push(args.entity or args.Entity) | |||
push(args.id or args.ID) | |||
push(args[1]) | |||
for _, v in ipairs(ordered) do | |||
local s = trim(tostring(v)) | |||
if s ~= '' then | |||
local id = s:match('^[QPL]%d+$') | |||
if not id then id = s:match('^.-:([QPL]%d+)$') end | |||
if id then return id end | |||
end | |||
end | |||
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 | |||
function util.getContentLangCode() | |||
local code = 'de' | |||
if mw.getContentLanguage then | |||
local cl = mw.getContentLanguage() | |||
if cl and cl.getCode then | |||
code = cl:getCode() | |||
end | |||
end | |||
return code | |||
end | |||
function util.getEntityLabelOrLemma(entityId) | |||
if not entityId then return nil end | |||
local etype = util.getEntityType(entityId) | |||
if etype == 'lexeme' then | |||
local ok, entity = pcall(mw.wikibase.getEntity, entityId) | |||
if ok and type(entity) == 'table' and type(entity.lemmas) == 'table' then | |||
local lang = util.getContentLangCode() | |||
local l = entity.lemmas[lang] | |||
if l and type(l) == 'table' and l.value and l.value ~= '' then | |||
return l.value | |||
end | |||
for _, obj in pairs(entity.lemmas) do | |||
if type(obj) == 'table' and obj.value and obj.value ~= '' then | |||
return obj.value | |||
end | |||
end | |||
end | |||
end | |||
local ok, label = pcall(mw.wikibase.getLabel, entityId) | |||
if ok and type(label) == 'string' and label ~= '' then return label end | |||
return entityId | |||
end | |||
function util.getDescription(entityId) | |||
local ok, desc = pcall(mw.wikibase.getDescription, entityId) | |||
if ok and type(desc) == 'string' and desc ~= '' then return desc end | |||
return nil | |||
end | |||
function util.buildEntityLink(entityId, linkText, target) | |||
linkText = trim(tostring(linkText or '')):gsub('[\r\n]+', ' ') | |||
target = (target and tostring(target):lower()) or 'special' | |||
if target == 'ns' then | |||
local etype = util.getEntityType(entityId) | |||
local prefix = | |||
(etype == 'item' and 'Item:') or | |||
(etype == 'property' and 'Property:') or | |||
(etype == 'lexeme' and 'Lexeme:') or | |||
'' | |||
local text = (linkText ~= '' and linkText) or entityId | |||
return string.format('[[%s%s|%s]]', prefix, entityId, text) | |||
end | |||
-- Default: Special:EntityPage/... | |||
local text = (linkText ~= '' and linkText) or entityId | |||
return string.format('[[Special:EntityPage/%s|%s]]', entityId, text) | |||
end | |||
function util.setShortdesc(frame, text) | |||
if not frame then return '' end | |||
text = trim(tostring(text or '')):gsub('[\r\n]+', ' ') | |||
if text == '' then return '' end | |||
local ok, result = pcall(function() | |||
return frame:callParserFunction('SHORTDESC', text) | |||
end) | |||
if not ok then return '' end | |||
return result or '' | |||
end | end | ||
return | return util | ||
Version vom 16. Dezember 2025, 13:10 Uhr
Die Dokumentation für dieses Modul kann unter Modul:Wikibase/Doku erstellt werden
local util = {}
local function trim(s)
if type(s) ~= 'string' then return s end
return (s:gsub('^%s+', ''):gsub('%s+$', ''))
end
util.trim = trim
function util.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
function util.getEntityType(id)
if type(id) ~= 'string' then return nil end
if id:match('^Q%d+$') then return 'item' end
if id:match('^P%d+$') then return 'property' end
if id:match('^L%d+$') then return 'lexeme' end
return nil
end
function util.detectEntityId(args)
local ordered = {}
local function push(v)
if v ~= nil and v ~= '' then table.insert(ordered, v) end
end
push(args.L or args.lexeme or args.l)
push(args.P or args.p)
push(args.Q or args.q)
push(args.entity or args.Entity)
push(args.id or args.ID)
push(args[1])
for _, v in ipairs(ordered) do
local s = trim(tostring(v))
if s ~= '' then
local id = s:match('^[QPL]%d+$')
if not id then id = s:match('^.-:([QPL]%d+)$') end
if id then return id end
end
end
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
function util.getContentLangCode()
local code = 'de'
if mw.getContentLanguage then
local cl = mw.getContentLanguage()
if cl and cl.getCode then
code = cl:getCode()
end
end
return code
end
function util.getEntityLabelOrLemma(entityId)
if not entityId then return nil end
local etype = util.getEntityType(entityId)
if etype == 'lexeme' then
local ok, entity = pcall(mw.wikibase.getEntity, entityId)
if ok and type(entity) == 'table' and type(entity.lemmas) == 'table' then
local lang = util.getContentLangCode()
local l = entity.lemmas[lang]
if l and type(l) == 'table' and l.value and l.value ~= '' then
return l.value
end
for _, obj in pairs(entity.lemmas) do
if type(obj) == 'table' and obj.value and obj.value ~= '' then
return obj.value
end
end
end
end
local ok, label = pcall(mw.wikibase.getLabel, entityId)
if ok and type(label) == 'string' and label ~= '' then return label end
return entityId
end
function util.getDescription(entityId)
local ok, desc = pcall(mw.wikibase.getDescription, entityId)
if ok and type(desc) == 'string' and desc ~= '' then return desc end
return nil
end
function util.buildEntityLink(entityId, linkText, target)
linkText = trim(tostring(linkText or '')):gsub('[\r\n]+', ' ')
target = (target and tostring(target):lower()) or 'special'
if target == 'ns' then
local etype = util.getEntityType(entityId)
local prefix =
(etype == 'item' and 'Item:') or
(etype == 'property' and 'Property:') or
(etype == 'lexeme' and 'Lexeme:') or
''
local text = (linkText ~= '' and linkText) or entityId
return string.format('[[%s%s|%s]]', prefix, entityId, text)
end
-- Default: Special:EntityPage/...
local text = (linkText ~= '' and linkText) or entityId
return string.format('[[Special:EntityPage/%s|%s]]', entityId, text)
end
function util.setShortdesc(frame, text)
if not frame then return '' end
text = trim(tostring(text or '')):gsub('[\r\n]+', ' ')
if text == '' then return '' end
local ok, result = pcall(function()
return frame:callParserFunction('SHORTDESC', text)
end)
if not ok then return '' end
return result or ''
end
return util