No edit summary |
mNo edit summary |
||
(24 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local p = {} |
local p = {} |
||
− | local cargo = mw.ext.cargo |
+ | local cargo = mw.ext.cargo |
− | local |
+ | local w_frame = require( 'module:frame' ) |
+ | local w_table = require( 'module:table' ) |
||
+ | |||
function p.query( f ) |
function p.query( f ) |
||
− | + | local args = w_frame.args( f ) |
|
− | local query_result = cargo.query( args.tables, args.fields or '', { |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | having = args.having, |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | + | local query_result = cargo.query( args.tables or '', args.fields or '_pageName', { |
|
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | parsed_result = parsed_result .. mw.getCurrentFrame():expandTemplate{ |
||
− | + | having = args.having, |
|
⚫ | |||
⚫ | |||
⚫ | |||
− | } |
||
⚫ | |||
− | end |
||
⚫ | |||
− | elseif format == "list" then |
||
+ | if #query_result == 0 then |
||
⚫ | |||
+ | return args.default |
||
− | parsed_result = parsed_result .. ';' .. table.concat( element, ';' ) |
||
− | + | end |
|
+ | |||
− | parsed_result = parsed_result:sub( 1 ) |
||
+ | if args['unique on'] then |
||
⚫ | |||
+ | query_result = w_table.unique( query_result, args['unique on'] ) |
||
− | parsed_result = '<span style="color: red">[format not yet coded, sorry…]</span>' |
||
− | + | end |
|
⚫ | |||
− | return parsed_result |
||
⚫ | |||
+ | local frame = mw.getCurrentFrame() |
||
⚫ | |||
+ | parsed_result[index] = frame:expandTemplate{ |
||
+ | title = args.template, |
||
⚫ | |||
+ | } |
||
+ | end |
||
⚫ | |||
⚫ | |||
+ | for _, value in pairs( element ) do |
||
+ | parsed_result[index] = value |
||
+ | break |
||
+ | end |
||
+ | end |
||
+ | end |
||
+ | return ( args.intro or '' ) .. table.concat( parsed_result, args.delimiter or '' ) .. ( args.outro or '' ) |
||
end |
end |
||
+ | |||
return p |
return p |
Latest revision as of 17:32, 24 June 2020
This module contains Cargo related functions.
query
[]
Usage[]
{{#invoke: cargo | query
| tables =
| join on =
| fields =
| where =
| group by =
| having =
| order by =
| limit =
| offset =
| unique on =
| template =
| intro =
| outro =
| default =
}}
Description[]
An alternative to the {{#cargo_query: }}
parser function. Differences with the parser function:
default
defaults to an empty string instead ofNo results
.more results text
can not be used, this function does not add any link to additional results.no html
can not be used, as the formatting does not contain additional HTML.max display chars
can not be used, it could be added to this function if necessary.format
can not be used, this function works liketemplate
if thetemplate
parameter is used and likelist
otherwise.named args
can not be used, all arguments are always named.
Parameter | Description | Type | Status | |
---|---|---|---|---|
Unique on | unique on
|
Adds an extra filtering step on the result of the query, before calling the formatting template if any. Should be a column name from the output of the query. | String | optional |
The above documentation is transcluded from Module:Cargo/doc. (edit | history)
local p = {}
local cargo = mw.ext.cargo
local w_frame = require( 'module:frame' )
local w_table = require( 'module:table' )
function p.query( f )
local args = w_frame.args( f )
local query_result = cargo.query( args.tables or '', args.fields or '_pageName', {
join = args['join on'],
where = args.where,
groupBy = args['group by'],
having = args.having,
orderBy = args['order by'] or '_pageName ASC',
limit = args.limit,
offset = args.offset
} )
if #query_result == 0 then
return args.default
end
if args['unique on'] then
query_result = w_table.unique( query_result, args['unique on'] )
end
local parsed_result = {}
if args.template then
local frame = mw.getCurrentFrame()
for index, element in ipairs( query_result ) do
parsed_result[index] = frame:expandTemplate{
title = args.template,
args = w_table.igsub( element, '_', ' ' )
}
end
else
for index, element in ipairs( query_result ) do
for _, value in pairs( element ) do
parsed_result[index] = value
break
end
end
end
return ( args.intro or '' ) .. table.concat( parsed_result, args.delimiter or '' ) .. ( args.outro or '' )
end
return p