« Module:Wikidata/Récup » : différence entre les versions

De Lagny-sur-Marne Wiki
Aller à la navigation Aller à la recherche
0x010D (discussion | contributions)
esssai
0x010D (discussion | contributions)
Aucun résumé des modifications
Ligne 16 : Ligne 16 :
local function hastargetvalue(claim, target)
local function hastargetvalue(claim, target)
return basic.getMainId(claim) == target
return basic.getMainId(claim) == target
end
local function hasrank(claim, target)
if target == 'valid' then
return hasrank(claim, 'preferred') or hasrank(claim, 'normal')
end
if claim.rank == target then
return true
end
return false
end
end


Ligne 45 : Ligne 35 :
return normal
return normal
end
end
end
local function withrank(claims, target)
if target == 'best' then
return bestranked(claims)
end
for pos, claim in pairs(claims)  do
if target == 'valid' then
if claim.rank == 'deprecated' then
table.remove(claims, pos)
end
elseif claim.rank ~= target then
table.remove(claims, pos)
end
end
return claims
end
end


local function hasqualifier(claim, qualifier, qualifiervalues)
local function hasqualifier(claim, qualifier, qualifiervalues)
local claimqualifs = string.upper(claim.qualifiers)
local claimqualifs = claim.qualifiers
qualifier = string.upper(qualifier)
   if (not claimqualifs) or not(claimqualifs[qualifier]) then
   if (not claimqualifs) or not(claimqualifs[qualifier]) then
   return false
   return false
Ligne 111 : Ligne 118 :
end
end


local function numval(claims, numval) -- retourn les numval premières valeurs de la table claims
local function firstvals(claims, numval) -- retourn les numval premières valeurs de la table claims
     local numval = tonumber(numval) or 0 -- raise a error if numval is not a positive integer ?
     local numval = tonumber(numval) or 0 -- raise a error if numval is not a positive integer ?
     if #claims <= numval then
     if #claims <= numval then
Ligne 182 : Ligne 189 :
if not entity.claims[property] then
if not entity.claims[property] then
return nil
return nil
end
if args.rank == 'best' then --rang par défaut, filré à la fin pour ne pas exlure les données de rang inférieur mais répondant à la requête
args.rank = nil
end
end
local claims = entity.claims[property]
local claims = entity.claims[property]
Ligne 191 : Ligne 195 :
end
end
     -- ~= '' lorsque le paramètre est écrit mais laissé blanc dans une fonction frame
     -- ~= '' lorsque le paramètre est écrit mais laissé blanc dans une fonction frame
   
     local filters = { -- liste des arguments déclenchat un filtre des données
     local filters = { -- liste des arguments déclenchat un filtre des données
targetvalue = {hastargetvalue, args.targetvalue},
    --[numéro] = {param déclenche, fonction à utiliser, paramètres à utiliser}
rank = {hasrank, args.rank},
[1] = {
qualifier = {hasqualifier, args.qualifier, args.qualifiervalue},
'targetvalue',
source = {hassource, args.source, args.sourceproperty},
hastargetvalue,
withdate = {hasdate},
{'targetvalue'}
isinlanguage = {'isinlanguagee', args.isinlanguage},
},
rank = {hasrank, args.rank},
[3] = {
'qualifier',
hasqualifier,
{'qualifier', 'qualifiervalue'}
},
[4] = {
'source',
hassource,
{'source', 'sourceproperty'}
},
[5] = {
'withdate',
hasdate,
{},
},
[6] = {
'withdate',
hasdate,
{},
},
[7] = {
'withdate',
hasdate,
{},
},
[8] = {
'excludespecial',
notspecial,
{},
},
}
}
 
for i, filter in pairs(filters) do
    for arg, argval in pairs(args) do
if args[filter[1]] then
    local filter = filters[arg]
local filterfun = filter[2]
    if filter then
local filterarg1, filterarg2, filterarg3 = args[filter[3][1]], args[filter[3][2]], args[filter[3][3]]
    local valid = false
for pos, claim in pairs(claims) do
    for k, claim in pairs(claims) do
local valid = filterfun(claim, filterarg1, filterarg2, fiterarg3)
    local valid = filter[1](claim, filter[2], filter[3], filter[4])
if not valid then
    if (not valid) then
table.remove(claims, pos)
    table.remove(claims, claim)
end
    end
end
    end
end
    end
end
end
claims = withrank(claims, args.rank or 'best')
if args.sorttype then
if args.sorttype then
claims = p.sortclaims(claims, args.sorttype)
claims = p.sortclaims(claims, args.sorttype)
end
end
    if (not args.rank) then
if args.numval then
    claims = bestranked(claims)
claims = firstvals(claims, args.numval)
    end
end
return claims
    return claims
end
end


return p
return p

Version du 18 août 2015 à 12:54

local p = {} local datequalifiers = {'P585', 'P571', 'P580', 'P582'} local basic = require "Module:Wikibase"

local function severalProperties(args) local newargs = args for i, j in pairs(args.property) do newargs.property = j local newclaims = p.getClaims(newargs) if claims then return claims end end end

local function hastargetvalue(claim, target) return basic.getMainId(claim) == target end

