Module:NewsQueryTeam

local util_args = require('Module:ArgsUtil') local util_cargo = require("Module:CargoUtil") local util_html = require("Module:HtmlUtil") local util_news = require("Module:NewsUtil") local util_sort = require("Module:SortUtil") local util_source = require("Module:SourceUtil") local util_table = require("Module:TableUtil") local util_text = require("Module:TextUtil") local util_time = require("Module:TimeUtil") local util_vars = require("Module:VarsUtil") local i18n = require('Module:i18nUtil')

local m_team = require('Module:Team') local m_region = require('Module:Region') local m_league = require('Module:League')

local NewsQueryAbstract = require('Module:NewsQueryAbstract') local NewsQuery = NewsQueryAbstract:extends local TabsDynamic = require('Module:TabsDynamic').constructor

local DEBUG = false

local COLUMNS = { 'Region', 'Subject', 'Sentence', classes = { Sentence = 'news-sentence' } }

local PRELOADS_TO_IGNORE = { 'expire_notleave', }

local lang = mw.getLanguage('en')

local h = {}

local p = {}

function p.main(frame) local args = util_args.merge h.setConstants(args) return NewsQuery(args):run end

function h.setConstants(args) DEBUG = util_args.castAsBool(args.debug) end

function NewsQuery:init(args) self:super('init', args) util_table.mergeArrays(self.PRELOADS_TO_IGNORE, PRELOADS_TO_IGNORE) end

function NewsQuery:getQuery(args) local query = self:super('getQuery', args) local new = { tables = { 'TeamRedirects=TR', 'RosterChanges=RC' }, join = h.getJoin, where = self:getWhere(args, query.where), fields = { 'RC.PreloadSortNumber' }, orderBy = 'News.Date_Sort ASC, N_LineInDate ASC, News.Date_Display, News.IsApproxDate', groupBy = 'News.NewsId', types = { PreloadSortNumber = 'number' }, }	return util_cargo.concatQueriesAnd(query, new) end

function h.getJoin local ret = { 'News.Teams HOLDS TR.AllName', 'News.NewsId=RC.NewsId', }	return ret end

function NewsQuery:getWhere(args, where) local tbl = { where, ('TR._pageName="%s"'):format(h.getTeam(args.team)), util_news.getExcludedPreloadsWhereCondition(self.PRELOADS_TO_IGNORE), }	return util_cargo.concatWhere(tbl) end

function h.getTeam(team) if not team then return mw.title.getCurrentTitle.baseText end return m_team.teamlinkname(team) end

function NewsQuery:groupDataByDate(data) local byDate = self:super('groupDataByDate', data) local ret = {} for _, date in ipairs(byDate) do		util_sort.tablesByKeys(			byDate[date],			{ 'N_LineInDate', 'PreloadSortNumber' },			{ true, true }		) util_table.initDict(ret, byDate[date].year) util_table.pushDict(ret[byDate[date].year], date, byDate[date]) end return ret end

function h.getYearContent(byDate) local ul = mw.html.create('ul') :addClass('news-list') :addClass('news-team-list') :addClass('hoverable-rows') for _, date in ipairs(byDate) do		h.printDateAndContent(ul, byDate[date]) end return tostring(ul) end

-- print function NewsQuery:makeOutput(byDate, args) local tabs = {} for _, year in ipairs(byDate) do		tabs[#tabs+1] = { name = year, content = h.getYearContent(byDate[year]) }	end return TabsDynamic(tabs, #tabs) end

function h.printDateAndContent(ul, lines) local li = ul:tag('li') for i, line in ipairs(lines) do		h.printLine(li, i, line) h.printRef(li, line) if i < #lines then li:wikitext(' ') end end util_news.printEditButton(li, lines.page) end

function h.printLine(li, i, line) if i == 1 then li:wikitext(line.SentenceWithDate) else li:wikitext(line.Sentence) end end

function h.printRef(li, line) if DEBUG then util_source.printPopupRef(li, line.Source) return end li:wikitext(util_source.makeRef(line.Source)) end

return p