跳转到内容

模組:Wikidata

被永久保护的模块
维基百科,自由的百科全书

这是本页的一个历史版本,由Yangfl留言 | 贡献2014年9月21日 (日) 04:41编辑。这可能和当前版本存在着巨大的差异。

--copy from https://test2.wikipedia.org/wiki/Module:Wikidata
local i18n = {
    ["errors"] = {
        ["property-param-not-provided"] = "未提供属性参数。",
        ["entity-not-found"] = "实体未找到。",
        ["unknown-claim-type"] = "未知声明类型。",
        ["unknown-snak-type"] = "未知snak类型。",
        ["unknown-datavalue-type"] = "未知数据值类型。",
        ["unknown-entity-type"] = "未知实体类型。",
        ["unknown-value-module"] = "你必须设置value-module和value-function参数。",
        ["value-module-not-found"] = "未找到value-module的模块位置。",
        ["value-function-not-found"] = "未找到value-function的模块位置。"
    },
    ["somevalue"] = "<u>未知值</u>",
    ["novalue"] = "<u>无值</u>"
}
 

function getEntityFromId( id )
    return mw.wikibase.getEntity() --TODO support for getting other entities
end

function getEntityIdFromValue( value )
    if value['entity-type'] == 'item' then
        return 'q' .. value['numeric-id']
    elseif value['entity-type'] == 'property' then
        return 'p' .. value['numeric-id']
    else
        return formatError( 'unknown-entity-type' )
    end
end

function formatError( key )
    return '<span class="error">' .. i18n.errors[key] .. '</span>'
end


function formatStatements( options )
    if not options.property then
        return formatError( 'property-param-not-provided' )
    end

    --Get entity
    local entity = nil
    if options.entity and type( options.entity ) == "table" then
        entity = options.entity
    else
        entity = getEntityFromId( options.entityId )
    end

    if not entity then
        return '' --TODO error?
    end

    if not entity.claims or not entity.claims[string.lower(options.property)] then
        return '' --TODO error?
    end

    --Format statement and concat them cleanly
    local formattedStatements = {}
    for i, statement in pairs( entity.claims[options.property:lower()] ) do
        if options.rank == 'one' then
            return formatStatement( statement, options ) --Output only one value
        else
            table.insert( formattedStatements, formatStatement( statement, options ) )
        end
    end
    return mw.text.listToText( formattedStatements, options.separator, options.conjunction )
end

function formatStatement( statement, options )
    if not statement.type or statement.type ~= 'statement' then
        return formatError( 'unknown-claim-type' )
    end

    return formatSnak( statement.mainsnak, options )
    --TODO reference and qualifiers
end

function formatSnak( snak, options )
    if snak.snaktype == 'somevalue' then
        return i18n['somevalue']
    elseif snak.snaktype == 'novalue' then
        return i18n['novalue']
    elseif snak.snaktype == 'value' then
        return formatDatavalue( snak.datavalue, options )
    else
        return formatError( 'unknown-snak-type' )
    end
end

function formatDatavalue( datavalue, options )
    --Use the customize handler if provided
    if options['value-module'] or options['value-function'] then
        if not options['value-module'] or not options['value-function'] then
            return formatError( 'unknown-value-module' )
        end
        local formatter = require ('Module:' .. options['value-module'])
        if not formatter then
            return formatError( 'value-module-not-found' )
        end
        local fun = formatter[options['value-function']]
        if not fun then
            return formatError( 'value-function-not-found' )
        end
        return fun( datavalue.value, options )
    end

    --Default formatters
    if datavalue.type == 'wikibase-entityid' then
        return formatEntityId( getEntityIdFromValue( datavalue.value ), options )
    elseif datavalue.type == 'string' then
        if options.pattern and options.pattern ~= '' then
            return formatFromPattern( datavalue.value, options )
        else
            return datavalue.value
        end
    elseif datavalue.type == 'time' then
        local Time = require 'Module:Time'
        return Time.newFromWikidataValue( datavalue.value ):toHtml()
    else
        return formatError( 'unknown-datavalue-type' )
    end
