Modul:Autocategories
Utseende
Moduldokumentasjon
[opprett]
-- module for replicating categories listed at Wikidata
-- © John Erling Blad, Creative Commons by Attribution 3.0
-- don't pollute with globals
require('Module:No globals')
-- @var our exposed table
local Autocategories = {}
-- find the first entry that seems to be a Q-id
-- @param table args argument list from the frame
-- @return string first matching argument
function Autocategories._findQid( args )
for key, value in ipairs( args ) do
if value ~= '' and string.match(value, '^Q%d+$') then
return value
end
end
local entity = mw.wikibase.getEntity()
if entity then
return entity['id']
end
return nil
end
-- value for all statements
-- @param table statement well-defined structure for a statement
-- @return values for the provided statements
function Autocategories._findValue(statements)
local values = {}
for _,statement in pairs(statements) do
local mainsnak = statement["mainsnak"]
if mainsnak then
local datavalue = mainsnak['datavalue']
if datavalue then
local value = datavalue['value']
if value then
values[1+#values] = value
end
end
end
end
return values
end
-- find the P-ids from the argument lists
-- note that this maintain argument sequence
-- @param table vector forwarded from the method call
-- @return table hash of matching arguments
function Autocategories._findPids( pids, args )
for _,value in pairs( args ) do
local _,_,action,pid = string.find(value, '^%s*(!?)(P%d+)%s*$')
if pid then
pids[pid] = action == '' and true or nil
end
end
return pids
end
-- load a list of statements and format the entries according to build directive
-- @param string id some entity to load
-- @param string properety id for the property to use
-- @return table
function Autocategories._loadStatements( id, prop )
local entity = id and mw.wikibase.getEntity( id ) or mw.wikibase.getEntity()
statements = entity:getBestStatements( prop )
if not statements then
return {}
end
return statements
end
-- find the P-ids from the frame lists
-- note that this does not maintain argument sequence
-- @param table frames forwarded from the method call
-- @return table hash of matching arguments
function Autocategories.findPids( ... )
local pids = {}
for i = 1,arg.n do
local frame = arg[i]
pids = Autocategories._findPids( pids, frame.args )
end
local keys = {}
for key,_ in pairs( pids ) do
keys[1+#keys] = key
end
return keys
end
-- do all allowable categorizations
-- @param table frame arguments from invoke
-- @return string status report during development, should be an empty string
function Autocategories.run( frame )
--local qid = Autocategories._findQid( frame.args )
--first call is when the call is encapsulated in a template
local pids = frame:getParent() and Autocategories.findPids( frame, frame:getParent() ) or Autocategories.findPids( frame )
if #pids == 0 then
return ''
end
local entity = mw.wikibase.getEntity()
if not entity then
return ''
end
for _,pid in pairs(pids) do
-- local statements = Autocategories._loadStatements( qid, pid )
local statements = entity:getBestStatements( pid )
if not statements then
return ''
end
return mw.dumpObject( Autocategories._findValue( statements ) )
end
--return table.concat( pids, ', ' )
return mw.dumpObject(pids)
end
-- export the accesspoint
return Autocategories