Module:CheckValue

Documentation for this module may be created at Module:CheckValue/doc

-- Module that check if properties have certain values
-- Author: User:Paperoastro
-- THIS IS A WORK IN PROGRESS
local wikidata = require('Module:Wikidata')

-------------------------------------------------------------------------------
-- text for the header of the table and other messagges
local h_txt = {}

h_txt["en"] = {"Item", "Property", "Expected value", "Found value"}
h_txt["it"] = {"Elemento", "Proprietà", "Valore atteso", "Valore trovato"}

-------------------------------------------------------------------------------
-- Text for error messages
local emsg = {}

emsg["en"] = {
	"Error in ChechValue.checkValue: input parameters must be at least 3!",
	"does not found"}

-------------------------------------------------------------------------------
-- LOCAL FUNCTIONS
-------------------------------------------------------------------------------
-- get error messages for languages
local function getEMsg( lang, num )
	if emsg[lang] == nil then return emsg["en"][num] end
	return emsg[lang][num]
end

-- return a link to an object (item or property). If the object does not exists,
-- return a "red link"
--local function makeLink ( id, lab, pr )
local function makeLink ( id, lang )

	-- Link to an item
	if string.upper(id:sub(1,1)) == 'Q' then
		if mw.wikibase.getEntityObject( id ) then
			return wikidata.formatEntity( id, {lang=lang} )
		else
			return "[[" .. id .. "]]"
		end
	-- Link to a property
	elseif string.upper(id:sub(1,1)) == 'P' then
		if mw.wikibase.getEntityObject( id ) then
			return "[[Property:" .. id .. "|" ..  wikidata._getLabel( id, lang ) .. "]]"
		else
			return "[[Property:" .. id .. "|" .. id .."]]"
		end
	-- Wikilink to the id
	else
		return "[[" .. id .. "]]"
	end
end  -- function makeLink

-------------------------------------------------------------------------------
-- PUBLIC FUNCTIONS
-------------------------------------------------------------------------------
local p = {}

-------------------------------------------------------------------------------
-- return the header of the table
function p.Header( frame )
    local lang = frame:preprocess("{{int:lang}}") -- user language

    if ( h_txt[lang] == nil ) then
        lang = "en"
    end

	local s = "{|class=\"wikitable\"\n" .. "|-\n!"
	for i = 1,4 do
	    s = s .. h_txt[lang][i] .. " !! "
    end

    return s .. "\n"
end

-------------------------------------------------------------------------------
-- return the footer of the tabe
function p.Footer()
    return "|}\n"
end

-------------------------------------------------------------------------------
-- check the value of the property and return a line of the table
function p.checkValue( frame )
    local lang = frame:preprocess("{{int:lang}}") -- user language
 
    -- check input parameters
    local nargs = 0
    for _ in pairs(frame.args) do nargs = nargs + 1 end

    if ( nargs < 3 ) then
        return "\n|}\n" .. getEMsg( lang, 1 )
    end

    nargs = nargs - nargs % 3  -- consider only completed trios

    local s = ""

    for i = 1,nargs,3 do
	    local itemId = frame.args[i]       -- item to check
        local propertyId = frame.args[i+1] -- property of the item to check
        local expectedId = frame.args[i+2] -- value that should have the property
		
        local pos = {}  -- position of the property in the item
        local np = 0    -- numbers of properties found
		
        -- columns of the output table
        local column = {"", "", "", "", "[[File:X_mark.svg|16px]]"}

        -- text for the colums
        column[1] = makeLink( itemId, lang )
		column[2] = makeLink( propertyId, lang )
		column[3] = makeLink( expectedId, lang )
		
		-- if exist objects itemId, propertyId and expectedId, compare the value
		-- of propertyId in itemId with expectedId
		if mw.wikibase.getEntityObject( itemId ) and
			mw.wikibase.getEntityObject( propertyId ) and
			mw.wikibase.getEntityObject( expectedId ) then

			-- find property "property" in the item
			claims = wikidata.getClaims( {item=itemId, property=propertyId} )
			
			-- property "propertyId" not found in "itemId"
			if not claims then
				column[4] = propertyId .. " " .. getEMsg( lang, 2 )
				
			-- property "propertyId" found in "itemId"
			else
				for _,claim in pairs( claims ) do
					local foundId = wikidata.getDatavalue( claim.mainsnak, {format="raw"} )
					
					if column[4] ~= "" then column[4] = column[4] .. "<br/>" end
					column[4] = column[4] ..  makeLink( foundId, lang )
					
					-- check expected and found values
					if foundId == expectedId then
						column[5] = "[[File:Yes_check.svg|16px]]"
					end
				end  -- for claim
			end  -- if not claims
		end -- if mw.wikibase.getEntityObject

        -- Write the output
        s = s .. "|-\n" ..
            "| " .. column[1] .. " || " .. column[2] .. " || " .. column[3] ..
            " || " .. column[4] .. " || " .. column[5] .. "\n"
    end  -- for i

    return s 	
end

-------------------------------------------------------------------------------
-- check the value of the property and return a line of the table
function p.testcase( frame )
    frame.args = {"Q15053549", "P279", "Q6999"
    	,"Q27521", "P279", "Q6999"
        ,"Q6", "P279", "Q6999"
        ,"Q1", "P1", "Q6999"
        ,"Q27521", "P279", "Q6"
    }
    
    return p.Header( frame ) .. p.checkValue( frame ) .. p.Footer()
end

return p