Module:RamaTesting

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

This module pulls information from Wikidata about an item (typicall museum exhibit item, painting, etc.). It provides the technology behind User:Rama/Catdef, which automatically fills a "Category Description" template when given a Wikidata Qid.

Code

local p = {}

----------------
-- These taken for debugging purposes from 
-- http://lua-users.org/wiki/
-- on advise from Sam <3
local function table_print (tt, indent, done)
  done = done or {}
  indent = indent or 0
  if type(tt) == "table" then
    local sb = {}
    for key, value in pairs (tt) do
      table.insert(sb, string.rep (" ", indent)) -- indent it
      if type (value) == "table" and not done [value] then
        done [value] = true
        table.insert(sb, "{\n");
        table.insert(sb, table_print (value, indent + 2, done))
        table.insert(sb, string.rep (" ", indent)) -- indent it
        table.insert(sb, "}\n");
      elseif "number" == type(key) then
        table.insert(sb, string.format("\"%s\"\n", tostring(value)))
      else
        table.insert(sb, string.format(
            "%s = \"%s\"\n", tostring (key), tostring(value)))
       end
    end
    return table.concat(sb)
  else
    return tt .. "\n"
  end
end

local function to_string( tbl )
    if     "nil"    == type( tbl ) then return tostring(nil)
    elseif "table"  == type( tbl ) then return table_print(tbl)
    elseif "string" == type( tbl ) then return tbl
    else    return tostring(tbl) end
end

----------------
-- from http://stackoverflow.com/questions/1426954/split-string-in-lua
local function split(s, delimiter)
    result = {};
    for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match);
    end
    return result;
end
----------------
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	
	-- claims = entity.claims[property_id]
	-- return claims[1].mainsnak.datavalue.value.text
	return entity:formatPropertyValues( property_id ).value
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 subPropertyQID( q_id, property_id, sub_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]['qualifiers']   == nil then return nil end
	if entity['claims'][property_id][1]['qualifiers'][sub_property_id]   == nil then return nil end
	if entity['claims'][property_id][1]['qualifiers'][sub_property_id][1]   == nil then return nil end
	if entity['claims'][property_id][1]['qualifiers'][sub_property_id][1]['datavalue']   == nil then return nil end
	if entity['claims'][property_id][1]['qualifiers'][sub_property_id][1]['datavalue']['value']   == nil then return nil end
	if entity['claims'][property_id][1]['qualifiers'][sub_property_id][1]['datavalue']['value']['id']   == nil then return nil end
	
	return entity['claims'][property_id][1]['qualifiers'][sub_property_id][1]['datavalue']['value']['id']
end

----------------

local function propertyValueForLang( q_id, property_id, lang_id )
	local entity     = mw.wikibase.getEntityObject(q_id)
	if not entity then
		return "No entity " .. q_id .. " found :( "
	end	
	
	if not lang_id then
		return "Error: No lang_id given :( "
	end

	local return_text = nil
	claims = entity.claims[property_id]
	if not claims then return nil end
	
	for i,j in pairs(claims) do 
		this_text = j.mainsnak.datavalue.value.text
		this_lang = j.mainsnak.datavalue.value.language
		if this_lang == lang_id then
			-- return this_text .. " -> " .. lang_id
			return_text = this_text
		end
	end
	if return_text == nil then
		return_text = "returning in default language: " .. propertyValue ( q_id, property_id )
		-- return_text =  propertyValue ( q_id, property_id )
	end
	-- At this stage, return_text can still be nil! That is normal (e.g. no subtitle)
	return return_text
end

