Module:Topic list:修订间差异

来自Arcaea中文维基
无编辑摘要
无编辑摘要
第1行: 第1行:
local p = {}
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)
function table.unique(t, bArray)
   local check = {}
local check = {}
   local n = {}
local n = {}
   local idx = 1
local idx = 1
   for k, v in pairs(t) do
for k, v in pairs(t) do
     if not check[v] then
if not check[v] then
       if bArray then
if bArray then
         n[idx] = v
n[idx] = v
         idx = idx + 1
idx = idx + 1
       else
else
         n[k] = v
n[k] = v
       end
end
       check[v] = true
check[v] = true
     end
end
   end
end
   return n
return n
end
end


function get_len(tb)
function getTimeStyle(time)
  local len = 0
local _, _, year, month, day, hour, min = time:find '(%d+)年(%d+)月(%d+)日%s%(.-%)%s(%d+):(%d+)'
  for k,v in pairs(tb) do
   len= len+1
  end
return len
end


function time_style(time)
-- divided by 86400 is to convert seconds to days
local _, _, y, m, d, _hour, _min, _sec = string.find(time, "(%d+)年(%d+)月(%d+)日%s%(.-%)%s(%d+):(%d+)");
local diff = os.difftime(os.time(), os.time {year = year, month = month, day = day, hour = hour, min = min}) / 86400
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);
if diff >= 30 then
local time_txt = string.format("%02d", t_time.month).."月"..string.format("%02d", t_time.day).."日  "..string.format("%02d", t_time.hour)..
return 'background-color: #bbb;'
":"..string.format("%02d", t_time.min)..":"..string.format("%02d", t_time.sec);
end
 
if diff >= 7 then
 
