Zum Inhalt springen

Modul:WikibaseP

aus AufEinWort, der freien Forschung

Die Dokumentation für dieses Modul kann unter Modul:WikibaseP/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 value = tostring(v):lower()
	return value == 'nein' or value == '0' or value == 'no' or value == 'false'
end

-- SMW-Konfiguration der Property; ohne P-Abschnitt bleibt die einfache Anzeige.
local function getSmwConfig()
	return cfg and cfg.smw and cfg.smw.P
end

local function buildSmw(label, subtitle)
	local c = getSmwConfig()
	if not c then return '' end
	return util.smwAnnotate({
		[c.propArt] = c.valArt or 'P',
		[c.propLabel] = label or '',
		[c.propDesc] = subtitle or '',
	})
end

local function buildVisibleSmwTable(label, subtitle)
	local c = getSmwConfig()
	if not c then return '' end
	local t = mw.html.create('table')
	t:addClass('aew-wb-smwtable')
	local function row(key, value)
		if not value or value == '' then return end
		local tr = t:tag('tr')
		tr:tag('th'):wikitext(key)
		tr:tag('td'):wikitext(value)
	end
	row(c.propArt, c.valArt or 'P')
	row(c.propLabel, label or '')
	row(c.propDesc, subtitle or '')
	return tostring(t)
end

local function computeSubtitle(args, entityId)
	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 Untertitel
		end
	end
	return util.getDescription(entityId)
end

local function shouldSetShortdesc(args)
	local set = args.set
	if not set or set == '' then return true end
	set = tostring(set):lower()
	if set == 'nein' or set == '0' then return false end
	return true
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 sub = computeSubtitle(args, entityId)
	if sub and sub ~= '' and shouldSetShortdesc(args) then
		util.setShortdesc(frame, sub)
	end

	-- Wie bei WikibaseQ: Annotationen unabhängig von show und set erzeugen.
	-- set steuert weiterhin ausschließlich SHORTDESC.
	local smw = buildSmw(label, sub)
	if not isNo(args.show) then
		local tableHtml = buildVisibleSmwTable(label, sub)
		if tableHtml ~= '' then
			return smw .. link .. '<br />' .. tableHtml
		end
	end

	if sub and sub ~= '' then
		return smw .. link .. '<br />' .. util.trim(sub)
	end
	return smw .. link
end

function p.shortdesc(frame)
	local args = util.getArgs(frame)
	local entityId = util.detectEntityId(args)
	if not entityId then return '' end
	local sub = computeSubtitle(args, entityId)
	if not sub or sub == '' then return '' end
	return util.setShortdesc(frame, sub)
end

return p