« 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 1 : Ligne 1 :
local p = {}
local p = {}
local datequalifiers = {'P585', 'P571', 'P580', 'P582'}


local function severalProperties(args)
local function severalProperties(args)
Ligne 5 : Ligne 6 :
for i, j in pairs(args.property) do
for i, j in pairs(args.property) do
newargs.property = j
newargs.property = j
local newclaims = p.getClaims(newargs)
local newstatements = p.getstatements(newargs)
if claims then
if statements then
return claims
return statements
end
end
end
end
end
end


local function hastargetvalue(claim, target)
local function hastargetvalue(statement, target)
if target == nil then
if target == nil then
return true
return true
end
end
return samevalue(claim.mainsnak, target)
return samevalue(statement.mainsnak, target)
end
end


local function hasrank(claim, target)
local function hasrank(statement, target)
if target == 'valid' then
if target == 'valid' then
return hasrank(claim, 'preferred') or hasrank(claim, 'normal')
return hasrank(statement, 'preferred') or hasrank(statement, 'normal')
end
end
if claim.rank == target then
if statement.rank == target then
return true
return true
end
end
Ligne 29 : Ligne 30 :
end
end


local function bestranked(claims)
local function bestranked(statements)
if not claims then
if not statements then
return nil
return nil
end
end
local preferred, normal = {}, {}
local preferred, normal = {}, {}
for i, j in pairs(claims) do
for i, j in pairs(statements) do
if j.rank == 'preferred' then
if j.rank == 'preferred' then
table.insert(preferred, j)
table.insert(preferred, j)
Ligne 48 : Ligne 49 :
end
end


local function hasqualifier(claim, qualifier, qualifiervalues)
local function hasqualifier(statement, qualifier, qualifiervalues)
    qualifier = string.upper(qualifier)
local statementqualifs = statements.qualifiers
 
   if (not statementqualifs) or not(statementqualifs[qualifier]) then
    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
   return false
   end
   end


   if (not qualifiervalues) or (qualifiervalues == {}) then
   if (not qualifiervalues) or (qualifiervalues == {}) then
   return true -- si aucune valeur spécifique n'est exigée
   return true -- si aucune valeur spécifique n'est exigée, c'est bon
   end
   end


Ligne 66 : Ligne 63 :
   end
   end


   for i, j in pairs(claim.qualifiers[qualifier]) do
   for i, j in pairs(statement.qualifiers[qualifier]) do
   local val = p.getRawvalue(j)
   local val = p.getRawvalue(j)
   for k, l in pairs(qualifiervalues) do
   for k, l in pairs(qualifiervalues) do
Ligne 77 : Ligne 74 :
  end
  end


local function hassource(statement, source, sourceproperty)
local function hassource(statement, targetsource, sourceproperty)
     sourceproperty = string.upper(sourceproperty or 'P248')
     sourceproperty = sourceproperty or 'P248'
    local sourcevalue = string.upper(source or '')
if not statement.references or not statement.references[sourceproperty] then
if not statement.references or not statement.references[sourceproperty] then
return false
return false
end
end
if not source then -- si toutes les sources sont valides, du moment qu'elles utilisent sourceproperty
if not targetsource then -- si toutes les sources sont valides, du moment qu'elles utilisent sourceproperty
return true
return true
end
end
for i, j in pairs(statement.references[sourceproperty]) do
for _, source in pairs(statement.references[sourceproperty]) do
if p.getRawvalue(j) == source then
if (source.snaktype == 'value') and (source.datavalue.value['numeric-id'] == targetsource) then
return true
return true
end
end
Ligne 95 : Ligne 91 :


local function hasdate(statement)
local function hasdate(statement)
    if statement.qualifiers and (statement.qualifiers['P585'] or statement.qualifiers['P580'] or statement.qualifiers['P582']) then
local statementqualifs = statements.qualifiers
    return true
if not statementqualifs then
    end
return false
end
for _, qualif in pairs(statementqualifs) do
if statementsqualifs[qualif] and statementsqualifs[qualif][1].snaktype == 'value' then
return true
end
end
return false
return false
end
end
Ligne 115 : Ligne 117 :
end
end


