Module:SubpageTable
来自OTTOWiki
更多操作
local p = {}
function p.showSubpages(frame)
local args = frame.args
local currentTitle = mw.title.getCurrentTitle()
-- 参数处理
local namespace = args.namespace or currentTitle.namespace
local limit = tonumber(args.limit) or 100
local class = args.class or 'wikitable sortable'
local order = args.order or 'title'
local sort = args.sort or 'ascending'
local showCount = args.showcount or 'yes'
-- 构建DPL查询
local dplQuery = {
'namespace = ' .. namespace,
'titlematch = ' .. currentTitle.prefixedText .. '/%',
'mode = userformat',
'listseparators = {| class="' .. class .. '"\\n! 页面名称\\n! 最后修改\\n! 大小\\n! 编辑者\\n|-,\\n| [[%PAGE%]]\\n| %DATE%\\n| %SIZE% 字节\\n| %USER%,\\n|-,\\n|}',
'ordermethod = ' .. order,
'order = ' .. sort,
'count = ' .. limit,
'distinct = true',
'suppresserrors = true'
}
-- 执行DPL
local dplResult = frame:preprocess(table.concat({'<dpl>', table.concat(dplQuery, '\n|'), '</dpl>'}, ''))
-- 添加计数信息
if showCount == 'yes' then
local countInfo = mw.html.create('div')
:addClass('subpage-count')
:css('font-size', '90%')
:css('color', '#666')
:css('margin-top', '5px')
:wikitext('注:显示最近 ' .. limit .. ' 个子页面')
dplResult = dplResult .. tostring(countInfo)
end
return dplResult
end
function p.getSubpageStats(frame)
local currentTitle = mw.title.getCurrentTitle()
local subpages = {}
local totalSize = 0
local pageCount = 0
-- 使用DPL获取子页面统计
local dplQuery = {
'namespace = ' .. (frame.args.namespace or currentTitle.namespace),
'titlematch = ' .. currentTitle.prefixedText .. '/%',
'mode = userformat',
'listseparators = ,%PAGE%:%SIZE%,',
'count = 500',
'suppresserrors = true'
}
local rawResult = frame:preprocess(table.concat({'<dpl>', table.concat(dplQuery, '\n|'), '</dpl>'}, ''))
-- 解析结果并计算统计
for pageInfo in string.gmatch(rawResult, '([^,]+)') do
local page, size = string.match(pageInfo, '(.+):(%d+)')
if page and size then
table.insert(subpages, {
title = page,
size = tonumber(size)
})
totalSize = totalSize + tonumber(size)
pageCount = pageCount + 1
end
end
-- 生成统计信息 - 修复了br标签问题
local stats = mw.html.create('div')
:addClass('subpage-stats')
:css('border', '1px solid #ccc')
:css('padding', '10px')
:css('background', '#f9f9f9')
-- 使用段落而不是br标签
stats:tag('p')
:css('margin', '0 0 5px 0')
:tag('strong')
:wikitext('子页面统计:')
:done()
:done()
stats:tag('p')
:css('margin', '0 0 3px 0')
:wikitext('总数量: ' .. pageCount .. ' 个页面')
:done()
stats:tag('p')
:css('margin', '0 0 3px 0')
:wikitext('总大小: ' .. totalSize .. ' 字节')
:done()
stats:tag('p')
:css('margin', '0')
:wikitext('平均大小: ' .. (pageCount > 0 and math.floor(totalSize / pageCount) or 0) .. ' 字节')
:done()
return tostring(stats)
end
function p.smartSubpageTable(frame)
local args = frame.args
local result = {}
-- 显示统计信息
if args.stats ~= 'no' then
table.insert(result, p.getSubpageStats(frame))
end
-- 显示子页面表格
table.insert(result, p.showSubpages(frame))
return table.concat(result, '\n')
end
return p