模組:Property
外观
-- [[Template:P]]
local p = {}
-- Get the property of given wikidata entity.
-- 取得wikidata中指定實體項目的指定屬性。
-- https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua
function p.data(frame)
if not mw.wikibase then
return '未安裝 Extension:Wikibase ?'
end
entity = mw.wikibase.getEntity(frame.args[2])
-- if not entity then return nil end
if entity == nil then
return '不存在此項目: ' .. frame.args[2]
end
if frame.args[1] == nil then
return mw.wikibase.label(entity.id)
end
-- return entity:formatPropertyValues(frame.args[1])
property = entity['claims'][frame.args[1]]
if property == nil then
if frame.args[2] == nil then
prefix = '本頁'
else
prefix = '此項目 [[d:' .. frame.args[2] .. ']] '
end
return prefix .. '不存在此屬性: ' .. frame.args[1]
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 property['amount'] ~= nil then
return tonumber(property['amount'])
end
if property['time'] ~= nil then
BCE, y, m, d = string.match(property['time'], "^([+\\-]?)(%d+)-(%d+)-(%d+)T")
if BCE then
y = '前'..y
end
if property['precision'] == 7 then
return y..'世紀'
end
-- TODO: other precisions
return y..'年'..m..'月'..d..'日'
end
if property['numeric-id'] ~= nil then
return mw.wikibase.label('Q' .. property['numeric-id'])
end
return '無法處理此屬性: ' .. frame.args[1] .. ',請修改 Module:property。'
end
return p