Hearthstone Wiki
Register
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 4: Line 4:
 
return tostring(
 
return tostring(
 
mw.html.create('strong')
 
mw.html.create('strong')
  +
:addClass('error')
 
:wikitext('Cargo Query Error: ', msg)
 
:wikitext('Cargo Query Error: ', msg)
 
:done()
 
:done()
  +
) .. '[[Category:Pages with cargo query errors]]'
)
 
 
end
 
end
   

Latest revision as of 17:17, 15 August 2021

The query function of this module is an alternative to #cargo_query with format=template. It should not be used as an alternative to #cargo_query for any other format.

The latter is subject to an intermittent bug that causes output to appear as unrendered HTML requiring a purge to correct. The query function avoids the bug by processing the template transclusion in Lua after running the query through Cargo, rather than having Cargo perform both steps.

Usage is similar to a #cargo_query call:

{{#invoke:cargo|query
  |tables=
  |fields=
  |where=
  |join=
  |groupBy=
  |having=
  |orderBy=
  |template=
  |intro=
  |outro=
  |default=
  |limit=
  |offset=
}}

See Extension:Cargo/Querying data for more information, as the parameters here have the same usage as in #cargo_query with the following exceptions:

  • table cannot be used as an alternative to tables
  • join is used instead of join on
  • groupBy is used instead of group by
  • orderBy is used instead of order by
  • This function always behaves as if named args is yes

local p = {}

function p.makeError(msg)
	return tostring(
		mw.html.create('strong')
			:addClass('error')
			:wikitext('Cargo Query Error: ', msg)
		:done()
	) .. '[[Category:Pages with cargo query errors]]'
end

function p.query( frame )
    local success, results = pcall(mw.ext.cargo.query, frame.args.tables, frame.args.fields, {
        where = frame.args.where,
        join = frame.args.join,
        groupBy = frame.args.groupBy,
        having = frame.args.having,
        orderBy = frame.args.orderBy,
        limit = frame.args.limit,
        offset = frame.args.offset,
    } )
    
    if not success then 
    	return p.makeError(results)
    end
    
    local out
    if #results > 0 then
    	out = frame.args.intro or ''
        for index = 1, #results, 1 do
            out = out .. '\n' .. frame:expandTemplate{ title = frame.args.template, args = results[index] }
        end
    	out = out .. '\n' .. ( frame.args.outro or '' )
    else
        out = frame.args.default or ''
    end

    if frame.args.debug == "yes" then
        out = out .. "\n----\n" .. frame.args.where .. "\n----\nResults: " .. #results
    end
    	
    return out
end
	
return p