Přeskočit na obsah

Modul:WikidataQualifiers

Z Wikizdrojů, volně dostupné knihovny

Dokumentaci tohoto modulu lze vytvořit na stránce Nápověda:Modul:WikidataQualifiers

-- Modul:WikidataQualifiers
-- Autor: ChatGPT / upraveno pro české Wikizdroje
-- Účel: Zjišťovat přítomnost kvalifikátorů (např. P155, P156) u výroku, typicky P1433.
-- Modul:WikidataQualifiers (verze s kontrolou podle hodnoty P1433)
local p = {}

local function getEntity()
	local id = mw.wikibase.getEntityIdForCurrentPage()
	if not id then return nil end
	return mw.wikibase.getEntityObject(id)
end

-- Vrátí QID (nebo nil) z vlastnosti dané entity
local function getPropId(entity, prop)
	if not entity or not entity.claims or not entity.claims[prop] then return nil end
	for _, st in pairs(entity.claims[prop]) do
		if st.mainsnak and st.mainsnak.datavalue then
			local v = st.mainsnak.datavalue.value
			if v and v.id then return v.id end
		end
	end
	return nil
end

local function hasQualifierForProp(entity, prop, qualifier)
	if not entity or not entity.claims or not entity.claims[prop] then return false end
	for _, statement in pairs(entity.claims[prop]) do
		if statement.qualifiers and statement.qualifiers[qualifier] then
			return true
		end
	end
	return false
end

function p.missing(frame)
	local args = frame.args
	local prop = args.property or args[1]
	local qual = args.qualifier or args[2]
	if not prop or not qual then return "" end
	local entity = mw.wikibase.getEntityObject()
	if not entity or not entity.claims or not entity.claims[prop] then return "" end

	local found_with_qual = false
	local found_without_qual = false

	for _, st in pairs(entity.claims[prop]) do
		if st.mainsnak and st.mainsnak.datavalue then
			if st.qualifiers and st.qualifiers[qual] then
				found_with_qual = true
			else
				found_without_qual = true
			end
		end
	end

	-- pokud aspoň jeden výskyt má kvalifikátor, považuj za OK
	if found_with_qual then
		return ""
	elseif found_without_qual then
		return "1"
	else
		return ""
	end
end


function p.has(frame)
	local args = frame.args
	local prop = args.property or args[1]
	local qual = args.qualifier or args[2]
	if not prop or not qual then return "" end
	local entity = getEntity()
	if not entity then return "" end

	if hasQualifierForProp(entity, prop, qual) then
		return "1"
	else
		return ""
	end
end

return p