Module:CargoQuery: Difference between revisions

no edit summary
(update to most recent version in leaguepedia (one to many support))
No edit summary
 
(53 intermediate revisions by 2 users not shown)
Line 12: Line 12:
local args = h.merge()
local args = h.merge()
local intro = frame:preprocess(args.intro or '')
local outro = frame:preprocess(args.outro or '')
local template = frame:preprocess(args.template or 'single query result')
local delimiter = args.delimiter or ''
local colcount = args.columns or nil
local format = args.format or nil
result = p.query(args)
if (result == args.default or result == '') then
return result
end
local tbl = {}
if format == "table" then
return (intro .. tostring(h.makeTable(result)) .. outro)
else
for i, row in ipairs(result) do
row.index = i
tbl[#tbl+1] = frame:expandTemplate{ title = template, args = row }
end
if colcount ~= nil then
intro = (intro .. '<div style="-webkit-column-count:' ..
    colcount .. '; -moz-column-count:' ..
colcount .. '; column-count:' .. colcount .. ';">')
outro = '</div>' ..outro
end
if format == "ul" then
intro = intro .. "<ul><li>"
outro = "</ul>" .. outro
delimiter = "<li>"
end
return intro .. table.concat(tbl, delimiter) .. outro
end
end
function p.query(args)
local frame = mw.getCurrentFrame()
local query = {}
local query = {}
for k, v in pairs(args) do
for k, v in pairs(args) do
if string.sub(k, 0, 2) == 'q?' then
if string.sub(k, 0, 2) == 'q?' then
local key = string.sub(k, 3)
local key = string.sub(k, 3)
query[PARAM_LOOKUP[key] or key] = v
query[PARAM_LOOKUP[key] or key] = v
elseif PARAM_LOOKUP[k] then
elseif PARAM_LOOKUP[k] then
query[PARAM_LOOKUP[k]] = v
query[PARAM_LOOKUP[k]] = v
Line 36: Line 73:
h.concatOneToManyFieldsInEachRow(result, args.one_to_many_sep or ',')
h.concatOneToManyFieldsInEachRow(result, args.one_to_many_sep or ',')
end
end
local tbl = {}
return result
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
end


-- This function on Leaguepedia is part of Module:ArgsUtil but is copied here to avoid dependencies
-- This function on Leaguepedia is part of Module:ArgsUtil but is copied here to avoid dependencies
function h.merge()
function h.merge(mergeParent)
    mergeParent = mergeParent or true
local f = mw.getCurrentFrame()
local f = mw.getCurrentFrame()
local origArgs = f.args
local origArgs = f.args
local parentArgs = f:getParent().args


local args = {}
local args = {}
if mergeParent then
local parentArgs = f:getParent().args
    for k, v in pairs(parentArgs) do
v = mw.text.trim(tostring(v))
if v ~= '' then
args[k] = v
end
    end
end
for k, v in pairs(origArgs) do
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)
v = mw.text.trim(v)
if v ~= '' then
if v ~= '' then
Line 141: Line 171:
end
end
end
end
end
function p.compound(frame)
local util_args = require('Module:Args Utility')
local util_table = require('Module:Table Utility')
local args = h.merge(false)
template = frame:preprocess(args['template'] or '')
if template == nil or template == '' then
    template='single query result'
end
delimiter = frame:preprocess(args['delimiter'] or '')
metasep = frame:preprocess(args['metaseparator'] or ';')
local splitargs = {}
for a, b in ipairs(args) do
    splitargs[a] = util_args.splitNamedArgs(frame:preprocess(b), '%s*' .. metasep .. '%s*')
end
splitargs['default'] = ''
local result = {}
for a, b in ipairs(splitargs) do
    tempresult = p.query(b)
    if tempresult ~= nil and tempresult ~= '' then
        result[#result+1] = tempresult
    end
end
if result == args.default or result == '' then
return result
end
resulttbl = util_table.mergeArrays(nil, unpack(result))
local tbl = {}
for i, row in ipairs(resulttbl) do
row.index = i
tbl[#tbl+1] = frame:expandTemplate{title = args['template'], args = row}
end
        if #tbl == 0 then
          return frame:preprocess(args['default'] or 'no results')
        end
local intro = frame:preprocess(args['intro'] or '')
local outro = frame:preprocess(args['outro'] or '')
return intro  .. table.concat(tbl, delimiter) .. outro
end
function h.getColNames(row)
cols = {}
for colname, _ in pairs(row)do
table.insert(cols, colname)
end
return cols
end
function h.makeTable(result)
local util_html = require'Module:HTML Utility'
    local tbl = mw.html.create('table')
        :addClass('wikitable')
    colNames = h.getColNames(result[1])
    util_html.printHeader(tbl, colNames)
    util_html.printRowsByList(tbl, result, colNames)
    return tbl
end
end


return p
return p