模块:Sort

Stapx Steve讨论 | 贡献2022年8月13日 (六) 04:55的版本 (修 bug 中 ……)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

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

--Sort By Stapx Steve
--本模块包分类页面生成相关的功能

local p = {} --p代表一个包(package)

function p.createUser(frame)
	-- 获取标题
	local name = tostring(mw.title.getCurrentTitle())
	name = "分类:玩家"
	-- 获取完整链接
	local main = "https://" .. mw.uri.fullUrl( "主页").host
	-- 访问 API 获取分类列表
	local api_url = main .. "/api.php?action=query&format=json&list=categorymembers&cmlimit=max&cmtitle=" .. name
	local lists = mw.ext.externaldata.getWebData {
    	url = api_url
		, data = "pid=$.query.categorymembers[*].pageid, pname=$.query.categorymembers[*].title, perror=$.error.code"
		, format = "json"
		, 'use jsonpath'
	}
	-- 报错弹出
	if(lists == nil)
	then
		return "什么都没获取到呢 ……"
	end
	if(lists.perror)
	then
		return "请不要在非分类页面使用此方法: " .. lists.perror
	end
	-- 解析 table
	local str = ""
	for key, value in pairs(lists) do
		if(type(value) == "table")
		then
			-- lists[key]["pid"]
			-- lists[key]["pname"]
			-- 访问 API 获取页面概要文本
			local api_url = main .. "/api.php?action=query&format=json&prop=extracts&exchars=20&explaintext=1&titles=" .. lists[key]["pname"]
			local list_page = mw.ext.externaldata.getWebData {
				url = api_url
				, data = "text = $.query.pages." .. lists[key]["pid"] .. ".extract"
				, format = "json"
				, 'use jsonpath'
			}
			-- 页面名
			local uname = lists[key]["pname"]
			-- 用户名
			-- 特判部分带下划线的名字
			local utitle = lists[key]["pname"]
			utitle = (string.gsub(utitle, "L Ac", "L_Ac"))
			-- 获取游戏名
			uname = require( 'Module:Info' ).getGameID( uname )
			if(uname == nil)
			then
				uname = "none"
			end
			-- 生成卡片
			str = str .. "{{玩家卡片|".. uname .."|[[" .. utitle .. "]]|" .. (string.gsub(list_page.text, "\n", " ")) .. "}}"
		end
	end
	return frame:preprocess(str)
end

return p