欢迎来到奇葩栖息地!欢迎加入Discord服务器:XfrfHCzfbW请先至特殊:参数设置验证邮箱后再进行编辑。特殊:参数设置挑选自己想要使用的小工具!不会编辑?请至这里学习Wikitext语法。

距离淮安市2022年中考还有142天。

模块:Topic list

来自奇葩栖息地
[创建 | 历史 | 清除缓存]文档页面
此模块没有文档页面。如果你知道如何使用模块,请创建它。
  1local p = {}
  2
  3local getArgs = require('Module:Arguments').getArgs
  4
  5function conv(talk)
  6	-- 关键词替换
  7	-- 如有字符导致生成错误,请在这里添加转换
  8	talk = talk
  9		:gsub('===(.-)===', '%1')
 10		:gsub('Special:用户贡献/(%d+%.%d+%.%d+%.%d+)', 'User:ip:%1') -- handle ip user first
 11		:gsub('Special:Contributions/(%d+%.%d+%.%d+%.%d+)', 'User:ip:%1')
 12		:gsub('Special:Contribs/(%d+%.%d+%.%d+%.%d+)', 'User:ip:%1')
 13		:gsub('用户', 'User')
 14		:gsub('user', 'User')
 15	return talk
 16end
 17
 18function titleStrip(talk)
 19	talk = talk
 20		:gsub('===(.-)===', '%1')
 21	return talk
 22end
 23
 24function titleConv(title)
 25	-- 论题转换
 26	title = title
 27		:gsub('<.->', '')
 28		:gsub('%[%[:?.-|(.-)%]%]', '%1')
 29		:gsub('%[%[:?(.-)%]%]', '%1')
 30	return title
 31end
 32
 33function table.unique(t, bArray)
 34	local check = {}
 35	local n = {}
 36	local idx = 1
 37	for k, v in pairs(t) do
 38		if not check[v] then
 39			if bArray then
 40				n[idx] = v
 41				idx = idx + 1
 42			else
 43				n[k] = v
 44			end
 45			check[v] = true
 46		end
 47	end
 48	return n
 49end
 50
 51function getTimeStyle(time)
 52	if time == '未知日期' then
 53		return 'background-color: #fee;'
 54	end
 55	local _, _, year, month, day, hour, min = time:find '(%d+)年(%d+)月(%d+)日%s%(.-%)%s(%d+):(%d+)'
 56
 57	-- divided by 86400 is to convert seconds to days
 58	local diff = os.difftime(os.time(), os.time {year = year, month = month, day = day, hour = hour, min = min}) / 86400
 59
 60	if diff >= 30 then
 61		return 'background-color: #bbb;'
 62	end
 63	if diff >= 7 then
 64		return 'background-color: #ddd;'
 65	end
 66	if diff >= 1 then
 67		return ''
 68	end
 69	return 'background-color: #efe;'
 70end
 71
 72function close(text)
 73	if text:match 'closed%-topic%-yes' then
 74		return 'background-color: #efe;'
 75	end
 76	if text:match 'closed%-topic%-no' then
 77		return 'background-color: #fee;'
 78	end
 79	return ""
 80end
 81
 82function trim(text)
 83	-- remove special strip markers and spaces
 84	text = mw.text.killMarkers(text):gsub('^[%s\t\r\n\f]*(.-)[%s\t\r\n\f]*$', '%1')
 85	return text
 86end
 87
 88function makeUserLink(text)
 89	if text == '?' then
 90		return text
 91	end
 92	-- 输入用户名以及带有ip:前缀的ip用户。
 93	local ipUser = text:match 'ip:(.*)'
 94	if ipUser then
 95		return '[[Special:用户贡献/' .. ipUser .. '|' .. ipUser .. ']]'
 96	else
 97		return '[[User:' .. text .. '|' .. text .. ']]'
 98	end
 99end
