Module:Check isxn: Difference between revisions

Content deleted Content added
testing
sync from sandbox;
 
(8 intermediate revisions by 4 users not shown)
Line 1:
--[[
-- This template is a copy of the ISXN validation code from [[Module:Citation/CS1]]
 
-- which allows for validating ISBN, ISMN, and ISSN without invoking a citation template
-- This templatecode is aderived copy offrom the ISXN validation code fromat [[Module:Citation/CS1]]. It allows validating ISBN,
-- which allows for validating ISBN, ISMN, and ISSN without invoking a citation template.
 
]]
 
local p = {}
 
 
--[[--------------------------< E R R _ M S G _ S U P L _ T >--------------------------------------------------
 
error message supplements for check_isbn(); adapted from a similarly named table at Module:Citation/CS1/Configuration
 
]]
 
local err_msg_supl_t = {
['char'] = 'invalid character',
['check'] = 'checksum',
['form'] = 'invalid form',
['group'] = 'invalid group id',
['length'] = 'length',
['prefix'] = 'invalid prefix',
}
 
 
--[[--------------------------< IS _ V A L I D _ I S X N >-----------------------------------------------------
Line 44 ⟶ 65:
return temp % 10 == 0; -- sum modulo 10 is zero when isbn-13/ismn is correct
end
 
 
--[[--------------------------< C H E C K _ I S B N >------------------------------------------------------------
 
Determines whether an ISBN string is valid. Implements an ISBN validity check for {{ISBN}}, {{ISBNT}}, {{SBN}}, and
{{Format ISBN}}.
 
]]
 
local function check_isbn( (isbn_str, error_string frame)
local function return_result (check, err_type) -- local function to render the various error returns
if nil ~= isbn_str:match("[^%s-0-9X]") then return false; end -- fail if isbn_str contains anything but digits, hyphens, or the uppercase X
if not check then -- <check> false when there is an error
isbn_str = isbn_str:gsub( "-", "" ):gsub( " ", "" ); -- remove hyphens and spaces
local template = ((frame.args.template_name and '' ~= frame.args.template_name) and frame.args.template_name) or nil; -- get template name
local len = isbn_str:len();
if not template then
return '<span class="error" style="font-size:100%">&nbsp;calling template requires template_name parameter</span>';
end
 
local out_t = {'<span class="error" style="font-size:100%">'}; -- open the error message span
table.insert (out_t, '&nbsp;Parameter error in {{[[Template:'); -- open 'template' markup; open wikilink with Template namespace
table.insert (out_t, template); -- template name wikilink
table.insert (out_t, '|'); -- its pipe
table.insert (out_t, template); -- wikilink label
table.insert (out_t, ']]}}:&nbsp;'); -- close wikilink; close 'template' markup
table.insert (out_t, err_type); -- type of isbn error
table.insert (out_t, '</span>') -- close the error message span
if 0 == mw.title.getCurrentTitle().namespace then -- categorize only when this template is used in mainspace
local category = table.concat ({'[[Category:Pages with ISBN errors]]'});
table.insert (out_t, category);
end
return table.concat (out_t); -- make a big string and done
end
return ''; -- no error, return an empty string
end
 
if nil ~= isbn_str:match ('[^%s-0-9X]') then
if return nilreturn_result ~=(false, isbn_str:match("[^%s-0-9X]"err_msg_supl_t.char) then return false; end -- fail if isbn_str contains anything but digits, hyphens, or the uppercase X
end
 
isbn_strlocal id = isbn_str:gsub ( "'[%s-"]', "" ):gsub( " ", "" ''); -- remove hyphens and spaceswhitespace
 
local len = isbn_strid:len();
if len ~= 10 and len ~= 13 then
return return_result (false, err_msg_supl_t.length); -- fail if incorrect length
return error_string;
end
 
if len == 10 then
if isbn_strid:match( "('^%d*X?$" ') == nil then -- fail if isbn_str has 'X' anywhere returnbut false;last endposition
return return_result (false, err_msg_supl_t.form);
return is_valid_isxn(isbn_str, 10) and '' or error_string;
end
if id:find ('^63[01]') then -- 630xxxxxxx and 631xxxxxxx are (apparently) not valid isbn group ids but are used by amazon as numeric identifiers (asin)
return return_result (false, err_msg_supl_t.group); -- fail if isbn-10 begins with 630/1
end
return return_result (is_valid_isxn (id, 10), err_msg_supl_t.check); -- pass if isbn-10 is numerically valid (checksum)
else
if id:match ('^%d+$') == nil then
local temp = 0;
return return_result (false, err_msg_supl_t.char); -- fail if ISBN-13 is not all digits
if isbn_str:match( "^97[89]%d*$" ) == nil then return false; end -- isbn13 begins with 978 or 979; ismn begins with 979
end
return is_valid_isxn_13 (isbn_str) and '' or 'test';
if id:match ('^97[89]%d*$') == nil then
return return_result (false, err_msg_supl_t.prefix); -- fail when ISBN-13 does not begin with 978 or 979
end
if id:match ('^9790') then
return return_result (false, err_msg_supl_t.group); -- group identifier '0' is reserved to ISMN
end
return return_result (is_valid_isxn_13 (id), err_msg_supl_t.check); -- pass if isbn-10 is numerically valid (checksum)
end
end
 
 
--[[--------------------------< C H E C K _ I S M N >------------------------------------------------------------
Line 78 ⟶ 142:
]]
 
local function check_ismn (id, error_string)
local text;
local valid_ismn = true;
Line 92 ⟶ 156:
return valid_ismn and '' or error_string
end
 
 
--[[--------------------------< I S S N >----------------------------------------------------------------------
Line 106 ⟶ 171:
]]
 
local function check_issn(id, error_string)
local issn_copy = id; -- save a copy of unadulterated issn; use this version for display if issn does not validate
local text;
local valid_issn = true;
 
if not id:match ('^%d%d%d%d%-%d%d%d[%dX]$') then
return error_string;
end
id=id:gsub( "[%s-–]", "" ); -- strip spaces, hyphens, and endashes from the issn
 
Line 121 ⟶ 190:
return valid_issn and '' or error_string
end
 
 
------------------------------< E N T R Y P O I N T S >--------------------------------------------------====
 
function p.check_isbn(frame)
return check_issncheck_isbn(frame.args[1] or frame:getParent().args[1], frame.args['error'] or frame:getParent().args['error'] or 'error')
end
 
function p.check_ismn(frame)
return check_issncheck_ismn(frame.args[1] or frame:getParent().args[1], frame.args['error'] or frame:getParent().args['error'] or 'error')
end