跳转到内容

模块:Test:修订间差异

来自LabAnimalWiki
Czhen留言 | 贡献
无编辑摘要
Czhen留言 | 贡献
无编辑摘要
 
第1行: 第1行:
local p = {}
local p = {} -- 初始化模块对象


function p.listExtensions(frame)
function p.getRandomPages(frame)
     local result = {"当前 MediaWiki 实例的 mw.ext 对象中包含以下内容:\n"}
     local args = frame.args
      
    -- 从模板参数获取分类名称和要获取的数量,若无参数则使用默认值
     -- 检查 mw.ext 本身是否存在
    local category = args['category'] or '分类名称' -- 请务必修改“分类名称”或通过参数传入
     if type(mw.ext) ~= 'table' then
     local count = tonumber(args['count']) or 8 -- 默认获取8条
         return "mw.ext 不是一个表(table),或者不可用。"
 
     -- 1. 检查 mw.ext.DPL 是否可用
     if type(mw.ext) ~= 'table' or type(mw.ext.DPL) ~= 'table' or type(mw.ext.DPL.dump) ~= 'function' then
         return "''错误:DPL 扩展未安装、未启用 Lua 支持或版本不兼容。'"
        -- 作为降级方案,可以在此尝试返回普通的DPL3调用wikitext,但无法在Lua内随机化
        -- return string.format('<dpl>\ncategory = %s\ncount = %d\nordermethod = title\nmode = unordered\n</dpl>', category, count)
    end
 
    -- 2. 使用DPL3获取分类下的页面列表
    local success, dplResult = pcall(function()
        return mw.ext.DPL.dump({
            category = category,
            namespace = '0', -- 通常为主名字空间,可根据需要调整或通过参数指定
            -- 为了性能,初次获取可以限制一个较大的数量,比如100,然后从中随机选择。如果分类页面很少,可以调小。
            count = 100,
            ordermethod = 'title', -- 初始排序方式,后续会随机化
            mode = 'none', -- 不直接输出,仅获取数据
            ignoreerrors = true,
        })
    end)
 
    -- 3. 处理DPL3调用可能出现的错误
    if not success then
        return "''DPL 查询执行过程中出错:' .. dplResult"
    end
    if type(dplResult) ~= 'table' or #dplResult == 0 then
        return "''分类“" .. category .. "”下未找到页面或分类不存在。'"
     end
     end
      
 
     -- 遍历 mw.ext 中的所有键值对
     -- 4. 随机化处理页面列表
     for key, value in pairs(mw.ext) do
     -- 设置随机种子,以增加随机性。注意:在缓存期内,由于页面输出被缓存,结果仍固定。
        -- 粗略判断类型
     math.randomseed(os.time())
         local valueType = type(value)
    -- 使用 Fisher-Yates 洗牌算法对获取到的页面数组进行随机排序
         table.insert(result, "* '''" .. key .. "''' (类型: " .. valueType .. ")\n")
    for i = #dplResult, 2, -1 do
         local j = math.random(i)
         dplResult[i], dplResult[j] = dplResult[j], dplResult[i]
     end
     end
      
 
     if #result == 1 then
     -- 5. 截取前 count 个结果
         table.insert(result, "未发现任何注册的扩展对象。")
     local selectedPages = {}
    for i = 1, math.min(count, #dplResult) do
         table.insert(selectedPages, dplResult[i].title) -- 获取页面标题
        -- 如果需要更多信息,例如页面URL或命名空间,可以使用 dplResult[i].fulltext 等
     end
     end
      
 
     return table.concat(result)
    -- 6. 格式化输出为Wiki文本列表(这里以无序列表为例)
    local outputList = {}
    for _, title in ipairs(selectedPages) do
        table.insert(outputList, '* [[' .. title .. ']]') -- 创建Wiki链接
     end
 
     return table.concat(outputList, '\n') -- 用换行符连接列表项
end
end


return p
return p

2025年9月8日 (一) 10:55的最新版本

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

local p = {} -- 初始化模块对象

function p.getRandomPages(frame)
    local args = frame.args
    -- 从模板参数获取分类名称和要获取的数量,若无参数则使用默认值
    local category = args['category'] or '分类名称' -- 请务必修改“分类名称”或通过参数传入
    local count = tonumber(args['count']) or 8 -- 默认获取8条

    -- 1. 检查 mw.ext.DPL 是否可用
    if type(mw.ext) ~= 'table' or type(mw.ext.DPL) ~= 'table' or type(mw.ext.DPL.dump) ~= 'function' then
        return "''错误:DPL 扩展未安装、未启用 Lua 支持或版本不兼容。'"
        -- 作为降级方案,可以在此尝试返回普通的DPL3调用wikitext,但无法在Lua内随机化
        -- return string.format('<dpl>\ncategory = %s\ncount = %d\nordermethod = title\nmode = unordered\n</dpl>', category, count)
    end

    -- 2. 使用DPL3获取分类下的页面列表
    local success, dplResult = pcall(function()
        return mw.ext.DPL.dump({
            category = category,
            namespace = '0', -- 通常为主名字空间,可根据需要调整或通过参数指定
            -- 为了性能,初次获取可以限制一个较大的数量,比如100,然后从中随机选择。如果分类页面很少,可以调小。
            count = 100,
            ordermethod = 'title', -- 初始排序方式,后续会随机化
            mode = 'none', -- 不直接输出,仅获取数据
            ignoreerrors = true,
        })
    end)

    -- 3. 处理DPL3调用可能出现的错误
    if not success then
        return "''DPL 查询执行过程中出错:' .. dplResult"
    end
    if type(dplResult) ~= 'table' or #dplResult == 0 then
        return "''分类“" .. category .. "”下未找到页面或分类不存在。'"
    end

    -- 4. 随机化处理页面列表
    -- 设置随机种子,以增加随机性。注意:在缓存期内,由于页面输出被缓存,结果仍固定。
    math.randomseed(os.time())
    -- 使用 Fisher-Yates 洗牌算法对获取到的页面数组进行随机排序
    for i = #dplResult, 2, -1 do
        local j = math.random(i)
        dplResult[i], dplResult[j] = dplResult[j], dplResult[i]
    end

    -- 5. 截取前 count 个结果
    local selectedPages = {}
    for i = 1, math.min(count, #dplResult) do
        table.insert(selectedPages, dplResult[i].title) -- 获取页面标题
        -- 如果需要更多信息,例如页面URL或命名空间,可以使用 dplResult[i].fulltext 等
    end

    -- 6. 格式化输出为Wiki文本列表(这里以无序列表为例)
    local outputList = {}
    for _, title in ipairs(selectedPages) do
        table.insert(outputList, '* [[' .. title .. ']]') -- 创建Wiki链接
    end

    return table.concat(outputList, '\n') -- 用换行符连接列表项
end

return p