Leaguepedia | League of Legends Esports Wiki
Advertisement

Edit the documentation or categories for this module.


local tabs = require('Module:AutomatedTabs').main
local util = require('Module:Util')
local util_cargo = require('Module:CargoUtil')
local util_form = require('Module:FormUtil')
local util_dpl = require('Module:DPLUtil')
local util_table = require('Module:TableUtil')

--[[ json structure:
	{
		links = a, b, c, d,
		names = ,
		a = { links = , names = }, b = { links = , names = }
		names table is added only after construction of links table
		construction is recursive
	}
]]
local p = {}

function p.getPageList(frame, title, fulltitle)
	local pages = frame:callParserFunction{name = '#dpl', args = {
		'',
		titleregexp = '^' .. util_dpl.escape(title) .. '/.*',
		format = ',%PAGE%,;,',
		namespace = '',
		debug = 0,
		notuses = 'Template:NoDPLTabs',
		skipthispage = 'no'
	}}
	if pages == '' and title == fulltitle then return nil end
	local pgtbl = mw.text.split(pages,';')
	p.processPageTable(pgtbl)
	return pgtbl
end

function p.processPageTable(pgtbl)
	for k, v in ipairs(pgtbl) do
		if not k or k == '' then
			table.remove(pgtbl,k)
		else
			pgtbl[k] = mw.text.split(v,'/')
			table.remove(pgtbl[k],1) -- get rid of the base page
		end
	end
	return
end

function p.jsonFromPages(pages, tabstype)
	local data = mw.loadData('Module:SubpageSettings')[tabstype]
	local json = { links = { 'Overview' } }
	p.jsonLoop(json, pages)
	p.processJson(json, data.lookup, data.order, '')
	return json
end

function p.jsonLoop(json, pages)
	-- go through all of the pages from our dpl list, and for each page add its data to the json
	while #pages > 0 do
		local page = table.remove(pages,1)
		p.pageRecursion(json, page)
		p.addMissingPages(json)
	end
	return
end

