打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

Module:SubpageTable:修订间差异

来自OTTOWiki
琪露若
琪露若留言 | 贡献 (debug)
 
琪露若
琪露若留言 | 贡献 (debug)
第9行: 第9行:
     local limit = tonumber(args.limit) or 100
     local limit = tonumber(args.limit) or 100
     local class = args.class or 'wikitable sortable'
     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'
     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 subpages = {}
     local totalSize = 0
     local allPages = mw.site.allPages({
    local pageCount = 0
         prefix = currentTitle.prefixedText .. '/',
   
         namespace = namespace,
    -- 使用DPL获取子页面统计
         limit = limit
    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>'}, ''))
    -- 构建表格
     local tableHtml =  
        '{| class="' .. class .. '"\n' ..
        '! 页面名称\n' ..
        '! 最后修改\n' ..
        '! 大小\n' ..
        '! 编辑者\n'
      
      
    -- 解析结果并计算统计
     for page in allPages do
     for pageInfo in string.gmatch(rawResult, '([^,]+)') do
         local pageTitle = mw.title.new(page)
         local page, size = string.match(pageInfo, '(.+):(%d+)')
         if pageTitle then
         if page and size then
             local revision = pageTitle.getCurrentRevision and pageTitle:getCurrentRevision()
             table.insert(subpages, {
            local timestamp = revision and revision.timestamp or '未知'
                title = page,
            local user = revision and revision.user or '未知'
                size = tonumber(size)
              
             })
             tableHtml = tableHtml ..
             totalSize = totalSize + tonumber(size)
                '|-\n' ..
            pageCount = pageCount + 1
                '| [[' .. pageTitle.prefixedText .. ']]\n' ..
                '| ' .. timestamp .. '\n' ..
                '| ' .. pageTitle.length .. ' 字节\n' ..
                '| ' .. user .. '\n'
         end
         end
     end
     end
      
      
     -- 生成统计信息
     tableHtml = tableHtml .. '|}'
    local stats = mw.html.create('div')
        :addClass('subpage-stats')
        :css('border', '1px solid #ccc')
        :css('padding', '10px')
        :css('background', '#f9f9f9')
      
      
     stats:tag('strong')
     -- 添加计数信息
         :wikitext('子页面统计:')
    if showCount == 'yes' then
         :done()
        local count = 0
         :tag('br')
         for _ in mw.site.allPages({prefix = currentTitle.prefixedText .. '/', namespace = namespace}) do
   
            count = count + 1
    stats:wikitext('总数量: ' .. pageCount .. ' 个页面')
         end
        :tag('br')
          
        :wikitext('总大小: ' .. totalSize .. ' 字节')
        tableHtml = tableHtml ..
        :tag('br')
            '<div class="subpage-count" style="font-size:90%; color:#666; margin-top:5px;">' ..
        :wikitext('平均大小: ' .. (pageCount > 0 and math.floor(totalSize / pageCount) or 0) .. ' 字节')
            '注:共 ' .. count .. ' 个子页面(显示最近 ' .. limit .. ' 个)' ..
   
            '</div>'
    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
     end
      
      
    -- 显示子页面表格
     return tableHtml
    table.insert(result, p.showSubpages(frame))
   
     return table.concat(result, '\n')
end
end


return p
return p

2025年10月3日 (五) 01:16的版本

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 showCount = args.showcount or 'yes'
    
    -- 获取子页面
    local subpages = {}
    local allPages = mw.site.allPages({
        prefix = currentTitle.prefixedText .. '/',
        namespace = namespace,
        limit = limit
    })
    
    -- 构建表格
    local tableHtml = 
        '{| class="' .. class .. '"\n' ..
        '! 页面名称\n' ..
        '! 最后修改\n' ..
        '! 大小\n' ..
        '! 编辑者\n'
    
    for page in allPages do
        local pageTitle = mw.title.new(page)
        if pageTitle then
            local revision = pageTitle.getCurrentRevision and pageTitle:getCurrentRevision()
            local timestamp = revision and revision.timestamp or '未知'
            local user = revision and revision.user or '未知'
            
            tableHtml = tableHtml ..
                '|-\n' ..
                '| [[' .. pageTitle.prefixedText .. ']]\n' ..
                '| ' .. timestamp .. '\n' ..
                '| ' .. pageTitle.length .. ' 字节\n' ..
                '| ' .. user .. '\n'
        end
    end
    
    tableHtml = tableHtml .. '|}'
    
    -- 添加计数信息
    if showCount == 'yes' then
        local count = 0
        for _ in mw.site.allPages({prefix = currentTitle.prefixedText .. '/', namespace = namespace}) do
            count = count + 1
        end
        
        tableHtml = tableHtml ..
            '<div class="subpage-count" style="font-size:90%; color:#666; margin-top:5px;">' ..
            '注:共 ' .. count .. ' 个子页面(显示最近 ' .. limit .. ' 个)' ..
            '</div>'
    end
    
    return tableHtml
end

return p