Halo Esports Wiki
Advertisement

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

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_table = require("Module:TableUtil")
local util_text = require("Module:TextUtil")
local util_title = require("Module:TitleUtil")
local util_vars = require("Module:VarsUtil")
local i18n = require('Module:i18nUtil')

local m_team = require('Module:Team')

local NewsQueryAbstract = require('Module:NewsQueryAbstract')
local NewsQuery = NewsQueryAbstract:extends()

local PRELOADS_TO_IGNORE = {
	
}

local lang = mw.getLanguage('en')

LIMIT = 9999
MIN_DAYS = 9999
LINES_COUNTER = 0
DAY_COUNTER = 0
local SETTINGS = require('Module:NewsQueryFrontpage/Settings')

local h = {}

local p = {}

function p.main(frame)
	local args = util_args.merge()
	i18n.init('NewsQueryFrontpage', 'NewsQuery')
	h.setPreload(args)
	return NewsQuery(args):run()
end

function h.setPreload(args)
	if not args.preload then
		error(i18n.print('errorMissingPreload'))
	end
	if not SETTINGS[args.preload:lower()] then
		error(i18n.print('errorInvalidPreload'))
	end
	SETTINGS = SETTINGS[args.preload:lower()]
end

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

function NewsQuery:makeOutput(byDate, args)
	local output = mw.html.create()
	h.printIntro(output, args)
	local tbl = output:tag('table')
		:addClass('news-table')
	for _, date in ipairs(byDate) do
		util_vars.setVar('currentDate', date)
		h.printDate(tbl, byDate[date])
		h.printDateContent(tbl, byDate[date])
		if LINES_COUNTER + 1 >= LIMIT and DAY_COUNTER > MIN_DAYS then
			break
		end
	end
	return output
end

function NewsQuery:getQuery(args)
	local query = self:super('getQuery', args)
	local new = {
		where = self:getWhere(args),
	}
	return util_cargo.concatQueriesAnd(query, new)
end

function NewsQuery:getWhere(args)
	local tbl = {
		util_cargo.whereFromArg('News.Region="%s"', args.region),
		util_news.getExcludedNewsPreloadsWhereCondition(self.PRELOADS_TO_IGNORE),
	}
	util_table.mergeArrays(
		tbl,
		h.getExclusionWhereConditions(args),
		h.getDateWhereConditionsFromTitle()
	)
	return util_cargo.concatWhere(tbl)
end

function h.getExclusionWhereConditions(args)
	return SETTINGS.where
end

function h.getDateWhereConditionsFromTitle()
	local startDate = h.getStartDateFromTitle()
	if not startDate then return {} end
	local startTimestamp
	local function assignTimestamp(date)
		startTimestamp = lang:formatDate('Y-m-d', startDate)
	end
	if not pcall(assignTimestamp, startDate) then
		error(i18n.print('error_wrongTitle'))
	end
	local ret = {
		('Date_Sort >= "%s"'):format(startTimestamp),
		('Date_Sort <= "%s"'):format(
			lang:formatDate('Y-m-d', h.getMonthEndDateFromStartDate(startDate))
		)
	}
	return ret
end

function h.getStartDateFromTitle()
	local title = mw.title.getCurrentTitle().text
	if util_title.titleparts(title, 1, 3) == '' then return nil end
	return ('1 %s %s'):format(
		util_title.titleparts(title,1, 3),
		util_title.titleparts(title,1, 2)
	)
end

function h.getMonthEndDateFromStartDate(startDate)
	return ('%s + 1 month - 1 day'):format(startDate)
end

function h.printIntro(output, args)
	return
end

function h.printDate(tbl, dateData)
	LINES_COUNTER = LINES_COUNTER + 1
	DAY_COUNTER = DAY_COUNTER + 1
	if DAY_COUNTER > MIN_DAYS and LINES_COUNTER >= LIMIT then return end
	local th = tbl:tag('tr'):tag('th')
		:attr('colspan', #SETTINGS.columns)
		:wikitext(dateData.date)
	h.printEditButton(th, dateData.page, dateData.date)
end

function h.printEditButton(th, page, date)
	if not page then return end
	th:tag('div')
		:addClass('news-edit-button')
		:addClass('logged-in-link')
		:wikitext(i18n.print('edit'))
		:attr('data-href', h.pageWithAnchor(page, date))
end

function h.pageWithAnchor(page, date)
	return ('%s#%s'):format(page, date)
end

function h.printDateContent(tbl, lines)
	for _, line in ipairs(lines) do
		util_vars.setVar('currentRow', line.NewsId)
		LINES_COUNTER = LINES_COUNTER + 1
		if LINES_COUNTER > LIMIT and DAY_COUNTER > MIN_DAYS then break end
		h.printLine(tbl, line)
	end
end

function h.printLine(tbl, line)
	util_html.printRowByList(tbl, line, SETTINGS.columns)
end

return p
Advertisement