Aller au contenu

« Module:Wikidata » : différence entre les versions

De Wreck
Modèle:Infobox>Zolo
nouvelles fonctionnalités
Modèle:Infobox>Zolo
Aucun résumé des modifications
Ligne 9 : Ligne 9 :
         ["unknown-value-module"] = "You must set both value-module and value-function parameters.",
         ["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-module-not-found"] = "The module pointed by value-module not found.",
         ["value-function-not-found"] = "The function pointed by value-function not found."
         ["value-function-not-found"] = "The function pointed by value-function not found.",
        ["ambigous"] = "Ambigu : plusieurs valeurs possible"
     },
     },
     ["somevalue"] = "''unknown value''",
     ["somevalue"] = "''unknown value''",
Ligne 23 : Ligne 24 :
entity =  getEntityFromId( id )
entity =  getEntityFromId( id )
return  entity.id
return  entity.id
end  
end


function getEntityIdFromValue( value )
function getEntityIdFromValue( value )
Ligne 55 : Ligne 56 :
         return '' --TODO error?
         return '' --TODO error?
     end
     end
if not entity.claims or not entity.claims[string.lower(options.property)] then
if entity.claims[property] then
        return nil
claims = entity.claims[property]
    end
else
    claims = entity.claims[property]
return nil
    if options.excludespecial == 'true' then
end
if options.excludespecial == 'true' then
     oldclaims = claims
     oldclaims = claims
     claims = {}
     claims = {}
Ligne 66 : Ligne 68 :
     table.insert(claims, statement)
     table.insert(claims, statement)
     end
     end
end
end
if options.targetvalue then
targetvalue = options.targetvalue
oldclaims = claims
claims = {}
for i, statement in pairs(oldclaims) do
if formatDatavalue(statement.mainsnak) == targetvalue then
table.insert(claims, statement)
end
end
end
end
end
if options.qualifier then -- ne marche que pour les propriétés de type item
if options.qualifier then -- ne marche que pour les propriétés de type item
qualifier = options.qualifier
qualifiervalue = options.qualifiervalue
oldclaims = claims
oldclaims = claims
claims = {}
claims = {}
qualifier = options.qualifier
if options.qualifiervalue then
qualifiervalue =  mw.ustring.gsub(options.qualifiervalue, "Q", "")
qualifiervalue = tonumber(qualifiervalue)
end
for i, statement in pairs(oldclaims) do
for i, statement in pairs(oldclaims) do
if statement.qualifiers[qualifier] then
if statement.qualifiers[qualifier] then
if qualifiervalue then
if qualifiervalue then
for j, qualif in pairs(statement.qualifiers[qualifier]) do
for j, qualif in pairs(statement.qualifiers[qualifier]) do
if qualif.datavalue.value['numeric-id'] == qualifiervalue then  
if formatDatavalue(qualif) == qualifiervalue then  
table.insert(claims, statement)
table.insert(claims, statement)
end
end
Ligne 90 : Ligne 99 :
end
end
end
end
if options.source then -- ne marche que pour les propriétés de type item
if options.source then  
if options.sourceproperty then  
if options.sourceproperty then  
sourceproperty = options.sourceproperty
sourceproperty = options.sourceproperty
else
else
sourceproperty = "P248"
sourceproperty = "p248"
end
end
sourcevalue  = mw.ustring.gsub(options.source, "Q", "")
sourcevalue  = options.source
sourcevalue  = tonumber(sourcevalue)
oldclaims = claims
oldclaims = claims
claims = {}
claims = {}
Ligne 106 : Ligne 114 :
if k == sourceproperty then
if k == sourceproperty then
for l, m in pairs(prop) do
for l, m in pairs(prop) do
if m.datavalue.value['numeric-id'] == sourcevalue then
if formatDatavalue(m) == "Q" .. sourcevalue then
table.insert(claims, statement)
table.insert(claims, statement)
end
end
Ligne 119 : Ligne 127 :
return claims
return claims
end  
end  
function formatDatavalue(snak, formatting)
datatype = snak.datavalue.type
if datatype == 'wikibase-entityid' then
if formatting then
return formatEntityId("Q" .. tostring(snak.datavalue.value['numeric-id']))
else
return "Q" .. tostring(snak.datavalue.value['numeric-id'])
end
elseif datatype == 'string' then
return snak.datavalue.value
elseif datatype == 'time' then
lang = mw.language.getContentLanguage()
return lang:formatDate()
else return formatError( 'unknown-datavalue-type' )
end
end


