Module:WikidataF

See fr:Module:Wikidata

See fr:Module:Wikidata


Usage

{{#invoke:WikidataF|function_name}}

--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


Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.