« Module:Wikidata/Récup » : différence entre les versions
Aucun résumé des modifications |
Aucun résumé des modifications |
||
| Ligne 6 : | 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 | local newclaims = p.getClaims(newargs) | ||
if | if claims then | ||
return | return claims | ||
end | end | ||
end | end | ||
end | end | ||
local function hastargetvalue( | local function hastargetvalue(claim, target) | ||
if target == nil then | if target == nil then | ||
return true | return true | ||
end | end | ||
return samevalue( | return samevalue(claim.mainsnak, target) | ||
end | end | ||
local function hasrank( | local function hasrank(claim, target) | ||
if target == 'valid' then | if target == 'valid' then | ||
return hasrank( | return hasrank(claim, 'preferred') or hasrank(claim, 'normal') | ||
end | end | ||
if | if claim.rank == target then | ||
return true | return true | ||
end | end | ||
| Ligne 30 : | Ligne 30 : | ||
end | end | ||
local function bestranked( | local function bestranked(claims) | ||
if not | if not claims then | ||
return nil | return nil | ||
end | end | ||
local preferred, normal = {}, {} | local preferred, normal = {}, {} | ||
for i, j in pairs( | for i, j in pairs(claims) do | ||
if j.rank == 'preferred' then | if j.rank == 'preferred' then | ||
table.insert(preferred, j) | table.insert(preferred, j) | ||
| Ligne 49 : | Ligne 49 : | ||
end | end | ||
local function hasqualifier( | local function hasqualifier(claim, qualifier, qualifiervalues) | ||
local | local claimqualifs = claim.qualifiers | ||
if (not | if (not claimqualifs) or not(claimqualifs[qualifier]) then | ||
return false | return false | ||
end | end | ||
| Ligne 63 : | Ligne 63 : | ||
end | end | ||
for i, j in pairs( | for i, j in pairs(claim.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 74 : | Ligne 74 : | ||
end | end | ||
local function hassource( | local function hassource(claim, targetsource, sourceproperty) | ||
sourceproperty = sourceproperty or 'P248' | sourceproperty = sourceproperty or 'P248' | ||
if not | if not claim.references or not claim.references[sourceproperty] then | ||
return false | return false | ||
end | end | ||
| Ligne 82 : | Ligne 82 : | ||
return true | return true | ||
end | end | ||
for _, source in pairs( | for _, source in pairs(claim.references[sourceproperty]) do | ||
if (source.snaktype == 'value') and (source.datavalue.value['numeric-id'] == targetsource) then | if (source.snaktype == 'value') and (source.datavalue.value['numeric-id'] == targetsource) then | ||
return true | return true | ||
| Ligne 90 : | Ligne 90 : | ||
end | end | ||
local function hasdate( | local function hasdate(claim) | ||
local | local claimqualifs = claims.qualifiers | ||
if not | if not claimqualifs then | ||
return false | return false | ||
end | end | ||
for _, qualif in pairs( | for _, qualif in pairs(claimqualifs) do | ||
if | if claimsqualifs[qualif] and claimsqualifs[qualif][1].snaktype == 'value' then | ||
return true | return true | ||
end | end | ||
| Ligne 117 : | Ligne 117 : | ||
end | end | ||
local function numval( | 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 ? | local numval = tonumber(numval) or 0 -- raise a error if numval is not a positive integer ? | ||
if # | if #claims <= numval then | ||
return | return claims | ||
end | end | ||
local | local newclaims = {} | ||
while # | while #newclaims < numval do | ||
table.insert( | table.insert(newclaims, claims[#newclaims + 1]) | ||
end | end | ||
return | return newclaims | ||
end | end | ||
local function chronosort(claims, inverted) | |||
local function getTimestamp(claim) | |||
local claimqualifs = claim.qualifiers | |||
if not claimqualifs then | |||
local function chronosort( | |||
local function getTimestamp( | |||
local | |||
if not | |||
return nil | return nil | ||
end | end | ||
for _, qualif in pairs(datequalifiers) do | for _, qualif in pairs(datequalifiers) do | ||
if | if claimqualifs[qualif] and claimqualifs[qualif][1].snaktype == 'value' then | ||
return | return claimqualifs[qualif][1].datavalue.value.time | ||
end | end | ||
end | end | ||
end | end | ||
table.sort( | table.sort(claims, function(a,b) | ||
local timeA = | local timeA = getTimestamp(a) or '' | ||
local timeB = | local timeB = getTimestamp(b) or '' | ||
if inverted then | if inverted then | ||
return | return timeA > timeB | ||
else | else | ||
return | return timeB > timeA | ||
end | end | ||
end | end | ||
) | ) | ||
return | return claims | ||
end | end | ||
function p. | function p.sortclaims(claims, sorttype) | ||
if sorttype == 'chronological' then | if sorttype == 'chronological' then | ||
return chronosort( | return chronosort(claims) | ||
elseif sorttype == 'inverted' then | elseif sorttype == 'inverted' then | ||
return chronosort( | return chronosort(claims, true) | ||
elseif type(sorttype) == 'function' then | elseif type(sorttype) == 'function' then | ||
table.sort( | table.sort(claims, sorttype) | ||
return | return claims | ||
end | end | ||
return | return claims | ||
end | end | ||
function p. | function p.getClaims( args ) -- returns a table of the claims matching some conditions given in args | ||
if args. | if args.claims then -- if claims have already been set, return them | ||
return args. | return args.claims | ||
end | end | ||
if not args.property then | if not args.property then | ||
| Ligne 189 : | Ligne 181 : | ||
entity = mw.wikibase.getEntityObject( args.entity ) | entity = mw.wikibase.getEntityObject( args.entity ) | ||
end | end | ||
if (not entity) or (not entity. | if (not entity) or (not entity.claims) then | ||
return nil | return nil | ||
end | end | ||
local property = string.upper(args.property) | local property = string.upper(args.property) | ||
if not entity. | if not entity.claims[property] then | ||
return nil | return nil | ||
end | end | ||
| Ligne 199 : | Ligne 191 : | ||
args.rank = 'best' | args.rank = 'best' | ||
end | end | ||
local | local claims = {} | ||
-- ~= '' 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, | for i, claim in pairs(entity.claims[property]) do | ||
if | if | ||
( | ( | ||
not args.excludespecial | not args.excludespecial | ||
or | or | ||
not (isSpecial( | not (isSpecial(claim.mainsnak)) | ||
) | ) | ||
and | and | ||
| Ligne 212 : | Ligne 204 : | ||
not args.targetvalue | not args.targetvalue | ||
or | or | ||
hastargetvalue( | hastargetvalue(claim, args.targetvalue) | ||
) | ) | ||
and | and | ||
| Ligne 218 : | Ligne 210 : | ||
not args.qualifier | not args.qualifier | ||
or | or | ||
hasqualifier( | hasqualifier(claim, args.qualifier, args.qualifiervalue or args.qualifiervalues) | ||
) | ) | ||
and | and | ||
| Ligne 224 : | Ligne 216 : | ||
not args.source | not args.source | ||
or | or | ||
hassource( | hassource(claim, args.source, args.sourceproperty) | ||
) | ) | ||
and | and | ||
not args.isinlanguage | not args.isinlanguage | ||
or | or | ||
isinlanguage( | isinlanguage(claim.mainsnak, args.isinlanguage) | ||
and | and | ||
args.rank == 'best' -- rank == best est traité à a fin | args.rank == 'best' -- rank == best est traité à a fin | ||
or | or | ||
hasrank( | hasrank(claim, rank) | ||
then | then | ||
table.insert( | table.insert(claims, claim) | ||
end | end | ||
end | end | ||
if # | if #claims == 0 then | ||
return nil | return nil | ||
end | end | ||
if args.rank == 'best' then | if args.rank == 'best' then | ||
claims = bestranked(claims) | |||
end | end | ||
if args.sorttype then | if args.sorttype then | ||
claims = p.sortclaims(claims, args.sorttype) | |||
end | end | ||
if args.numval then | if args.numval then | ||
return numval( | return numval(claims, args.numval) | ||
end | end | ||
return | return claims | ||
end | end | ||
return p | return p | ||
Version du 18 août 2015 à 09:58
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 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) local claimqualifs = claim.qualifiers
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 (source.snaktype == 'value') and (source.datavalue.value['numeric-id'] == 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 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 chronosort(claims, inverted) local function getTimestamp(claim) local claimqualifs = claim.qualifiers if not claimqualifs then return nil end for _, qualif in pairs(datequalifiers) do if claimqualifs[qualif] and claimqualifs[qualif][1].snaktype == 'value' then return claimqualifs[qualif][1].datavalue.value.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 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, claim in pairs(entity.claims[property]) do if ( not args.excludespecial or not (isSpecial(claim.mainsnak)) ) and (
not args.targetvalue or hastargetvalue(claim, args.targetvalue) )
and ( not args.qualifier or hasqualifier(claim, args.qualifier, args.qualifiervalue or args.qualifiervalues) )
and ( not args.source or hassource(claim, args.source, args.sourceproperty) ) and not args.isinlanguage or isinlanguage(claim.mainsnak, args.isinlanguage) and args.rank == 'best' -- rank == best est traité à a fin or hasrank(claim, rank) then table.insert(claims, claim) 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