米斯特利亚Wiki正在建设中,本WIKI编辑权限开放!欢迎参与~!

全站通知:

模块:ProcessDataPage

来自米斯特利亚WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

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

local p={}


---
-- @description 获取指定查询条件下,查询所有页面所需的#ask查询次数
-- @param condition string 查询条件
-- @return number 查询次数
---
local function getAskTimes(condition)
    local frame = mw.getCurrentFrame();
    local itemCount = frame:callParserFunction{
        name="#ask:" .. condition,
        args={
            format="count"
        }
    }
    return math.ceil(itemCount / 1000);
end

-- 获取Data页面下的指定属性Json
function p.getPageCategoryJson(frame)
    local pageName = frame.args[1]
    local propertyName = frame.args[2]
    local jsonStr =  frame:callParserFunction{
        name="#show:" .. pageName,
        args={
            "?"..propertyName
        }
    }
    return mw.text.jsonDecode(jsonStr);
end

---
-- @description 根据输入的分类字符串,生成一个json,key为页面名,value为该页面所属分类
-- @param frame table
-- @return string json字符串
---
function p.setPageCategoryJson(frame)
	local categoryStr = frame.args[1]
	local categoryList = {}
	for category in string.gmatch(categoryStr, "[^,]+") do
		table.insert(categoryList, category)
	end
    -- smw ask 每个分类
    local categoryJson = {};
    for _, category in ipairs(categoryList) do
        local condition = "[[分类:" .. category .. "]]";
        local askTimes = getAskTimes(condition);
        for i = 1, askTimes do
            local offset = (i - 1) * 1000;
            -- 调用ask获取1000条数据,返回页面名list
            local itemList = frame:callParserFunction{
                name="#ask:" .. condition,
                args={
                    link="none",
                    limit=1000,
                    offset=tostring(offset),
                    sep=","
                }
            }
            -- 遍历页面名list,每个页面名为key,分类名为val
            for item in string.gmatch(itemList, "[^,]+") do
                categoryJson[item] = category;
            end


        end
    end
    -- 转换为json字符串
    local jsonStr = mw.text.jsonEncode(categoryJson);
    return jsonStr;
end

return p