跳转到内容
主菜单
主菜单
移至侧栏
隐藏
导航
首页
最近更改
随机页面
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 5 -- 默认显示5条,最好从参数传入 -- 使用DPL调用获取分类下的页面列表 -- 注意:DPL的调用方式可能因版本和配置而异,以下是一种常见方式 local dpl_output = mw.ext.dpl.dump({ category = category, notcategory = args['notcategory'], -- 可选:排除的分类 namespace = args['namespace'] or '0', -- 可选:名字空间,默认为主名字空间(0) ordermethod = 'title', -- 初始排序方式,后续会随机化 mode = 'unordered', ignoreerrors = true, -- 忽略错误 -- 为了性能,初次获取可以限制一个较大的数量,然后从中随机选择 count = 50 -- 假设我们从分类中最多取50页再进行随机选择 }) -- 检查DPL是否返回了结果 if type(dpl_output) ~= 'table' or #dpl_output == 0 then return "''该分类下暂无页面或获取页面时出错。''" end -- 将DPL输出中的页面标题提取到一个新表中 local pages = {} for _, page in ipairs(dpl_output) do table.insert(pages, page.title) end -- 随机化页面列表 math.randomseed(os.time()) -- 设置随机种子,增加随机性 for i = #pages, 2, -1 do local j = math.random(i) pages[i], pages[j] = pages[j], pages[i] -- 交换元素(Fisher-Yates洗牌算法) end -- 只取前`count`个结果 local selected_pages = {} for i = 1, math.min(count, #pages) do table.insert(selected_pages, pages[i]) end -- 格式化输出为Wiki文本列表 local output = {} for _, title in ipairs(selected_pages) do -- 创建页面链接 table.insert(output, '* [[' .. title .. ']]') end return table.concat(output, '\n') end return p
该页面使用的模板:
模块:Test/doc
(
查看源代码
)
返回
模块:Test
。
搜索
搜索
查看“︁模块:Test”︁的源代码
添加话题