Moduuli:Valtion tiedot

Wikipediasta
Siirry navigaatioon Siirry hakuun

Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:Valtion tiedot/ohje

local p = {}

-- This is used to get a value, or a comma separated list of them if multiple values exist
p.testHeadOfTheState = function(frame)
	local propertyID = mw.text.trim(frame.args[1] or "")
	local input_parm = mw.text.trim(frame.args[2] or "")
	local expected_result = mw.text.trim(frame.args[3] or "")
	if input_parm == "FETCH_WIKIDATA" then
		local entity = mw.wikibase.getEntityObject()
		
		if not entity then
			return "";
		end
		
		local claims = entity.claims[propertyID]
		if claims then
			-- if wiki-linked value output as link if possible
			
			if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
				local out_preferred = {}
				local out_normal = {}

				for k, v in pairs(claims) do
					
					if not v.mainsnak.datavalue then
						return ""
					end
					
					local sitelink = mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"])
					local label = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"])
					if label == nil then label = "Q" .. v.mainsnak.datavalue.value["numeric-id"] end
					
					local rank = v.rank
					if rank == "preferred" then
						out_preferred[#out_preferred + 1] = label
					end	
					
					if rank == "normal" then
						out_normal[#out_normal + 1] = label
					end
				end
			    
			    local found=1;
				if (#out_preferred > 0) then
					return p.isWikidataValueExpected(out_preferred, expected_result)
					--return table.concat(out_preferred, "; ")
				else
					return p.isWikidataValueExpected(out_normal, expected_result)
--					return table.concat(out_normal, "; ")
				end
				
			else
				local out = {}
				out[#out + 1] =  tostring(entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value)
				return p.isWikidataValueExpected(out, expected_result)
			end
		else
			return ""
		end
	else
		return input_parm
	end
end

p.isWikidataValueExpected = function(input, expected_result)
	local found=1
	for k,target_str in pairs(input) do 
		 if (target_str and found) then
		 	found=mw.ustring.find( expected_result, target_str, 1, true )
		 end
	end
	if found then
		return 1
	else
		return ""
	end
end	

-- Tulostaa muuttujan sisällön muotoilemattomana json:na.
function p.dump(o)
    if type(o) == 'table' then
        local s = '{ '
        for k,v in pairs(o) do
                if type(k) ~= 'number' then k = '"'..k..'"' end
                if type(v) == 'nil' then k = '"'..tostring(k)..'"' end

                s = s .. '['..k..'] = ' .. p.dump(v) .. ','
        end
        return s .. '}'
    else
        return tostring(o)
    end
end

function p.debug()
   	local str=mw.wikibase.entity.claimRanks
   	return p.dump(str)
end



return p