跳转到内容
主菜单
主菜单
移至侧栏
隐藏
导航
首页
最近更改
随机页面
MediaWiki帮助
特殊页面
LabAnimalWiki
搜索
搜索
外观
创建账号
登录
个人工具
创建账号
登录
查看“︁模块:Test”︁的源代码
模块
讨论
English
阅读
查看源代码
查看历史
工具
工具
移至侧栏
隐藏
操作
阅读
查看源代码
查看历史
清除缓存
常规
链入页面
相关更改
页面信息
Cargo数据
获取短链接
外观
移至侧栏
隐藏
←
模块:Test
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于这些用户组的用户执行:
管理员
、contenteditor
您可以查看和复制此页面的源代码。
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
该页面使用的模板:
模块:Test/doc
(
查看源代码
)
返回
模块:Test
。
搜索
搜索
查看“︁模块:Test”︁的源代码
添加话题