Module:Ask: Difference between revisions

From 20R1
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


function p.test( frame )
-- Force-load SMW if it isn't available yet (works on all SMW 4.x wikis)
local smw = require( 'ext.smw.api' )
local function getSMW()
    local smw = mw.smw
local queryString = '[[Club Name::+]]|?Start Date|limit=10'
    if smw then return smw end
local result = smw.getQueryResult( queryString )
 
    -- Dummy query forces SMW to initialise the Lua library
-- Then iterate:
    pcall(function()
local data = result:results()  -- table of pages → printouts
        mw.getCurrentFrame():preprocess '{{#ask: [[Category:__SMW_DUMMY_NEVER_MATCH__]] |limit=0}}'
    end)
 
    return mw.smw
end
end


function p.query( frame )
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 args = frame.args
     local property= args.property or 'Club Name'
     local conditions = args.conditions or args[1] or '[[Category:Example]]'
    local filter= args.filter or '+'
    local condition = args.condition or ''


     local query = '[[' .. property.. '::' .. filter.. ']]'
    -- Build the query table
    if condition ~= '' then table.insert( query, condition )  
     local query = {}
    else table.insert( 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
     end
    --table.insert( query, '?Club Name' )
    --table.insert( query, '?Population' )
    table.insert( query, 'format=table' )
    table.insert( query, 'limit=' .. (args.limit or '50') )


     local results = smw.ask( query )
    -- Add default printouts if none given
     if not results then return 'No results' end
    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


     --local html = {'<table class="wikitable">\n<tr><th>Page</th><th>Name</th><th>Population</th></tr>\n'}
     -- Simple wikitable output
     local html = {'<table class="wikitable">\n<tr><th>Page</th><th>Name</th><th>Population</th></tr>\n'}
     local out = {'{| class="wikitable sortable"\n! Page'}
     for _, row in ipairs( results ) do
     for _, row in ipairs(results) do
         table.insert( html, string.format(
         local printouts = {}
             '<tr><td>[[:%s]]</td><td>%s</td><td>%s</td></tr>\n',
        for key, values in pairs(row.printouts) do
            row.fulltext
             if not key:find('^_') then  -- skip internal properties
            --row.printouts.Name and row.printouts.Name[1] or '',
                table.insert(printouts, tostring(values[1] or ''))
             --row.printouts.Population and row.printouts.Population[1] or ''
             end
        ) )
        end
        table.insert(out, '|- \n| [[' .. row.fulltext .. ']] || ' .. table.concat(printouts, ' || '))
     end
     end
     table.insert( html, '</table>' )
     table.insert(out, '|}\n')
     return table.concat( html )
     return table.concat(out)
end
end


return p
return p

Latest revision as of 02:42, 20 November 2025

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