模組:沙盒/Carrotkit/人氣維基人列表

local f = {}
local util = require('Module:沙盒/逆襲的天邪鬼/talkpage')

local function date2num(date)
    local y,m,d,h,min = string.gmatch(date, util.DATEPATTERN)()
    if y and m and d and h and min then
        return y*100000000+m*1000000+d*10000+h*100+min
    else
        return 0
    end
end

local function comparedate(date1, date2)
    return date2num(date1) < date2num(date2)
end

local function split(text, sep)
    local sep, fields = sep or ",", {}
    local pattern = string.format("([^%s]+)", sep)
    string.gsub(text, pattern, function(c) fields[#fields+1] = c end)
    return fields
end

local function getlastsign(r)
    local lastname = nil
    local lastdate = ''

    for k, v in pairs(r.users) do
        if comparedate(lastdate, v.lastdate) then
            lastname = k
            lastdate = v.lastdate
        end
    end

    return {name=lastname, date=lastdate}
end

local function getsigncount(data)
    local num = 0
    for k, v in pairs(data.topics) do
        num = num + v.count
    end
    return num
end

function f.signs(frame)
    local result = {'{| class="wikitable sortable"', '|-', '! 用戶名 !! 簽名冊連結 !! 簽名數量 !! data-sort-type="number" | 更新時間'}
    local users = {}

    -- 獲取信息
    for i, info in ipairs(frame.args) do
        local user, offset = {}, 0
        local t = split(info, ',')
        user.name = mw.text.trim(t[1])
        user.signlink = mw.text.trim(t[2] or user.name)
        offset = tonumber(t[3] or 0)
        
        local data = util.getcount{text=mw.title.new(user.signlink):getContent() or ''}
        user.signcount = offset + getsigncount(data)
        user.lastdate = getlastsign(data).date
        
        users[#users+1] = user
    end

    table.sort(users, function(a, b)
    	if a.signcount == b.signcount then
    		return comparedate(b.lastdate, a.lastdate)
    	else
    		return a.signcount > b.signcount 
    	end
	end)

    for i, user in ipairs(users) do
        table.insert(result, '|-')
        table.insert(result, string.format('| [[User:%s|%s]] || [[%s]] || %s || data-sort-value="%s" | %s', user.name, user.name, user.signlink, user.signcount, date2num(user.lastdate), user.lastdate))
    end

    table.insert(result, '|}')
    return table.concat(result, '\n')
end

local function maketplink(tp)
	local links = split(tp, ';')
	local r = {}
	for i, l in ipairs(links) do
		local l2 = mw.text.trim(l)
		local t = string.match(l2, '^Template:(.*)') or string.match(l2, '^模板:(.*)') or l2
		table.insert(r, string.format('{{[[%s|%s]]}}', l2, t))
	end
	return table.concat(r, '<br>')
end

function f.worship(frame)
	local result = {'{| class="wikitable sortable"', '|-', '! 用戶名 !! 崇拜用戶框連結 !! 崇拜者數量'}
    local users = {}

    -- 獲取信息
    for i, info in ipairs(frame.args) do
        local user, offset = {}, 0
        local t = split(info, ',')
        user.name = mw.text.trim(t[1])
        user.tplink = mw.text.trim(t[2] or user.name)
        user.category = mw.text.trim(t[3] or user.name)
        offset = tonumber(t[4] or 0)
        
        user.count = offset + mw.site.stats.pagesInCategory(user.category)
        
        users[#users+1] = user
    end

    table.sort(users, function(a, b) return a.count > b.count end)
    for i, user in ipairs(users) do
        table.insert(result, '|-')
        table.insert(result, string.format('| [[User:%s|%s]] || %s || %s', user.name, user.name, maketplink(user.tplink), user.count))
    end

    table.insert(result, '|}')
    return table.concat(result, '\n')
end

return f