Module:Archives/bots and Module:Archives/bots/sandbox: Difference between pages
(Difference between pages)
imported>Izno -main function |
imported>Izno sure |
||
| Line 16: | Line 16: | ||
years = 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) | function p._human_readable_duration(args) | ||
| Line 21: | Line 121: | ||
if time == nil or time < 0 then | if time == nil or time < 0 then | ||
return | return archives_error('archives-bots-error-number', cfg.error_positive_number) | ||
end | end | ||
| Line 28: | Line 127: | ||
if scale[unit] == nil then | if scale[unit] == nil then | ||
return | return archives_error('archives-bots-error-time-unit', cfg.error_time_unit) | ||
end | end | ||