function p.propertyValueForLang( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then
		return "Parameters qid and 1 are empty"
	end
	
	local property_id = frame.args.property or frame.args[2]
	if property_id == nil then
		return "Parameters property and 2 are empty"
	end
	
	local lang_id = frame.args.language or frame.args[3] 
	if lang_id == nil then
		return "Parameters language and 3 are empty"
	end

	return propertyValueForLang(q_id, property_id, lang_id)
end

----------------
local function propertyValueInEnglish( q_id, property_id )
	--[[
	Need to implement this ASAP for:
	    - institution
	    - dimension units
	    - provenance event
	--]]
	
	local entity     = mw.wikibase.getEntityObject(q_id)
	if not entity then return "No entity " .. q_id .. " found :( " end	
	local property_value_in_local_language = entity:formatPropertyValues( property_id ).value   -- e.g. "Titre", in French
	local property_qid                     = propertyQID( q_id, property_id )                   -- e.g. P1476
	local property_value_in_english        = mw.wikibase.getLabelByLang( property_qid, "en" )   -- e.g. "Title"
	return property_value_in_english
	-- return property_qid
end

local function subPropertyValueInEnglish( q_id, property_id, sub_property_id )
	--[[
	Needed to implement :
	    - provenance event
	--]]
	
	local entity     = mw.wikibase.getEntityObject(q_id)
	if not entity then return "No entity " .. q_id .. " found :( " end	
	
	local property_qid                     = subPropertyQID( q_id, property_id, sub_property_id )
	if not property_qid then return nil end
	local property_value_in_english        = mw.wikibase.getLabelByLang( property_qid, "en" )
	return property_value_in_english
	-- return property_qid
end

----------------
local function subPropertyValue(q_id, property_id, subproperty_id)
	local entity     = mw.wikibase.getEntityObject(q_id)
	if not entity then	return "No entity " .. q_id .. " found :( "	end	
	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]['qualifiers'] == nil then return nil end
	
	local bestStatements = entity:getBestStatements( property_id )
	if entity['claims'][property_id][1]['qualifiers'][subproperty_id] == nil then return nil end
	local entityClaims   = entity['claims'][property_id][1]['qualifiers'][subproperty_id][1]
	return mw.wikibase.renderSnak( entityClaims ) 
end

----------------
function p.title( frame )
	local work = frame.args.qid
	return "this is my argument " .. work .. " this was my argument"
