模块:Discussions:修订间差异

来自「荏苒之境」
创建页面,内容为“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 =…”
 
无编辑摘要
第1行: 第1行:
local utils_str = require("Module:Utils/String")
local escape_url = utils_str.escape_url
local discussions = {}
local discussions = {}


第5行: 第9行:
local function get_topics(title)
local function get_topics(title)
local d, e = mw.ext.externaldata.getExternalData {
local d, e = mw.ext.externaldata.getExternalData {
     url = API_PAGEINFO..title,
     url = API_PAGEINFO..escape_url(title),
     format = "json"
     format = "json"
}
}
第25行: 第29行:
local limit = tonumber(frame.args.limit) or 5
local limit = tonumber(frame.args.limit) or 5
local topics = get_topics(title)
local topics = get_topics(title)
mw.logObject(topics)
for i = 1, #topics do
for i = 1, #topics do
第36行: 第41行:
local t = {"<ul>"}
local t = {"<ul>"}
for i = #topics, math.max(1, #topics - limit) do
for i = #topics, math.max(1, #topics - limit), -1 do
t[#t+1] = "<li>"
local topic = topics[i]
t[#t+1] = topics[i].html
t[#t+1] = "<li>[[Special:GoToComment/"
t[#t+1] = "</li>"
t[#t+1] = topic.id
t[#t+1] = "|"
t[#t+1] = topic.html
t[#t+1] = "]]</li>"
end
end
t[#t+1] = "</ul>"
t[#t+1] = "</ul>"

2025年8月24日 (日) 14:52的版本

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

local utils_str = require("Module:Utils/String")

local escape_url = utils_str.escape_url

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..escape_url(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)
	mw.logObject(topics)
	
	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), -1 do
		local topic = topics[i]
		t[#t+1] = "<li>[[Special:GoToComment/"
		t[#t+1] = topic.id
		t[#t+1] = "|"
		t[#t+1] = topic.html
		t[#t+1] = "]]</li>"
	end
	t[#t+1] = "</ul>"
	return table.concat(t)
end

return discussions