Jump to content

Module:Property

विकिपीडिया से
मॉड्यूल बिबरनलेख[बनाईं]
-- [[Module:Property]]

local p = {}

-- trim string
function trim(s)
	-- incase nil
	if s == nil then return '' end
	s = tostring(s)
	return s:gsub("^%s+", ""):gsub("%s+$", "")
end


-- Get the property of given wikidata entity.
-- [[:mw:Extension:Wikibase Client/Lua]]
function p.data(frame)
	if mw.wikibase == nil then
		return 'Not yet install Extension:Wikibase ?'
	end

	local entity = trim(frame.args[2])
	if not entity:match('^[QP]%d{1,10}$') then
		entity = mw.wikibase.resolvePropertyId(entity) or entity
	end
	-- entity = mw.wikibase.getEntity(trim(frame.args[2]) or nil)
	if entity == '' then
		entity = mw.wikibase.getEntity()
		if entity == nil or entity.claims == nil then return 'No wikidata entity' end
	else
		entity = mw.wikibase.getEntity(entity)
		if entity == nil then
			return 'No wikidata entity: ' .. frame.args[2]
		end
	end


	local property_id = trim(frame.args[1])

	if property_id == '' then
		-- https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua#mw.wikibase.label
		local language = trim(frame.args[3])
		if language == '' then
			-- return mw.wikibase.label(entity.id)
			return entity:getLabel()
		end
		return entity:getLabel(language)
	end

	if property_id == 'link' then
		local language = trim(frame.args[3])
		if language == '' then language = 'en' end
		return entity:getSitelink(language .. 'wiki')
	end

	if not property_id:match('^P%d{1,10}$') then
		property_id = mw.wikibase.resolvePropertyId(property_id) or property_id
	end

	-- return entity:formatPropertyValues(property_id)


	local property = entity.claims[property_id]
	if property == nil then
		local prefix
		if trim(frame.args[2]) == '' then
			prefix = 'The page'
		else
			prefix = 'The entity [[d:' .. trim(frame.args[2]) .. ']]'
		end
		return prefix .. ' has no property: ' .. property_id
	end

	property = property[1]['mainsnak']['datavalue']

	-- ['datavalue']: e.g.,
	-- {"value":{'entity-type':'item','numeric-id':1647152},type:'wikibase-entityid'}
	-- {"value":{"amount":"+10","unit":"1","upperBound":"+10","lowerBound":"+10"},"type":"quantity"}
	-- {"value":{"time":"-0800-01-01T00:00:00Z","timezone":0,"before":0,"after":0,"precision":7,"calendarmodel":"http://www.wikidata.org/entity/Q1985727"},"type":"time"}

	property = property['value']

	if type(property) ~= 'table' then
		return property
	end

	if property['amount'] ~= nil then
		return tonumber(property['amount'])
	end

	if property['numeric-id'] ~= nil then
		return mw.wikibase.label('Q' .. property['numeric-id'])
	end

	-- TODO: other types
	-- https://www.mediawiki.org/wiki/Wikibase/API
	-- https://www.mediawiki.org/wiki/Wikibase/Indexing/RDF_Dump_Format#Value_representation
	return 'Can not parse the type: ' .. property_id .. ', please fix Module:property.'
end


return p