Leaguepedia | League of Legends Esports Wiki
Advertisement
Leaguepedia | League of Legends Esports Wiki

This module lets you get around the |no html bug that Cargo has by avoiding |format=template. This module implicitly assumed you ARE using named args in your template (corresponding to |named_args=yes; you do not need to specify this.)

Unlike |format=template, this wrapper will NOT rename parameters with underscores in them to use spaces instead.

Parameters[]

  • You may specify all parameters the same as if they were parameters in #cargo_query.

Example[]

{{#invoke:CargoQuery|main
 |table=Teams,Tenures=Ten
 |join on=Teams._pageName=Ten.Team
 |where=Teams.Region="Europe" AND Ten._pageName IS NOT NULL AND Ten.IsCurrent = "1"
 |fields=Teams._pageName=Page

 |template=User:RheingoldRiver/cargo query example/template
 |intro={{(!}} class="wikitable"
 |outro={{!)}}

 |limit=100
}}

One-To-Many[]

!!! This is OPTIONAL functionality. You can ignore this entire section !!!

This template allows for one-to-many support, allowing you to specify a field to treat equivalently to a List of ... field. Syntax is as follows:

  • |one_to_many=, a list of fields you want concatenated together.
  • |one_to_many_group=, the thing you want our one-to-many values grouped by. UNLIKE the |group by= parameter, this should be the FINAL name of a field, so something on the RIGHT SIDE of the equals sign in your |fields= parameter.
  • |one_to_many_sep=, defaults to comma. Currently you must pick the same separator for all of your one-to-many fields. \s can be used to specify a space.

Example[]

{{#invoke:CargoQuery|main
 |table=Teams,Tenures=Ten
 |join on=Teams._pageName=Ten.Team
 |where=Teams.Region="Europe" AND Ten._pageName IS NOT NULL AND Ten.IsCurrent = "1"
 |fields=Teams._pageName=Page

 |one_to_many=Ten.Player=IDs
 |one_to_many_group=Page
 |one_to_many_sep=,

 |template=User:RheingoldRiver/cargo query example/template
 |intro={{(!}} class="wikitable"
 |outro={{!)}}

 |limit=100
}}

Here, the one_to_many is a list of players on the team in question. For each ONE team, there are MANY players, so the field IDs that's sent to the template is a comma-separated list of players, grouped by team page.

404 Multigaming e.V. Akre,Bravado,Dehaste,Gabbo,Reclamation,Riku (Leon Ali)
7more7 Pompa Team Hajima
7more7 Pompa Team Academy Maquk,Oran
Absolved Apples,April (Jakub Kupisz),Dont Ban Nida,FireVortex,Jaeger,Khantos,Nash (Alf-Kristian Sund),Rames,reje,Sahira,Scuffed
Absolved Female Nyjcia
Abyssal Esport Club Borch,ElDestructor,escoX,King (Iñigo Mira),Orthran,ShooLow
ACTINA PACT xHarleen
Ad hoc gaming DeliveryPanda
Adriatic Wolves Haruhiro,Spappolino
Aequilibritas E-Sports DeliveryPanda,StormFury,Stuii
Aethra Esports Boykuh
Aethra Esports Strijders Boykuh,GigCeez,Muska,Rosco,Simptastisch,Yorange,Yung Mus
Afterglow Esports Edru,Febern,Hiponix,Kooovi,Walther
AGO ROGUE Klaj,Lucker,Lurox,Pyrka,Sencux,Sinmivak
ANc Outplayed Anonymouss,bobista,Enryy,imnxtamess,Mauki,Pencil,Shredder,Vyct0r
Anexis eSports eRot1c,Grom
Anorthosis Famagusta Esports Dom1nant,Hades (Nikolaos Zachariadis),J0J0C,Jaxplank,Marin (Michał Baranowski),marlon (Igor Tomczyk),Piwek,Rigas,Sophiesticateed
Anorthosis Famagusta Esports Revolution Faded (Anastasios Koutsouras),Haze (Afonso Maia),Mazi (Bartosz Mazur),Mo (Mohamed Fawzy),Rektless,Rigas
Arctic Dawn Gaming Gupiter
Arctic Gaming Almo,ElOjoNinja,Pirla,PochiPoom,Rydle,Svensson
Arena Quesito BSleeping,Darko,Gaax,Snow (Boris Nicolas)
Aris Esports Cephei,Pepperinos
Astralis AoD,Donby,iHansen,Jeskla,promisq,WhiteKnight,xPeke,Zanzarah
Astralis SB Bolyy1

