Benutzer:MisterSynergy/Spielwiese/Modul:Wikidata-Vergleich

local p = {}

function p.compareValues(frame)
-- initialize
-- 	local pframe = frame:getParent()
-- 	local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
-- 	local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template
	
	local property = frame.args['eigenschaft'] or nil
	local localValue = frame.args['lokaler_identifikator'] or ''
	local namespaces = frame.args['namensräume'] or '0'

-- 	local cat_no_item = frame.args['kat_kein_objekt'] or 'Kategorie:Wikipedia:Artikel ohne Wikidata-Datenobjekt' -- better call {{Wikidata-Registrierung}} explicitly in the template
	local cat_local_missing = frame.args['kat_fehlt_lokal'] or nil
	local cat_wd_missing = frame.args['kat_fehlt_in_wd'] or nil
	local cat_wd_different = frame.args['kat_wd_verschieden'] or nil
-- 	local cat_same_value = frame.args['kat_gleicher_wert'] or nil -- undesired at dewiki
--	local cat_using_wd = frame.args['kat_nutzt_wd'] or nil -- we don't use identifiers directly yet

	if not property then -- no P-identifier provided
		return ''
	end
	if not string.match(property, 'P[1-9][0-9]*') then -- invalid P-identifier provided
		return ''
	end
	local propObject = mw.wikibase.getEntity(property)
	if not propObject then
		return ''
	end
	if propObject.datatype ~= 'external-id' then
		return ''
	end

-- check namespace
	local namespaceOk = false
	local ns = mw.title.getCurrentTitle().namespace
	for v in mw.text.gsplit( namespaces, ',', true) do
		if tonumber(v) == ns then
			namespaceOk = true
		end
	end
	if not namespaceOk then -- ignore if not in approved namespace
		return ''
	end

-- check whether the article is connected to Wikidata
	local entity = mw.wikibase.getEntity()
	if not entity then -- ignore case
		if cat_no_item then
			return '[[' .. cat_no_item .. ']]'
		end
		return ''
	end

-- check whether any claims of the given property are found in the connected item
	local claims = entity.claims or {}
	local hasProp = claims[property]
	if not hasProp then -- no claim of that property
		if localValue ~= '' and cat_wd_missing then
			return '[[' .. cat_wd_missing .. ']]'
		end
		return ''
	end

-- claim(s) of that property found
	if #hasProp == 1 then -- exactly one claim found, so simply process it
		local claim = claims[property][1]
		if (claim.mainsnak.snaktype == 'novalue' or claim.mainsnak.snaktype == 'somevalue') and claim.rank ~= 'deprecated' and localValue ~= '' and cat_wd_missing then -- Wikidata: novalue or unknown value, but local value given
			return '[[' .. cat_wd_missing .. ']]'
		end
		if claim.mainsnak.snaktype == 'value' and claim.rank ~= 'deprecated' then -- Wikidata custom value
			if localValue == '' and cat_local_missing then -- no local value provided
				return '[[' .. cat_local_missing .. ']]'
			elseif localValue ~= claim.mainsnak.datavalue.value and cat_wd_different then -- different local value provided
				return '[[' .. cat_wd_different .. ']]'
			end
		end
	else -- more than one claim found, loop over them
		if localValue == '' then -- no or empty local value provided
			local throw_wp_missing_cat = true
			for i, claim in ipairs(hasProp) do
				if (claim.mainsnak.snaktype == 'novalue' or claim.mainsnak.snaktype == 'somevalue') and claim.rank ~= 'deprecated' then
					throw_wp_missing_cat = false -- disputable
				end
			end
			if throw_wp_missing_cat and cat_local_missing then
				return '[[' .. cat_local_missing .. ']]'
			end
		else -- custom local value provided
			local throw_wd_missing_cat = true
			local throw_different_value_cat = true
			for i, claim in ipairs(hasProp) do
				if claim.mainsnak.snaktype == 'value' and claim.rank ~= 'deprecated' and claim.mainsnak.datavalue.value == localValue then
					throw_wd_missing_cat = false
					throw_different_value_cat = false
				end
			end
			local cats = ''
			if throw_wd_missing_cat and cat_wd_missing then
				cats = cats .. '[[' .. cat_wd_missing .. ']]'
			end
			if throw_different_value_cat and cat_wd_different then
				cats = cats .. '[[' .. cat_wd_different .. ']]'
			end
			return cats
		end
	end
	return '' -- all other cases are okay
end

return p or ''