Modul:Benutzer:MisterSynergy/test

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Die Dokumentation für dieses Modul kann unter Modul:Benutzer:MisterSynergy/test/Doku erstellt werden

local function check_reference(reference, cas_from_template)
	if not reference.snaks then
		return false -- empty reference
	end

	for ref_prop_qualifier, values in pairs(reference.snaks) do
		if ref_prop_qualifier == 'P248' then
			for i, value in ipairs(values) do
				if value.snaktype == 'value' and value.datavalue.value.id == 'Q18907859' then
					return true
				end
			end
		end
		if ref_prop_qualifier == 'P854' then
			for i, value in ipairs(values) do
				if value.snaktype == 'value' and value.datavalue.value == 'https://commonchemistry.cas.org/detail?cas_rn=' .. cas_from_template then
					return true
				end
			end
		end
	end

	return false -- reference without qualifiers we are looking for
end

local function check_claim(statement, cas_from_template)
	if statement.rank == 'deprecated' then
		return false -- ignore claim with deprecated rank
	end

	if statement.mainsnak.snaktype ~= 'value' then
		return false -- ignore claim with 'unknown value' or 'no value'
	end

	if statement.mainsnak.datavalue.value ~= cas_from_template then
		return false -- ignore claim with different CAS number than in infobox
	end

	if not statement.references then
		return false -- ignore claim without references
	end

	for i, reference in ipairs(statement.references) do
		local success, result = pcall(check_reference, reference, cas_from_template)
		if success and result then
			return true
		end
	end

	return false -- ignore, if no suitable references were found
end

local p = {}

function p.entry_CAS_Common_Chemistry(frame)
	local cas_from_template = mw.text.trim(frame.args['cas'] or '')
	if cas_from_template == nil or #cas_from_template == 0 then
		return '' -- no local CAS number provided
	end

	local statements
	local qid = mw.wikibase.getEntityIdForCurrentPage()
	if qid then
		statements = mw.wikibase.getAllStatements( qid, 'P231' )
	end
	if statements == nil or #statements == 0 then -- no CAS property P231 found in connected Wikidata item
		return ''
	end

	for i, statement in ipairs(statements) do
		local success, result = pcall(check_claim, statement, cas_from_template)
		if success and result then
			return '1'
		end
	end

	return '' -- no suitable claims found at Wikidata
end

return p