local function bestranked(claims) if not claims then return nil end local preferred, normal = {}, {} for i, j in pairs(claims) do if j.rank == 'preferred' then table.insert(preferred, j) elseif j.rank == 'normal' then table.insert(normal, j) end end if #preferred > 0 then return preferred else return normal end end

local function withrank(claims, target) if target == 'best' then return bestranked(claims) end for pos, claim in pairs(claims) do if target == 'valid' then if claim.rank == 'deprecated' then table.remove(claims, pos) end elseif claim.rank ~= target then table.remove(claims, pos) end end return claims end

local function hasqualifier(claim, qualifier, qualifiervalues) local claimqualifs = claim.qualifiers qualifier = string.upper(qualifier)

  	if (not claimqualifs) or not(claimqualifs[qualifier]) then
  		return false
  	end
  	if (not qualifiervalues) or (qualifiervalues == {}) then
  		return true -- si aucune valeur spécifique n'est exigée, c'est bon
  	end
  if type(qualifiervalues) == 'string' then
  		qualifiervalues = {qualifiervalues}
  	end
  	for i, j in pairs(claim.qualifiers[qualifier]) do
  		local val = p.getRawvalue(j)
  		for k, l in pairs(qualifiervalues) do
  			if l == val then
  				return true
  			end
  		end
  end
  return false
end

local function hassource(claim, targetsource, sourceproperty)

   sourceproperty = sourceproperty or 'P248'

if not claim.references or not claim.references[sourceproperty] then return false end if not targetsource then -- si toutes les sources sont valides, du moment qu'elles utilisent sourceproperty return true end for _, source in pairs(claim.references[sourceproperty]) do if basic.getId(source) == targetsource then return true end end return true end

local function hasdate(claim) local claimqualifs = claims.qualifiers if not claimqualifs then return false end for _, qualif in pairs(claimqualifs) do if claimsqualifs[qualif] and claimsqualifs[qualif][1].snaktype == 'value' then return true end end return false end

local function isinlanguage(snak, lang) -- ne fonctionne que pour les monolingualtext / étendre aux autres types en utilisant les qualifiers ? if basic.isSpecial(snak) then return false end if snak.datavalue.type == 'monolingualtext' and snak.datavalue.value.language == lang then return true end return false end

local function firstvals(claims, numval) -- retourn les numval premières valeurs de la table claims

   local numval = tonumber(numval) or 0 -- raise a error if numval is not a positive integer ?
   if #claims <= numval then
   	return claims
   end
   local newclaims = {}
   while #newclaims < numval do
   	table.insert(newclaims, claims[#newclaims + 1])
   end
   return newclaims

end

local function chronosort(claims, inverted) local function getTimestamp(claim) local claimqualifs = claim.qualifiers if not claimqualifs then return nil end for _, qualif in pairs(datequalifiers) do local vals = claimqualifs[qualif] if vals and basic.isValue(vals[1]) then return basic.getValue(vals[1]).time end end end table.sort(claims, function(a,b) local timeA = getTimestamp(a) or local timeB = getTimestamp(b) or if inverted then return timeA > timeB else return timeB > timeA end end ) return claims end

function p.sortclaims(claims, sorttype) if sorttype == 'chronological' then return chronosort(claims) elseif sorttype == 'inverted' then return chronosort(claims, true) elseif type(sorttype) == 'function' then table.sort(claims, sorttype) return claims end return claims end

function p.getClaims( args ) -- returns a table of the claims matching some conditions given in args if args.claims then -- if claims have already been set, return them return args.claims end if not args.property then return error( 'property-param-not-provided' ) end if type(args.property) == 'table' then return severalProperties(args) end --Get entity local entity = args.entity

	if type(entity) ~= 'table' then
       entity = mw.wikibase.getEntityObject( args.entity )
   end

if (not entity) or (not entity.claims) then return nil end local property = string.upper(args.property) if not entity.claims[property] then return nil end local claims = entity.claims[property] if not claims then return nil end

   -- ~=  lorsque le paramètre est écrit mais laissé blanc dans une fonction frame
   local filters = { -- liste des arguments déclenchat un filtre des données
   	--[numéro] = {param déclenche, fonction à utiliser, paramètres à utiliser}

[1] = { 'targetvalue', hastargetvalue, {'targetvalue'} }, [3] = { 'qualifier', hasqualifier, {'qualifier', 'qualifiervalue'} }, [4] = { 'source', hassource, {'source', 'sourceproperty'} }, [5] = { 'withdate', hasdate, {}, }, [6] = { 'withdate', hasdate, {}, }, [7] = { 'withdate', hasdate, {}, }, [8] = { 'excludespecial', notspecial, {}, }, } for i, filter in pairs(filters) do if args[filter[1]] then local filterfun = filter[2] local filterarg1, filterarg2, filterarg3 = args[filter[3][1]], args[filter[3][2]], args[filter[3][3]] for pos, claim in pairs(claims) do local valid = filterfun(claim, filterarg1, filterarg2, fiterarg3) if not valid then table.remove(claims, pos) end end end end

claims = withrank(claims, args.rank or 'best') if args.sorttype then claims = p.sortclaims(claims, args.sorttype) end

	if args.numval then
		claims = firstvals(claims, args.numval)
	end
   return claims

end

return p