Module:ExternalContentQuery

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_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 TabsDynamic = require('Module:TabsDynamic').constructor local ExternalContentQueryAbstract = require('Module:ExternalContentQueryAbstract')

local YEAR local SECONDS_IN_DAY = 24 * 60 * 60

local h = {}

local p = {}

function p.main(frame) local args = util_args.merge(true) i18n.init('ExternalContentQuery') return ExternalContentQueryAbstract:main(args) end

function ExternalContentQueryAbstract:groupDataByTab(data, args) local tabs = h.initTabTable(args) h.castTabDates(tabs) h.addDataToTabs(tabs, data) return tabs end

function h.initTabTable(args) if args.dates then return h.initTabTableFromArgs(args) else return h.initDefaultTabTable end end

function h.initTabTableFromArgs(args) local ret = {} local dates = util_text.split(args.dates) local names = util_text.split(args.names) for k, v in ipairs(dates) do		ret[k] = { date = v, name = names[k] } end return ret end

function h.initDefaultTabTable local ret = { { date = '*', name = 'Before 2010' } } for _, v in ipairs(util_game.years) do ret[#ret+1] = { date = v .. '-01-01', name = v } end return ret end

function h.castTabDates(tabs) for _, row in ipairs(tabs) do		if row.date ~= '*' then row.date = util_time.strToDate(row.date) end end end

function h.addDataToTabs(tabs, data) local currentTabIndex = 1 for _, row in ipairs(data) do		currentTabIndex = h.getUpdatedIndex(tabs, row, currentTabIndex) tabs[currentTabIndex][#tabs[currentTabIndex]+1] = row end end

function h.getUpdatedIndex(tabs, row, currentTabIndex) while h.belongsInTab(tabs[currentTabIndex+1], row) do		currentTabIndex = currentTabIndex + 1 end return currentTabIndex end

function h.belongsInTab(tab, row) if not tab then return false end if not row.Date.day or not row.Date.month or not row.Date.year then error(i18n.print('error_incompleteDate'), row.Title) end return os.difftime(os.time(tab.date), os.time(row.Date)) < SECONDS_IN_DAY end

return p