end
----------------
function p.propertyValue( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then	return "Parameters qid and 1 are empty"	end
	
	local property_id = frame.args.property or frame.args[2]
	if property_id == nil then return "Parameters property and 2 are empty" end
	
	return propertyValue(q_id, property_id)
end

----------------
function p.propertyQID( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then	return "Parameters qid and 1 are empty"	end
	
	local property_id = frame.args.property or frame.args[2]
	if property_id == nil then return "Parameters property and 2 are empty" end
	
	-- return propertyQID(q_id, property_id)
	return "PLOUF !"
end
----------------
function p.propertyValueInEnglish( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then	return "Parameters qid and 1 are empty"	end
	
	local property_id = frame.args.property or frame.args[2]
	if property_id == nil then return "Parameters property and 2 are empty" end
	
	return propertyValueInEnglish(q_id, property_id)
end
----------------
function p.subPropertyValue( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then return "Parameters qid and 1 are empty"	end
	
	local property_id = frame.args.property or frame.args[2]
	if property_id == nil then	return "Parameters property and 2 are empty" end
	
	local sub_property_id = frame.args.subproperty or frame.args[3]
	if sub_property_id == nil then return "Parameters subproperty and 3 are empty" end

	return subPropertyValue(q_id, property_id, sub_property_id)
end

----------------
function p.label( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then
		return "Parameters qid and 1 are empty"
	end
	local label = mw.wikibase.label( q_id )
	return "Label is \"" .. label .. "\"."
end

----------------
function p.description( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then
		return "Parameters qid and 1 are empty"
	end
	local work = mw.wikibase.description( q_id )
	work = work:gsub("^%l", string.upper)
	return work
end
----------------
function p.displayDate( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then
		return "Parameters qid and 1 are empty"
	end
	
	local work = propertyValue( q_id, "P571" )
	local isBCE = "CE"
	if string.match(work, "BCE") then isBCE = "BCE" end
	if string.match(work, "century") then
    	local date_number=work:match"([^.]*)."
    	work = frame:expandTemplate{ title = 'Other_date', args = { "century", date_number, era=isBCE } }
	elseif string.match(work, "millennium") then
		local date_number=work:match"([^.]*)."
    	work = frame:expandTemplate{ title = 'Other_date', args = { "millennium", date_number, era=isBCE } }
	end
	
	local sourcingCircumstances = subPropertyValue( q_id, "P571", "P1480" )
	if sourcingCircumstances ~= nil then work = sourcingCircumstances .. " " .. work end
	
	--local work2 = propertyValueInEnglish( q_id, "P571" )
	--if not work2 then work2 = "propertyValueInEnglish returned nil :(" end
	--return work .. "<br />" .. work2
	return work
end
----------------
function p.displayDateInterval( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then
		return "Parameters qid and 1 are empty"
	end
	
	local earliestDate = subPropertyValue( q_id, "P571", "P1319" )
	local latestDate   = subPropertyValue( q_id, "P571", "P1326" )
	
	work = ""
	if     (earliestDate ~= nil) and (latestDate == nil) then work = frame:expandTemplate{ title = 'After', args = { earliestDate } }
	elseif (earliestDate == nil) and (latestDate ~= nil) then work = frame:expandTemplate{ title = 'Before', args = { latestDate } }
	elseif (earliestDate ~= nil) and (latestDate ~= nil) then work = frame:expandTemplate{ title = 'Between', args = { earliestDate, latestDate } }
	-- work = work .. frame:expandTemplate{ title = 'Weight', args = { mass_unit, mass_value } }
	-- if     (earliestDate ~= nil) and (latestDate == nil) then work = "After " .. earliestDate
	-- elseif (earliestDate == nil) and (latestDate ~= nil) then work = "Before " .. latestDate
	-- elseif (earliestDate ~= nil) and (latestDate ~= nil) then work = "Between " .. earliestDate .. " and " .. latestDate
	else    work = nil end 
	return work
end

----------------
function p.displayDiscovery( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then
		return "Parameters qid and 1 are empty"
	end
	
	local discoverer            = propertyValue( q_id, "P61")
	local time_of_discovery     = propertyValue( q_id, "P575")
	local location_of_discovery = propertyValue( q_id, "P189")
	
        -- {{ProvenanceEvent|time=1950-03-01|type=discovery|newowner=Elias Cohen|place=The Hague}}
        work = frame:expandTemplate{ title = 'ProvenanceEvent', args = { type="discovery", newowner=discoverer, time = time_of_discovery, place=location_of_discovery } }

	return work
end

----------------
function p.displayObjectHistory( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then
		return "Parameters qid and 1 are empty"
	end
	
	-- Start seeking info in Subproperties of "Collection":
	local collectionStartTime = subPropertyValue( q_id, "P195", "P580")
	-- local collectionHasCause  = subPropertyValue( q_id, "P195", "P828")
	local collectionHasCause  = subPropertyValueInEnglish( q_id, "P195", "P828")
	local collectionDonator   = subPropertyValue( q_id, "P195", "P1028")

	-- Else, get from "Discovery"
	local discoverer            = propertyValue( q_id, "P61")
	local time_of_discovery     = propertyValue( q_id, "P575")
	local location_of_discovery = propertyValue( q_id, "P189")
	local wasDiscovered         = (discoverer and discoverer ~= "") or (time_of_discovery and time_of_discovery ~= "") or (location_of_discovery and location_of_discovery ~= "" )

	local ProvenanceEventOldOwner = collectionDonator
	local ProvenanceEventNewOwner = propertyValue( q_id, "P195" )

	-- Formating cause/EventType
	local ProvenanceEventType = collectionHasCause
	if ProvenanceEventType == nil and wasDiscovered then ProvenanceEventType = "discovery" 
	elseif 	ProvenanceEventType == "purchasing" then ProvenanceEventType = "purchase"
	elseif 	ProvenanceEventType == "lease" then ProvenanceEventType = "loan"
	end
	
	if (wasDiscovered and ProvenanceEventType == "discovery") then ProvenanceEventNewOwner = discoverer end
	local ProvenanceEventTime = collectionStartTime
	local ProvenanceEventPlace = location_of_discovery
	
    -- work = frame:expandTemplate{ title = 'ProvenanceEvent', args = { type="discovery", newowner=discoverer, time = time_of_discovery, place=location_of_discovery } }
    work = frame:expandTemplate{ title = 'ProvenanceEvent', args = { type=ProvenanceEventType, oldowner=ProvenanceEventOldOwner, newowner=ProvenanceEventNewOwner, time = ProvenanceEventTime, place=ProvenanceEventPlace } }
	if (not ProvenanceEventType and not wasDiscovered) then return "" end
	return work
end

----------------
local function getDimensionValue( q_id, property_id )
	local work = propertyValue( q_id, property_id)
	if work == nil then	return nil end
	work = split(work, " ")[1]
	-- is cases like "15±1 centimetre" we return "15" because the template does not support errors
	work = split(work, "±")[1]
	-- if decimal separator is a comma, restor a dot
	work = work:gsub(",", ".")
	return work
end

----------------
local function getDimensionUnit( q_id, property_id )
	--[[	
	-- local work = propertyValueInEnglish( q_id, property_id)
	local get_unit_label_en = require('Module:Wikidata dimension')._get_unit_label_en
	local work = get_unit_label_en( q_id, property_id )
	if work == nil then	return nil end
	local unit = split(work, " ")[2]
	if     unit == "centimetre" or unit == "centimeter" then return "cm"
	elseif unit == "milimetre"  or unit == "milimeter"  then return "mm"
	elseif unit == "metre"      or unit == "meter"      then return "m"
	elseif unit == "kilometre"  or unit == "kilometer"  then return "km"	
	elseif unit == "inch"                               then return "in"		
	elseif unit == "foot"                               then return "ft"	
	elseif unit == "yard"                               then return "yd"
    elseif unit == "mile"                               then return "mi"			
	elseif unit == "gram"       or unit == "gramme"     then return "g"
	elseif unit == "kilogram"   or unit == "kilogramme" then return "kg"
	end	
	]]--	
	
	local work = propertyValue( q_id, property_id)
	if work == nil then	return nil end
	local unit = split(work, " ")[2]
	if     unit == "centimetre" or unit == "centimeter" or unit == "centimètre"  then return "cm"
	elseif unit == "milimetre"  or unit == "milimeter"  or unit == "milimètre"   then return "mm"
	elseif unit == "metre"      or unit == "meter"      or unit == "mètre"       then return "m"
	elseif unit == "kilometre"  or unit == "kilometer"  or unit == "kilomètre"   then return "km"	
	elseif unit == "inch"                               or unit == "pouce"       then return "in"		
	elseif unit == "foot"                               or unit == "pied"        then return "ft"	
	elseif unit == "yard"                                                        then return "yd"
    elseif unit == "mile"                                                        then return "mi"			
	elseif unit == "gram"       or unit == "gramme"                              then return "g"
	elseif unit == "kilogram"   or unit == "kilogramme"                          then return "kg"
	end

end

----------------
function p.displayDimensions( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then
		return "Parameters qid and 1 are empty"
	end

	length_value   = getDimensionValue(q_id, "P2043")
	length_unit    = getDimensionUnit (q_id, "P2043")
	height_value   = getDimensionValue(q_id, "P2048")
	height_unit    = getDimensionUnit (q_id, "P2048")
	width_value    = getDimensionValue(q_id, "P2049")
	width_unit     = getDimensionUnit (q_id, "P2049")
	depth_value    = getDimensionValue(q_id, "P2610")
	depth_unit     = getDimensionUnit (q_id, "P2610")
	diameter_value = getDimensionValue(q_id, "P2386")
	diameter_unit  = getDimensionUnit (q_id, "P2386")
	mass_value     = getDimensionValue(q_id, "P2067")
	mass_unit      = getDimensionUnit (q_id, "P2067")
	scale_value    = getDimensionValue(q_id, "P1752")

	--[[
	test1 = tostring (diameter_unit == nil)
	test2 = tostring (diameter_unit ~= nil)
	if diameter_unit then return "[diameter_unit=" .. diameter_unit .. "]" .. test1 .. ' ' .. test2  end
	]]--

	local work = ""

	if (length_value and length_value~="") or (height_value and height_value~="") or (width_value and width_value~="") or (depth_value and depth_value~="")  or (mass_value and mass_value~="") or (diameter_value and diameter_value~="") then
		local reference_unit = "default reference_unit value"
		
		if     length_unit   then reference_unit = length_unit
		elseif height_unit   then reference_unit = height_unit		
		elseif width_unit    then reference_unit = width_unit
		elseif depth_unit    then reference_unit = depth_unit
		elseif mass_unit     then reference_unit = mass_unit
		elseif diameter_unit then reference_unit = diameter_unit
		else return "Error in determing reference dimension unit"	end
		
		--[[
		if     length_unit   then reference_unit = length_unit
		elseif height_unit   then reference_unit = height_unit			
		elseif width_unit    then reference_unit = width_unit
		elseif depth_unit    then reference_unit = depth_unit
		elseif mass_unit    then reference_unit = mass_unit
		elseif diameter_unit then reference_unit = diameter_unit
		else return "Error in determing reference length unit"	end
		]]--
		
		--[[
		if     (length_unit ~= nil)   then reference_unit = length_unit
		elseif (height_unit ~= nil)   then reference_unit = height_unit			
		elseif (width_unit ~= nil)    then reference_unit = width_unit
		elseif (depth_unit ~= nil)    then reference_unit = depth_unit
		elseif (mass_value ~= nil)    then reference_unit = mass_value
		elseif (diameter_unit ~= nil) then reference_unit = diameter_unit
			-- return "[diameter_unit=" .. diameter_unit .. " -- reference_unit=" .. reference_unit .. "]"
		else return "Error in determing reference length unit"	end
		]]--
	
		if (reference_unit ~= length_unit) and (length_unit ~= nil) then
			return "Consistency error with length unit"
		end
		if (reference_unit ~= height_unit) and (height_unit ~= nil) then
			return "Consistency error with height unit"
		end
		if (reference_unit ~= width_unit) and (width_unit ~= nil) then
			return "Consistency error with width unit"
		end
		if (reference_unit ~= depth_unit) and (depth_unit ~= nil) then
			return "Consistency error with depth unit"
		end
		if (reference_unit ~= diameter_unit) and (diameter_unit ~= nil) then
			return "Consistency error with diameter unit (reference_unit=" .. reference_unit .. ", diameter_unit=" .. diameter_unit .. ")"
		end		

		work = work .. frame:expandTemplate{ title = 'Size', args = { unit = reference_unit , length = length_value , height = height_value, width = width_value, thickness = depth_value, diameter = diameter_value } }
	end

	if (mass_value ~= nil) and (mass_unit ~= nil) then
		work = work .. frame:expandTemplate{ title = 'Weight', args = { mass_unit, mass_value } }
	end
	
	if (scale_value ~= nil) and (scale_value ~= "") then 
		scale_string = ""
		if     (tonumber(scale_value) == 1) then scale_string = "1:1"
		elseif (tonumber(scale_value) >  1) then scale_string = "1:" .. scale_value
			-- Convention in Wikidata is that larger-than-lifesize scales have a negative number
		elseif (tonumber(scale_value) <  1) then scale_string = abs(scale_value) .. ":1" 
		else return "Error in determing scale"	end
		
		if (work ~= "") then work = work .. "<br />" end
		-- work = work .. frame:expandTemplate{ title = 'scale' } .. ": " .. scale_string
		local default_language = to_string ( frame:callParserFunction( "int", "lang" ) )
		work = work .. mw.wikibase.getLabelByLang( "Q193642", default_language ) .. ": " .. scale_string
	end
	
	return work
end
----------------

function p.get_default_language( frame )
	return frame:callParserFunction( "int", "lang" )
end
----------------
function p.displayTitle( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then
		return "Parameters qid and 1 are empty"
	end
	-------------
	local default_language = to_string ( frame:callParserFunction( "int", "lang" ) ) -- get the default language. Yes this is horrible.
	local work = ""
	local q_id_label = mw.wikibase.label( q_id )
	local object_title    = propertyValueForLang( q_id, "P1476", default_language )
	if not object_title or object_title == "" then object_title = q_id_label end
	
	local wikipedia_language = default_language
	if wikipedia_language == "en-gb" then wikipedia_language = "en" end
	local object_wikipedia_article = mw.wikibase.sitelink( q_id, wikipedia_language.."wiki" )
	-- local object_wikipedia_article = mw.wikibase.sitelink( q_id, 'enwiki' )
	-- local object_wikipedia_article = mw.wikibase.sitelink( q_id )
	-- if object_wikipedia_article and object_wikipedia_article ~= "" then object_title = object_title .. " " .. default_language .. "." .. object_wikipedia_article end
	if object_wikipedia_article and object_wikipedia_article ~= "" then object_title = "[[:" .. wikipedia_language..":"..object_wikipedia_article.."|"..object_title.. "]]" end
	local object_subtitle = propertyValueForLang( q_id, "P1680", default_language  )
	if object_title and not object_subtitle then work = work .. object_title end
	if object_title and object_subtitle then work = work .. object_title .. "<br/>" .. object_subtitle end
	
	if work == "" or work == nil and not q_id_label == "" then work = q_id_label end
	
--	work = work .. "<br/>" .. q_id_label
	if not (work == "" or work == nil) then work = work:gsub("^%l", string.upper) end
	return work
end
----------------
function p.displayDescription( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then
		return "Parameters qid and 1 are empty"
	end
	-------------
	--local default_language = to_string ( frame:callParserFunction( "int", "lang" ) ) -- get the default language. Yes this is horrible.
	--local work = ""
	--local object_title    = propertyValueForLang( q_id, "P1476", default_language )
	--local object_subtitle = propertyValueForLang( q_id, "P1680", default_language  )
	--if object_title and not object_subtitle then work = work .. object_title end
	--if object_title and object_subtitle then work = work .. object_title .. "<br/>" .. object_subtitle end
	
	--if work == "" then work = work .. mw.wikibase.description( q_id ) end
	local work = mw.wikibase.description( q_id ) or ""
	work = work:gsub("^%l", string.upper)
	return work
end

----------------
function p.displayInstitution( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then
		return "Parameters qid and 1 are empty"
	end

    local work = ""
	local local_institution = propertyValue( q_id, "P195" )
	
	-- Provisions in case institution exists but displaying it is semantically incorrect
	-- for instance for "The Tower of Blue Horses", the institutin is "national Gallery" but the painting was since lost
	local institution_end_time = subPropertyValue( q_id, "P195", "P582")
	local location_is_unknown  = ( propertyValue( q_id, "P276" ) == "unknown value" )
	if (institution_end_time or location_is_unknown) then return nil end
	
	--[[
	local institutionFile = mw.title.makeTitle( "Institution", local_institution) 
	if institutionFile.exists then return frame:expandTemplate{ title = "Institution:" .. local_institution , args = { } } end
	--]]	
	local english_institution = propertyValueInEnglish( q_id, "P195" )
	local institutionFile = mw.title.makeTitle( "Institution", english_institution) 
	if institutionFile.exists then return frame:expandTemplate{ title = "Institution:" .. english_institution , args = { } } end
	
	return local_institution 
end

----------------
function p.displayCreator( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then
		return "Parameters qid and 1 are empty"
	end

	local creator     = propertyValue( q_id, "P170" )
	if not creator or creator == "" then return "" end
	local creatorFile = mw.title.makeTitle( "Creator", creator) 
	if creatorFile.exists then return frame:expandTemplate{ title = "Creator:" .. creator , args = { } } end
	return creator 
end

----------------
function p.displayAccessionNumber( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then
		return "Parameters qid and 1 are empty"
	end

	local accession_number     = propertyValue( q_id, "P217" )
	if not accession_number or accession_number == "" then return "" end
	local local_institution = propertyValue( q_id, "P195" )
	
	if string.match(local_institution, "Louvre") then
	    -- {{Louvre number|Sb=2823}}
		-- local department, number = accession_number:match"(%a+).(%d+)"
		return frame:expandTemplate{ title = 'Louvre_number', args = { accession_number } }
	end
	
	return accession_number
end

----------------
function p.displayMedium( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then
		return "Parameters qid and 1 are empty"
	end
	
	local entity     = mw.wikibase.getEntityObject(q_id)
	if not entity then return "No entity " .. q_id .. " found :( " end	
	
	work =""
	local list_separator = "— "
	local eol_separator  = "<br/>"
	local medium                 = propertyValue( q_id, "P186" )
	-- local medium_applies_to_part = subPropertyValue( q_id, "P186", "P518")
	
	----		
--	if medium and medium ~= "" then
--		medium_data = entity.claims["P186"]
--
--		for i, j in pairs(medium_data) do 
--			medium_value = j.mainsnak.datavalue.value.id
--			
--			if j.qualifiers and j.qualifiers["P518"] and j.qualifiers["P518"][1] and j.qualifiers["P518"][1].datavalue and j.qualifiers["P518"][1].datavalue.value and j.qualifiers["P518"][1].datavalue.value.text then 
--				medium_applies_to_part = j.qualifiers["P518"][1].datavalue.value.text
--				if medium_applies_to_part and medium_applies_to_part ~="" then 
--					work  = work .. list_separator .. medium_value .. " (" .. medium_applies_to_part .. ") " .. eol_separator 
--				end
--			else
--				work  = work .. list_separator .. medium_value  .. eol_separator
--				-- work  = work .. list_separator .. "PLOUF"  .. eol_separator 
--			end
--		end
--	end
	----
	
	-- local creatorFile = mw.title.makeTitle( "Creator", creator) 
	-- if creatorFile.exists then return frame:expandTemplate{ title = "Creator:" .. creator , args = { } } end
	-- if medium_applies_to_part then return medium .. " (" .. medium_applies_to_part .. ")" end
	return medium 
	-- return work
end

----------------
function p.displayReference( frame )
	local q_id = frame.args.qid or frame.args[1]
	if q_id == nil then
		return "Parameters qid and 1 are empty"
	end
	
	local entity     = mw.wikibase.getEntityObject(q_id)
	if not entity then return "No entity " .. q_id .. " found :( " end		
	
	-- work ="BEGIN<br/>"
	work =""
	-- local list_separator = "<br/>"
	local list_separator = "— "
	local eol_separator  = "<br/>"
	
	local atlas_ID               = propertyValue( q_id, "P1212")
	local joconde_ID             = propertyValue( q_id, "P347")
	local orsay_ID               = propertyValue( q_id, "P4659")
	local eMeg_ID                = propertyValue( q_id, "P4157")
	local described_at_URL       = propertyValue( q_id, "P973")
	local described_at_URL_title = subPropertyValue( q_id, "P973", "P1476")
	
	--if atlas_ID and atlas_ID ~= "" then                                     -- Louvre catalogue (Atlas database)
	--	work = work .. frame:expandTemplate{ title = 'Louvre online', args = { atlas_ID } }
	--elseif joconde_ID and joconde_ID ~= "" then                             -- French national catalogue (Joconde database)
	--	work = work .. frame:expandTemplate{ title = 'Joconde', args = { joconde_ID } }
	--elseif orsay_ID and orsay_ID ~= "" then                             -- Orsay Museum (Orsay database)
	--	work = work .. frame:expandTemplate{ title = 'Orsay online', args = { orsay_ID } }
	--elseif eMeg_ID and eMeg_ID ~= "" then                                   -- Musée d'ethnographie de Genève catalogue (eMeg database)
	--	work = work .. frame:expandTemplate{ title = 'MEG online', args = { eMeg_ID } }
	--elseif described_at_URL and described_at_URL ~= "" then
	--	ref = entity.claims["P973"]
		
	if atlas_ID and atlas_ID ~= "" then                                     -- Louvre catalogue (Atlas database)
		work = work .. list_separator .. frame:expandTemplate{ title = 'Louvre online', args = { atlas_ID } } .. eol_separator end
	if joconde_ID and joconde_ID ~= "" then                             -- French national catalogue (Joconde database)
		work = work .. list_separator .. frame:expandTemplate{ title = 'Joconde', args = { joconde_ID } } .. eol_separator end
	if orsay_ID and orsay_ID ~= "" then                             -- Orsay Museum (Orsay database)
		work = work .. list_separator .. frame:expandTemplate{ title = 'Orsay online', args = { orsay_ID } } .. eol_separator end
	if eMeg_ID and eMeg_ID ~= "" then                                   -- Musée d'ethnographie de Genève catalogue (eMeg database)
		work = work .. list_separator .. frame:expandTemplate{ title = 'MEG online', args = { eMeg_ID } } .. eol_separator end
		
	if described_at_URL and described_at_URL ~= "" then
		ref = entity.claims["P973"]

		for i, j in pairs(ref) do 
			reference_URL = j.mainsnak.datavalue.value
			
			if j.qualifiers and j.qualifiers["P1476"] and j.qualifiers["P1476"][1] and j.qualifiers["P1476"][1].datavalue and j.qualifiers["P1476"][1].datavalue.value and j.qualifiers["P1476"][1].datavalue.value.text then 
				reference_title = j.qualifiers["P1476"][1].datavalue.value.text
				if reference_title and reference_title ~="" then 
					work  = work .. list_separator .. frame:expandTemplate{ title = 'URL', args = { reference_URL, reference_title  } } .. eol_separator 
				end
			else
				work  = work .. list_separator .. frame:expandTemplate{ title = 'URL', args = { reference_URL  } }  .. eol_separator 
			end
		end
	--else
	--	work = work .. "no reference provided"
	end
	
	if work == "" then work = "no reference provided" end
	
	return work
end

-------------------
return p