Mòdul:Proves/Vriullop

De la Viquipèdia, l'enciclopèdia lliure
Aquesta és una versió anterior d'aquesta pàgina, de data 00:01, 15 des 2013 amb l'última edició de Vriullop (discussió | contribucions). Pot tenir inexactituds o contingut no apropiat no present en la versió actual.
Icona de documentació de mòdul Documentació del mòdul [ mostra ] [ modifica el codi ] [ mostra l'historial ] [ refresca ]

Mòdul Proves (codi · ús · discussió · proves · tests · casos prova | subpàgines · enllaços)

A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]


local i18n = {
    ["errors"] = {
        ["property-param-not-provided"] = "Falta el paràmetre de la propietat.",
        ["entity-not-found"] = "Entitat no trobada.",
        ["unknown-claim-type"] = "Tipus d'afirmació (claim) desconegut.",
        ["unknown-snak-type"] = "Tipus de connector (snak) desconegut.",
        ["unknown-datavalue-type"] = "Tipus de valor (datavalue) desconegut.",
        ["unknown-entity-type"] = "Tipus d'entitat desconegut.",
        ["unknown-value-module"] = "You must set both value-module and value-function parameters.",
        ["value-module-not-found"] = "The module pointed by value-module not found.",
        ["value-function-not-found"] = "The function pointed by value-function not found."
    },
    ["somevalue"] = "''valor desconegut''",
    ["novalue"] = "''cap valor aplicable''"
}
 
function getEntityFromId( id )
    return mw.wikibase.getEntity() --TODO support for getting other entities
end

function getClaims( args ) -- returns a table of the claims (TODO: matching some conditions given in args)
    if not args.property then
        return formatError( 'property-param-not-provided' )
    end
    --Get entity
    local entity = nil
    local property = string.lower(args.property)
    if args.entity and type( args.entity ) == "table" then
        entity = args.entity
    else
        entity = getEntityFromId( args.entityId )
    end
 
    if not entity or not entity.claims or not entity.claims[property] then
        return nil
    end
	return entity.claims[property]
end

function numOfClaims( claims )
	if type(claims) ~= "table" then
		return 0
	elseif claims == {} then 
		return 0
	elseif claims[0] then -- taula Wikibase no modificada amb una clau 0
		return #claims + 1
	else
		return #claims
	end
end

function getDatavalue(snak, formatting)
	if snak.snaktype ~= 'value' then 
		return nil
	end
	datatype = snak.datavalue.type
 
	if datatype == 'wikibase-entityid' then
		if formatting == 'raw' then 
			return "Q" .. tostring(snak.datavalue.value['numeric-id'])
		else
			return formatEntityId("Q" .. tostring(snak.datavalue.value['numeric-id']))
		end
 
	elseif datatype == 'string' then
		local astring = snak.datavalue.value
		if formatting == 'weblink' then 
			return '[' .. astring ..  ' ' ..  mw.text.split( astring, '//' )[2] .. ']'
		else 
			return astring
		end
 
	elseif datatype == 'time' then -- format example: +00000001809-02-12T00:00:00Z
		return p.formatTimevalue(snak.datavalue.value, formatting)
 
	elseif datatype == 'globecoordinate' then
		out = snak.datavalue.value
		if out.globe == 'http://www.wikidata.org/wiki/Q2' then -- by far the most common case
			out.globe = 'earth'
		else 
			globetable = { Q2 = 'earth', Q111 = 'mars', Q405 = 'moon'}
			f = mw.getCurrentFrame() -- to use titleparts to get the relevant part of the value.globe, better method ?? 
			out.globe = globetable[f:callParserFunction( '#titleparts', { snak.datavalue.value.globe, '', 2 } )] -- error handling needed
		end
		return out
 
	else return formatError( 'unknown-datavalue-type' )
	end
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 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 getDatavalue( snak, options.formatting )
    else
        return formatError( 'unknown-snak-type' )
    end
end
 
function formatEntityId( entityId )
	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 '[[wikidata:' ..  entityId .. '|' .. label or entityId .. ']]'
	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 ) -- Format statement and concat them cleanly
	local args = frame.args
 
	--If a value is already set, use it
	if args.value and args.value ~= '' then
		return args.value
	end

	local rawStatements = getClaims( args )
	if not rawStatements or numOfClaims(rawStatements) == 0 then
		return nil
	end

	--Format statement and concat them cleanly
	local sortedLabels = {} -- l'índex 0 passa a ser 1, necessari per deixar-ho ordenat
	for i, statement in pairs( rawStatements ) do
		sortedLabels[i+1] = statement
	end
	local formattedStatements = {}
	for i, statement in pairs( sortedLabels ) do
		if args.rank == 'one' then
			return formatStatement( statement, args ) --Output only one value
		else
			table.insert( formattedStatements, formatStatement( statement, args ) )
		end
	end
 
	return mw.text.listToText( formattedStatements, args.separator, args.conjunction )
end

 
-- Return the site link (for the current site) for a given data item.
function p.getSiteLink( frame )
    if frame.args[1] == nil then
        entity = mw.wikibase.getEntity()
        if not entity then
        	return nil
        end
        id = entity.id
    else
        id = frame.args[1]
    end
 
    return mw.wikibase.sitelink( id )
end

function p.formatTimevalue(value, formatting) -- date managed in the dedicated Module:Wikidata/Dates
	if formatting == 'raw' then return value.time
	else
		local wdate = require('Module:Wikidata/Dates') 
		return wdate.objecttotext(wdate.dateobject(value))
	end
end

return p