Edit the documentation or categories for this module.
Required Arguments[]
q?tables
q?fields
Optional Query Arguments[]
- Anything supported here. Place
q?
prior to the argument name.
Optional Non-Query Arguments[]
|defaults=
, formatted the same as the|q?where=
string, withfieldname=defaultvalue
in the case that the query returns no results.|default=
, the default value to be used if no default is specified in|defaults=
for that particular field.
If no defaults are specified, any query that returns no results will define every variable as Undefined
.
Usage[]
This template is NOT meant to be called directly. It is instead a "helper" module to be called within other templates (or modules). Probably when you use this module, all arguments will be sent directly in the invoke, except for the condition of the |q?where
, which should be an argument to the parent template (e.g. |q?where="default"
).
local util_args = require("Module:ArgsUtil")
local util_vars = require("Module:VarsUtil")
local p = {}
function p.cargoVardefines (frame)
local args = util_args.merge()
-- All arguments that are for the query will start with q?
local cargoquery = {}
for key, value in pairs(args) do
if string.sub(key, 0, 2) == 'q?' then -- if first two characters are q?
cargoquery[string.sub(key, 3)] = value -- the rest of the string becomes the key for the cargo query table
end
end
-- q?tables and q?fields are mandatory, the rest of the fields (if there are any) are added as extra args
local result = mw.ext.cargo.query(cargoquery.tables, cargoquery.fields, cargoquery)
if #result ~= 0 then
for _, row in ipairs(result) do
for field, value in pairs(row) do
util_vars.setVar(field, value)
end
end
else
local aliasname = ""
local defaults = {}
local default = args['default'] or 'Undefined'
if args['defaults'] then
for item in string.gmatch(args['defaults'], '[^,]+') do
mw.text.trim(item)
key, value = string.match(item, "(.*)=(.*)$" )
defaults[key] = value
end
end
for item in string.gmatch(cargoquery.fields, '[^,]+') do
aliasname = string.match(item, "=(.*)$" ) or item
varname = string.match(item, "^([^=]*)")
defaultname = defaults[varname] or default
util_vars.setVar(aliasname, defaultname)
end
end
end
return p