Module:NewsQueryAbstract

From Call of Duty Esports Wiki
Jump to: navigation, search

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

local util_args = require('Module:ArgsUtil')
local util_cargo = require("Module:CargoUtil")
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_vars = require("Module:VarsUtil")
local i18n = require('Module:i18nUtil')

local m_team = require('Module:Team')

local LCS = require('Module:LuaClassSystem').class
local League = require('Module:League')

local lang = mw.getLanguage('en')
DEBUG = false

local h = {}

local p = LCS()

p.SUBJECT_SIZE = 60

function p:init(args)
	self.args = args
	self.PRELOADS_TO_IGNORE = {
		'contract_data_fuzzy',
		'contract_data_removal',
	}
end

function p:run()
	util_vars.setVar('scriptStatus', 'failure')
	i18n.init('NewsQuery')
	self:setConstants(self.args)
	local data = self:getData(self.args)
	self:markupData(data)
	local byDate = self:groupDataByDate(data)
	local output = self:makeOutput(byDate, self.args)
	util_vars.setVar('scriptStatus', 'success')
	return output
end

function p:setConstants(args)
	if util_args.castAsBool(args.debug) then DEBUG = true end
	h.setLimit(args)
end

function h.setLimit(args)
	if args.limit then
		LIMIT = tonumber(args.limit) or -1
	end
	if args.min_days then
		MIN_DAYS = tonumber(args.min_days) or -1
	end
end

-- query
function p:getData(args)
	return util_cargo.queryAndCast(self:getQuery(args))
end

function p:getQuery(args)
	local ret = {
		tables = {
			'NewsItems=News',
		},
		fields = {
			'News.Date_Sort=Date',
			'News.Sentence',
			'News.SentenceWithDate',
			'News.Subject',
			'News.SubjectType',
			'News.SubjectLink',
			'News.Region [region]',
			'News._pageName',
			'News._pageNamespace [namespace]',
			'News.Source',
			'News.IsApproxDate',
			'News.NewsId',
			'News.N_LineInDate',
		},
		where = h.getWhere(args),
		orderBy = 'News.Date_Sort DESC, News.N_LineInDate DESC',
		groupBy = 'News.NewsId',
		limit = args.query_limit or args.limit,
	}
	return ret
end

function h.getWhere(args)
	local tbl = {
		'News.Date_Sort IS NOT NULL',
	}
	return util_cargo.concatWhere(tbl)
end

-- process
function p:markupData(data)
	for _, row in ipairs(data) do
		self:markupRow(row)
	end
end

function p:markupRow(row)
	row.Subject = self:markupSubject(row.Subject, row.SubjectType, row.SubjectLink)
	row.Region = row.Region:image()
	if DEBUG then
		row.SentenceWithDate = row.SentenceWithDate .. util_text.intLinkOrText(row._pageName)
		row.Sentence = row.Sentence .. util_text.intLinkOrText(row._pageName)
	end
end

function p:markupSubject(subject, subjecttype, link)
	if not subject or subject == 'TBD' then
		return m_team.rightshortlinked('free agent', {size=self.SUBJECT_SIZE})
	end
	if subjecttype == 'Team' then
		return m_team.rightshortlinked(subject, {size=self.SUBJECT_SIZE})
	elseif subjecttype == 'Tournament' then
		return h.makeTournamentSubject(subject, link)
	elseif subjecttype == 'Player' then
		return h.makePlayerSubject()
	end
end

function h.makeTournamentSubject(subject, link)
	local league = League(subject)
	local div = mw.html.create('div')
	div:tag('div')
		:addClass('news-league-icon')
		:wikitext(league:imagelink{link = link})
	div:tag('div')
		:addClass('news-league-text')
		:wikitext(league:link{link = link, len = 'short'})
	return tostring(div)
end

function h.makePlayerSubject()
	local wrapper = mw.html.create('div')
		:addClass('news-subject-player-wrapper')
	wrapper:tag('div')
		:addClass('news-subject-player')
	wrapper:tag('div')
		:addClass('news-subject-player-text')
		:wikitext('--')
	return tostring(wrapper)
end

function p:groupDataByDate(data)
	local byDate = {}
	for _, row in ipairs(data) do
		util_vars.setVar('currentRow', row.NewsId)
		local dateKey = self:getDateKey(row)
		util_table.initDict(byDate, dateKey)
		util_table.push(byDate[dateKey], row)
		h.setPagenameIfData(byDate[dateKey], row)
		h.setGroupedProperties(byDate[dateKey], row)
	end
	return byDate
end

function p:getDateKey(row)
	return (row.Date_Display or row.Date) .. tostring(row.IsApproxDate)
end

function h.setPagenameIfData(dateData, row)
	if row._pageNamespace ~= 'Data' then return end
	dateData.page = row._pageName
end

function h.setGroupedProperties(dateData, row)
	dateData.date = util_news.getDisplayDateForSentence(row)
	dateData.year = lang:formatDate('Y', row.Date)
	dateData.region = row.Region
	dateData.subject = row.Subject
end

-- output

function p:makeOutput(byDate, args)
	-- abstract
end

return p