Zum Inhalt springen

Modul:WikibaseQ

aus AufEinWort, der freien Forschung
Dies ist eine alte Version dieser Seite, zuletzt bearbeitet am 15. Oktober 2026 um 13:15 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

local function buildSmw(entityId, label, desc)
	local c = cfg and cfg.smw and cfg.smw.Q
	if not c then
		return ''
	end

	return util.smwAnnotate({
		[c.propArt]   = c.valArt or 'Q',
		[c.propLabel] = label or '',
		[c.propDesc]  = desc or '',
	})
end


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

	local artVal = c.valArt or 'Q'

	-- 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

	row(c.propArt,   artVal)
	row(c.propLabel, label or '')
	row(c.propDesc,  desc or '')
	for _, entry in ipairs(extraRows or {}) do
		row(util.smwLiteral(entry.property), util.smwLiteral(entry.value))
	end

	return tostring(t)
end

function p.link(frame)
	local args = util.getArgs(frame)
	local entityId = util.detectEntityId(args)
	if not entityId 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 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 = util.getConfiguredQStatements(
		entityId, (cfg and cfg.smw and cfg.smw.Q) or {})
	local smw = buildSmw(entityId, label, desc) .. util.smwAnnotateRows(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

	-- Sichtbare SMW-Anzeige: Link + (SMW-Tabelle)
	local tableHtml = buildVisibleSmwTable(entityId, label, desc, extraRows)
	if tableHtml ~= '' then
		return smw .. link .. '<br />' .. 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