Module:CargoQuery: Difference between revisions

Jump to navigation Jump to search
no edit summary
mNo edit summary
No edit summary
 
(15 intermediate revisions by 2 users not shown)
Line 14: Line 14:
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 '')
local template = frame:preprocess(args.template or 'single query result')
local delimiter = args.delimiter or ''
local delimiter = args.delimiter or ''
Line 19: Line 20:
local format = args.format or nil
local format = args.format or nil
local query = {}
result = p.query(args)
for k, v in pairs(args) do
if (result == args.default or result == '') then
if string.sub(k, 0, 2) == 'q?' then
return result
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
if args.one_to_many then
query.fields = query.fields .. ',' .. query.one_to_many
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.one_to_many then
result = h.groupOneToManyFields(result, h.getOneToManyTableFromArgs(args))
h.concatOneToManyFieldsInEachRow(result, args.one_to_many_sep or ',')
end
end
local tbl = {}
local tbl = {}
Line 49: Line 30:
for i, row in ipairs(result) do
for i, row in ipairs(result) do
row.index = i
row.index = i
tbl[#tbl+1] = frame:expandTemplate{ title = args.template, args = row }
tbl[#tbl+1] = frame:expandTemplate{ title = template, args = row }
end
end
if colcount ~= nil then
if colcount ~= nil then
Line 200: Line 181:
template = frame:preprocess(args['template'] or '')
template = frame:preprocess(args['template'] or '')
if template == nil or template == '' then
if template == nil or template == '' then
     error('A template must be specified!')
     template='single query result'
end
end
delimiter = frame:preprocess(args['delimiter'] or '')
delimiter = frame:preprocess(args['delimiter'] or '')
Line 216: Line 197:
     tempresult = p.query(b)
     tempresult = p.query(b)
     if tempresult ~= nil and tempresult ~= '' then
     if tempresult ~= nil and tempresult ~= '' then
         result[#result+1] = p.query(b)
         result[#result+1] = tempresult
     end
     end
end
end
if result == args.default or result == '' then
return result
end
resulttbl = util_table.mergeArrays(nil, unpack(result))
resulttbl = util_table.mergeArrays(nil, unpack(result))
local tbl = {}
local tbl = {}
Line 233: Line 218:
end
end


function getColNames(row)
function h.getColNames(row)
cols = {}
cols = {}
for colname, _ in pairs(row)do
for colname, _ in pairs(row)do
Line 245: Line 230:
     local tbl = mw.html.create('table')
     local tbl = mw.html.create('table')
         :addClass('wikitable')
         :addClass('wikitable')
     colNames = getColNames(result)
     colNames = h.getColNames(result[1])
     util_html.printHeader(tbl, colNames)
     util_html.printHeader(tbl, colNames)
     util_html.printRowsByList(tbl, result, colNames)
     util_html.printRowsByList(tbl, result, colNames)
     return result
     return tbl
end
end


return p
return p

Navigation menu