Module:Media page

From the Tesseract Wiki, the wiki for all things Marvel Cinematic Universe
Jump to navigation Jump to search

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

--<nowiki>

-- IMPORTS
local pt = require('Module:Paramtest')
local hc = pt.has_content

-- exposed table
local p = {}

local lang = mw.getContentLanguage()
local title = mw.title.getCurrentTitle()

local looporder = {
	{ 'December', 31 },
	{ 'November', 30 },
	{ 'October', 31 },
	{ 'September', 30 },
	{ 'August', 31 },
	{ 'July', 31 },
	{ 'June', 30},
	{ 'May', 31 },
	{ 'April', 30 },
	{ 'March', 31 },
	{ 'February', 29 },
	{ 'January', 31 }
}

local hexmonthconv = {
	January = '1',
	February = '2',
	March = '3',
	April = '4',
	May = '5',
	June = '6',
	July = '7',
	August = '8',
	September = '9',
	October = 'A',
	November = 'B',
	December = 'C',
}

function lookup(cat)
	local r = mw.getCurrentFrame():preprocess(string.format([=[
{{#dpl:
|category=%s
|include={Infobox Film}:release,{Infobox Episode}:release
|format=,¦MD¦%%PAGE%%@D@,,
|ordermethod=sortkey
}}
]=], cat))
	local ret = { }
	local keys = {}
	for v in mw.text.gsplit(r, '|MD|', true) do
		if v:find('@') then
			local u,_d = unpack(mw.text.split(v, '@D@'))
			u = mw.text.trim(u)
			u = string.sub(u, 1, -1)
			_d = mw.text.trim(_d)
			local y,m,d = unpack(mw.text.split(lang:formatDate('Y-F-j', _d), '-', true))
			y = tonumber(y)
			d = tonumber(d)
			if not ret[y] then
				ret[y] = {}
			end
			if not ret[y][m] then
				ret[y][m] = {}
			end
			if not ret[y][m][d] then
				ret[y][m][d] = {u}
			else
				table.insert(ret[y][m][d], u)
			end
		end
	end
	return ret
end

function p.date_cat(frame)
	local f = frame:getParent()
	local d
	if hc(f.args[1]) then
		d = f.args[1]
	else
		-- assumes 2016 so that leap years work
		d = string.gsub(title.text, ' ?media?', '') .. ' 2016'
	end
	
	local ret = 'This category contains media released on ' .. lang:formatDate('[[F j]]', d) .. ', sorted chronologically.'
	-- cats only in category namespace
	if title.namespace == 14 then
		--media by day, sorted by hexmonth,day: 54 (May 4)
		ret = ret .. '[[Category:Media by day|' .. lang:formatDate('d', d) .. ']]' .. hexmonthconv[lang:formatDate('F', f)]
		--media by month, sorted by [space]day
		ret = ret .. '[[Category:' .. lang:formatDate('F', d) .. ' media| ' .. lang:formatDate('d', d) .. ']]'
	end
	
	return ret
end

function p.year_cat(frame)
	local f = frame:getParent()
	local d
	if hc(f.args[1]) then
		d = f.args[1]
	else
		d = string.gsub(title.text, ' ?media?', '')
	end
	
	local ret = 'This category contains media posted in [[' .. d .. ']], sorted chronologically.'
	-- cats only in category namespace
	if title.namespace == 14 then
		--media by year, sorted by [space]year
		ret = ret .. '[[Category:Media by year| ' .. d .. ']]'
	end
	
	return ret
end

function p.datedpl(frame)
	local a = frame:getParent().args
	local year = lang:formatDate('Y', a.release)
	local page = a['%TITLE%']
	local fixed_page = page:gsub('%b()', ''):match'^(.*%S)%s*$'
	
	if hc(a.phase) then
		return string.format("'''%s''' – Film: ''[[%s|%s]]''",year,page,fixed_page)
	else
		return string.format("'''%s''' – Episode: \"[[%s|%s]]\"",year,page,fixed_page)
	end
end

function p.medialistdpl(frame)
	local a = frame:getParent().args
	local year = lang:formatDate('Y', a.release)
	local page = a['%TITLE%']
	local fixed_page = page:gsub('%b()', ''):match'^(.*%S)%s*$'

	if hc(a.phase) then
		return string.format("'''%s''' – ''[[%s|%s]]''",year,page,fixed_page)
	else
		return string.format("'''%s''' – \"[[%s|%s]]\"",year,page,fixed_page)
	end
end

function p.year(frame)
	return p._year(frame:getParent().args)
end
function p._year(args)
	local year = args[1] or mw.title.getCurrentTitle().text
	local data = lookup(year..' media')
	data = data[tonumber(year)]
	local mediaQ = mw.smw.ask{ '[[Category:' .. year .. ' media]]', '?#-', limit = 1000}
	local media = {}
	if mediaQ == nil then
		return '\n\'\'No media has been released this year so far. If you believe this is a mistake, leave a message in [[Module talk:Media page|this talk page]].\'\''
	end
	for _,v in ipairs(mediaQ) do
		table.insert(media, v[1])
	end
	local ret = {}
	local data_m, data_d
	for _,m in ipairs(looporder) do
		data_m = data[m[1]]
		if data_m then
			table.insert(ret, '\n\n=='..m[1]..'==')
			for d = m[2], 1, -1 do
				data_d = data_m[d]
				if data_d then
					table.insert(ret, string.format('\n* [[%s %s]]', m[1], tostring(d)))
					table.sort(data_d)
					for _,u in ipairs(data_d) do
						fixed_u = u:gsub('%b()', ''):match'^(.*%S)%s*$'
						local mediaBullet = string.format('\n** [[%s|%s]]', u, fixed_u)
						table.insert(ret, mediaBullet)
					end
				end
			end
		end
	end
	return mw.text.trim(table.concat(ret, ''))
end

return p