Module:Wikidata exhibition history

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Documentation for this module may be created at Module:Wikidata exhibition history/doc

Code

local p = {}
--=========
local getArgs       = require('Module:Arguments').getArgs
local propertyValue = require('Module:RamaTesting').propertyValue
----------------
local function propertyValue( q_id, property_id )
	local entity     = mw.wikibase.getEntityObject(q_id)
	if not entity then return "No entity " .. q_id .. " found :( " end
	if entity:formatPropertyValues( property_id ) then
		return entity:formatPropertyValues( property_id ).value
	else
		return ""
	end
end
----------------
local function propertyQID( q_id, property_id )
	local entity     = mw.wikibase.getEntityObject(q_id)
	if not entity then return "No entity " .. q_id .. " found :( " end	
	
	-- return mw.wikibase.getBestStatements( q_id, property_id )[1]
	if entity['claims'][property_id]                                              == nil then return nil end
	if entity['claims'][property_id][1]                                           == nil then return nil end
	if entity['claims'][property_id][1]['mainsnak']                               == nil then return nil end
	if entity['claims'][property_id][1]['mainsnak']['datavalue']                  == nil then return nil end
	if entity['claims'][property_id][1]['mainsnak']['datavalue']['value']         == nil then return nil end
	if entity['claims'][property_id][1]['mainsnak']['datavalue']['value']['id']   == nil then return nil end
	
	return entity['claims'][property_id][1]['mainsnak']['datavalue']['value']['id']
end

----------------
local function propertyQIDs( q_id, property_id )
	local entity     = mw.wikibase.getEntityObject(q_id)
	if not entity then return "No entity " .. q_id .. " found :( " end	
	
	-- return mw.wikibase.getBestStatements( q_id, property_id )[1]
	if entity['claims'][property_id]                                              == nil then return nil end
	work = ""
	for i,j in entity['claims'][property_id] do
		if entity['claims'][property_id][i]                                           == nil then return nil end
		if entity['claims'][property_id][i]['mainsnak']                               == nil then return nil end
		if entity['claims'][property_id][i]['mainsnak']['datavalue']                  == nil then return nil end
		if entity['claims'][property_id][i]['mainsnak']['datavalue']['value']         == nil then return nil end
		if entity['claims'][property_id][i]['mainsnak']['datavalue']['value']['id']   == nil then return nil end
		
		work = work .. entity['claims'][property_id][i]['mainsnak']['datavalue']['value']['id'] .. "<br />"
	end
	return work
end

-------------
function p.display_exhibition_history( frame )
	local qid = getArgs(frame)["qid"]
	local history = propertyValue(qid, "P608") .. " <br/> " .. propertyQIDs(qid, "P608")
	
	
	
	--[[
	local creator = propertyValue( qid, "P170" )
	if not creator or creator == "" then return "" end
	
	-- if Wikidata item has a "Creator" property (P170) and if the associated Q-item contains a "Commons Creator page" property (P1472),
	-- then retrieve Commons Creator page and use it to call a {{Creator:...}} template
	-- (robust if the naming conventions are different on Wikidata and Commons)
	local creator_qid  = propertyQID( qid, "P170" )
	if creator_qid and creator_qid ~= "" then 
		local creator_page = propertyValue( creator_qid, "P1472" )
		if creator_page and creator_page ~= "" then
			local creatorFile = mw.title.makeTitle( "Creator", creator_page)
			if creatorFile.exists then return frame:expandTemplate{ title = "Creator:" .. creator_page , args = { } } end
		end
		-- return creator_page 
	end
	
	-- if not the case, fallback on the Label for the "Creator" property (P170)
	-- Test if a "Creator:" page of that name exists on Commons, and use it
	local creatorFile = mw.title.makeTitle( "Creator", creator) 
	if creatorFile.exists then return frame:expandTemplate{ title = "Creator:" .. creator , args = { } } end
	-- else, fallback on displaying the Creator Label 
	return creator
	]]--
	
	
	return history
end
--=========
return p