打开/关闭菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

模块:Cargo:修订间差异

来自骷髅女孩Wiki
创建页面,内容为“local cargo = {} local extCargo = mw.ext.cargo local checkType = require( 'libraryUtil' ).checkType local mArguments local cache = {} --- Flatten key value pairs table into string --- --- @param t table Table to be flatten --- @return string local function flattenTableToString( t ) local stringTable = {} for k, v in pairs( t ) do table.insert( stringTable, string.format( '%s:%s', k, v ) ) end return table.concat( stringTable, ',' ) end --- Format resu…”
 
无编辑摘要
 
第1行: 第1行:
-- 可调用的功能:cacheQueryResults, getQueryResultsFromWikitext
-- cargo表用于存储该模块的函数和变量;extCargo为引入Cargo扩展的功能
-- checkType从libraryUtil模块中引入checkType函数来检查参数类型(libraryUtil好像没有用)
-- mArguments存储Arguments模块中getArgs函数获取的模板参数
-- cache用于缓存Cargo查询的结果
local cargo = {}
local cargo = {}
local extCargo = mw.ext.cargo
local extCargo = mw.ext.cargo
第4行: 第9行:
local mArguments
local mArguments
local cache = {}
local cache = {}
 
-- 将表t中每个键值对格式化为k:v的格式并插入到stringTable中
 
-- 使用逗号将stringTable中所有字符串连接起来,只返回字符串
--- Flatten key value pairs table into string
---
--- @param t table Table to be flatten
--- @return string
local function flattenTableToString( t )
local function flattenTableToString( t )
local stringTable = {}
local stringTable = {}
第17行: 第18行:
return table.concat( stringTable, ',' )
return table.concat( stringTable, ',' )
end
end
 
-- 将Cargo查询的results表按format格式化为Wiki文本
 
-- 如果format为debug,则用<pre>标签把结果表字符串(dumpObject方法)包裹后返回
--- Format results from Cargo query into wikitext for output
-- format不指定的情况下,创建一个带有wikitable类的表格,遍历results中的每一行结果创建表格行,返回键值对
---
--- @param results table Results from Cargo query
--- @param format string Format for the Cargo query
--- @return string
local function formatQueryResultsToWikitext( results, format )
local function formatQueryResultsToWikitext( results, format )
if format == 'debug' then
if format == 'debug' then
return '<pre>' .. mw.dumpObject( results ) .. '</pre>'
return '<pre>' .. mw.dumpObject( results ) .. '</pre>'
-- Default to table output
else
else
local table = mw.html.create( 'table' ):addClass( 'wikitable' )
local table = mw.html.create( 'table' ):addClass( 'wikitable' )
local headerRow = mw.html.create( 'tr' )
local headerRow = mw.html.create( 'tr' )
table:node( headerRow )
table:node( headerRow )
for i, result in pairs( results ) do
for i, result in pairs( results ) do
local bodyRow = mw.html.create( 'tr' )
local bodyRow = mw.html.create( 'tr' )
for k, v in pairs( result ) do
for k, v in pairs( result ) do
if i == 1 then
if i == 1 then
第42行: 第36行:
bodyRow:tag( 'td' ):wikitext( v )
bodyRow:tag( 'td' ):wikitext( v )
end
end
table:node( bodyRow )
table:node( bodyRow )
end
end
return tostring( table )
return tostring( table )
end
end
end
end
 
-- 缓存Cargo查询结果,检查并初始化缓存表中相应的索引(3层嵌套表)
 
