来自勇者斗恶龙X WIKI_DQX日服PC中文版图鉴 DQX攻略大全

此模块的文档可以在模块:Util/doc创建

-----------------------------------------------------------------------------
-- 通用功能模块
-----------------------------------------------------------------------------

local util = {}

-----------------------------------------------------------------------------
-- 通用功能
-----------------------------------------------------------------------------

function util.ref(ref_text, frame)
    return frame:callParserFunction('#tag', 'ref', ref_text)
end

-----------------------------------------------------------------------------
-- util.smw
-- SMW相关功能
-- 主要复制自 http://pathofexile.gamepedia.com/index.php?title=Module:Util
-- 根据需要和unicode问题有所调整
-----------------------------------------------------------------------------

util.smw = {}

function util.smw._parser_function(frame, parser_function, args)
    -- Executes a semantic parser functions and sets the arguments args
    --
    -- This function is a helper for handling tables since +sep= parameter
    -- appears to be broken.
    --
    -- frame          : frame object
    -- parser_function: the whole parser function string
    -- args           : table of arguments    
    for k, v in pairs(args) do
        if type(v) == 'table' then
            for _, value in ipairs(v) do
                frame:callParserFunction(parser_function, {[k] = value})
            end
            args[k] = nil
        elseif type(v) == 'boolean' then
            args[k] = tostring(v)
        end
    end
    frame:callParserFunction(parser_function, args)
end

function util.smw.set(args, frame)
    util.smw._parser_function(frame, '#set:', args)
end

function util.smw.subobject(frame, id, args)
    util.smw._parser_function(frame, '#subobject:' .. id, args)
end

function util.smw.count(param, frame)
    query = {param, format='count'}
    return tonumber(frame:callParserFunction('#ask', query))
end

function util.smw.query(query, frame)
    -- Executes a semantic media wiki #ask query and returns the result as an
    -- array containing each row as table.
    --
    -- query: table of query arguments to pass
    -- frame: current frame object
    
    -- the characters here for sep/header/propsep are control characters; I'm farily certain they should not appear in regular text.
    query.sep = '�'
    query.propsep = '<PROP>'
    query.headersep = '<HEAD>'
    query.format = 'array'
    query.headers = 'plain'
    
    local result = frame:callParserFunction('#ask', query)
    
    -- "<span class=\"smw-highlighter\" data-type=\"4\" data-state=\"inline\" data-title=\"Error\"><span class=\"smwtticon warning\"></span><div class=\"smwttcontent\">Some subquery has no valid condition.</div></span>"
    if mw.ustring.find(result, 'data%-title="Error"') ~= nil then
        error(mw.ustring.sub(result, mw.ustring.find(result, '<span class="smw-highlighter"', 1, true), -1))
    end

    local out = {}
    
    for row_string in mw.ustring.gmatch(result, '[^�]+') do
        local row = {}
        for _, str in ipairs(mw.text.split(row_string, query.propsep)) do
            local kv = mw.text.split(str, query.headersep)
            if #kv == 1 then
                row[#row+1] = kv[1] 
            elseif #kv == 2 then
                row[kv[1]] = kv[2]
            end
        end
        out[#out+1] = row
    end
    
    return out
end

return util