Module:CargoQuery: Difference between revisions

no edit summary
mNo edit summary
No edit summary
 
(45 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local util_args = require('Module:Args Utility')
local process_args = require('Module:ProcessArgs')
local PARAM_LOOKUP = {
local PARAM_LOOKUP = {
['order by'] = 'orderBy',
['order by'] = 'orderBy',
Line 14: Line 11:
function p.main(frame)
function p.main(frame)
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)
result = p.query(args)
 
if (result == args.default or result == '') then
        local tbl = {}
return result
for i, row in ipairs(result) do
end
row.index = i
local tbl = {}
tbl[#tbl+1] = frame:expandTemplate{ title = args.template, args = row }
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
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


function p.query(args)
function p.query(args)
  local frame = mw.getCurrentFrame()
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 57: Line 77:


-- 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 153: Line 174:


function p.compound(frame)
function p.compound(frame)
        args = process_args.merge(true)
local util_args = require('Module:Args Utility')
        local splitargs = {}
local util_table = require('Module:Table Utility')
for a, b in ipairs(args) do
    splitargs[a] = util_args.splitNamedArgs(b, '%s*;%s*')
end


local result = {}
local args = h.merge(false)
for a, b in ipairs(args) do
    result[a] = p.query(splitargs[a])
end


local tbl = {}
template = frame:preprocess(args['template'] or '')
for i, row in ipairs(result[1]) do
if template == nil or template == '' then
row.index = i
    template='single query result'
tbl[#tbl+1] = frame:expandTemplate{ title = args['template'], args = row }
end
end
delimiter = frame:preprocess(args['delimiter'] or '')
metasep = frame:preprocess(args['metaseparator'] or ';')


part1 = table.concat(tbl,args['delimiter'] or '')
local splitargs = {}
local tbl2 = {}
for a, b in ipairs(args) do
    splitargs[a] = util_args.splitNamedArgs(frame:preprocess(b), '%s*' .. metasep .. '%s*')
end
splitargs['default'] = ''


for j, rowj in ipairs(result2) do
local result = {}
row.index = i
for a, b in ipairs(splitargs) do
tbl2[#tbl2+1] = frame:expandTemplate{ title = args['template'], args = row }
    tempresult = p.query(b)
    if tempresult ~= nil and tempresult ~= '' then
        result[#result+1] = tempresult
    end
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 intro = frame:preprocess(args['intro'] or '')
local outro = frame:preprocess(args['outro'] or '')
local outro = frame:preprocess(args['outro'] or '')
return intro .. (part1 or '') ..(#part1 > 0 and (args['delimiter'] or '')) .. table.concat(tbl2, args['delimiter'] or '') .. outro
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