Module:Topic list

来自Arcaea中文维基
Star0讨论 | 贡献2021年4月4日 (日) 15:51的版本

可在Module:Topic list/doc创建此模块的帮助文档

local p = {}

function split(input, delimiter)
    input = tostring(input)
    delimiter = tostring(delimiter)
    if (delimiter == "") then return false end
    local pos, arr = 0, {}
    for st, sp in function() return string.find(input, delimiter, pos, true) end do
        table.insert(arr, string.sub(input, pos, st - 1))
        pos = sp + 1
    end
    table.insert(arr, string.sub(input, pos))
    return arr
end

function trim(str)
   return (string.gsub(str, "^%s*(.-)%s*$", "%1"))
end

function LuaReomve(str,remove)
    local lcSubStrTab = {}
    while true do
        local lcPos = string.find(str,remove)
        if not lcPos then
            lcSubStrTab[#lcSubStrTab+1] =  str    
            break
        end
        local lcSubStr  = string.sub(str,1,lcPos-1)
        lcSubStrTab[#lcSubStrTab+1] = lcSubStr
        str = string.sub(str,lcPos+1,#str)
    end
    local lcMergeStr =""
    local lci = 1
    while true do
        if lcSubStrTab[lci] then
            lcMergeStr = lcMergeStr .. lcSubStrTab[lci] 
            lci = lci + 1
        else 
            break
        end
    end
    return lcMergeStr
end

function table.unique(t, bArray)
    local check = {}
    local n = {}
    local idx = 1
    for k, v in pairs(t) do
        if not check[v] then
            if bArray then
                n[idx] = v
                idx = idx + 1
            else
                n[k] = v
            end
            check[v] = true
        end
    end
    return n
end

function get_len(tb)
  local len = 0
  for k,v in pairs(tb) do
    len= len+1
  end
return len
end

function time_style(time)
	local _, _, y, m, d, _hour, _min, _sec = string.find(time, "(%d+)年(%d+)月(%d+)日%s%(.-%)%s(%d+):(%d+)");
	local n_short_time = os.date(os.time({year=y, month = m, day = d, hour = _hour, min = _min}))
	local n_long_time =os.date(os.time())

	local t_time = time_diff(n_long_time,n_short_time);
	local time_txt = string.format("%02d", t_time.month).."月"..string.format("%02d", t_time.day).."日   "..string.format("%02d", t_time.hour)..
						":"..string.format("%02d", t_time.min)..":"..string.format("%02d", t_time.sec);


	if string.format("%02d", t_time.month)~="00" then return [[background-color: #bbb;]] end
	if tonumber(string.format("%02d", t_time.day))>=7 then return [[background-color: #ddd;]] end
	if tonumber(string.format("%02d", t_time.day))>=1 then return [[]] end
	return [[background-color: #efe;]]
end

function time_diff(long_time,short_time)
	local n_short_time,n_long_time,carry,diff = os.date('*t',short_time),os.date('*t',long_time),false,{}
	local colMax = {60,60,24,os.date('*t',os.time{year=n_short_time.year,month=n_short_time.month+1,day=0}).day,12,0}
	n_long_time.hour = n_long_time.hour - (n_long_time.isdst and 1 or 0) + (n_short_time.isdst and 1 or 0) -- handle dst
	for i,v in ipairs({'sec','min','hour','day','month','year'}) do
		diff[v] = n_long_time[v] - n_short_time[v] + (carry and -1 or 0)
		carry = diff[v] < 0
		if carry then
			diff[v] = diff[v] + colMax[i]
		end
	end
	return diff
end

function close(text,timestyle)
	--mcwiki
	local yes=string.match(text, "closed%-topic%-yes")
	local no=string.match(text, "closed%-topic%-no")
	--arcaeawiki
	local closed=string.match(text, "以下讨论已结束")
	if yes then return [[background-color: #6495ED;]] end
	if no then return [[background-color: #fee;]] end
	if closed then return [[background-color: #6495ED;]] end
	return timestyle
end

function cov(talk)
	--关键词替换
	--如有字符导致生成错误,请在这里添加转换
	talk=string.gsub(talk, "===(.-)===", "%1")
	talk=string.gsub(talk, "用户", "User")
	talk=string.gsub(talk, "user", "User")
	return talk
end

function link(text)
	--此处添加导致链接工作不正常的文本
	text=trim(trim(text))
	text=string.gsub(text, " (.*)", "%1")
	text=LuaReomve(text,"%[")
	text=LuaReomve(text,"%]")
	return text
end

function user_link(text)
	--输入用户名以及带有ip:前缀的ip用户。
	local ipuser=string.match(text,"ip:(.*)")
	if ipuser then
		return "[[Special:用户贡献/"..ipuser.."|"..ipuser.."]]"
	else
		return "[[User:"..text.."|"..text.."]]"
	end
end

function talk_list(pagename)
	--输入页面名,返回一个包含文本的讨论信息表
	--例如:talklist[1][text]
	local talk=cov( mw.getCurrentFrame():expandTemplate{ title = pagename } ).."=="
	local match=string.gmatch(talk,"==\n(.-)==")
	local talk=""
	local talklist={}
	local i=1
	while true
	do
		talk=match()
		if talk==nil then break end
		talklist[i]=talk
		i=i+1
	end
	
	return talklist

end

function title_list(pagename)
	local talk=cov(mw.getCurrentFrame():expandTemplate{ title = pagename })
	local match=string.gmatch(talk,"==(.-)==")
	local title=""
	local titlelist={}
	local i=1
	while true
	do
		title=match()
		if title==nil then break end
		titlelist[i]=title
		i=i+1
	end
	
	return titlelist
end

function talk_time(talk)
	local result=string.match(talk,"[%s%S]*(%d%d%d%d.*) %(CST%)")
	return result
end

function user_manage(text)
	--输入讨论的文本,输出和User有关的转换部分table
	--1-4分别为发言条数,发言人数,发起人,最后发言的人。
	text=string.gsub(text, "Special:User贡献/", "User:ip:")
	--别问为什么是User贡献,问就是神奇代码
	local match=string.gmatch (text,"User:([^\n]-)%|[^\n]-CST")
	local userlist={}
	local user,firstuser,lastuser="","",""
	local i=1
	while true
	do
		user=match()
		if user==nil then break end
		lastuser=user
		if i==1 then firstuser=user end 
		userlist[i]=user
		i=i+1
	end
	local user_len=get_len(userlist)
	local user_len_ind=get_len(table.unique(userlist))
	local result={user_len,user_len_ind,firstuser,lastuser}

	return result
end

function get_table(talktitle,talktext)
	local result=[[<table class="wikitable sortable mw-collapsible" style="text-align:center"><tr><th>#</td><th>话题</td><th>发言</td>
					<th>参与</td><th>发起者</td><th>最近发言</td><th>最后发言时间(CST)</td></tr>]]
	local tableend=[[</table>]]
	local part=""

	local userinf,time
	local userstyle,userstyleind,timestyle
	for i=1,get_len(talktext) 
	do
		time,userinf=talk_time(talktext[i]),user_manage(talktext[i])
		--对表格添加样式
		if userinf[1]==1 then userstyle=[[background-color: #fcc;"]] else userstyle=[[]] end
		if userinf[2]==1 then userstyleind=[[background-color: #fcc;"]] else userstyleind=[[]] end
		if userinf[1]>=7 then userstyle=[[background-color: #ffe;"]] end
		if userinf[2]>=7 then userstyleind=[[background-color: #ffe;"]] end
		timestyle=close(talktext[i],time_style(time))
		--组装表格一个论题的部分
		part=[[<tr><th>]]..i..[[</td><td>]]..link(talktitle[i])..[[</td><td style="]]..userstyle..[[">]]..userinf[1]..[[</td><td style="]]..userstyleind..[[">]]..userinf[2]..
				[[</td><td>]]..user_link(userinf[3])..[[</td><td style="]]..timestyle..[[">]]..user_link(userinf[4])..[[</td><td style="]]..timestyle..[[">]]..tostring(time)
		result=result..part
	end
	result=result..tableend

	return result

end

function p.main(frame)
	local talktitle,talktext=title_list(frame.args[1]),talk_list(frame.args[1])
	for i=1,get_len(talktitle) do talktitle[i]=frame:preprocess(frame:preprocess(talktitle[i])) end
	local result=tostring(get_table(talktitle,talktext))
	if get_len(talktitle)==get_len(talktext) then return result else return frame:expandTemplate{ title = 'error', args = { 'Topic list 获取错误。' } } end
end

return p