Module:Ask
From 20R1
Documentation for this module may be created at Module:Ask/doc
local p = {}
-- Force-load SMW if it isn't available yet (works on all SMW 4.x wikis)
local function getSMW()
local smw = mw.smw
if smw then return smw end
-- Dummy query forces SMW to initialise the Lua library
pcall(function()
mw.getCurrentFrame():preprocess '{{#ask: [[Category:__SMW_DUMMY_NEVER_MATCH__]] |limit=0}}'
end)
return mw.smw
end
function p.query(frame)
local smw = getSMW()
if not smw then
return '<strong class="error">Semantic MediaWiki Lua interface not available on this wiki.</strong>'
end
local args = frame.args
local conditions = args.conditions or args[1] or '[[Category:Example]]'
-- Build the query table
local query = {}
for line in mw.text.gsplit(conditions, '\n', true) do
line = mw.text.trim(line)
if line ~= '' then
table.insert(query, line)
end
end
-- Add default printouts if none given
if not string.find(conditions, '|%?') then
table.insert(query, '?Modification date') -- fallback
end
-- Optional parameters
if args.limit then table.insert(query, 'limit=' .. args.limit) end
if args.sort then table.insert(query, 'sort=' .. args.sort) end
if args.order then table.insert(query, 'order=' .. args.order) end
local results = smw.ask(query)
if not results or #results == 0 then
return "''No results found.''"
end
-- Simple wikitable output
local out = {'{| class="wikitable sortable"\n! Page'}
for _, row in ipairs(results) do
local printouts = {}
for key, values in pairs(row.printouts) do
if not key:find('^_') then -- skip internal properties
table.insert(printouts, tostring(values[1] or ''))
end
end
table.insert(out, '|- \n| [[' .. row.fulltext .. ']] || ' .. table.concat(printouts, ' || '))
end
table.insert(out, '|}\n')
return table.concat(out)
end
return p