--- Cache Cargo query result
--- TODO: Make it smarter so that it separate the tables and fields into different caches
---
--- @param results table Results from Cargo query
--- @param tables string Tables param from Cargo query
--- @param fields string Fields param from Cargo query
--- @param argsString string Flatten args param from Cargo query
local function cacheQueryResults( results, tables, fields, argsString )
local function cacheQueryResults( results, tables, fields, argsString )
-- Init index
if not cache[ tables ] then
if not cache[ tables ] then
cache[ tables ] = {}
cache[ tables ] = {}
第69行: 第52行:
cache[ tables ][ fields ][ argsString ] = {}
cache[ tables ][ fields ][ argsString ] = {}
end
end
-- mw.log( string.format( '[Cargo] Cached Cargo query result at: cache[\'%s\'][\'%s\'][\'%s\']', tables, fields, argsString ) )
cache[ tables ][ fields ][ argsString ] = results
cache[ tables ][ fields ][ argsString ] = results
end
end
 
-- 执行cargo_query并返回结果(部分参数见:https://www.mediawiki.org/wiki/Extension:Cargo/Other_features#Lua_support)
 
-- tables为查询的表格名,fields为查询的字段名,args为查询的其他参数,默认为空表
--- Return the result of a Cargo query (#cargo_query)
---
--- For info on the parameters:
--- @see https://www.mediawiki.org/wiki/Extension:Cargo/Other_features#Lua_support
---
--- @param tables string The set of Cargo tables to be queried
--- @param fields string The field or set of fields to be displayed
--- @param args table Optional parameters for the query
--- @param shouldCache bool Whether the query should be cached, default to true
--- @return table
function cargo.getQueryResults( tables, fields, args, shouldCache )
function cargo.getQueryResults( tables, fields, args, shouldCache )
-- args are optional
args = args or {}
args = args or {}
 
-- shouldCache表示是否缓存结果,默认为true
if shouldCache ~= false then shouldCache = true end
if shouldCache ~= false then shouldCache = true end
 
-- checkType的用法是检查@param1的函数中第@param2个参数传入的参数值@param3是否为期望的类型@param4
-- 如果并非正确的类型,会抛出错误
checkType( 'Module:Cargo/cargo.getQueryResults', 1, tables, 'string' )
checkType( 'Module:Cargo/cargo.getQueryResults', 1, tables, 'string' )
checkType( 'Module:Cargo/cargo.getQueryResults', 2, fields, 'string' )
checkType( 'Module:Cargo/cargo.getQueryResults', 2, fields, 'string' )
checkType( 'Module:Cargo/cargo.getQueryResults', 3, args, 'table' )
checkType( 'Module:Cargo/cargo.getQueryResults', 3, args, 'table' )
local argsString = flattenTableToString( args )
local argsString = flattenTableToString( args )
 
-- 若缓存中有查询结果,直接返回查询结果
if shouldCache and cache[ tables ] and cache[ tables ][ fields ] and cache[ tables ][ fields ][ argsString ] then
if shouldCache and cache[ tables ] and cache[ tables ][ fields ] and cache[ tables ][ fields ][ argsString ] then
return cache[ tables ][ fields ][ argsString ]
return cache[ tables ][ fields ][ argsString ]
end
end
 
    -- 否则,使用pcall调用extCargo.query执行查询,如果查询失败,报错
    -- pcall的用法是检测@param1函数以及传递给它的参数是否成功执行,成功的话success为true,否则false
local success, results = pcall( extCargo.query, tables, fields, args )
local success, results = pcall( extCargo.query, tables, fields, args )
-- mw.logObject( { [ 'tables' ] = tables, [ 'fields' ] =  fields, [ 'args' ] =  args }, '[Cargo] Run Cargo query with the following params' )
if not success then
if not success then
error( 'Failed to query Cargo table' )
error( 'Failed to query Cargo table' )
end
end
 
-- 缓存查询结果
-- mw.logObject( results, '[Cargo] Found Cargo query result' )
if shouldCache then
if shouldCache then
cacheQueryResults( results, tables, fields, argsString )
cacheQueryResults( results, tables, fields, argsString )
第113行: 第82行:
return results
return results
end
end
 
-- 实现Cargo query功能,引入Arguments模块获取模板参数getArgs
 
-- 从模板参数中提取tables和fields,构建查询参数表args,包含where、join等参数
--- Implement {{Cargo query}} template
-- 调用getQueryResults函数执行查询
--- TODO: Implement formats
---
--- @return string
function cargo.getQueryResultsFromWikitext( frame )
function cargo.getQueryResultsFromWikitext( frame )
mArguments = require( 'Module:Arguments' )
mArguments = require( 'Module:Arguments' )
local templateArgs = mArguments.getArgs( frame )
local templateArgs = mArguments.getArgs( frame )
local tables = templateArgs[ 'tables' ]
local tables = templateArgs[ 'tables' ]
local fields = templateArgs[ 'fields' ]
local fields = templateArgs[ 'fields' ]
if not tables or not fields then
if not tables or not fields then
error( 'Missing tables or fields parameter' )
error( 'Missing tables or fields parameter' )
end
end
local args = {
local args = {
[ 'where' ] = templateArgs[ 'where' ],
[ 'where' ] = templateArgs[ 'where' ],
第139行: 第102行:
[ 'offset' ] = templateArgs[ 'offset' ]
[ 'offset' ] = templateArgs[ 'offset' ]
}
}
-- Get query results
local results = cargo.getQueryResults( tables, fields, args )
local results = cargo.getQueryResults( tables, fields, args )
local format = templateArgs[ 'format' ]
local format = templateArgs[ 'format' ]
return formatQueryResultsToWikitext( results, format )
return formatQueryResultsToWikitext( results, format )
end
end
 
-- 不返回任何值,返回本模块,供其他模块或页面使用其中的函数
 
--- Debug function
function cargo.test( chara )
chara = chara or 'Venom'
 
local tables = 'MoveData_GGACR'
    local fields = 'chara,name,input,damage,guard,startup,active,recovery,onBlock,onHit,level,images,hitboxes,notes,caption,hitboxCaption,type,gbp,gbm,tension,prorate,invuln,cancel,blockstun,groundHit,airHit,hitstop,gatlings,frcWindow'
    local args = {
        where  = 'MoveData_GGACR.chara="' .. chara .. '"',
        orderBy = 'MoveData_GGACR._rowID',
    }
local results = cargo.getQueryResults( tables, fields, args )
mw.logObject( results, '[Cargo] Test function output' )
end
 
 
return cargo
return cargo

2025年1月11日 (六) 00:27的最新版本

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

-- 可调用的功能:cacheQueryResults, getQueryResultsFromWikitext
-- cargo表用于存储该模块的函数和变量;extCargo为引入Cargo扩展的功能
-- checkType从libraryUtil模块中引入checkType函数来检查参数类型(libraryUtil好像没有用)
-- mArguments存储Arguments模块中getArgs函数获取的模板参数
-- cache用于缓存Cargo查询的结果
local cargo = {}
local extCargo = mw.ext.cargo
local checkType = require( 'libraryUtil' ).checkType
local mArguments
local cache = {}
-- 将表t中每个键值对格式化为k:v的格式并插入到stringTable中
-- 使用逗号将stringTable中所有字符串连接起来,只返回字符串
local function flattenTableToString( t )
	local stringTable = {}
	for k, v in pairs( t ) do
		table.insert( stringTable, string.format( '%s:%s', k, v ) )
	end
	return table.concat( stringTable, ',' )
end
-- 将Cargo查询的results表按format格式化为Wiki文本
-- 如果format为debug,则用<pre>标签把结果表字符串(dumpObject方法)包裹后返回
-- format不指定的情况下,创建一个带有wikitable类的表格,遍历results中的每一行结果创建表格行,返回键值对
local function formatQueryResultsToWikitext( results, format )
	if format == 'debug' then
		return '<pre>' .. mw.dumpObject( results ) .. '</pre>'
	else
		local table = mw.html.create( 'table' ):addClass( 'wikitable' )
		local headerRow = mw.html.create( 'tr' )
		table:node( headerRow )
		for i, result in pairs( results ) do
			local bodyRow = mw.html.create( 'tr' )
			for k, v in pairs( result ) do
				if i == 1 then
					headerRow:tag( 'th' ):wikitext( k )
				end
				bodyRow:tag( 'td' ):wikitext( v )
			end
			table:node( bodyRow )
		end
		return tostring( table )
	end
end
-- 缓存Cargo查询结果,检查并初始化缓存表中相应的索引(3层嵌套表)
local function cacheQueryResults( results, tables, fields, argsString )
	if not cache[ tables ] then
		cache[ tables ] = {}
	end
	if not cache[ tables ][ fields ] then
		cache[ tables ][ fields ] = {}
	end
	if not cache[ tables ][ fields ][ argsString ] then
		cache[ tables ][ fields ][ argsString ] = {}
	end
	cache[ tables ][ fields ][ argsString ] = results
end
-- 执行cargo_query并返回结果(部分参数见:https://www.mediawiki.org/wiki/Extension:Cargo/Other_features#Lua_support)
-- tables为查询的表格名,fields为查询的字段名,args为查询的其他参数,默认为空表
function cargo.getQueryResults( tables, fields, args, shouldCache )
	args = args or {}
	-- shouldCache表示是否缓存结果,默认为true
	if shouldCache ~= false then shouldCache = true end
	-- checkType的用法是检查@param1的函数中第@param2个参数传入的参数值@param3是否为期望的类型@param4
	-- 如果并非正确的类型,会抛出错误
	checkType( 'Module:Cargo/cargo.getQueryResults', 1, tables, 'string' )
	checkType( 'Module:Cargo/cargo.getQueryResults', 2, fields, 'string' )
	checkType( 'Module:Cargo/cargo.getQueryResults', 3, args, 'table' )
	local argsString = flattenTableToString( args )
	-- 若缓存中有查询结果,直接返回查询结果
	if shouldCache and cache[ tables ] and cache[ tables ][ fields ] and cache[ tables ][ fields ][ argsString ] then
		return cache[ tables ][ fields ][ argsString ]
	end
    -- 否则,使用pcall调用extCargo.query执行查询,如果查询失败,报错
    -- pcall的用法是检测@param1函数以及传递给它的参数是否成功执行,成功的话success为true,否则false
	local success, results = pcall( extCargo.query, tables, fields, args )
	if not success then
		error( 'Failed to query Cargo table' )
	end
	-- 缓存查询结果
	if shouldCache then
		cacheQueryResults( results, tables, fields, argsString )
	end
	return results
end
-- 实现Cargo query功能,引入Arguments模块获取模板参数getArgs
-- 从模板参数中提取tables和fields,构建查询参数表args,包含where、join等参数
-- 调用getQueryResults函数执行查询
function cargo.getQueryResultsFromWikitext( frame )
	mArguments = require( 'Module:Arguments' )
	local templateArgs = mArguments.getArgs( frame )
	local tables = templateArgs[ 'tables' ]
	local fields = templateArgs[ 'fields' ]
	if not tables or not fields then
		error( 'Missing tables or fields parameter' )
	end
	local args = {
		[ 'where' ] = templateArgs[ 'where' ],
		[ 'join' ]  = templateArgs[ 'join on' ],
		[ 'groupBy' ]  = templateArgs[ 'group by' ],
		[ 'having' ] = templateArgs[ 'having' ],
		[ 'orderBy' ] = templateArgs[ 'order by' ],
		[ 'limit' ] = templateArgs[ 'limit' ],
		[ 'offset' ] = templateArgs[ 'offset' ]
	}
	local results = cargo.getQueryResults( tables, fields, args )
	local format = templateArgs[ 'format' ]
	return formatQueryResultsToWikitext( results, format )
end
-- 不返回任何值,返回本模块,供其他模块或页面使用其中的函数
return cargo