Jump to content

Module:Archives

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Izno (talk | contribs) at 01:29, 17 August 2021 (some function factoring, finish note_auto). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
p = {}

-- borrow from Module:String.endswith
local function ends_with_s(str)
	return mw.ustring.sub(str, -1, -1) == 's'
end

local function is_filled(var)
	return var and var ~= ''
end

-- simple helper for simple cases
local function var_or_default(var, default)
	if is_filled(var) then
		return var
	else
		return default
	end
end

local function wikilink(link, display)
	if display then
		return '[[' .. link .. '|' .. display .. ']]'
	else
		return '[[' .. link .. ']]'
	end
end

local function talk_other(demospace, talk)
	if is_filled(demospace) then return demospace end
	
	if mw.title.getCurrentTitle().isTalkPage then return talk end
	
	return nil -- just return nil rather than 'other' since we have no need
end

local function image(builder)
	
end

local function archive_list(builder)
	
end

local function make_title(builder)


end

local function search_box(frame, is_banner, root, search)

	if search.search and search.search == 'no' then return nil end
	
	local prefix = ''
	if is_filled(search.prefix) then
		prefix = search.prefix
	-- this double-check elseif may move out to code-proper
	elseif is_filled(root) then
		prefix = root
	else
		prefix = mw.title.getCurrentTitle().prefixedText .. '/'
	end
	
	-- break
	local sbreak
	if search.sbreak and (search.sbreak == 'yes' or search.sbreak == 'no') then
		sbreak = search.sbreak
	end
	-- set sbreak's default
	if not sbreak then
		if is_banner then
			sbreak = 'no'
		else
			sbreak = 'yes'
		end
	end
	
	-- width
	local width = ''
	if not is_banner then
		width = var_or_default(search.width, '22')
	end
	
	-- label
	local label = var_or_default(search.label, 'Search archives')
	
	local inputbox_options = {
		['bgcolor'] = 'transparent',
		['type'] = 'fulltext',
		['prefix'] = prefix,
		['break'] = sbreak,
		['width'] = width,
		['searchbuttonlabel'] = label
	}
	
	local inputbox_content = ''
	for k, v in pairs(inputbox_options) do
		inputbox_content = inputbox_content .. '\n' .. k .. '=' .. v
	end
		
	local inputbox = frame:extensionTag{
		name = 'inputbox',
		content = inputbox_content
	}	

	local div = mw.html.create('div'):addClass('archives-search'):wikitext(inputbox)
	
	return div
end

local function note_auto(frame, auto)
	if not is_filled(auto.age) and not is_filled(auto.target) then return nil end
	
	local age = var_or_default(auto.age, nil)
	local target = var_or_default(auto.target, nil)
	
	local target_text = ''
	if target then
		target_text = mw.ustring.format(
			"This page has '''[[%s|archives]]'''. ",
			frame:callParserFunction( '#rel2abs', target )
		)
	end
	local age_text = ''
	if age then
		local units = var_or_default(auto.units, 'day')
		-- there's probably a friendlier l10n way to do this check on units...
		-- TODO make it friendlier. maybe split it to a separate function?
		if age ~= '1' and mw.ustring.sub(units, -1, -1) ~= 's' then
			units = units .. 's'
		end
		
		local age_with_units = age .. ' ' .. units
		
		local has_bot = is_filled(auto.bot)
		local has_minthreadsleft = is_filled(auto.minthreadsleft)
		if has_bot and has_minthreadsleft then
			age_text = mw.ustring.format(
				"Threads older than '''%s''' may be automatically archived by <span class=\"nowraplinks\">%s</span> when more than %s threads are present.",
				age_with_units,
				wikilink('User:' .. auto.bot, auto.bot),
				var_or_default(auto.minthreadsleft, '4')
			)
		elseif has_bot then
			age_text = mw.ustring.format(
				"Threads older than '''%s''' may be automatically archived by <span class=\"nowraplinks\">%s</span>.",
				age_with_units,
				wikilink('User:' .. auto.bot, auto.bot)
			)
		elseif has_minthreadsleft then
			age_text = mw.ustring.format(
				"Threads older than '''%s''' may be automatically archived when more than %s threads are present.",
				age_with_units,
				var_or_default(auto.minthreadsleft, '4')
			)
		else
			age_text = mw.ustring.format(
				"Threads older than '''%s''' may be automatically archived.",
				age_with_units
			)
		end
	end
	
	return mw.html.create('div'):addClass('archives-auto'):wikitext(target_text .. age_text)
end



--[[
pass the frame down for a minute because we do a lot of work with a frame

]]
function p._main(args, frame)
	local frame = frame -- data continuity
	local list = args.list
	local list1 = args[1]
	local root = args.root
	local box_width = args['box-width']
	local image = args.image
	local image_alt = args.alt
	local image_link = args.link
	local image_size = args['image-size']
	local collapsed = args.collapsed
	local collapsible = args.collapsible
	local title = args.title
	local archive_list = args.archivelist
	

	local is_banner = false
	if (args.banner and args.banner == 'yes') or (args.large and args.large == 'yes') then
		is_banner = true
	end
	
	local demospace = var_or_default(args.demospace, nil)
	
	local archives = mw.html.create()
	archives
		:tag('div')
			:addClass('archives plainlinks')
			-- banner is roughly mbox, small is roughly mbox-small
			:addClass(is_banner and 'archives-banner' or 'archives-small')
			-- archives-talk has same-ish styles as tmbox tmbox-notice
			-- base styles are same-ish as ombox ombox-notice
			:addClass(talk_other(demospace, 'archives-talk'))
			
			:node(search_box(frame, is_banner, root, {
				search = args.search,
				prefix = args.prefix,
				width = args['search-width'],
				sbreak = args['search-break'],
				label = args['search-button-label']
			}))
			:node(note_auto(frame, {
				age = args.age,
				target = args.target,
				units = args.units,
				bot = args.bot,
				minthreadsleft = args.minthreadsleft
			}))

	return tostring(archives)
	
end

function p.main(frame)
	return p._main(require('Module:Arguments').getArgs(frame), frame)
end

return p