模組:Citation/CS1/Error/Antigng
外观
--[[
本模块包含了与CS1模块所需,与错误/维护信息相关的函数。错误/维护信息及相关的追踪分类存放于本模块定义的表z中。
请留意过去本站及当前的英文站相应的模块中,该表曾导出供其它模块使用;现已取消。
请勿在其它模块中直接使用或操作表z。由于不兼容性,请勿直接使用英文站模块中定义、和表z有关的函数。
目前该模块导出8个函数,其中:
add_maint_cat()和add_prop_cat()分别用于向表z添加配置模块中定义的"引文格式1维护"分类和"CS1屬性"分类
set_error()和append_error()的用途都是向表z添加配置模块中定义的"引文格式1错误"分类,同时生成一条报错信息;
前者会将该条信息作为返回值输出,后者则直接将该条信息插入表z中。
主模块最后会调用make_error_tail(),在引文的尾部集中输出存储于表z的有关信息。
因此,如报错信息需显示在引文中部,应使用set_error(),并手动将其返回的报错信息插入引文中部;
如报错信息可集中显示在引文尾部,只需使用append_error()即可。
reset_error()用于清除表z中指定的信息。
select_one()用于在多个可能有效的参数中挑选一个,如多于一个参数有效会自动报"xx与yy与zz...只需其一"错误。
throw_error()产生一条配置模块中定义的"内部错误"信息,并终止程序。
]]
--[[--------------------------< F O R W A R D D E C L A R A T I O N S >--------------------------------------
]]
local z = {
error_categories = {}; -- for categorizing citations that contain errors
error_ids = {};
message_tail = {};
maintenance_cats = {}; -- for categorizing citations that aren't erroneous per se, but could use a little work
properties_cats = {}; -- for categorizing citations based on certain properties, language of source for instance
};
local cfg;
local in_array, is_set, substitute, wrap_style;
local make_internal_link;
--[[--------------------------< T H R O W _ E R R O R >-------------------------------------------------------
Terminate the program with a pre-defined excuse.
]]
local function throw_error (arg)
error (cfg.internal_errors[arg]);
end
--[[--------------------------< E R R O R _ C O M M E N T >----------------------------------------------------
Wraps error messages with css markup according to the state of hidden.
]]
local function error_comment (content, hidden)
return wrap_style (hidden and 'hidden-error' or 'visible-error', content);
end
--[[--------------------------< S E T _ E R R O R >--------------------------------------------------------------
Sets an error condition and returns the appropriate error message. The actual placement of the error message in the output is
the responsibility of the calling function.
]]
local added_error_cats = {};
local function set_error (error_id, arguments, raw, prefix, suffix)
local error_state = cfg.error_conditions[error_id];
prefix = prefix or '';
suffix = suffix or '';
if error_state == nil then
throw_error ('undefined_error');
elseif is_set (error_state.category) then
local category = substitute (error_state.category, arguments);
if not added_error_cats[error_id] then
table.insert (z.error_categories, category)
added_error_cats[error_id] = true; -- note that we've added this category
end
end
local message = substitute (error_state.message, arguments);
message = table.concat (
{
message,
' (',
make_internal_link (
table.concat (
{
cfg.messages['help page link'],
'#',
error_id
}),
cfg.messages['help page label']),
')'
});
z.error_ids[error_id] = true;
if in_array (error_id, {'bare_url_missing_title', 'trans_missing_title'})
and z.error_ids['citation_missing_title'] then
return '', false;
end
message = table.concat ({prefix, message, suffix});
if raw == true then
return message, error_state.hidden;
end
return error_comment (message, error_state.hidden);
end
--[[--------------------------< A P P E N D _ E R R O R >--------------------------------------------------------
Sets an error condition, then appends the appropriate error message to z.message_tail.
]]
local function append_error (error_id, arguments, prefix, suffix)
table.insert (z.message_tail, {set_error (error_id, arguments, true, prefix, suffix)});
end
--[[-------------------------< I S _ A L I A S _ U S E D >-----------------------------------------------------
This function is used by select_one() to determine if one of a list of alias parameters is in the argument list
provided by the template.
Input:
args – pointer to the arguments table from calling template
alias – one of the list of possible aliases in the aliases lists from Module:Citation/CS1/Configuration
index – for enumerated parameters, identifies which one
enumerated – true/false flag used to choose how enumerated aliases are examined
value – value associated with an alias that has previously been selected; nil if not yet selected
selected – the alias that has previously been selected; nil if not yet selected
error_list – list of aliases that are duplicates of the alias already selected
Returns:
value – value associated with alias we selected or that was previously selected or nil if an alias not yet selected
selected – the alias we selected or the alias that was previously selected or nil if an alias not yet selected
]]
local function is_alias_used (args, alias, index, enumerated, value, selected, error_list)
if enumerated then -- is this a test for an enumerated parameters?
alias = alias:gsub ('#', index); -- replace '#' with the value in index
else
alias = alias:gsub ('#', ''); -- remove '#' if it exists
end
if is_set (args[alias]) then -- alias is in the template's argument list
if value ~= nil and selected ~= alias then -- if we have already selected one of the aliases
local skip;
for _, v in ipairs (error_list) do -- spin through the error list to see if we've added this alias
if v == alias then
skip = true;
break; -- has been added so stop looking
end
end
if not skip then -- has not been added so
table.insert (error_list, alias); -- add error alias to the error list
end
else
value = args[alias]; -- not yet selected an alias, so select this one
selected = alias;
end
end
return value, selected; -- return newly selected alias, or previously selected alias
end
--[[--------------------------< A D D _ M A I N T _ C A T >------------------------------------------------------
Adds a category to z.maintenance_cats using names from the configuration file with additional text if any.
To prevent duplication, the added_maint_cats table lists the categories by key that have been added to z.maintenance_cats.
]]
local added_maint_cats = {};
local function add_maint_cat (key, arguments)
if not added_maint_cats [key] then
added_maint_cats [key] = true; -- note that we've added this category
table.insert (z.maintenance_cats, substitute (cfg.maint_cats [key], arguments));
-- make name then add to table
end
end
--[[--------------------------< A D D _ P R O P _ C A T >--------------------------------------------------------
Adds a category to z.properties_cats using names from the configuration file with additional text if any.
]]
local added_prop_cats = {} -- list of property categories that have been added to z.properties_cats
local function add_prop_cat (key, arguments)
if not added_prop_cats [key] then
added_prop_cats [key] = true; -- note that we've added this category
table.insert (z.properties_cats, substitute (cfg.prop_cats [key], arguments));
-- make name then add to table
end
end
--[[--------------------------< S E L E C T _ O N E >----------------------------------------------------------
Chooses one matching parameter from a list of parameters to consider. The list of parameters to consider is just
names. For parameters that may be enumerated, the position of the numerator in the parameter name is identified
by the '#' so |author-last1= and |author1-last= are represented as 'author-last#' and 'author#-last'.
Because enumerated parameter |<param>1= is an alias of |<param>= we must test for both possibilities.
Generates an error if more than one match is present.
]]
local function select_one (args, aliases_list, error_condition, index)
local value = nil; -- the value assigned to the selected parameter
local selected = ''; -- the name of the parameter we have chosen
local error_list = {};
if index ~= nil then index = tostring (index); end
for _, alias in ipairs (aliases_list) do -- for each alias in the aliases list
if alias:match ('#') then -- if this alias can be enumerated
if '1' == index then -- when index is 1 test for enumerated and non-enumerated aliases
value, selected = is_alias_used (args, alias, index, false, value, selected, error_list); -- first test for non-enumerated alias
end
value, selected = is_alias_used (args, alias, index, true, value, selected, error_list); -- test for enumerated alias
else
value, selected = is_alias_used (args, alias, index, false, value, selected, error_list); --test for non-enumerated alias
end
end
if #error_list > 0 and 'none' ~= error_condition then -- for cases where this code is used outside of extract_names()
local error_str = '';
for _, k in ipairs (error_list) do
if error_str ~= '' then error_str = error_str .. cfg.messages['parameter-separator'] end
error_str = error_str .. wrap_style ('parameter', k);
end
if #error_list > 1 then
error_str = error_str .. cfg.messages['parameter-final-separator'];
else
error_str = error_str .. cfg.messages['parameter-pair-separator'];
end
error_str = error_str .. wrap_style ('parameter', selected);
append_error (error_condition, {error_str});
end
return value, selected;
end
--[[--------------------------< M A K E _ E R R O R _ T A I L >--------------------------------------------------
The function generates error/maintenance-related messages and/or tracking categories from "z" defined in this module.
]]
local function make_error_tail (flag)
local error_text = '';
if #z.message_tail ~= 0 then
error_text = ' ';
for i,v in ipairs (z.message_tail) do
if is_set (v[1]) then
if i == #z.message_tail then
error_text = error_text .. error_comment (v[1], v[2]);
else
error_text = error_text .. error_comment (v[1] .. '; ', v[2]);
end
end
end
end
if #z.maintenance_cats ~= 0 then
local maintenance_text = '';
for _, v in ipairs (z.maintenance_cats) do -- append maintenance categories
maintenance_text = maintenance_text .. substitute (cfg.messages['maintenance-item'], {v, make_internal_link (':Category:' .. v, cfg.messages['maintenance-link'])})
end
error_text = error_text .. wrap_style ('maintenance', maintenance_text);
-- maintenance mesages (realy just the names of the categories for now)
end
if not flag then
for _, v in ipairs (z.error_categories) do
error_text = error_text .. make_internal_link ('Category:' .. v);
end
for _, v in ipairs (z.maintenance_cats) do -- append maintenance categories
error_text = error_text .. make_internal_link ('Category:' .. v);
end
for _, v in ipairs (z.properties_cats) do -- append maintenance categories
error_text = error_text .. make_internal_link ('Category:' .. v);
end
end
return error_text;
end
--[[--------------------------< R E S E T _ E R R O R >-------------------------------------------------------
Reset error/maintenance messages/categories in z.
]]
local function reset_error (args)
if (in_array ('err_cats', args)) then
z.error_categories = {};
end
if (in_array ('prop_cats', args)) then
z.properties_cats = {};
end
if (in_array ('maint_cats', args)) then
z.maintenance_cats = {};
end
if (in_array ('err_ids', args)) then
z.error_ids = {};
end
if (in_array ('msg_tail', args)) then
z.message_tail = {};
end
end
--[[--------------------------< S E T _ S E L E C T E D _ M O D U L E S >--------------------------------------
Sets local cfg table to same (live or sandbox) as that used by the other modules.
]]
local function set_selected_modules (cfg_table_ptr, utilities_page_ptr, links_page_ptr)
cfg = cfg_table_ptr;
in_array = utilities_page_ptr.in_array;
is_set = utilities_page_ptr.is_set;
substitute = utilities_page_ptr.substitute;
wrap_style= utilities_page_ptr.wrap_style;
make_internal_link = links_page_ptr.make_internal_link;
end
--[[--------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------
]]
return {
add_maint_cat = add_maint_cat, -- exported functions
add_prop_cat = add_prop_cat,
append_error = append_error,
make_error_tail = make_error_tail,
reset_error = reset_error,
select_one = select_one,
set_error = set_error,
throw_error = throw_error,
set_selected_modules = set_selected_modules
}