Module:Archives/bots/sandbox

local p = {}

-- reproduced from Module:Convert
local scale = {
	second = 1,
	seconds = 1,
	minute = 60,
	minutes = 60,
	hour = 3600,
	hours = 3600,
	day = 86400,
	days = 86400,
	month = 2629800,
	months = 2629800,
	year = 31557600,
	years = 31557600
}

local cfg = {
	error_positive_number = 'Human readable duration error: First argument must be a positive number ([[Template:Human readable duration|help]])',
	error_time_unit = 'Human readable duration error: Second argument must be a valid time unit ([[Template:Human readable duration|help]])',
}

-- from http://lua-users.org/wiki/SimpleRound
local function round(num, decimal_places)
	local mult = 10^(decimal_places or 0)
	return math.floor(num * mult + 0.5) / mult
end

local function parse_archive_bot_settings(parameter, parsed_page, is_rounded, template_index)
	
	local good_para = false
	for _, v in ipairs({
		'bot', 'age', 'units', 'minthreadsleft', 'min', 'minkeepthreads'
	}) do
		if v == parameter then good_para = true end
		if good_para then break end
	end
	if not good_para then return '' end
	
	local tpv = require('Template parameter value')
	local get_parameter = tpv.getParameter
	local find_template = tpv.getTemplate
	
	if not is_filled(parsed_page) then parsed_page = mw.getCurrentTitle().fulltext end
	
	local miszabot_config = 'User:MiszaBot/config'
	local cb_config = 'User:ClueBot III/ArchiveThis'
	if find_template(parsed_page, miszabot_config) then
		local match = mw.ustring.match
		local age_exists, age = get_parameter(parsed_page, miszabot_config, 'algo')
		local minthreads_exists, minthreads = get_parameter(parsed_page, miszabot_config, 'minthreadsleft')
		
		if parameter == 'bot' then
			return 'Lowercase sigmabot III'
		elseif parameter == 'age' and age_exists then
			return match(age, '^%s*old%s*%((%d+)[dh]?%)%s*$')
		elseif parameter == 'units' and age_exists then
			local units = match(age, '^%s*old%s*%(%d+([dh]?)%)%s*$')
			if units == 'd' then return 'days'
			elseif units == 'h' then return 'hours'
			else return 'seconds' end
		elseif minthreads_exists then -- already took care of the other invalids, so this is just minthreads
			return minthreads
		end
	elseif find_template(parsed_page, cluebot_config) then
		if template_index then
			template_index = tonumber(template_index)
			if template_index == nil then template_index = 1 end
		end
		
		local age_exists, age = get_parameter(
			parsed_page, cluebot_config, 'age', { template_index = template_index }
		)
		local minthreads_exists, minthreads = get_parameter(
			parsed_page, cluebot_config, 'minkeepthreads', { template_index = template_index }
		)
		if not is_filled(is_rounded) then is_rounded = nil end
		
		if parameter == 'bot' then
			return 'User:ClueBot III'
		elseif parameter == 'age' then
			if not is_rounded then return age end
			
			local num_age = tonumber(age)
			if num_age <= 24 then return age end
			
			-- this formula converts from hours to days with half day precision
			-- i.e. it pops out n+0 or n+0.5 days
			-- apparently 7.6 days is undesirable
			local double_days = 2 * num_age / 24
			local rounded_double_days = round(double_days, 0)
			local days_with_halves = rounded_double_days / 2
			return tostring(days_with_halves)
		elseif parameter == 'units' then
			if not is_rounded or tonumber(age) <= 24 then return 'hours' end
			return 'days'
		else -- already took care of the other invalids, so this is just minthreads
			return minthreads
		end
	end
	return ''
end

function p._parse_ext_settings(args)
	
end

function p.parse_ext_settings(frame)
	local args = frame:getParent().args
	return p._parse_archive_bot(args)
end

local function archives_error(specific_class, error_text)
	local format = mw.ustring.format
	return format('<strong class="archives-bots-error %s">%s</strong>', specific_class, error_text)
end

function p._human_readable_duration(args)
	local time = tonumber(args[1])

	if time == nil or time < 0 then
		return archives_error('archives-bots-error-number', cfg.error_positive_number)
	end

	local unit = string.lower(args[2] or "")

	if scale[unit] == nil then
		return archives_error('archives-bots-error-time-unit', cfg.error_time_unit)
	end

	local timeseconds = time * scale[unit]

	if timeseconds < 59.75 then -- rounds to 59.5 seconds or less
		local converted = math.floor(timeseconds * 2 + 0.5) / 2
		return converted .. " second" .. (converted ~= 1 and "s" or "")
	elseif timeseconds < 3585 then -- rounds to 59.5 minutes or less
		local converted = math.floor(timeseconds / scale.minute * 2 + 0.5) / 2
		return converted .. " minute" .. (converted ~= 1 and "s" or "")
	elseif timeseconds < 258300 and timeseconds ~= 86400 and timeseconds ~= 172800 then -- rounds to 71.5 hours or less, excluding 24 and 48 hours exactly
		local converted = math.floor(timeseconds / scale.hour * 2 + 0.5) / 2
		return converted .. " hour" .. (converted ~= 1 and "s" or "")
	elseif timeseconds < 4341600 then -- rounds to 50 days or less
		local converted = math.floor(timeseconds / scale.day * 2 + 0.5) / 2
		return converted .. " day" .. (converted ~= 1 and "s" or "")
	elseif timeseconds < 48651300 then -- rounds to 18 months or less (rounds to nearest integer instead of 0.5)
		local converted = math.floor(timeseconds / scale.month + 0.5)
		return converted .. " month" .. (converted ~= 1 and "s" or "")
	else -- anything over 18 months rounds to nearest 0.5 years
		local converted = math.floor(timeseconds / scale.year * 2 + 0.5) / 2
		return converted .. " year" .. (converted ~= 1 and "s" or "")
	end
	
end

function p.human_readable_duration(frame)
	local args = frame:getParent().args
	return p._human_readable_duration(args)
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.