end

function formatEntityId( entityId, options )
    local label = mw.wikibase.label( entityId )
    local link = mw.wikibase.sitelink( entityId )
    if link then
        if label then
            return '[[' .. link .. '|' .. label .. ']]'
        else
            return '[[' .. link .. ']]'
        end
    else
        return label --TODO what if no links and label + fallback language?
    end
end

function formatFromPattern( str, options )
    return mw.ustring.gsub( options.pattern, '$1', str ) .. '' --Hack to get only the first result of the function
end

local p = {}

function p.formatStatements( frame )
    local args = frame.args

    --If a value if already set, use it
    if args.value and args.value ~= '' then
        return args.value
    end
    return formatStatements( frame.args )
end

function p.formatStatementsFromLua( options )
    --If a value if already set, use it
    if options.value and options.value ~= '' then
        return options.value
    end
    return formatStatements( options )
end

-- This is used to get a normal wiki-linked value, or a comma separated list of them if multiple values exist
p.getValue = function(frame)
	local propertyID = mw.text.trim(frame.args[1] or "")
	local input_parm = mw.text.trim(frame.args[2] or "")
	if input_parm == "FETCH_WIKIDATA" then
		local entity = mw.wikibase.getEntity()
		if entity.claims[propertyID] ~= nil then
			local out = {}
			for k, v in pairs(entity.claims[propertyID]) do
				if v.mainsnak.snaktype == 'value' then
					if (mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"])) then
						out[#out + 1] = "[[" .. mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"]) .. "]]"
					else
						out[#out + 1] = "[[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"]) .. "]]<abbr title='此维基尚无此条目'>[*]</abbr>"
					end
				end
			end
			return table.concat(out, ",")
		else
			return ""
		end
	else
		return input_parm
	end
end
 
p.getQualifierValue = function(frame)
	local propertyID = mw.text.trim(frame.args[1] or "")
	local qualifierID = mw.text.trim(frame.args[2] or "")
	local input_parm = mw.text.trim(frame.args[3] or "")
	if input_parm == "FETCH_WIKIDATA" then
		local entity = mw.wikibase.getEntity()
		if entity.claims[propertyID] ~= nil then
			local out = {}
			for k, v in pairs(entity.claims[propertyID]) do
				for k2, v2 in pairs(v.qualifiers[qualifierID]) do
					if v2.snaktype == 'value' then
						if (mw.wikibase.sitelink("Q" .. v2.datavalue.value["numeric-id"])) then
							out[#out + 1] = "[[" .. mw.wikibase.sitelink("Q" .. v2.datavalue.value["numeric-id"]) .. "]]"
						else
							out[#out + 1] = "[[:d:Q" .. v2.datavalue.value["numeric-id"] .. "|" .. mw.wikibase.label("Q" .. v2.datavalue.value["numeric-id"]) .. "]]<abbr title='此维基尚无此条目'>[*]</abbr>"
						end
					end
				end
			end
			return table.concat(out, ",")
		else
			return ""
		end
	else
		return input_parm
	end
end
 
-- This is used to get a value like 'male' (for property p21) which won't be linked
p.getRawValue = function(frame)
	local propertyID = mw.text.trim(frame.args[1] or "")
	local input_parm = mw.text.trim(frame.args[2] or "")
	if input_parm == "FETCH_WIKIDATA" then
		local entity = mw.wikibase.getEntity()
		if entity.claims[propertyID] ~= nil then
			local out = {}
			for k, v in pairs(entity.claims[propertyID]) do
				if v.mainsnak.snaktype == 'value' then
					if v.mainsnak.datavalue.value["numeric-id"] then
						out[#out + 1] = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"])
					else
						out[#out + 1] = v.mainsnak.datavalue.value
					end
				end
			end
			local ret = table.concat(out, ",")
			return string.upper(string.sub(ret, 1, 1)) .. string.sub(ret, 2)
		else
			return ""
		end
	else
		return input_parm
	end
end
 
p.getRawQualifierValue = function(frame)
	local propertyID = mw.text.trim(frame.args[1] or "")
	local qualifierID = mw.text.trim(frame.args[2] or "")
	local input_parm = mw.text.trim(frame.args[3] or "")
	if input_parm == "FETCH_WIKIDATA" then
		local entity = mw.wikibase.getEntity()
		if entity.claims[propertyID] ~= nil then
			local out = {}
			for k, v in pairs(entity.claims[propertyID]) do
				for k2, v2 in pairs(v.qualifiers[qualifierID]) do
					if v2.snaktype == 'value' then
						if v2.datavalue.value["numeric-id"] then
							out[#out + 1] = mw.wikibase.label("Q" .. v2.datavalue.value["numeric-id"])
						else
							out[#out + 1] = v2.datavalue.value
						end
					end
				end
			end
			local ret = table.concat(out, ",")
			return string.upper(string.sub(ret, 1, 1)) .. string.sub(ret, 2)
		else
			return ""
		end
	else
		return input_parm
	end
end
 
-- This is used to get a date value for date_of_birth (p569), etc. which won't be linked -- consolidate by testing if entity.claims[propertyID].mainsnak.datavalue.type is "time"
-- Dates are stored as 28 characters if the year  >99 -- e.g. +00000002014-01-01T00:00:00Z for 2014
-- Dates are stored as 26 characters if the year =<99 -- e.g. +000000050-01-01T00:00:00Z   for 50 CE
p.getDateValue = function(frame)
	local propertyID = mw.text.trim(frame.args[1] or "")
	local input_parm = mw.text.trim(frame.args[2] or "")
	local date_format = mw.text.trim(frame.args[3] or "dmy")
	if input_parm == "FETCH_WIKIDATA" then
		local entity = mw.wikibase.getEntity()
		if entity.claims[propertyID] ~= nil then
			local out = {}
			local dt = {}
			for k, v in pairs(entity.claims[propertyID]) do
				if v.mainsnak.snaktype == 'value' then
					local d = v.mainsnak.datavalue.value.time
					if #d > 26 then
						dt.year = string.sub(d, 9, 12)
						dt.month = string.sub(d, 14, 15)
						dt.day = string.sub(d, 17, 18)
					else
						dt.year = string.sub(d, 9, 10)
						dt.month = string.sub(d, 12, 13)
						dt.day = string.sub(d, 15, 16)
					end
					if date_format == "dmy" then
						out[#out + 1] = os.date("%e %B %Y", os.time(dt))
					else
						out[#out + 1] = os.date("%B %e, %Y", os.time(dt))
					end
				end
			end
			return table.concat(out, ",")
		else
			return ""
		end
	else
		return input_parm
	end
end
 
p.getQualifierDateValue = function(frame)
	local propertyID = mw.text.trim(frame.args[1] or "")
	local qualifierID = mw.text.trim(frame.args[2] or "")
	local input_parm = mw.text.trim(frame.args[3] or "")
	local date_format = mw.text.trim(frame.args[4] or "dmy")
	if input_parm == "FETCH_WIKIDATA" then
		local entity = mw.wikibase.getEntity()
		if entity.claims[propertyID] ~= nil then
			local out = {}
			local dt = {}
			for k, v in pairs(entity.claims[propertyID]) do
				for k2, v2 in pairs(v.qualifiers[qualifierID]) do
					if v2.snaktype == 'value' then
						local d = v2.datavalue.value.time
						if #d > 26 then
							dt.year = string.sub(d, 9, 12)
							dt.month = string.sub(d, 14, 15)
							dt.day = string.sub(d, 17, 18)
						else
							dt.year = string.sub(d, 9, 10)
							dt.month = string.sub(d, 12, 13)
							dt.day = string.sub(d, 15, 16)
						end
						if date_format == "dmy" then
							out[#out + 1] = os.date("%e %B %Y", os.time(dt))
						else
							out[#out + 1] = os.date("%B %e, %Y", os.time(dt))
						end
					end
				end
			end
			return table.concat(out, ",")
		else
			return ""
		end
	else
		return input_parm
	end
end

return p