return 'background-color: #ddd;'
if string.format("%02d", t_time.month)~="00" then return [[background-color: #bbb;]] end
end
if tonumber(string.format("%02d", t_time.day))>=7 then return [[background-color: #ddd;]] end
if diff >= 1 then
if tonumber(string.format("%02d", t_time.day))>=1 then return [[]] end
return ''
return [[background-color: #efe;]]
end
return 'background-color: #efe;'
end
end


function time_diff(long_time,short_time)
function close(text)
local n_short_time,n_long_time,carry,diff = os.date('*t',short_time),os.date('*t',long_time),false,{}
if text:match 'closed%-topic%-yes' then
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}
return 'background-color: #efe;'
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
end
for i,v in ipairs({'sec','min','hour','day','month','year'}) do
if text:match 'closed%-topic%-no' then
diff[v] = n_long_time[v] - n_short_time[v] + (carry and -1 or 0)
return 'background-color: #fee;'
carry = diff[v] < 0
if carry then
diff[v] = diff[v] + colMax[i]
end
end
end
return diff
return ""
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
end


function cov(talk)
function conv(talk)
--关键词替换
-- 关键词替换
--如有字符导致生成错误,请在这里添加转换
-- 如有字符导致生成错误,请在这里添加转换
talk=string.gsub(talk, "===(.-)===", "%1")
talk = talk
talk=string.gsub(talk, " 用户", "User")
:gsub('===(.-)===', '%1')
talk=string.gsub(talk, "user", "User")
:gsub('Special:用户贡献/', 'User:ip:') -- ip user handle first
:gsub(' 用户', 'User')
:gsub('user', 'User')
:gsub('<.->', '')
return talk
return talk
end
end


function link(text)
function trim(text)
-- 此处添加导致链接工作不正常的文本
-- remove special strip markers and spaces
text=trim(trim(text))
text = mw.text.killMarkers(text):gsub('^[\t\r\n\f]*(.-)[\t\r\n\f]*$', '%1')
text=string.gsub(text, " (.*)", "%1")
text=LuaReomve(text,"%[")
text=LuaReomve(text,"%]")
return text
return text
end
end


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


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


return talkList
end
end


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


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


function user_manage(text)
function getUserInfo(text)
--输入讨论的文本,输出和User有关的转换部分table
-- 输入讨论的文本,输出和User有关的转换部分table
--1-4分别为发言条数,发言人数,发起人,最后发言的人。
local userList = {}
text=string.gsub(text, "Special:User贡献/", "User:ip:")
for user in text:gmatch 'User:([^\n]-)%|[^\n]-CST' do
--别问为什么是User贡献,问就是神奇代码
userList[#userList + 1] = 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
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
local userNum = #userList
local uniqueUserNum = #(table.unique(userList))
 
return {userNum = userNum, uniqueUserNum = uniqueUserNum, firstUser = userList[1], lastUser = userList[#userList]}
end
end


function get_table(talktitle,talktext)
function generateTable(talkTitle, talkText)
local result=[[<table class="wikitable sortable mw-collapsible" style="text-align:center"><tr><th>#</td><th> 话题</td><th> 发言</td>
local body = {
<th> 参与</td><th> 发起者</td><th> 发言</td><th> 最后发言时间(CST)</td></tr>]]
'{| class="wikitable sortable collapsible talktable" style="text-align: center;"',
local tableend=[[</table>]]
'|-',
local part=""
'! # !!  话题 !!  发言 条数 !!  参与 人数 !!  发起者 !!  发言 者 ',
'! class="talkpage-topic-list-time" |  最后发言时间 (CST)'
}


local userinf,time
local userInfo, time
local userstyle,userstyleind,timestyle
local userStyle, uniqueUserStyle, timeStyle, serialStyle
for i=1,get_len(talktext)
 
do
for i = 1, #talkText do
time,userinf=talk_time(talktext[i]),user_manage(talktext[i])
time, userInfo = getTalkTime(talkText[i]), getUserInfo(talkText[i])
--对表格添加样式
-- 对表格添加样式
if userinf[1]==1 then userstyle=[[background-color: #fcc;"]] else userstyle=[[]] end
userStyle = userInfo.userNum == 1 and 'background-color: #fcc;' or ''
if userinf[2]==1 then userstyleind=[[background-color: #fcc;"]] else userstyleind=[[]] end
uniqueUserStyle = userInfo.uniqueUserNum == 1 and 'background-color: #fcc;' or ''
if userinf[1]>=7 then userstyle=[[background-color: #ffe;"]] end
timeStyle = getTimeStyle(time)
if userinf[2]>=7 then userstyleind=[[background-color: #ffe;"]] end
serialStyle = close(talkText[i])
timestyle=close(talktext[i],time_style(time))
-- 组装表格一个 题的部分
--组装表格一个 题的部分
table.insert(
part=[[<tr><th>]]..i..[[</td><td>]]..link(talktitle[i])..[[</td><td style="]]..userstyle..[[">]]..userinf[1]..[[</td><td style="]]..userstyleind..[[">]]..userinf[2]..
body,
[[</td><td>]]..user_link(userinf[3])..[[</td><td style="]]..timestyle..[[">]]..user_link(userinf[4])..[[</td><td style="]]..timestyle..[[">]]..tostring(time)
table.concat(
result=result..part
{
'|-',
'! style="' .. serialStyle .. '" | ' .. i,
'| ' .. '[[#' .. trim(talkTitle[i]) .. '|' .. trim(talkTitle[i]) .. ']]',
'| style="' .. userStyle .. '" | ' .. userInfo.userNum,
'| style="' .. uniqueUserStyle .. '" | ' .. userInfo.uniqueUserNum,
'| ' .. makeUserLink(userInfo.firstUser),
'| style="' .. timeStyle .. '" | ' .. makeUserLink(userInfo.lastUser),
'| class="talkpage-topic-list-time" style="' .. timeStyle .. '" | ' .. time
},
'\n'
)
)
end
end
result=result..tableend
table.insert(body, '|}')


return result
return table.concat(body, '\n')
end


end
function p.main()
local args = require([[Module:ProcessArgs]]).merge(true)
local f = mw.getCurrentFrame()
 
local talkTitle, talkText = getTitleList(args[1]), getTalkList(args[1])
for i = 1, #talkTitle do
talkTitle[i] = f:preprocess(talkTitle[i])
end
local result = generateTable(talkTitle, talkText)


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


return p
return p

2021年4月13日 (二) 18:22的版本

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

local p = {}

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 getTimeStyle(time)
	local _, _, year, month, day, hour, min = time:find '(%d+)年(%d+)月(%d+)日%s%(.-%)%s(%d+):(%d+)'

	-- divided by 86400 is to convert seconds to days
	local diff = os.difftime(os.time(), os.time {year = year, month = month, day = day, hour = hour, min = min}) / 86400

	if diff >= 30 then
		return 'background-color: #bbb;'
	end
	if diff >= 7 then
		return 'background-color: #ddd;'
	end
	if diff >= 1 then
		return ''
	end
	return 'background-color: #efe;'
end

function close(text)
	if text:match 'closed%-topic%-yes' then
		return 'background-color: #efe;'
	end
	if text:match 'closed%-topic%-no' then
		return 'background-color: #fee;'
	end
	return ""
end

function conv(talk)
	-- 关键词替换
	-- 如有字符导致生成错误,请在这里添加转换
	talk = talk
		:gsub('===(.-)===', '%1')
		:gsub('Special:用户贡献/', 'User:ip:') -- ip user handle first
		:gsub('用户', 'User')
		:gsub('user', 'User')
		:gsub('<.->', '')
	return talk
end

function trim(text)
	-- remove special strip markers and spaces
	text = mw.text.killMarkers(text):gsub('^[\t\r\n\f]*(.-)[\t\r\n\f]*$', '%1')
	return text
end

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

function getTalkList(pageName)
	-- 输入页面名,返回一个包含文本的讨论信息表
	-- 例如:talklist[1][text]
	local talk = conv(mw.getCurrentFrame():expandTemplate {title = ':' .. pageName}) .. '=='
	local talkList = {}
	for topic in talk:gmatch '==\n(.-)==' do
		talkList[#talkList + 1] = topic
	end

	return talkList
end

function getTitleList(pageName)
	local talk = conv(mw.getCurrentFrame():expandTemplate {title = ':' .. pageName})
	local titleList = {}
	for title in talk:gmatch '==(.-)==' do
		titleList[#titleList + 1] = title
	end

	return titleList
end

function getTalkTime(talk)
	return talk:match '[%s%S]*(%d%d%d%d.*) %(CST%)'
end

function getUserInfo(text)
	-- 输入讨论的文本,输出和User有关的转换部分table
	local userList = {}
	for user in text:gmatch 'User:([^\n]-)%|[^\n]-CST' do
		userList[#userList + 1] = user
	end

	local userNum = #userList
	local uniqueUserNum = #(table.unique(userList))

	return {userNum = userNum, uniqueUserNum = uniqueUserNum, firstUser = userList[1], lastUser = userList[#userList]}
end

function generateTable(talkTitle, talkText)
	local body = {
		'{| class="wikitable sortable collapsible talktable" style="text-align: center;"',
		'|-',
		'! # !! 话题 !! 发言条数 !! 参与人数 !! 发起者 !! 最后发言者 ',
		'! class="talkpage-topic-list-time" | 最后发言时间(CST)'
	}

	local userInfo, time
	local userStyle, uniqueUserStyle, timeStyle, serialStyle

	for i = 1, #talkText do
		time, userInfo = getTalkTime(talkText[i]), getUserInfo(talkText[i])
		-- 对表格添加样式
		userStyle = userInfo.userNum == 1 and 'background-color: #fcc;' or ''
		uniqueUserStyle = userInfo.uniqueUserNum == 1 and 'background-color: #fcc;' or ''
		timeStyle = getTimeStyle(time)
		serialStyle = close(talkText[i])
		-- 组装表格一个话题的部分
		table.insert(
			body,
			table.concat(
				{
					'|-',
					'! style="' .. serialStyle .. '" | ' .. i,
					'| ' .. '[[#' .. trim(talkTitle[i]) .. '|' .. trim(talkTitle[i]) .. ']]',
					'| style="' .. userStyle .. '" | ' .. userInfo.userNum,
					'| style="' .. uniqueUserStyle .. '" | ' .. userInfo.uniqueUserNum,
					'| ' .. makeUserLink(userInfo.firstUser),
					'| style="' .. timeStyle .. '" | ' .. makeUserLink(userInfo.lastUser),
					'| class="talkpage-topic-list-time" style="' .. timeStyle .. '" | ' .. time
				},
				'\n'
			)
		)
	end
	table.insert(body, '|}')

	return table.concat(body, '\n')
end

function p.main()
	local args = require([[Module:ProcessArgs]]).merge(true)
	local f = mw.getCurrentFrame()

	local talkTitle, talkText = getTitleList(args[1]), getTalkList(args[1])
	for i = 1, #talkTitle do
		talkTitle[i] = f:preprocess(talkTitle[i])
	end
	local result = generateTable(talkTitle, talkText)

	if #talkTitle == #talkText then
		return result
	else
		return error 'Topic list获取错误。'
	end
end

return p