function p.pageRecursion(json, page)
	-- the page should always exist, but when we reach the end of our recursion for this one page, #page will be 0
	if not page or #page == 0 then
		return
	end
	-- pull out the first titlepart of the page, and the rest of the page will be dealt with later when we call this again
	titlepart = table.remove(page, 1)
	if #page > 0 then
		-- if we dont have an entry at this level yet
		if not json[titlepart] then
			-- assume the 1st thing is going to be called Overview
			json[titlepart] = { links = { 'Overview' } }
		end
		-- recursion
		-- json[titlepart] is one step later, and page has been truncated
		p.pageRecursion(json[titlepart],page)
	elseif #page == 0 then
		-- if we are on the very last part of the page then just append the current page to the list
		json.links[#json.links+1] = titlepart
	end
	return
end

function p.addMissingPages(json)
	-- if there's no overview for statistics for example, we still want to print the stats
	for k, v in pairs(json) do
		-- actually pretty sure everything in the json is a table but we'll just double check to avoid errors
		-- want to skip this for links obviously
		if type(v) == 'table' and k ~= 'links' then
			if not util_table.keyOf(json.links, k) then
				-- k is the set of sub-subpages, so if there's no subpage of this then we want to add it
				json.links[#json.links+1] = k
				-- also in this case that means there's no overview. because we made an overview by default
				-- we now need to remove it from the list of sub-subpages
				if v.links[1] then
					table.remove(v.links,1)
				end
			end
			p.addMissingPages(v)
		end
	end
end

function p.processJson(json, lookup, order, index)
	p.processJsonLinks(json, order)
	-- add the name list
	json.names = {}
	for k, link in ipairs(json.links) do
		json.names[k] = lookup[index .. link] or link
	end
	-- recursion
	for k, v in pairs(json) do
		if v.links then
			p.processJson(v, lookup, order[k] or {}, k)
		end
	end
	return
end

function p.processJsonLinks(json, order)
	-- add any links that we expect to be missing based on the child tab row not having any overview page
	-- but only add if there actually is a child tab row
	if order.children_nooverview then
		for k, v in ipairs(order.children_nooverview) do
			if json[v] then
				json.links[#json.links+1] = v
			end
		end
	end
	-- now sort links
	util_table.sortByKeyOrder(json.links, order)
	-- remove overview if we don't have an overview, since json creation automatically adds one
	if order.nooverview then
		table.remove(json.links,1)
	end
	return
end

function p.linkAdjustments(json, title, tabstype)
	local tbl = { fr = { find = {}, replace = {} }, cd = {} }
	local data = mw.loadData('Module:SubpageSettings')[tabstype].order
	p.linkAdjustmentsRecursion(json, title, data, tbl)
	return tbl
end

function p.linkAdjustmentsRecursion(json, title, data, tbl)
	for _, v in ipairs(json.links) do
		if json[v] and json[v].links and data[v] then
			if data[v .. '_linkrecent'] then
				new_title = util.concatSubpage(title, v)
				tbl.cd[new_title] = util.concatSubpage(new_title, json[v].links[#json[v].links])
			end
			p.linkAdjustmentsRecursion(json[v], new_title, data[v], tbl)
		end
	end
	return
end

-- navbox stuff
function p.navboxArgs(tabstype, title)
	if tabstype == 'Champion' or tabstype == 'Item' then
		return nil
	end
	local team = (tabstype == 'Team') and title or p.getTeamFromPlayer(title)
	local orgnavbox = p.orgNavboxArgs(team)
	local navboxes = { orgnavbox }
	if tabstype == 'Team' then
		table.insert(navboxes,1,p.cplnArgs(team))
	end
	return navboxes
end

function p.orgNavboxArgs(team)
	return {
		title = 'OrgNavbox',
		events = { showAll = true },
		args = { team, suffix = 'Yes' },
	}
end

function p.cplnArgs(team)
	return {
		title = 'CurrentLeagueParticipantNavbox',
		events = { showAll = true },
		args = { team, suffix = 'Yes' },
	}
end

function p.getTeamFromPlayer(title)
	local where = string.format('_pageName="%s"', title)
	return util_cargo.getOneResult('InfoboxPlayer', 'Team', {where = where})
end

function p.after(frame, fulltitle, tabstype)
	local titletbl = mw.text.split(fulltitle, '/')
	local text = {}
	if tabstype == 'Player' then
		p.after_player(text, frame, titletbl)
	elseif tabstype == 'Team' then
		p.after_team(text, frame, titletbl)
	end
	if not mw.title.makeTitle('',titletbl[1]).exists then
		text[#text+1] = '[[Category:Player Secondary Pages Without Primary Pages]]'
	end
	local output = table.concat(text)
	return output
end

function p.after_player(text, frame, titletbl)
	if titletbl[2] == 'Statistics' and titletbl[3] then
		text[#text+1] = p.playerConcept(titletbl) or ''
	end
	if titletbl[2] then
		if p.doWeLowercase('InfoboxPlayer', titletbl[1]) then
			text[#text+1] = frame:expandTemplate({ title = 'lowercase' })
		end
	end
	return
end

function p.playerConcept(titletbl)
	local player = titletbl[1]
	local year = titletbl[3]
	local concept = mw.title.makeTitle('Concept', player .. '/Games')
	if not concept.exists then
		return nil
	end
	local formlink = util_form.makeBaseQueryURL('CareerPlayerStats', 'CPS', { 'player', 'year'}, { player, year })
	return string.format("\n:''To see all stats for %s not separated by tournament, <span class=\"plainlinks\">[%s Click Here]</span>.",
		year,
		formlink.full
		)
end

function p.after_team(text, frame, titletbl)
	if titletbl[2] then
		if p.doWeLowercase('InfoboxTeam', titletbl[1]) then
			text[#text+1] = frame:expandTemplate({ title = 'lowercase' })
		end
	end
	return
end

function p.doWeLowercase(tbl, title)
	local where = string.format('_pageName="%s"',title)
	return util_cargo.getOneResult(tbl,'IsLowercase', {where = where}) == 'Yes'
end

function p.main(frame)
	if frame == mw.getCurrentFrame() then
		args = require('Module:ProcessArgs').merge(true)
	else
		frame = mw.getCurrentFrame()
	end
	local tabstype = args[1] or 'Player'
	local title_obj = mw.title.getCurrentTitle()
	local fulltitle = title_obj.prefixedText
	local title = tostring(title_obj.rootPageTitle)
	local pages = p.getPageList(frame, title, fulltitle)
	local json = pages and p.jsonFromPages(pages, tabstype)
	local linkadjustments = json and json.links and p.linkAdjustments(json, title, tabstype)
	local navbox = (args.nonavbox ~= 'Yes') and p.navboxArgs(tabstype, title)
	local after = title_obj.namespace == 0 and p.after(frame, fulltitle,tabstype)
	return tabs(json, linkadjustments, navbox, nil, title, after)
end
return p
Advertisement