Modul:OrganizeClaims
Utseende
Konsoll:
=mw.dumpObject(p.setOrder(mw.wikibase.getEntity('Q938247').claims['P569'],'P2446','references')[1].references[1].snaks)
=mw.dumpObject(p.setOrder(mw.wikibase.getEntity('Q938247').claims['P569'],'P2446','references')[1].references)
=mw.dumpObject(mw.wikibase.formatValues(p.setOrder(mw.wikibase.getEntity('Q938247').claims['P569'],'P2446','references')[1].references[1].snaks))
local OrganizeClaims = {}
--- How to order qualifiers for a specific claim
-- These are specific for each property.
local qualOrder = {
P54 = { 'P580', 'P582' }
}
--- How to order references for a specific claim
-- These are specific for each property.
local refOrder = {
P54 = { 'P580', 'P582' }
}
--- Split the flags and ids in two separate structures
-- @param varargs that are either strings representing flags or ids, or a table of such strings.
-- @return flags identified among the varargs
-- @return ids identified among the varargs
function OrganizeClaims.splitFlagsAndIds( ... )
local flags = {}
local ids = {}
local exist = {}
for _,v in ipairs( { ... } ) do
local tpe = type( v )
if tpe == 'table' then
local tmpFlags, tmpIds = OrganizeClaims.splitFlagsAndIds( unpack( v ) )
for k,w in pairs( tmpFlags ) do
flags[k] = w
end
for _,w in ipairs( tmpIds ) do
if not exist[w] then
table.insert( ids, w )
exist[w] = true
end
end
elseif tpe == 'string' then
if mw.wikibase.isValidEntityId( v ) then
if not exist[v] then
table.insert( ids, v )
exist[v] = true
end
else
flags[v] = true
end
end
end
return flags, ids
end
--- Buil a hash for redirection during sorting
-- @param ids as a list of identifiers
-- @return redirection as a hash
function OrganizeClaims.buildRedirection( ids )
local redirection = {}
for i,v in ipairs( ids ) do
redirection[v] = i
end
return redirection
end
--- Set order of qualifiers for the claim
-- Changes internal qualifiers-order in the claim.
-- @param claim to be changed
-- @param redirection is a hash used during sorting
-- @param options holds additional configuration
-- @return claim for further chaining
function OrganizeClaims.setQualifiersOrder( claim, redirection, options )
redirection = redirection or {}
options = options or { undefined = 999999 }
if not claim['qualifiers-order'] then -- normally this should be set if there are any qualifiers
claim['qualifiers-order'] = {}
end
local sortFunc = function( a, b ) return (redirection[a] or options['undefined']) < (redirection[b] or options['undefined']) end
table.sort( claim['qualifiers-order'], sortFunc )
return claim
end
--- Set order of references for the claim
-- Changes internal snaks-order in the claim.
-- @param claim to be changed
-- @param redirection is a hash used during sorting
-- @param options holds additional configuration
-- @return claim for further chaining
function OrganizeClaims.setReferencesOrder( claim, redirection, options )
redirection = redirection or {}
options = options or { undefined = 999999 }
for k,v in pairs( claim.references or {} ) do
if not claim['snaks-order'] then -- normally this should be set if there are any qualifiers
claim['snaks-order'] = {}
end
local sortFunc = function( a, b ) return (redirection[a] or options['undefined']) < (redirection[b] or options['undefined']) end
table.sort( v['snaks-order'], sortFunc )
end
return claim
end
--- Set order for claims
-- Changes internal qualifiers-order and snaks-order in the claims.
-- @param claims list of claims
-- @param vararg list of flags and ids
-- @return claims for further chaining
function OrganizeClaims.setOrder( claims, ... )
local flags, ids = OrganizeClaims.splitFlagsAndIds( ... )
local qualifiersRedirection = nil
local referencesRedirection = nil
local options = {}
options['undefined'] = ((flags['qualifier-undef-first'] or flags['udf'] or flags['undef-first']) and -999999)
or ((flags['qualifier-undef-last'] or flags['udl'] or flags['undef-last']) and 999999)
or 999999 -- this is the default, and must be set unless undefined properties will make the code fail in the else clause
if flags['qualifier-system-order'] or flags['qso'] or flags['system-order'] then
qualifiersRedirection = mw.wikibase.getPropertyOrder()
elseif flags['qualifier-library-order'] or flags['qlo'] or flags['library-order'] then
qualifiersRedirection = OrganizeClaims.buildRedirection( qualOrder[claim.property] or ids )
else
qualifiersRedirection = OrganizeClaims.buildRedirection( ids )
end
if flags['reference-system-order'] or flags['rso'] or flags['system-order'] then
referencesRedirection = mw.wikibase.getPropertyOrder()
elseif flags['reference-library-order'] or flags['rlo'] or flags['library-order'] then
referencesRedirection = OrganizeClaims.buildRedirection( refOrder[claim.property] or ids )
else
referencesRedirection = OrganizeClaims.buildRedirection( ids )
end
local setQualifiers = flags['qualifiers']
or flags['qualifier-system-order'] or flags['qso']
or flags['qualifier-library-order'] or flags['qlo']
local setReferences = flags['references']
or flags['reference-system-order'] or flags['rso']
or flags['reference-library-order'] or flags['rlo']
for _,claim in ipairs( claims ) do
if setQualifiers then
OrganizeClaims.setQualifiersOrder( claim, qualifiersRedirection, options )
end
if setReferences then
OrganizeClaims.setReferencesOrder( claim, referencesRedirection, options )
end
end
return claims
end
function OrganizeClaims.render( claims, ... )
local flags, ids = OrganizeClaims.splitFlagsAndIds( ... )
end
return OrganizeClaims