local function numval(claims, numval) -- retourn les numval premières valeurs de la table claims
local function numval(statements, numval) -- retourn les numval premières valeurs de la table statements
     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 #statements <= numval then
     return claims
     return statements
     end
     end
     local newclaims = {}
     local newstatements = {}
     while #newclaims < numval do
     while #newstatements < numval do
     table.insert(newclaims, claims[#newclaims + 1])
     table.insert(newstatements, statements[#newstatements + 1])
     end
     end
     return newclaims
     return newstatements
end
end


Ligne 135 : Ligne 137 :
end
end


local function chronosort(claims, inverted)
local function chronosort(statements, inverted)
table.sort(claims, function(a,b)
local function getTimestamp(statement)
local statementqualifs = statements.qualifiers
if not statementqualif then
return nil
end
for _, qualif in pairs(datequalifiers) do
if statementqualif[qualif] and statementqualif[1].snaktype == 'value' then
return statementqualif[1].datavalue.value
end
end
end
table.sort(statements, function(a,b)
local timeA = p.getDate(a)
local timeA = p.getDate(a)
local timeB = p.getDate(b)
local timeB = p.getDate(b)
Ligne 146 : Ligne 159 :
end
end
)
)
return claims
return statements
end
end


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


function p.getClaims( args ) -- returns a table of the claims matching some conditions given in args
function p.getstatements( args ) -- returns a table of the statements matching some conditions given in args
if args.claims then -- if claims have already been set, return them
if args.statements then -- if statements have already been set, return them
return args.claims
return args.statements
end
end
if not args.property then
if not args.property then
Ligne 176 : Ligne 189 :
         entity = mw.wikibase.getEntityObject( args.entity )
         entity = mw.wikibase.getEntityObject( args.entity )
     end
     end
if (not entity) or (not entity.claims) then
if (not entity) or (not entity.statements) then
return nil
return nil
end
end
local property = string.upper(args.property)
local property = string.upper(args.property)
if not entity.claims[property] then
if not entity.statements[property] then
return nil
return nil
end
end
Ligne 186 : Ligne 199 :
args.rank = 'best'
args.rank = 'best'
end
end
local claims = {}
local statements = {}
     -- ~= '' 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
     for i, statement in pairs(entity.claims[property]) do
     for i, statement in pairs(entity.statements[property]) do
     if
     if
     (
     (
Ligne 222 : Ligne 235 :
hasrank(statement, rank)
hasrank(statement, rank)
then
then
table.insert(claims, statement)
table.insert(statements, statement)
end
end
end
end
if #claims == 0 then
if #statements == 0 then
return nil
return nil
end
end
     if args.rank == 'best' then
     if args.rank == 'best' then
     claims = bestranked(claims)
     statements = bestranked(statements)
     end
     end
     if args.sorttype then
     if args.sorttype then
     claims = p.sortclaims(claims, args.sorttype)
     statements = p.sortstatements(statements, args.sorttype)
     end
     end


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


return p
return p

Version du 17 août 2015 à 20:06

local p = {} local datequalifiers = {'P585', 'P571', 'P580', 'P582'}

local function severalProperties(args) local newargs = args for i, j in pairs(args.property) do newargs.property = j local newstatements = p.getstatements(newargs) if statements then return statements end end end

local function hastargetvalue(statement, target) if target == nil then return true end return samevalue(statement.mainsnak, target) end

local function hasrank(statement, target) if target == 'valid' then return hasrank(statement, 'preferred') or hasrank(statement, 'normal') end if statement.rank == target then return true end return false end

local function bestranked(statements) if not statements then return nil end local preferred, normal = {}, {} for i, j in pairs(statements) 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(statement, qualifier, qualifiervalues) local statementqualifs = statements.qualifiers

  	if (not statementqualifs) or not(statementqualifs[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(statement.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, targetsource, sourceproperty)

   sourceproperty = sourceproperty or 'P248'

if not statement.references or not statement.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(statement.references[sourceproperty]) do if (source.snaktype == 'value') and (source.datavalue.value['numeric-id'] == targetsource) then return true end end return true end

local function hasdate(statement) local statementqualifs = statements.qualifiers if not statementqualifs then return false end for _, qualif in pairs(statementqualifs) do if statementsqualifs[qualif] and statementsqualifs[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 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(statements, numval) -- retourn les numval premières valeurs de la table statements

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

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(statements, inverted) local function getTimestamp(statement) local statementqualifs = statements.qualifiers if not statementqualif then return nil end for _, qualif in pairs(datequalifiers) do if statementqualif[qualif] and statementqualif[1].snaktype == 'value' then return statementqualif[1].datavalue.value end end end table.sort(statements, 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 statements end

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

function p.getstatements( args ) -- returns a table of the statements matching some conditions given in args if args.statements then -- if statements have already been set, return them return args.statements 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.statements) then return nil end local property = string.upper(args.property) if not entity.statements[property] then return nil end if not args.rank then args.rank = 'best' end local statements = {}

   -- ~=  lorsque le paramètre est écrit mais laissé blanc dans une fonction frame
   for i, statement in pairs(entity.statements[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(statements, statement) end end if #statements == 0 then return nil end

   if args.rank == 'best' then
   	statements = bestranked(statements)
   end
   if args.sorttype then
   	statements = p.sortstatements(statements, args.sorttype)
   end

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

return p