Jump to content

Module:Archives

From Wikisource

require('strict')

local p = {}

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local namespace_other = require('Module:Namespace other')._namespace_other
local InfoboxImage = require('Module:InfoboxImage').InfoboxImage
local archive_list = require('Module:Archive list').main

local function _archives(args)
	local current_frame = mw.getCurrentFrame()
	local current_title = mw.title.getCurrentTitle()
	
	local function expand_sheet(sheet)
		return current_frame:extensionTag('templatestyles', '', {src = sheet})
	end
	
	local style_sheets = table.concat({
		expand_sheet(namespace_other({
			'Module:Message box/tmbox.css',
			'Module:Message box/ombox.css',
			target = 'Talk',
			demospace = args.demospace
		})),
		expand_sheet('Template:Archives/styles.css'),
		expand_sheet('Template:Rule/styles.css')
	})
	
	local title = args.title or 'Archives'
	local index = args.index or 'Archive index'
	local list = args.list or args[1]
	local age = tonumber(args.age) or tonumber(args.archive_age)
	local target = args.target
	
	local banner = yesno(args.banner or args.large) or false
	local collapsed = yesno(args.collapsed) or false
	local collapsible = yesno(args.collapsible) or false
	local search = yesno(args.search) ~= false
	local editbox = yesno(args.editbox) ~= false
	
	local archive_list_page = mw.title.new(args.archivelist or current_title.fullText .. '/archivelist')
	local archive_list_page_exists = archive_list_page.exists
	
	local collapse_class = (collapsed and 'mw-collapsible mw-collapsed') or (collapsible and 'mw-collapsible') or nil
	local archivebox = mw.html.create('table')
		:attr('id', 'archivebox')
		:attr('role', 'presentation')
		:addClass(namespace_other({
			'tmbox tmbox-notice',
			'ombox ombox-notice',
			target = 'Talk',
			demospace = args.demospace
		}))
		:addClass((banner and 'wst-archivebox-banner') or 'mbox-small')
		:addClass(collapse_class)
		:addClass(args.class)
		:css({['width'] = args['box-width'], ['style'] = args['style']})
		:allDone()
	
	local title_tr = archivebox:tag('tr')
	local title_el
	local list_tr
	local list_td
	
	if args.image ~= 'none' then
		local image_td = title_tr:tag('td'):addClass('mbox-text')
		if banner then
			image_td:attr('rowspan', 1 + ((search and 1) or 0) + ((list and 1) or 0) + (((age or target) and 1) or 0))
		end
		local image_div = image_td:tag('div')
			:wikitext(InfoboxImage({args = {
				alt = args.alt,
				link = args.link,
				image = args.image or 'Replacement filing cabinet.svg',
				size = args['image-size'],
				sizedefault = '40px'
			}}))
		
		if not banner then
			image_div:tag('br')
			title_el = image_div
		end
	end
	
	title_el = title_el or title_tr:tag('td')
	if banner then
		title_el:wikitext(title .. ': ')
	else
		title_el:tag('b'):wikitext(title)
	end
	
	if list then
		list_tr = archivebox:tag('tr')
		list_td = list_tr:tag('td')
			:addClass('plainlinks wst-archivelist')
			:newline()
			:wikitext(list)
	else
		if banner then
			list_tr = title_tr
			list_td = title_el
		else
			list_tr = archivebox:tag('tr')
			list_td = list_tr:tag('td'):addClass('plainlinks')
		end
		
		local archive_index = mw.title.new(current_title.fullText .. '/' .. index)
		if archive_index.exists then
			list_td:wikitext('[[' .. archive_index.fullText .. '|Index]]')
		end
		
		if archive_list_page_exists then
			list_td:tag('div')
				:addClass('wst-manual-archivelist')
				:wikitext(current_frame:preprocess('{{' .. archive_list_page.fullText .. '}}'))
		else
			local archive_list_args = {
				['root'] = args.root,
				['nobr'] = banner,
				['prefix'] = args.prefix,
				['start'] = args.start,
				['auto'] = not banner and (args.auto or 'long')
			}
			local archive_list_text = archive_list(archive_list_args)
			list_td:wikitext(' '):wikitext(archive_list_text)
		end
	end
	
	if search then
		local inputbox_args = {
			['bgcolor'] = 'transparent',
			['color'] = '#202122',
			['type'] = 'fulltext',
			['prefix'] = (args.searchprefix or current_title.fullText) .. '/',
			['break'] = args['search-break'],
			['width'] = args['search-width'] or '22',
			['searchbuttonlabel'] = args['search-button-label'] or args['button-label'] or 'Search archives'
		}
		if banner then
			inputbox_args['break'] = inputbox_args['break'] or 'no'
			inputbox_args['width'] = nil
		else
			inputbox_args['break'] = inputbox_args['break'] or 'yes'
		end
		
		-- local inputbox = current_frame:extensionTag('inputbox', '', inputbox_args);
		-- TODO: figure out why this horrible code works and the sensible code doesn't
		local inputbox_args_strings = {}
		for k, v in pairs(inputbox_args) do
			table.insert(inputbox_args_strings, k .. '=' .. v)
		end
		local inputbox = current_frame:preprocess('{{#tag:inputbox|' .. table.concat(inputbox_args_strings, '\n') .. '\n}}')
		archivebox:tag('tr'):tag('td')
			:addClass('wst-searchbox')
			:wikitext(inputbox)
	end
	
	if age or target then
		local info_text_td = archivebox:tag('tr'):tag('td')
			:addClass('wst-archive-info')
			:tag('hr'):addClass('wst-rule wst-rule-center'):done()
		
		local info_text = {}
		
		if target then
			table.insert(info_text, 'This page has \'\'\'[[' .. mw.title.new(target).fullText .. '|archives]]\'\'\'.')
		end
		
		if target and age then
			table.insert(info_text, ' ')
		end
		
		if age then
			local units = args.units or args.archive_units or 'day'
			if age ~= 1 and string.sub(units, -1) ~= 's' then
				units = units .. 's'
			end
			
			table.insert(info_text, 'Sections older than \'\'\'' .. age .. ' ' .. units .. '\'\'\' may be automatically archived')
			
			local bot = args.bot or args.archive_bot
			if bot then
				table.insert(info_text, ' by ')
				table.insert(info_text, mw.html.create('span'):addClass('nowraplinks'):wikitext('[[User:' .. bot .. '|' .. bot .. ']]'))
			end
			
			if args.minthreadsleft then
				table.insert(info_text, ' when more than ' .. (tonumber(args.minthreadsleft) or 4) .. ' sections are present')
			end
			
			table.insert(info_text, '.')
		end
		
		for i, t in ipairs(info_text) do
			if type(t) == 'string' then
				info_text_td:wikitext(t)
			else
				info_text_td:node(t)
			end
		end
	end
	
	if not args.auto and list and editbox and archive_list_page_exists then
		archivebox:tag('tr'):tag('td')
			:addClass('wst-archivebox-edit-link plainlinks')
			:wikitext('[' .. mw.uri.fullUrl(archive_list_page, {['action'] = 'edit'}) .. ' Edit this box]')
	end
	
	local cats = ''
	if args['box-width'] then
		cats = '[[Category:' .. 'Archive boxes with unusual parameters' .. '|β]]'
	end
	
	return style_sheets .. tostring(archivebox) .. cats
end

function p.archives(frame)
	return _archives(getArgs(frame))
end

return p