What does one-to-many mean?[]

If you've made a table with a List of _-type field, you've already made a one-to-many relationship. The object of the table, the entity that all of the columns/fields describe, is the ONE; and each item in the list is the MANY.

But list-type fields only allow for single-attribute MANY objects: if you have |Team= TeamName |Roster= Player1, Player2, Player3 then you can't also put the players' positions into the Roster list. In situations where there's a bunch of different attributes you care about in the MANY, the MANY will have its own table. But given built-in Cargo, this situation is pretty hard to query. So, this template provides an easy interface to accessing one-to-many relationships, which allows you to treat any property from the relationship as if it were a list-of-type field in the parent table.


local PARAM_LOOKUP = {
	['order by'] = 'orderBy',
	['join on'] = 'join',
	['group by'] = 'groupBy',
	table = 'tables',
}

local h = {}

local p = {}
function p.main(frame)
	local args = h.merge()
	args.one_to_many_sep = args.one_to_many_sep and args.one_to_many_sep:gsub('\\s', ' ') or ','
	local query = {}
	for k, v in pairs(args) do
		if string.sub(k, 0, 2) == 'q?' then
			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)
	end
	local tbl = {}
	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

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

	local args = {}
	
	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)
		if v ~= '' then
			args[k] = v
		end
	end
	
	return args
end

function h.getOneToManyTableFromArgs(args)
	local oneToMany = {
		fields = mw.text.split(args.one_to_many, '%s*,%s*'),
		groupBy = { args.one_to_many_group },
	}
	return oneToMany
end

-- These functions on Leaguepedia are part of Module:CargoUtil but are copied here to avoide dependencies
-- Some code is updated to avoid further dependencies
function h.groupOneToManyFields(result, oneToMany)
	if not oneToMany then return result end
	local currentKey
	local groupedResult = {}
	local fields = h.parseFieldsForKeys(oneToMany.fields)
	for _, row in ipairs(result) do
		local newKey = h.getNewKey(row, oneToMany.groupBy)
		if newKey == currentKey then
			h.addRowToExistingGroup(groupedResult, row, fields)
		else
			h.addRowToNewGroup(groupedResult, row, fields)
			currentKey = newKey
		end
	end
	return groupedResult
end

function h.parseFieldsForKeys(fields)
	for i, v in ipairs(fields) do
		fields[i] = h.parseOneFieldForKey(v)
	end
	return fields
end

function h.getNewKey(row, groupBy)
	local toConcat = {}
	for _, v in ipairs(groupBy) do
		toConcat[#toConcat+1] = row[v]
	end
	return table.concat(toConcat)
end

function h.parseOneFieldForKey(str)
	if not str:find('=') then return str end
	return str:match('=(.+)')
end

function h.addRowToExistingGroup(groupedResult, row, fields)
	for _, v in ipairs(fields) do
		local parentRowValues = groupedResult[#groupedResult][v]
		parentRowValues[#parentRowValues+1] = row[v]
	end
end

function h.addRowToNewGroup(groupedResult, row, fields)
	for _, v in ipairs(fields) do
		row[v] = { row[v] }
	end
	groupedResult[#groupedResult+1] = row
end

function h.concatOneToManyFieldsInEachRow(result, sep)
	for _, row in ipairs(result) do
		for k, v in pairs(row) do
			if type(v) == 'table' then
				row[k] = table.concat(v, sep)
			end
		end
	end
end

return p
Advertisement