Module:ExternalContentQueryAbstract

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 Class = require('Module:Class').Class

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

local YEAR

local h = {}

local p = Class

function p:main(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 = h.getTables(args), fields = h.getFields, join = h.getJoin(args), where = h.getWhere(args), orderBy = 'EC.Date ASC', limit = 9999, types = { SeasonN = 'number', },	}	return ret end

function h.getTables(args) local tbl = { 'ExternalContent=EC', args.player and 'PlayerRedirects=PR' }	return util_table.removeFalseEntries(tbl) end

function h.getFields return { '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', } end

function h.getJoin(args) if not args.player then return nil end return 'EC.Players HOLDS PR.AllName' end

function h.getWhere(args) local tbl = { util_cargo.whereFromArg('EC.Publication="%s"', args.publication), h.getTournamentWhere(args), util_cargo.whereFromArg('PR._pageName = "%s"', util_title.target(args.player)), args.player and 'EC.Players__full IS NOT NULL', util_cargo.whereFromArg('EC.Teams HOLDS "%s"', args.team), }	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.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, 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') :addClass('hoverable-rows') 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) 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