function numOfClaims( options )
function numOfClaims( options )
claims = getClaims( options )  
claims = getClaims( options )  
if type(claims) ~= "table" then
if type(claims) ~= "table" then
return 0 -- à améliorer, mais c'est le plus simple avec le problème de clé des tables Wikibase
end
if claims == nil then
return 0
return 0
elseif claims == {} then
return 0
elseif claims[0] then -- table Wikibase non modifiée avec une clé 0 (buggy)
return #claims + 1
else
return #claims
end
end
return #claims + 1
end
end


function getClaimedIDs( options )  
function getClaimedIDs( options ) -- devrait plutôt être une option de function formatStatements
claims = getClaims( options )
claims = getClaims( options )
if type(claims) ~= 'table' then
if type(claims) ~= 'table' then
Ligne 145 : Ligne 172 :
end
end
return idlist
return idlist
end
function getQualifier( options )
claims = getClaims( options )
qualifier = options.qualifier
if claims == {} then
return nil
elseif #claims > 1 then
return formatError( 'ambiguous' )
else
qualifiers = {}
for i, j in pairs( claims[1].qualifiers[qualifier] ) do
table.insert(qualifiers, formatDatavalue(j))
end
return mw.text.listToText(qualifiers)
end
end
end


function formatStatements( options )--Format statement and concat them cleanly
function formatStatements( options )--Format statement and concat them cleanly
local formattedStatements = {}
local formattedStatements = {}
     for i, statement in pairs( getClaims( options ) ) do
local rawStatements = getClaims( options )
if rawStatements == nil then
return nil
end
     for i, statement in pairs( rawStatements ) do
         if options.rank == 'one' then
         if options.rank == 'one' then
             return formatStatement( statement, options ) --Output only one value
             return formatStatement( statement, options ) --Output only one value
Ligne 174 : Ligne 221 :
         return i18n['novalue']
         return i18n['novalue']
     elseif snak.snaktype == 'value' then
     elseif snak.snaktype == 'value' then
         return formatDatavalue( snak.datavalue, options )
         return formatDatavalue( snak, true)
     else
     else
         return formatError( 'unknown-snak-type' )
         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
    else
        return formatError( 'unknown-datavalue-type' )
     end
     end
end
end
Ligne 261 : Ligne 277 :
end
end


function p.getQualifier(frame)
return getQualifier(frame.args)
end
return p
return p

Version du 25 septembre 2013 à 15:15

La documentation pour ce module peut être créée à Module:Wikidata/doc

local i18n = {
    ["errors"] = {
        ["property-param-not-provided"] = "Paramètre propriété 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 possible"
    },
    ["somevalue"] = "''unknown value''",
    ["novalue"] = "''no value''"
}


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