100
101function getTalkList(pageName)
102	-- 输入页面名,返回一个包含文本的讨论信息表
103	-- 例如:talklist[1][text]
104	local talk = conv(mw.getCurrentFrame():expandTemplate {title = ':' .. pageName}) .. '=='
105	local talkList = {}
106	for topic in talk:gmatch '==\n(.-)==' do
107		talkList[#talkList + 1] = trim(topic)
108	end
109
110	return talkList
111end
112
113function getTitleList(pageName)
114	local talk = titleStrip(mw.getCurrentFrame():expandTemplate {title = ':' .. pageName})
115	local titleList = {}
116	for title in talk:gmatch '==(.-)==' do
117		titleList[#titleList + 1] = titleConv(trim(title))
118	end
119
120	return titleList
121end
122
123function getTalkTime(talk)
124	local result = talk:match '[%s%S]*(%d%d%d%d.*) %(CST%)' or '未知日期'
125
126	return result
127end
128
129function getUserInfo(text)
130	-- 输入讨论的文本,输出和User有关的转换部分table
131	local userList = {}
132	if text:match '[^\n]*User:([^\n]-)%|[^\n]-CST' == nil then
133		return {userNum = '?', uniqueUserNum = '?', firstUser = '?', lastUser = '?'}
134	end
135	for user in text:gmatch '[^\n]*User:([^\n]-)%|[^\n]-CST' do
136		userList[#userList + 1] = user
137	end
138
139	local userNum = #userList
140	local uniqueUserNum = #(table.unique(userList, true))
141
142	return {userNum = userNum, uniqueUserNum = uniqueUserNum, firstUser = userList[1], lastUser = userList[#userList]}
143end
144
145function generateTable(pageName, talkTitle, talkText)
146	local body = {
147		'{| class="wikitable sortable collapsible talktable" style="text-align: center; float: left;"',
148		'|-',
149		'! # !! 话题 !! 发言条数 !! 参与人数 ',
150		'! class="talkpage-topic-list-author" | 发起者',
151		'! class="talkpage-topic-list-last-editor" | 最后发言者',
152		'! class="talkpage-topic-list-time" | 最后发言时间(CST)'
153	}
154
155	local userInfo, time
156	local userStyle, uniqueUserStyle, timeStyle, serialStyle
157
158	for i = 1, #talkText do
159		time, userInfo = getTalkTime(talkText[i]), getUserInfo(talkText[i])
160		-- 对表格添加样式
161		userStyle = userInfo.userNum == 1 and 'background-color: #fcc;' or ''
162		uniqueUserStyle = userInfo.uniqueUserNum == 1 and 'background-color: #fcc;' or ''
163		timeStyle = getTimeStyle(time)
164		serialStyle = close(talkText[i])
165		-- 组装表格一个话题的部分
166		table.insert(
167			body,
168			table.concat(
169				{
170					'|-',
171					'! style="' .. serialStyle .. '" | ' .. i,
172					'| [[' .. pageName .. '#' .. talkTitle[i] .. '|' .. talkTitle[i].. ']]',
173					'| style="' .. userStyle .. '" | ' .. userInfo.userNum,
174					'| style="' .. uniqueUserStyle .. '" | ' .. userInfo.uniqueUserNum,
175					'| class="talkpage-topic-list-author" | ' .. makeUserLink(userInfo.firstUser),
176					'| class="talkpage-topic-list-last-editor" style="' .. timeStyle .. '" | ' .. makeUserLink(userInfo.lastUser),
177					'| class="talkpage-topic-list-time" style="' .. timeStyle .. '" | ' .. time
178				},
179				'\n'
180			)
181		)
182	end
183	table.insert(body, '|}')
184
185	return table.concat(body, '\n')
186end
187
188function p.main(frame)
189	local args = getArgs(frame)
190	local f = mw.getCurrentFrame()
191
192	local talkTitle, talkText = getTitleList(args[1]), getTalkList(args[1])
193	for i = 1, #talkTitle do
194		talkTitle[i] = f:preprocess(talkTitle[i])
195	end
196	local result = generateTable(args[1], talkTitle, talkText)
197
198	if #talkTitle == #talkText then
199		return result
200	else
201		return error 'Topic list获取错误。'
202	end
203end
204
205return p