Zum Inhalt springen

Modul:WikibaseQ

aus AufEinWort, der freien Forschung
Dies ist eine alte Version dieser Seite, zuletzt bearbeitet am 20. Oktober 2026 um 06:17 Uhr durch Nikolaos (Diskussion | Beiträge). Sie kann sich erheblich von der aktuellen Version unterscheiden.

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

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

local p = {}

local function isNo(v)
	if v == nil then return false end
	local s = tostring(v):lower()
	return (s == 'nein' or s == '0' or s == 'no' or s == 'false')
end

local function buildSubtitle(args, entityId)
	-- Q: sichtbarer Untertitel = Beschreibung
	local s = args.subtitle
	if s and s ~= '' then
		s = tostring(s)
		local low = s:lower()
		if low == 'nein' or low == '0' then return nil end
		if low ~= 'ja' and low ~= 'auto' then
			return util.trim(s) -- eigener Text
		end
	end
	return util.getDescription(entityId)
end

-- Präfixe gehören zum gespeicherten Wert, nicht zum P-Label/Attributnamen.
-- Slotweise lesen, damit die Zuordnung auch bei gleichen P-Labels eindeutig ist.
local function getPrefixedStatements(entityId)
    local c = (cfg and cfg.smw and cfg.smw.Q) or {}
    local rows, notices, seen = {}, {}, {}
    for slot = 1, 6 do
        local property = util.trim(tostring(c['prop' .. slot] or ''))
        if property ~= '' and not seen[property] then
            seen[property] = true
            if not property:match('^P[1-9]%d*$') then
                notices[#notices + 1] = 'Wikibase/config: prop' .. slot ..
                    ' enthält keine gültige P-ID: ' .. property
            else
                local values, warnings = util.getConfiguredQStatements(
                    entityId, { prop1 = property })
                local prefix = tostring(c['prefix' .. slot] or '')
                for _, entry in ipairs(values) do
                    entry.value = prefix .. entry.value
                    rows[#rows + 1] = entry
                end
                for _, warning in ipairs(warnings) do
                    notices[#notices + 1] = warning
                end
            end
        end
    end
    return rows, notices
end

-- Einmal pro render auflösen; Anzeige und Annotationen verwenden dieselben Namen.
local function getBaseRows(entityId, label, desc)
    local c = cfg and cfg.smw and cfg.smw.Q
    local rows = {}
    if not c then return rows end
    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
    -- SMW speichert das lokale Seitenziel, die Tabelle zeigt nur die Kennung.
    local target = util.getEntityPageTarget(entityId, cfg.entityNamespaces)
    local display = '[[:' .. target .. '|' .. entityId .. ']]'
    -- add(c.propArt, target, display)
    add(c.propArt, entityId, display)
    add(c.propLabel, label)
    add(c.propDesc, desc)
    return rows
end


-- Link zum gespeicherten SMW-Seitenwert (einschließlich Präfix).
-- Führender Doppelpunkt verhindert Kategoriezuordnung/Dateieinbettung.
local function buildOverviewLink(value)
    local text = util.smwLiteral(value)
    local title = mw.title.new(value)
    if not title then return text end
    local target = title.prefixedText
    if title.fragment and title.fragment ~= '' then
        target = target .. '#' .. title.fragment
    end
    return '[[:' .. util.smwLiteral(target) .. '|' .. text .. ']]'
end

local function buildVisibleSmwTable(baseRows, extraRows)
	local c = cfg and cfg.smw and cfg.smw.Q
	if not c then
		return ''
	end


	-- HTML statt Wikitext-Tabelle (vermeidet Pipe/Parser-Kanten)
	local t = mw.html.create('table')
	t:addClass('aew-wb-smwtable')

	local function row(k, v)
		if not v or v == '' then return end
		local tr = t:tag('tr')
		tr:tag('th'):wikitext(k)
		tr:tag('td'):wikitext(v)
	end

	for _, entry in ipairs(baseRows) do
		row(util.smwLiteral(entry.property), entry.display)
	end
	for _, entry in ipairs(extraRows or {}) do
		row(util.smwLiteral(entry.property), buildOverviewLink(entry.value))
	end

	return tostring(t)
end

function p.link(frame)
	local args = util.getArgs(frame)
	local entityId = util.detectEntityId(args)
	if not entityId or not entityId:match('^Q%d+$') then return '' end

	local label = args.label or util.getEntityLabelOrLemma(entityId) or entityId
	local line1 = string.format('%s %s', entityId, label)
	return util.buildEntityLink(entityId, line1, args.target)
end

function p.render(frame)
	local args = util.getArgs(frame)
	local entityId = util.detectEntityId(args)
	if not entityId or not entityId:match('^Q%d+$') then return '' end

	local label = args.label or util.getEntityLabelOrLemma(entityId) or entityId
	local line1text = string.format('%s %s', entityId, label)
	local link = util.buildEntityLink(entityId, line1text, args.target)

	local desc = buildSubtitle(args, entityId)

	-- Bisherige Annotationen plus konfigurierte Q-Aussagen.
	local extraRows, notices = getPrefixedStatements(entityId)
	local baseRows = getBaseRows(entityId, label, desc)
	local smw = util.setSmwRows(frame, baseRows) .. util.setSmwRows(frame, extraRows)
	for _, notice in ipairs(notices) do
		smw = smw .. '<span class="error">' .. util.smwLiteral(notice) .. '</span>'
	end

	-- Sichtbarkeit der SMW-Werte:
	-- Default = ja, abschaltbar mit |show=nein
	local show = not isNo(args.show)

	-- Fallback-Anzeige (wie bisher): Link + <br/> + Beschreibung
	if not show then
		if desc and desc ~= '' then
			return smw .. link .. '<br />' .. util.trim(desc)
		end
		return smw .. link
	end

	-- Die äußere Vorlage liefert die Überschrift „Wikibase“.
	local tableHtml = buildVisibleSmwTable(baseRows, extraRows)
	if tableHtml ~= '' then
		return smw .. tableHtml
	end

	-- Wenn config fehlt, fällt es auf die alte Anzeige zurück
	if desc and desc ~= '' then
		return smw .. link .. '<br />' .. util.trim(desc)
	end
	return smw .. link
end

function p.shortdesc(frame)
	-- Q: kein SHORTDESC-Setzen hier (Q hat Sitelink-System), daher leer
	return ''
end

return p