https://en.seminaverbi.bibleget.io/w/index.php?title=Module:Check_isxn&feed=atom&action=historyModule:Check isxn - Revision history2024-03-29T12:15:52ZRevision history for this page on the wikiMediaWiki 1.41.0https://en.seminaverbi.bibleget.io/w/index.php?title=Module:Check_isxn&diff=1156&oldid=prevJohnrdorazio: 1 revision imported2020-08-21T19:40:16Z<p>1 revision imported</p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<tr class="diff-title" lang="en">
<td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 19:40, August 21, 2020</td>
</tr><tr><td colspan="2" class="diff-notice" lang="en"><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>Johnrdoraziohttps://en.seminaverbi.bibleget.io/w/index.php?title=Module:Check_isxn&diff=1155&oldid=preven>The Anome: Changed protection level for "Module:Check isxn": High-risk Lua module ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite))2017-05-31T10:38:54Z<p>Changed protection level for "<a href="/wiki/Module:Check_isxn" title="Module:Check isxn">Module:Check isxn</a>": <a href="/w/index.php?title=Semina_Verbi:High-risk_templates&action=edit&redlink=1" class="new" title="Semina Verbi:High-risk templates (page does not exist)">High-risk Lua module</a> ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite))</p>
<p><b>New page</b></p><div>-- This template is a copy of the ISXN validation code from [[Module:Citation/CS1]]<br />
-- which allows for validating ISBN, ISMN, and ISSN without invoking a citation template<br />
<br />
local p = {}<br />
<br />
--[[--------------------------< IS _ V A L I D _ I S X N >-----------------------------------------------------<br />
<br />
ISBN-10 and ISSN validator code calculates checksum across all isbn/issn digits including the check digit. ISBN-13 is checked in check_isbn().<br />
If the number is valid the result will be 0. Before calling this function, issbn/issn must be checked for length and stripped of dashes,<br />
spaces and other non-isxn characters.<br />
<br />
]]<br />
<br />
local function is_valid_isxn (isxn_str, len)<br />
local temp = 0;<br />
isxn_str = { isxn_str:byte(1, len) }; -- make a table of byte values '0' → 0x30 .. '9' → 0x39, 'X' → 0x58<br />
len = len+1; -- adjust to be a loop counter<br />
for i, v in ipairs( isxn_str ) do -- loop through all of the bytes and calculate the checksum<br />
if v == string.byte( "X" ) then -- if checkdigit is X (compares the byte value of 'X' which is 0x58)<br />
temp = temp + 10*( len - i ); -- it represents 10 decimal<br />
else<br />
temp = temp + tonumber( string.char(v) )*(len-i);<br />
end<br />
end<br />
return temp % 11 == 0; -- returns true if calculation result is zero<br />
end<br />
<br />
<br />
--[[--------------------------< IS _ V A L I D _ I S X N _ 1 3 >----------------------------------------------<br />
<br />
ISBN-13 and ISMN validator code calculates checksum across all 13 isbn/ismn digits including the check digit.<br />
If the number is valid, the result will be 0. Before calling this function, isbn-13/ismn must be checked for length<br />
and stripped of dashes, spaces and other non-isxn-13 characters.<br />
<br />
]]<br />
<br />
local function is_valid_isxn_13 (isxn_str)<br />
local temp=0;<br />
<br />
isxn_str = { isxn_str:byte(1, 13) }; -- make a table of byte values '0' → 0x30 .. '9' → 0x39<br />
for i, v in ipairs( isxn_str ) do<br />
temp = temp + (3 - 2*(i % 2)) * tonumber( string.char(v) ); -- multiply odd index digits by 1, even index digits by 3 and sum; includes check digit<br />
end<br />
return temp % 10 == 0; -- sum modulo 10 is zero when isbn-13/ismn is correct<br />
end<br />
<br />
--[[--------------------------< C H E C K _ I S B N >------------------------------------------------------------<br />
<br />
Determines whether an ISBN string is valid<br />
<br />
]]<br />
<br />
local function check_isbn( isbn_str, error_string )<br />
if nil ~= isbn_str:match("[^%s-0-9X]") then -- fail if isbn_str contains anything but digits, hyphens, or the uppercase X<br />
return error_string;<br />
end<br />
isbn_str = isbn_str:gsub( "-", "" ):gsub( " ", "" ); -- remove hyphens and spaces<br />
local len = isbn_str:len();<br />
<br />
if len ~= 10 and len ~= 13 then<br />
return error_string;<br />
end<br />
<br />
if len == 10 then<br />
if isbn_str:match( "^%d*X?$" ) == nil then <br />
return error_string; <br />
end<br />
return is_valid_isxn(isbn_str, 10) and '' or error_string;<br />
else<br />
local temp = 0;<br />
if isbn_str:match( "^97[89]%d*$" ) == nil then -- isbn13 begins with 978 or 979; ismn begins with 979<br />
return error_string; <br />
end<br />
return is_valid_isxn_13 (isbn_str) and '' or error_string;<br />
end<br />
end<br />
<br />
--[[--------------------------< C H E C K _ I S M N >------------------------------------------------------------<br />
<br />
Determines whether an ISMN string is valid. Similar to isbn-13, ismn is 13 digits begining 979-0-... and uses the<br />
same check digit calculations. See http://www.ismn-international.org/download/Web_ISMN_Users_Manual_2008-6.pdf<br />
section 2, pages 9–12.<br />
<br />
]]<br />
<br />
local function check_ismn (id, error_string)<br />
local text;<br />
local valid_ismn = true;<br />
<br />
id=id:gsub( "[%s-–]", "" ); -- strip spaces, hyphens, and endashes from the ismn<br />
<br />
if 13 ~= id:len() or id:match( "^9790%d*$" ) == nil then -- ismn must be 13 digits and begin 9790<br />
valid_ismn = false;<br />
else<br />
valid_ismn=is_valid_isxn_13 (id); -- validate ismn<br />
end<br />
<br />
return valid_ismn and '' or error_string<br />
end<br />
<br />
--[[--------------------------< I S S N >----------------------------------------------------------------------<br />
<br />
Validate and format an issn. This code fixes the case where an editor has included an ISSN in the citation but has separated the two groups of four<br />
digits with a space. When that condition occurred, the resulting link looked like this:<br />
<br />
|issn=0819 4327 gives: [http://www.worldcat.org/issn/0819 4327 0819 4327] -- can't have spaces in an external link<br />
<br />
This code now prevents that by inserting a hyphen at the issn midpoint. It also validates the issn for length and makes sure that the checkdigit agrees<br />
with the calculated value. Incorrect length (8 digits), characters other than 0-9 and X, or checkdigit / calculated value mismatch will all cause a check issn<br />
error message.<br />
<br />
]]<br />
<br />
local function check_issn(id, error_string)<br />
local issn_copy = id; -- save a copy of unadulterated issn; use this version for display if issn does not validate<br />
local text;<br />
local valid_issn = true;<br />
<br />
if not id:match ('^%d%d%d%d%-%d%d%d[%dX]$') then<br />
return error_string;<br />
end<br />
<br />
id=id:gsub( "[%s-–]", "" ); -- strip spaces, hyphens, and endashes from the issn<br />
<br />
if 8 ~= id:len() or nil == id:match( "^%d*X?$" ) then -- validate the issn: 8 digits long, containing only 0-9 or X in the last position<br />
valid_issn=false; -- wrong length or improper character<br />
else<br />
valid_issn=is_valid_isxn(id, 8); -- validate issn<br />
end<br />
<br />
return valid_issn and '' or error_string<br />
end<br />
<br />
<br />
------------------------------< E N T R Y P O I N T S >--------------------------------------------------====<br />
<br />
function p.check_isbn(frame)<br />
return check_isbn(frame.args[1] or frame:getParent().args[1], frame.args['error'] or frame:getParent().args['error'] or 'error')<br />
end<br />
<br />
function p.check_ismn(frame)<br />
return check_ismn(frame.args[1] or frame:getParent().args[1], frame.args['error'] or frame:getParent().args['error'] or 'error')<br />
end<br />
<br />
function p.check_issn(frame)<br />
return check_issn(frame.args[1] or frame:getParent().args[1], frame.args['error'] or frame:getParent().args['error'] or 'error')<br />
end<br />
<br />
return p</div>en>The Anome