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

De Lagny-sur-Marne Wiki
Aller à la navigation Aller à la recherche
0x010D (discussion | contributions)
Aucun résumé des modifications
0x010D (discussion | contributions)
Aucun résumé des modifications
Ligne 174 : Ligne 174 :
local entity = args.entity
local entity = args.entity
  if type(entity) ~= 'table' then
  if type(entity) ~= 'table' then
         entity = getEntity( args.entity )
         entity = mw.wikibase.getEntityObject( args.entity )
     end
     end
if (not entity) or (not entity.claims) then
if (not entity) or (not entity.claims) then

Version du 17 août 2015 à 18:37

local p = {}

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) if target == nil then return true end return samevalue(claim.mainsnak, 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

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 hasqualifier(claim, qualifier, qualifiervalues)

   qualifier = string.upper(qualifier)
   if not qualifier then -- si aucun qualificatif est demandé, ça passe
   	return true
   end
  	if not claim.qualifiers or not claim.qualifiers[qualifier] then
  		return false
  	end
  	if (not qualifiervalues) or (qualifiervalues == {}) then
  		return true -- si aucune valeur spécifique n'est exigée
  	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(statement, source, sourceproperty)

   sourceproperty = string.upper(sourceproperty or 'P248')
   local sourcevalue = string.upper(source or )

if not statement.references or not statement.references[sourceproperty] then return false end if not source then -- si toutes les sources sont valides, du moment qu'elles utilisent sourceproperty return true end for i, j in pairs(statement.references[sourceproperty]) do if p.getRawvalue(j) == source then return true end end return true end

local function hasdate(statement)

   if statement.qualifiers and (statement.qualifiers['P585'] or statement.qualifiers['P580'] or statement.qualifiers['P582']) then
   	return true
   end

return false end

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

local function isSpecial(snak) if snak.snaktype == 'value' then return false end return true end

local function numval(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 comparedate(a, b) -- returns true if a is earlier than B or if a has a date but not b if a and b then return a.timestamp < b.timestamp elseif a then return true end end

local function chronosort(claims, inverted) table.sort(claims, function(a,b) local timeA = p.getDate(a) local timeB = p.getDate(b) if inverted then return comparedate(timeB, timeA) else return comparedate(timeA, timeB) 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 formatError( '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 if not args.rank then args.rank = 'best' end local claims = {}

   -- ~=  lorsque le paramètre est écrit mais laissé blanc dans une fonction frame
   for i, statement in pairs(entity.claims[property]) do
   	if
   		(
   		not args.excludespecial
   		or
   		not (isSpecial(statement.mainsnak))
   		)
   	and
   		(

not args.targetvalue or hastargetvalue(statement, args.targetvalue) )

   	and
   		(
   		not args.qualifier
   		or
   		hasqualifier(statement, args.qualifier, args.qualifiervalue or args.qualifiervalues)
   		)

and ( not args.source or hassource(statement, args.source, args.sourceproperty) ) and not args.isinlanguage or isinlanguage(statement.mainsnak, args.isinlanguage) and args.rank == 'best' -- rank == best est traité à a fin or hasrank(statement, rank) then table.insert(claims, statement) end end if #claims == 0 then return nil end

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

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

return p