Module:Archives/bots/sandbox
Appearance
< Module:Archives | bots
| This is the module sandbox page for Module:Archives/bots (diff). |
| This Lua module is used on approximately 32,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
This module converts a duration into a form with units which leave the value easier to read. It should be invoked through the template {{human readable duration}}, and is documented there.
| This module depends on the following other modules: |
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