Support wiki
This is the wiki.gg support wiki! It's a different site from your content wiki!
Module:CargoQuery
This module lets you get around the |no html
bug that Cargo has by avoiding Cargo's formatting.
To use, you only need to change #cargo_query:
to #invoke:CargoQuery|main|
, the usage otherwise imitates a regular Cargo query.
If |format=template
or it's undefined, you must specify the template that will handle each row of the query result using |template=
. You may additionally specify |intro=
, |outro=
, |delimiter=
, and |default=
.
If |format=gallery
you can set |mode=
to a value accepted by the corresponding attribute of the <gallery> tag. To set the captions, one of the returned fields must be aliased as caption
.
Use of Lua names of all query parameters, so |q?join=
, |q?groupBy
, etc is not necessary anymore, but is possible.
For simplicity of code, the named args parameter is required to be Yes, and you do not need to specify it.
Unlike Cargo's |format=template
, this wrapper will NOT rename parameters with underscores in them to use spaces instead.
Parameters & Invocation
{{#invoke:CargoQuery|main |tables= <!-- corresponds to table / tables --> |join on= <!-- corresponds to join on --> |fields= <!-- corresponds to fields --> |where= <!-- corresponds to where --> |group by= <!-- corresponds to group by --> |having= <!-- corresponds to having --> |order by= <!-- corresponds to order by --> |limit= <!-- corresponds to limit --> |format= <!-- template (default) or gallery --> |template= <!-- pagename of template (required). remember to name your args! --> |mode= <!-- traditional (default), nolines, packed, packed-overlay, packed-hover or slideshow --> |intro= |outro= |delimiter= |default= }}
-- <nowiki>
local PARAM_LOOKUP = {
['order by'] = 'orderBy',
['join on'] = 'join',
['group by'] = 'groupBy',
table = 'tables',
}
local h = {}
local p = {}
function p.main(frame)
local args = h.merge()
local query = {}
for k, v in pairs(args) do
if string.sub(k, 1, 2) == 'q?' then
local key = string.sub(k, 3)
query[PARAM_LOOKUP[key] or key] = v
elseif PARAM_LOOKUP[k] then
query[PARAM_LOOKUP[k]] = v
else
query[k] = v
end
end
local result = mw.ext.cargo.query(query.tables, query.fields, query)
if #result == 0 then
return frame:preprocess(args.default or '')
end
if (args.format or ''):lower() == 'gallery' then
local mode = args.mode or 'traditional'
return h.makeGallery(result, mode) -- gallery path ends here
end -- format is not gallery, so template stuff below
local tbl = {}
for i, row in ipairs(result) do
row.index = i
tbl[#tbl+1] = frame:expandTemplate{ title = args.template, args = row }
end
local intro = frame:preprocess(args.intro or '')
local outro = frame:preprocess(args.outro or '')
return intro .. table.concat(tbl,args.delimiter or '') .. outro
end
-- gallery format
-- expects to be given up to two fields per row
-- if one is called `caption` that will be the caption
-- other param (or only param) will be the image
function h.makeGallery(result, mode)
local strs = {}
for _, row in ipairs(result) do
local caption
local image
for k, v in pairs(row) do
if k == 'caption' then
caption = v
else
image = v
end
end
if caption then
strs[#strs+1] = ('%s|%s'):format(image, caption)
else
strs[#strs+1] = image
end
end
return mw.getCurrentFrame():callParserFunction{
name = '#tag:gallery',
args = {'\n' .. table.concat(strs, '\n'), mode = mode}
}
end
-- generic arg handler is bundled to avoid dependencies
function h.merge()
local f = mw.getCurrentFrame()
local origArgs = f.args
local parentArgs = f:getParent().args
local args = {}
for k, v in pairs(origArgs) do
v = mw.text.trim(tostring(v))
if v ~= '' then
args[k] = v
end
end
for k, v in pairs(parentArgs) do
v = mw.text.trim(v)
if v ~= '' then
args[k] = v
end
end
return args
end
return p
-- </nowiki>