Module:WikidataF

विकिपीडिया से
मॉड्यूल बिबरनलेख[बनाईं]
--script that retrieves basic data stored in Wikidata, for the datamodel, see https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua

local p = {}

local i18n = {
    ["errors"] = {
        ["property-param-not-provided"] = "Paramètre propriété non renseigné.",
        ["qualifier-param-not-provided"] = "Paramètre qualifier non renseigné.",
        ["entity-not-found"] = "Entité non trouvée.",
        ["unknown-claim-type"] = "type d'affirmation inconnu.",
        ["unknown-snak-type"] = "Type de snak inconnu.",
        ["unknown-datavalue-type"] = "Type de donnée non reconnu.",
        ["unknown-entity-type"] = "Type d'entité non reconnu.",
        ["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.",
        ["ambigous"] = "Ambigu : plusieurs valeurs possibles",
    },
    ["somevalue"] = "inconnu",
    ["novalue"] = "pas applicable"
}


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

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

local function withrank(claims, rank)
	if rank == 'best' then
		local preferred = withrank(claims, 'preferred')
		if #preferred > 0 then
			return preferred
		else
			return withrank(claims, 'normal')
		end
	end
	
	if rank == 'valid' then
		return validclaims(claims)
	end
	
	local oldclaims = claims
	local claims = {}
	for i, statement in pairs(oldclaims) do
		if statement.rank == rank then
				table.insert(claims, statement)
		end
	end
	return claims
end


function p.getDatavalue(snak, formatting)
	if snak.snaktype ~= 'value' then 
		return nil
	end
	
	local datatype = snak.datavalue.type
	local value = snak.datavalue.value
	
	if datatype == 'wikibase-entityid' then
		if formatting == 'raw' then 
			return "Q" .. tostring(value['numeric-id'])
		else
			return p.formatEntityId("Q" .. tostring(value['numeric-id']), formatting)
		end
		
	elseif datatype == 'string' then
		return value
		
	elseif datatype == 'time' then -- format example: +00000001809-02-12T00:00:00Z
		return value.time
	elseif datatype == 'globecoordinate' then
		-- retourne une table avec clés latitude, longitude, précision et globe à formater par un autre module (à changer ?)
		value.globe = mw.loadData('Module:Wikidata/Globes')[value.globe] -- transforme l'ID du globe en nom anglais utilisable par geohack
		if formatting == 'latitude' then
			return value.latitude
		elseif formatting == 'longitude' then
			return value.longitude
		else
			return value -- note : les coordonnées Wikidata peuvent être utilisée depuis Module:Coordinates. Faut-il aussi autoriser à appeler Module:Coordiantes ici ?
		end

	elseif datatype == 'url' then
		return value.url

	elseif datatype == 'quantity' then
		return value.amount

	else
		return formatError( 'unknown-datavalue-type' )
	end
end

function p.getClaims( args ) -- returns a table of the claims 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.upper(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
	claims = entity.claims[property]
	if args.rank ~= 'all' then -- à laisser vers la fin, quan les élimination sont faites
		if not args.rank or args.rank == '' then
			args.rank = 'best'
		end
		claims = withrank(claims, args.rank)
	end
	return claims
end

function p._formatStatements( args )--Format statement and concat them cleanly
	--If a value is already set, use it
	if args.value and args.value ~= '' then
		return args.value
	end
	local rawStatements = p.getClaims( args )
	local formattedStatements = {}
	if not rawStatements or #rawStatements == 0 then
			return nil
	end
    for i, statement in pairs( rawStatements ) do
 		table.insert( formattedStatements, formatStatement( statement, args ))    end
    return mw.text.listToText( formattedStatements, args.separator, args.conjunction )
    -- .. '[[Category:Page utilisant des données de Wikidata/'  ..  string.upper(args.property) .. ']]'
end

function p.formatStatements( frame )
	local args = {}
	if frame == mw.getCurrentFrame() then
		args = frame:getParent().args -- paramètres du modèle appelant (est-ce vraiment une bonne idée ?)
		for k, v in pairs(frame.args) do
			args[k] = v
		end
	else
		args = frame
	end
	return p._formatStatements( args )
end

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

    return p.formatSnak( statement.mainsnak, args )
end

function p.formatSnak( snak, args )
	if not args then args = {} end -- pour faciliter l'appel depuis d'autres modules
    if snak.snaktype == 'somevalue' then
        return i18n['somevalue']
    elseif snak.snaktype == 'novalue' then
        return i18n['novalue']
    elseif snak.snaktype == 'value' then
        return p.getDatavalue( snak, args.formatting)
    else
        return formatError( 'unknown-snak-type' )
    end
end

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

return p