模块:Test:修订间差异
外观
无编辑摘要 |
无编辑摘要 |
||
(未显示同一用户的3个中间版本) | |||
第3行: | 第3行: | ||
function p.getRandomPages(frame) | function p.getRandomPages(frame) | ||
local args = frame.args | local args = frame.args | ||
local category = args['category'] or '分类名称' -- | -- 从模板参数获取分类名称和要获取的数量,若无参数则使用默认值 | ||
local count = tonumber(args['count']) or | 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 "'' | 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 | end | ||
if type(dplResult) ~= 'table' or #dplResult == 0 then | |||
return "''分类“" .. category .. "”下未找到页面或分类不存在。'" | |||
end | end | ||
-- | -- 4. 随机化处理页面列表 | ||
math.randomseed(os.time()) -- | -- 设置随机种子,以增加随机性。注意:在缓存期内,由于页面输出被缓存,结果仍固定。 | ||
for i = # | math.randomseed(os.time()) | ||
-- 使用 Fisher-Yates 洗牌算法对获取到的页面数组进行随机排序 | |||
for i = #dplResult, 2, -1 do | |||
local j = math.random(i) | local j = math.random(i) | ||
dplResult[i], dplResult[j] = dplResult[j], dplResult[i] | |||
end | end | ||
-- | -- 5. 截取前 count 个结果 | ||
local | local selectedPages = {} | ||
for i = 1, math.min(count, # | for i = 1, math.min(count, #dplResult) do | ||
table.insert( | table.insert(selectedPages, dplResult[i].title) -- 获取页面标题 | ||
-- 如果需要更多信息,例如页面URL或命名空间,可以使用 dplResult[i].fulltext 等 | |||
end | end | ||
-- | -- 6. 格式化输出为Wiki文本列表(这里以无序列表为例) | ||
local | local outputList = {} | ||
for _, title in ipairs( | for _, title in ipairs(selectedPages) do | ||
table.insert(outputList, '* [[' .. title .. ']]') -- 创建Wiki链接 | |||
table.insert( | |||
end | end | ||
return table.concat( | 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