function getId( id ) -- semble nécessaire pour récupérer l'ID correspondant à la page Wikipédia active
	entity =  getEntityFromId( id )
	return  entity.id
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 getClaims( options )
    if not options.property then
        return formatError( 'property-param-not-provided' )
    end
    --Get entity
    local entity = nil
    local property = string.lower(options.property)
    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 entity.claims[property] then
		claims = entity.claims[property]
	else 
		return nil
	end
	if options.excludespecial == 'true' then
    	oldclaims = claims
    	claims = {}
    	for i, statement in pairs(oldclaims) do
    		if statement.mainsnak.snaktype == 'value' then
    			table.insert(claims, statement)
    		end
		end
	end
	if options.targetvalue then
		targetvalue = options.targetvalue
		oldclaims = claims
		claims = {}
		for i, statement in pairs(oldclaims) do
			if formatDatavalue(statement.mainsnak) == targetvalue then
				table.insert(claims, statement)
			end
		end
	end
	if options.qualifier then -- ne marche que pour les propriétés de type item
		qualifier = options.qualifier
		qualifiervalue = options.qualifiervalue
		oldclaims = claims
		claims = {}
		for i, statement in pairs(oldclaims) do
			if statement.qualifiers[qualifier] then
				if qualifiervalue then
					for j, qualif in pairs(statement.qualifiers[qualifier]) do
						if formatDatavalue(qualif) == qualifiervalue then 
							table.insert(claims, statement)
						end
					end
				else
					table.insert(claims, statement) 
				end
			end
		end
	end
	if options.source then 
		if options.sourceproperty then 
			sourceproperty = options.sourceproperty
		else
			sourceproperty = "p248"
		end
		sourcevalue  = options.source
		oldclaims = claims
		claims = {}
		for i, statement in pairs(oldclaims) do
			if statement.references then
				for j, reference in pairs(statement.references) do
					for k, prop in pairs(reference.snaks) do
						if k == sourceproperty then
							for l, m in pairs(prop) do
								if formatDatavalue(m) == "Q" .. sourcevalue then				
									table.insert(claims, statement)
								end
							end
						end
					end
				end
			end
		end
				
	end
	return claims
end 

function formatDatavalue(snak, formatting)
	datatype = snak.datavalue.type
	if datatype == 'wikibase-entityid' then
		if formatting then 
			return formatEntityId("Q" .. tostring(snak.datavalue.value['numeric-id']))
		else
			return "Q" .. tostring(snak.datavalue.value['numeric-id'])
		end
	elseif datatype == 'string' then
		return snak.datavalue.value
	elseif datatype == 'time' then
		lang = mw.language.getContentLanguage()
		return lang:formatDate()
	else return formatError( 'unknown-datavalue-type' )
	end
end

function numOfClaims( options )
	claims = getClaims( options ) 
	if type(claims) ~= "table" then
		return 0
	elseif claims == {} then 
		return 0
	elseif claims[0] then -- table Wikibase non modifiée avec une clé 0 (buggy)
		return #claims + 1
	else
	return #claims
	end
end

function getClaimedIDs( options ) -- devrait plutôt être une option de function formatStatements
	claims = getClaims( options )
	if type(claims) ~= 'table' then
		return formatError( 'unidentified-error' )
	end
	local idlist = ''
		for i, claim in pairs(claims) do
			if options.rank == 'one' then
				return "Q" .. claims[0].mainsnak.datavalue.value["numeric-id"]
			else
				idlist = idlist .. " Q" .. claim.mainsnak.datavalue.value["numeric-id"] -- liste simple permettant de retrouver facilement un ID donné
			end
		end
	return idlist
end

function getQualifier( options )
	claims = getClaims( options )
	qualifier = options.qualifier
	if claims == {} then
		return nil
	elseif #claims > 1 then
		return formatError( 'ambiguous' )
	else 
		qualifiers = {}
		for i, j in pairs( claims[1].qualifiers[qualifier] ) do
			table.insert(qualifiers, formatDatavalue(j))
		end
		return mw.text.listToText(qualifiers)
	end
end

function formatStatements( options )--Format statement and concat them cleanly
	local formattedStatements = {}
	local rawStatements = getClaims( options )
	if rawStatements == nil then
			return nil
	end
    for i, statement in pairs( rawStatements ) 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, true)
    else
        return formatError( 'unknown-snak-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.id(frame)
        return getId(frame.args[1])
end

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

function p.numOfClaims(frame)
    return numOfClaims( frame.args )
end 

function p.getClaimedIDs(frame)
	return getClaimedIDs(frame.args)
end

function p.getQualifier(frame)
	return getQualifier(frame.args)
end
return p