模块: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 =…”
 
无编辑摘要
 
(未显示同一用户的11个中间版本)
第1行: 第1行:
local utils_str = require("Module:Utils/String")
local escape_url = utils_str.escape_url
local discussions = {}
local discussions = {}


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


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",
    ["cache seconds"] = 0
}
}
if e then
if e then
error("发起请求失败")
error("发起请求失败")
end
end
return d[1]["__json"]["discussiontoolspageinfo"]["threaditemshtml"]
local res = d[1]["__json"]["discussiontoolspageinfo"]
return res and res["threaditemshtml"]
end
end


第25行: 第31行:
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)
if topics == nil or #topics == 0 then
return frame.args.no_result_text
end
for i = 1, #topics do
for i = 1, #topics do
第36行: 第46行:
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
local author = topic.replies[1].author
t[#t+1] = "</li>"
t[#t+1] = "<li>[["
t[#t+1] = title
t[#t+1] = "#"
t[#t+1] = topic.id
t[#t+1] = "|"
t[#t+1] = topic.html
t[#t+1] = "]] — [[User:"
t[#t+1] = author
t[#t+1] = "|"
t[#t+1] = author
t[#t+1] = "]]</li>"
end
end
t[#t+1] = "</ul>"
t[#t+1] = "</ul>"

2025年8月24日 (日) 16:54的最新版本

此模块的文档可以在模块: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&smaxage=0&maxage=0&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",
    	["cache seconds"] = 0
	}
	if e then
		error("发起请求失败")
	end
	local res = d[1]["__json"]["discussiontoolspageinfo"]
	return res and res["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)
	
	if topics == nil or #topics == 0 then
		return frame.args.no_result_text
	end
	
	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]
		local author = topic.replies[1].author
		
		t[#t+1] = "<li>[["
		t[#t+1] = title
		t[#t+1] = "#"
		t[#t+1] = topic.id
		t[#t+1] = "|"
		t[#t+1] = topic.html
		t[#t+1] = "]] — [[User:"
		t[#t+1] = author
		t[#t+1] = "|"
		t[#t+1] = author
		t[#t+1] = "]]</li>"
	end
	t[#t+1] = "</ul>"
	return table.concat(t)
end

return discussions