Call of Duty Esports Wiki
Register
Advertisement

Documentation for this module may be created at Module:ExternalContentQueryAbstract/doc

local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_esports = require('Module:EsportsUtil')
local util_game = require('Module:GameUtil')
local util_html = require('Module:HtmlUtil')
local util_map = require('Module:MapUtil')
local util_news = require("Module:NewsUtil")
local util_table = require('Module:TableUtil')
local util_text = require('Module:TextUtil')
local util_time = require('Module:TimeUtil')
local util_title = require('Module:TitleUtil')
local util_vars = require('Module:VarsUtil')
local i18n = require('Module:i18nUtil')

local LCS = require('Module:LuaClassSystem')

local TabsDynamic = require('Module:TabsDynamic').constructor

local YEAR

local h = {}

local p = LCS.class.abstract()

function p:init()
	i18n.init('ExternalContentQuery')
end

function p:run(args)
	h.setYear(args.year)
	local data = self:makeAndRunQuery(args)
	util_map.rowsInPlace(data, h.castDataRow)
	local dataByTab = self:groupDataByTab(data, args)
	return h.makeOutput(dataByTab, args)
end

function h.setYear(year)
	if year then
		YEAR = tonumber(year)
		return
	end
	local title = mw.title.getCurrentTitle().text
	for _, y in ipairs(util_game.years) do
		if title:find(y) then
			YEAR = tonumber(y)
			return
		end
	end
end

function p:makeAndRunQuery(args)
	return util_cargo.queryAndCast(self:getQuery(args))
end

function p:getQuery(args)
	local ret = {
		tables = {
			'ExternalContent=EC',
			'ExternalContent__Players=ECP',
			'PlayerRedirects=PR',
			'ExternalContent__Teams=ECT',
			'TeamRedirects=TRed',
			'Teams=Tms',
		},
		join = {
			'EC._ID=ECP._rowID',
			'ECP._value=PR.AllName',
			'EC._ID=ECT._rowID',
			'ECT._value=TRed.AllName',
			'TRed._pageName=Tms._pageName',
		},
		fields = {
			'EC.Title=Title',
			'EC.URL=URL',
			'EC.Date=Date',
			'EC.ContentType=ContentType',
			'EC.Publication=Publication',
			'EC.Authors=Authors',
			'EC.MediaType=MediaType',
			'EC._pageName=_pageName',
			'EC.Series=Series',
			'EC.SeriesSeason=Season',
			'EC.SeriesSeasonNumber=SeasonN [number]',
		},
		where = h.getWhere(args),
		orderBy = 'EC.Date ASC',
		groupBy = 'EC.Date, COALESCE(EC.N_ItemInDate, 1), EC._pageName',
	}
	return ret
end

function h.getWhere(args)
	local tbl = {
		util_cargo.whereFromArg('EC.Publication="%s"', args.publication),
		h.getTournamentWhere(args),
		util_cargo.whereFromArg('PR.OverviewPage = "%s"', util_title.target(args.player)),
		args.player and 'EC.Players__full IS NOT NULL',
		h.getTeamWhereCondition(args),
		util_cargo.whereFromArg('Tms.OrganizationPage="%s"', args.organization),
	}
	return util_cargo.concatWhere(tbl)
end

function h.getTournamentWhere(args)
	if args.type == 'tournament' or args.tournament then
		return util_cargo.whereFromArg('EC.Tournaments HOLDS "%s"', util_esports.getOverviewPage(args.tournament))
	end
	return nil
end

function h.getTeamWhereCondition(args)
	if not args.team then return nil end
	return util_cargo.whereFromArg('TRed._pageName="%s"', args.team:lower())
end

function h.castDataRow(row)
	row.Date = util_time.strToDate(row.Date)
	row.Authors = util_text.split(row.Authors)
	if not row.Title then
		error(('No title for url %s on page %s'):format(row.URL or '<no url provided>', row._pageName))
	end
	row.Link = util_text.extLink(row.URL, mw.text.nowiki(row.Title))
end

function p:groupDataByTab(data, args)
	-- abstract
end

function h.makeOutput(dataByTab, args)
	h.concatTabData(dataByTab)
	util_table.removeFalseEntries(dataByTab, #dataByTab)
	return TabsDynamic(dataByTab, h.getThis(args, dataByTab), args.right)
end

function h.concatTabData(dataByTab)
	for k, tab in ipairs(dataByTab) do
		tab.content = h.makeTabOutput(tab)
		if not tab.content then
			dataByTab[k] = false
		end
	end
end

function h.makeTabOutput(tab)
	if not tab[1] then return end
	local ul = mw.html.create('ul')
		:addClass('external-content')
	local includeYear = h.doWeIncludeYear(tab)
	for _, row in ipairs(tab) do
		local li = ul:tag('li')
		h.printRow(li, row, includeYear)
	end
	return tostring(ul)
end

function h.doWeIncludeYear(tab)
	if tostring(tab.name):match('%d%d%d%d') then return false end
	return not (h.isThisYear(tab[1].Date.year) and h.isThisYear(tab[#tab].Date.year))
end

function h.isThisYear(y)
	return tonumber(y) == YEAR
end

function h.printRow(li, row, includeYear)
	li:wikitext(h.makeSentence(row, includeYear))
	if row.MediaType == 'Video' then
		li:addClass('content-video')
	end
	li:addClass('content-' .. row.ContentType:lower())
	util_news.printEditButton(li, row._pageName)
end

function h.makeSentence(row, includeYear)
	if not row.Link then
		util_vars.log(row)
	end
	return ("%s, %s ''%s''"):format(
		h.makeDate(row, includeYear),
		row.Link,
		h.makeAuthorAndPublication(row)
	)
end

function h.makeDate(row, includeYear)
	if includeYear then
		return util_time.strFromTable(row.Date, 'F j, Y')
	else
		return util_time.strFromTable(row.Date, 'F j')
	end
end

function h.makeAuthorAndPublication(row)
	if not row.Authors then return h.makePublication(row) end
	local tbl = {
		row.ContentType == 'Interview' and 'with' or 'by',
		util_table.printList(row.Authors),
		h.makePublication(row),
	}
	return util_table.concat(tbl, ' ')
end

function h.makePublication(row)
	if not row.Publication then return '' end
	return ('on %s'):format(row.Publication)
end

function h.getThis(args, dataByTab)
	if args.This then
		return tonumber(args.This)
	elseif util_args.castAsBool(args.last) then
		return #dataByTab
	end
	return 1
end

return p
Advertisement