Module:CargoQuery: Difference between revisions

no edit summary
mNo edit summary
No edit summary
 
(42 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 util_table = require('Module:Table Utility')
local PARAM_LOOKUP = {
local PARAM_LOOKUP = {
['order by'] = 'orderBy',
['order by'] = 'orderBy',
Line 15: 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 58: 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 154: 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(frame:preprocess(b), '%s*;%s*')
end


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


resulttbl = util_table.merge(result[1], result[2])
template = frame:preprocess(args['template'] or '')
local tbl = {}
if template == nil or template == '' then
    template='single query result'
end
delimiter = frame:preprocess(args['delimiter'] or '')
metasep = frame:preprocess(args['metaseparator'] or ';')


template = frame:preprocess(args['template'] or '')
local splitargs = {}
delimiter = frame:preprocess(args['delimiter'] or '')
for a, b in ipairs(args) do
 
    splitargs[a] = util_args.splitNamedArgs(frame:preprocess(b), '%s*' .. metasep .. '%s*')
if template == nil or template == '' then
end
    error('A template must be specified!')
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
for i, row in ipairs(resulttbl) do
row.index = i
row.index = i
tbl[#tbl+1] = frame:expandTemplate{ title = args['template'], args = row }
tbl[#tbl+1] = frame:expandTemplate{title = args['template'], args = row}
end
end
         if #tbl == 0 then
         if #tbl == 0 then
Line 185: Line 216:
local outro = frame:preprocess(args['outro'] or '')
local outro = frame:preprocess(args['outro'] or '')
return intro  .. table.concat(tbl, delimiter) .. 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