模块:Discussions

来自「荏苒之境」
Sicusa留言 | 贡献2025年8月24日 (日) 14:37的版本 (创建页面,内容为“local discussions = {} local API_PAGEINFO = "http://localhost/api.php?action=discussiontoolspageinfo&format=json&prop=threaditemshtml&excludesignatures=1&formatversion=2&page=" local function get_topics(title) local d, e = mw.ext.externaldata.getExternalData { url = API_PAGEINFO..title, format = "json" } if e then error("发起请求失败") end return d[1]["__json"]["discussiontoolspageinfo"]["threaditemshtml"] end discussions.get_topics =…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

此模块的文档可以在模块:Discussions/doc创建

local discussions = {}

local API_PAGEINFO = "http://localhost/api.php?action=discussiontoolspageinfo&format=json&prop=threaditemshtml&excludesignatures=1&formatversion=2&page="

local function get_topics(title)
	local d, e = mw.ext.externaldata.getExternalData {
    	url = API_PAGEINFO..title,
    	format = "json"
	}
	if e then
		error("发起请求失败")
	end
	return d[1]["__json"]["discussiontoolspageinfo"]["threaditemshtml"]
end

discussions.get_topics = get_topics

local function get_topic_timestamp(name)
	local time_str = string.sub(name, string.find(name, "-[^-]*$") + 1)
	return tonumber(time_str)
end

discussions.show = function(frame)
	local title = frame.args.title
	local limit = tonumber(frame.args.limit) or 5
	local topics = get_topics(title)
	
	for i = 1, #topics do
		local topic = topics[i]
		topic.timestamp = get_topic_timestamp(topic.name)
	end
	
	table.sort(topics, function(t1, t2)
		return t1.timestamp < t2.timestamp
	end)
	
	local t = {"<ul>"}
	for i = #topics, math.max(1, #topics - limit) do
		t[#t+1] = "<li>"
		t[#t+1] = topics[i].html
		t[#t+1] = "</li>"
	end
	t[#t+1] = "</ul>"
	return table.concat(t)
end

return discussions