Module:DPL

From nUSA Wiki Archive
Revision as of 06:12, 27 January 2023 by Nusawiki (talk | contribs) (1 revision imported: Pauljkl)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:DPL/doc

-- From [[w:c:dev:Module:Sandbox/Dorumin/DPL]]

local p = {}
local arguments = require('Module:Arguments')

-- ExtDynamicPageList::$maxResultCount
p.COUNT = 500

-- Generates dpl parser function from a key-value table
function p.tag(tag, tbl)
    local buffer = {'{{#' .. tag .. '\n'}

    for key, val in pairs(tbl) do
        if type(val) == 'table' then
            for _, value in ipairs(val) do
                table.insert(buffer, '| ' .. key .. ' = ' .. value .. '\n')
            end
        else
            table.insert(buffer, '| ' .. key .. ' = ' .. val .. '\n')
        end
    end

    table.insert(buffer, '}}')

    return table.concat(buffer, '')
end

-- Gets a forum parser function and returns its wikitext
function p.forum(tbl)
    local frame = mw.getCurrentFrame()
    return frame:preprocess(p.tag('tag: forum', tbl))
end

-- Gets the DPL parser function and returns its wikitext after parsing
function p.parse(tbl)
    local frame = mw.getCurrentFrame()
    return frame:preprocess(p.tag('dpl:', tbl))
end

-- Returns all DPL results concatenated
-- The count parameter doesn't take effect
function p.recursive(tbl)
    tbl['noresultsheader'] = tbl['noresultsheader'] or '<no-results>'
    tbl['count'] = p.COUNT
    local offset = tbl['offset'] or 0
    local buffer = {}
    local parsed = p.parse(tbl)
    if parsed == tbl['noresultsheader'] then
        if tbl['noresultsheader'] == '<no-results>' then
            return ''
        else
            return tbl['noresultsheader']
        end
    end
    while parsed ~= tbl['noresultsheader'] do
        table.insert(buffer, parsed)
        offset = offset + p.COUNT
        tbl['offset'] = offset
        parsed = p.parse(tbl)
    end
    return table.concat(buffer)
end

-- Returns an array (really just a table) of all pages that match a query
-- The format parameter doesn't take effect
function p.list(tbl)
    tbl['format'] = ',,%PAGE%#,'
    local content = p.recursive(tbl)
    local results = {}
    for page in content:gmatch("[^#]+") do
        table.insert(results, page)
    end
    return results
end

function p.getArgs(frame)
    local args = arguments.getArgs(frame)
    local ret = {}

    for k, v in pairs(args) do
        ret[k] = {}

        for w in v:gmatch("([^¦]+)") do
            table.insert(ret[k], w)
        end
    end
    
    return ret
end

-- For usage in #invoke
function p.main(frame)
    return p.parse(p.getArgs(frame))
end

-- For usage in #invoke
function p.all(frame)
    return p.recursive(p.getArgs(frame))
end

return p