Module:Citation/CS1: रिवीजन सभ के बीचा में अंतर

Content deleted Content added
Created page with " local z = { error_categories = {}; -- for categorizing citations that contain errors error_ids = {}; message_tail = {}; maintenance_cats = {}; -- for categorizing citat..."
imported>Trappist the monk
Synch from sandbox;
लाइन 8:
}
 
--[[--------------------------< F O R W A R D D E C L A R A T I O N S >--------------------------------------
-- Whether variable is set or not
]]
local dates, year_date_check -- functions in Module:Citation/CS1/Date_validation
 
--[[--------------------------< I S _ S E T >------------------------------------------------------------------
 
Returns true if argument is set; false otherwise. Argument is 'set' when it exists (not nil) or when it is not an empty string.
This function is global because it is called from both this module and from Date validation
 
]]
function is_set( var )
return not (var == nil or var == '');
end
 
--[[--------------------------< F I R S T _ S E T >------------------------------------------------------------
-- First set variable or nil if none
 
function first_set(...)
First set variable or nil if none
 
]]
 
local function first_set(...)
local list = {...};
for _, var in pairs(list) do
Line 23 ⟶ 37:
end
 
--[[--------------------------< I N _ A R R A Y >--------------------------------------------------------------
-- Whether needle is in haystack
 
function inArray( needle, haystack )
Whether needle is in haystack
 
]]
 
local function in_array( needle, haystack )
if needle == nil then
return false;
Line 34 ⟶ 53:
end
return false;
end
 
--[[--------------------------< S U B S T I T U T E >----------------------------------------------------------
 
Populates numbered arguments in a message string using an argument table.
 
]]
 
local function substitute( msg, args )
return args and mw.message.newRawMessage( msg, args ):plain() or msg;
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 substitute( hidden and cfg.presentation['hidden-error'] or cfg.presentation['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 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
error( cfg.messages['undefined_error'] );
elseif is_set( error_state.category ) then
table.insert( z.error_categories, error_state.category );
end
local message = substitute( error_state.message, arguments );
message = message .. " ([[" .. cfg.messages['help page link'] ..
"#" .. error_state.anchor .. "|" ..
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
 
--[[--------------------------< C H E C K _ U R L >------------------------------------------------------------
 
Determines whether a URL string is valid.
 
At present the only check is whether the string appears to be prefixed with a URI scheme. It is not determined whether
the URI scheme is valid or whether the URL is otherwise well formed.
 
]]
 
local function check_url( url_str )
return url_str:sub(1,2) == "//" or url_str:match( "^[^/]*:" ) ~= nil; -- Protocol-relative or URL scheme
end
 
--[[--------------------------< S A F E _ F O R _ I T A L I C S >----------------------------------------------
 
Protects a string that will be wrapped in wiki italic markup '' ... ''
 
Note: We cannot use <i> for italics, as the expected behavior for italics specified by ''...'' in the title is that
they will be inverted (i.e. unitalicized) in the resulting references. In addition, <i> and '' tend to interact
poorly under Mediawiki's HTML tidy.
 
]]
 
local function safe_for_italics( str )
if not is_set(str) then
return str;
else
if str:sub(1,1) == "'" then str = "<span />" .. str; end
if str:sub(-1,-1) == "'" then str = str .. "<span />"; end
-- Remove newlines as they break italics.
return str:gsub( '\n', ' ' );
end
end
 
--[[--------------------------< S A F E _ F O R _ U R L >------------------------------------------------------
 
Escape sequences for content that will be used for URL descriptions
 
]]
 
local function safe_for_url( str )
if str:match( "%[%[.-%]%]" ) ~= nil then
table.insert( z.message_tail, { set_error( 'wikilink_in_url', {}, true ) } );
end
return str:gsub( '[%[%]\n]', {
['['] = '&#91;',
[']'] = '&#93;',
['\n'] = ' ' } );
end
 
--[[--------------------------< W R A P _ S T Y L E >----------------------------------------------------------
 
Applies styling to various parameters. Supplied string is wrapped using a message_list configuration taking one
argument; protects italic styled parameters. Additional text taken from citation_config.presentation - the reason
this function is similar to but separate from wrap_msg().
 
]]
 
local function wrap_style (key, str)
if not is_set( str ) then
return "";
elseif in_array( key, { 'italic-title', 'trans-italic-title' } ) then
str = safe_for_italics( str );
end
 
return substitute( cfg.presentation[key], {str} );
end
 
--[[--------------------------< E X T E R N A L _ L I N K >----------------------------------------------------
 
Format an external link with error checking
 
]]
 
local function external_link( URL, label, source )
local error_str = "";
if not is_set( label ) then
label = URL;
if is_set( source ) then
error_str = set_error( 'bare_url_missing_title', { wrap_style ('parameter', source) }, false, " " );
else
error( cfg.messages["bare_url_no_origin"] );
end
end
if not check_url( URL ) then
error_str = set_error( 'bad_url', {}, false, " " ) .. error_str;
end
return table.concat({ "[", URL, " ", safe_for_url( label ), "]", error_str });
end
 
--[[--------------------------< E X T E R N A L _ L I N K _ I D >----------------------------------------------
 
Formats a wiki style external link
 
]]
 
local function external_link_id(options)
local url_string = options.id;
if options.encode == true or options.encode == nil then
url_string = mw.uri.encode( url_string );
end
return mw.ustring.format( '[[%s|%s]]%s[%s%s%s %s]',
options.link, options.label, options.separator or "&nbsp;",
options.prefix, url_string, options.suffix or "",
mw.text.nowiki(options.id)
);
end
 
Line 41 ⟶ 228:
details of which parameter caused the error message are not provided. Only one error message is emitted regardless of the number of deprecated parameters in the citation.
]]
local function deprecated_parameter(name)
if true ~= Page_in_deprecated_cat then -- if we haven't been here before then set a
Page_in_deprecated_cat=true; -- sticky flag so that if there are more than one deprecated parameter the category is added only once
table.insert( z.message_tail, { seterrorset_error( 'deprecated_params', {name}, true ) } ); -- add error message
end
end
 
-- Populates numbered arguments in a message string using an argument table.
function substitute( msg, args )
return args and mw.message.newRawMessage( msg, args ):plain() or msg;
end
 
Line 65 ⟶ 247:
]]
 
local function kern_quotes (str)
local cap='';
local cap2='';
Line 106 ⟶ 288:
]]
 
local function format_script_value (script_value)
local lang=''; -- initialize to empty string
local name;
Line 119 ⟶ 301:
script_value = script_value:gsub ('^%l%l%s*:%s*', ''); -- strip prefix from script
-- is prefix one of these language codes?
if inArrayin_array (lang, {'ar', 'bg', 'bs', 'dv', 'el', 'fa', 'hy', 'ja', 'ka', 'ko', 'ku', 'he', 'ps', 'ru', 'sd', 'sr', 'th', 'uk', 'ug', 'yi', 'zh'}) then
table.insert( z.properties_cats, 'CS1 uses ' .. name .. '-language script ('..lang..')'); -- categorize in language-specific categories
else
Line 140 ⟶ 322:
]]
 
local function script_concatenate (title, script)
if is_set (script) then
script = format_script_value (script); -- <bdi> tags, lang atribute, categorization, etc; returns empty string on error
Line 148 ⟶ 330:
end
return title;
end
 
 
--[[--------------------------< W R A P _ S T Y L E >----------------------------------------------------------
 
Applies styling to various parameters. Supplied string is wrapped using a message_list configuration taking one
argument; protects italic styled parameters. Additional text taken from citation_config.presentation - the reason
this function is similar to but separate from wrap_msg().
 
]]
 
function wrap_style (key, str)
if not is_set( str ) then
return "";
elseif inArray( key, { 'italic-title', 'trans-italic-title' } ) then
str = safeforitalics( str );
end
 
return substitute( cfg.presentation[key], {str} );
end
 
Line 178 ⟶ 341:
]]
 
local function wrap_msg (key, str, lower)
if not is_set( str ) then
return "";
Line 190 ⟶ 353:
return substitute( cfg.messages[key], {str} );
end
end
--[[--------------------------< S E L E C T _ O N E >----------------------------------------------------------
 
Chooses one matching parameter from a list of parameters to consider
Generates an error if more than one match is present.
 
]]
 
local function select_one( args, possible, error_condition, index )
local value = nil;
local selected = '';
local error_list = {};
if index ~= nil then index = tostring(index); end
-- Handle special case of "#" replaced by empty string
if index == '1' then
for _, v in ipairs( possible ) do
v = v:gsub( "#", "" );
if is_set(args[v]) then
if value ~= nil and selected ~= v then
table.insert( error_list, v );
else
value = args[v];
selected = v;
end
end
end
end
for _, v in ipairs( possible ) do
if index ~= nil then
v = v:gsub( "#", index );
end
if is_set(args[v]) then
if value ~= nil and selected ~= v then
table.insert( error_list, v );
else
value = args[v];
selected = v;
end
end
end
if #error_list > 0 then
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);
table.insert( z.message_tail, { set_error( error_condition, {error_str}, true ) } );
end
return value, selected;
end
 
Line 199 ⟶ 422:
]]
 
local function format_chapter_title (chapter, transchapter, chapterurl, chapter_url_source)
local chapter_error = '';
Line 206 ⟶ 429:
if is_set (transchapter) then
chapter = wrap_style ('trans-quoted-title', transchapter);
chapter_error = " " .. seterrorset_error ('trans_missing_chapter');
end
if is_set (chapterurl) then
chapter = externallinkexternal_link (chapterurl, chapter, chapter_url_source); -- adds bare_url_missing_title error if appropriate
end
return chapter .. chapter_error;
Line 220 ⟶ 443:
end
if is_set (chapterurl) then
chapter = externallinkexternal_link (chapterurl, chapter); -- adds bare_url_missing_title error if appropriate
end
end
Line 231 ⟶ 454:
can be transparently aliased to single internal variable.
]]
local function argument_wrapper( args )
local origin = {};
Line 249 ⟶ 472:
if type( list ) == 'table' then
v, origin[k] = selectoneselect_one( args, list, 'redundant_parameters' );
if origin[k] == nil then
origin[k] = ''; -- Empty string, not nil
Line 281 ⟶ 504:
nil - unsupported parameters
]]
local function validate( name )
local name = tostring( name );
local state = whitelist.basic_arguments[ name ];
Line 304 ⟶ 527:
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.
 
]]
function errorcomment( content, hidden )
return substitute( hidden and cfg.presentation['hidden-error'] or cfg.presentation['visible-error'], content );
end
 
--[[
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.
]]
function seterror( 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
error( cfg.messages['undefined_error'] );
elseif is_set( error_state.category ) then
table.insert( z.error_categories, error_state.category );
end
local message = substitute( error_state.message, arguments );
message = message .. " ([[" .. cfg.messages['help page link'] ..
"#" .. error_state.anchor .. "|" ..
cfg.messages['help page label'] .. "]])";
z.error_ids[ error_id ] = true;
if inArray( 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 errorcomment( message, error_state.hidden );
end
 
-- Formats a wiki style external link
function externallinkid(options)
local url_string = options.id;
if options.encode == true or options.encode == nil then
url_string = mw.uri.encode( url_string );
end
return mw.ustring.format( '[[%s|%s]]%s[%s%s%s %s]',
options.link, options.label, options.separator or "&nbsp;",
options.prefix, url_string, options.suffix or "",
mw.text.nowiki(options.id)
);
end
 
-- Formats a wiki style internal link
local function internallinkidinternal_link_id(options)
return mw.ustring.format( '[[%s|%s]]%s[[%s%s%s|%s]]',
options.link, options.label, options.separator or "&nbsp;",
Line 372 ⟶ 537:
end
 
-- Format an external link with error checking
function externallink( URL, label, source )
local error_str = "";
if not is_set( label ) then
label = URL;
if is_set( source ) then
error_str = seterror( 'bare_url_missing_title', { wrap_style ('parameter', source) }, false, " " );
else
error( cfg.messages["bare_url_no_origin"] );
end
end
if not checkurl( URL ) then
error_str = seterror( 'bad_url', {}, false, " " ) .. error_str;
end
return table.concat({ "[", URL, " ", safeforurl( label ), "]", error_str });
end
 
--[[--------------------------< N O W R A P _ D A T E >--------------------------------------------------------
Line 398 ⟶ 547:
]]
 
local function nowrap_date (date)
local cap='';
local cap2='';
Line 405 ⟶ 554:
date = substitute (cfg.presentation['nowrap1'], date);
elseif date:match("^%a+%s*%d%d?,%s*%d%d%d%d$") or date:match ("^%d%d?%s*%a+%s*%d%d%d%d$") then
cap, cap2 = string.match (date, "^(.*)%s+(%d%d%d%d)$");
date = substitute (cfg.presentation['nowrap2'], {cap, cap2});
Line 411 ⟶ 560:
return date;
end
 
--[[--------------------------< IS _ V A L I D _ I S X N >-----------------------------------------------------
 
ISBN-10 and ISSN validator code calculates checksum across all isbn/issn digits including the check digit. ISBN-13 is checked in check_isbn().
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,
spaces and other non-isxn characters.
 
]]
 
local function is_valid_isxn (isxn_str, len)
local temp = 0;
isxn_str = { isxn_str:byte(1, len) }; -- make a table of bytes
len = len+1; -- adjust to be a loop counter
for i, v in ipairs( isxn_str ) do -- loop through all of the bytes and calculate the checksum
if v == string.byte( "X" ) then -- if checkdigit is X
temp = temp + 10*( len - i ); -- it represents 10 decimal
else
temp = temp + tonumber( string.char(v) )*(len-i);
end
end
return temp % 11 == 0; -- returns true if calculation result is zero
end
 
 
--[[--------------------------< C H E C K _ I S B N >------------------------------------------------------------
 
Determines whether an ISBN string is valid
 
]]
 
local function check_isbn( isbn_str )
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
isbn_str = isbn_str:gsub( "-", "" ):gsub( " ", "" ); -- remove hyphens and spaces
local len = isbn_str:len();
if len ~= 10 and len ~= 13 then
return false;
end
 
if len == 10 then
if isbn_str:match( "^%d*X?$" ) == nil then return false; end
return is_valid_isxn(isbn_str, 10);
else
local temp = 0;
if isbn_str:match( "^97[89]%d*$" ) == nil then return false; end -- isbn13 begins with 978 or 979
isbn_str = { isbn_str:byte(1, len) };
for i, v in ipairs( isbn_str ) do
temp = temp + (3 - 2*(i % 2)) * tonumber( string.char(v) );
end
return temp % 10 == 0;
end
end
 
--[[--------------------------< I S S N >----------------------------------------------------------------------
 
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
digits with a space. When that condition occurred, the resulting link looked like this:
 
|issn=0819 4327 gives: [http://www.worldcat.org/issn/0819 4327 0819 4327] -- can't have spaces in an external link
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
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
error message. The issn is always displayed with a hyphen, even if the issn was given as a single group of 8 digits.
 
]]
 
local function issn(id)
local issn_copy = id; -- save a copy of unadulterated issn; use this version for display if issn does not validate
local handler = cfg.id_handlers['ISSN'];
local text;
local valid_issn = true;
 
id=id:gsub( "[%s-–]", "" ); -- strip spaces, hyphens, and endashes from the issn
 
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
valid_issn=false; -- wrong length or improper character
else
valid_issn=is_valid_isxn(id, 8); -- validate issn
end
 
if true == valid_issn then
id = string.sub( id, 1, 4 ) .. "-" .. string.sub( id, 5 ); -- if valid, display correctly formatted version
else
id = issn_copy; -- if not valid, use the show the invalid issn with error message
end
text = external_link_id({link = handler.link, label = handler.label,
prefix=handler.prefix,id=id,separator=handler.separator, encode=handler.encode})
if false == valid_issn then
text = text .. ' ' .. set_error( 'bad_issn' ) -- add an error message if the issn is invalid
end
return text
end
 
Line 422 ⟶ 666:
]]
 
local function amazon(id, domain)
local err_cat = ""
 
if not id:match("^[%d%u][%d%u][%d%u][%d%u][%d%u][%d%u][%d%u][%d%u][%d%u][%d%u]$") then
err_cat = ' ' .. seterrorset_error ('bad_asin'); -- asin is not a mix of 10 uppercase alpha and numeric characters
else
if id:match("^%d%d%d%d%d%d%d%d%d[%dX]$") then -- if 10-digit numeric (or 9 digits with terminal X)
if checkisbncheck_isbn( id ) then -- see if asin value is isbn10
table.insert( z.maintenance_cats, "'CS1 maint: ASIN uses ISBN"'); -- add to maint category
elseif not is_set (err_cat) then
err_cat = ' ' .. seterrorset_error ('bad_asin'); -- asin is not isbn10
end
elseif not id:match("^%u[%d%u]+$") then
err_cat = ' ' .. seterrorset_error ('bad_asin'); -- asin doesn't begin with uppercase alpha
end
end
if not is_set(domain) then
domain = "com";
elseif inArrayin_array (domain, {'jp', 'uk'}) then -- Japan, United Kingdom
domain = "co." .. domain;
elseif inArrayin_array (domain, {'au', 'br', 'mx'}) then -- Australia, Brazil, Mexico
domain = "com." .. domain;
end
local handler = cfg.id_handlers['ASIN'];
return externallinkidexternal_link_id({link = handler.link,
label=handler.label , prefix="//www.amazon."..domain.."/dp/",id=id,
encode=handler.encode, separator = handler.separator}) .. err_cat;
Line 480 ⟶ 724:
]]
 
local function arxiv (id)
local handler = cfg.id_handlers['ARXIV'];
local year, month, version;
Line 491 ⟶ 735:
if ((not (90 < year or 8 > year)) or (1 > month or 12 < month)) or -- if invalid year or invalid month
((91 == year and 7 > month) or (7 == year and 3 < month)) then -- if years ok, are starting and ending months ok?
err_cat = ' ' .. seterrorset_error( 'bad_arxiv' ); -- set error message
end
elseif id:match("^%d%d[01]%d%.%d%d%d%d$") or id:match("^%d%d[01]%d%.%d%d%d%dv%d+$") then -- test for the 0704-1412 w/ & w/o version
Line 499 ⟶ 743:
if ((7 > year) or (14 < year) or (1 > month or 12 < month)) or -- is year invalid or is month invalid? (doesn't test for future years)
((7 == year) and (4 > month)) then --or -- when year is 07, is month invalid (before April)?
err_cat = ' ' .. seterrorset_error( 'bad_arxiv' ); -- set error message
end
elseif id:match("^%d%d[01]%d%.%d%d%d%d%d$") or id:match("^%d%d[01]%d%.%d%d%d%d%dv%d+$") then -- test for the 1501- format w/ & w/o version
Line 506 ⟶ 750:
month = tonumber(month);
if ((15 > year) or (1 > month or 12 < month)) then -- is year invalid or is month invalid? (doesn't test for future years)
err_cat = ' ' .. seterrorset_error( 'bad_arxiv' ); -- set error message
end
else
err_cat = ' ' .. seterrorset_error( 'bad_arxiv' ); -- arXiv id doesn't match any format
end
 
return externallinkidexternal_link_id({link = handler.link, label = handler.label,
prefix=handler.prefix,id=id,separator=handler.separator, encode=handler.encode}) .. err_cat;
end
Line 530 ⟶ 774:
]]
 
local function normalize_lccn (lccn)
lccn = lccn:gsub ("%s", ""); -- 1. strip whitespace
 
Line 560 ⟶ 804:
 
]]
local function lccn(lccn)
local handler = cfg.id_handlers['LCCN'];
local err_cat = ''; -- presume that LCCN is valid
Line 570 ⟶ 814:
if 8 == len then
if id:match("[^%d]") then -- if LCCN has anything but digits (nil if only digits)
err_cat = ' ' .. seterrorset_error( 'bad_lccn' ); -- set an error message
end
elseif 9 == len then -- LCCN should be adddddddd
if nil == id:match("%l%d%d%d%d%d%d%d%d") then -- does it match our pattern?
err_cat = ' ' .. seterrorset_error( 'bad_lccn' ); -- set an error message
end
elseif 10 == len then -- LCCN should be aadddddddd or dddddddddd
if id:match("[^%d]") then -- if LCCN has anything but digits (nil if only digits) ...
if nil == id:match("^%l%l%d%d%d%d%d%d%d%d") then -- ... see if it matches our pattern
err_cat = ' ' .. seterrorset_error( 'bad_lccn' ); -- no match, set an error message
end
end
elseif 11 == len then -- LCCN should be aaadddddddd or adddddddddd
if not (id:match("^%l%l%l%d%d%d%d%d%d%d%d") or id:match("^%l%d%d%d%d%d%d%d%d%d%d")) then -- see if it matches one of our patterns
err_cat = ' ' .. seterrorset_error( 'bad_lccn' ); -- no match, set an error message
end
elseif 12 == len then -- LCCN should be aadddddddddd
if not id:match("^%l%l%d%d%d%d%d%d%d%d%d%d") then -- see if it matches our pattern
err_cat = ' ' .. seterrorset_error( 'bad_lccn' ); -- no match, set an error message
end
else
err_cat = ' ' .. seterrorset_error( 'bad_lccn' ); -- wrong length, set an error message
end
 
if not is_set (err_cat) and nil ~= lccn:find ('%s') then
err_cat = ' ' .. seterrorset_error( 'bad_lccn' ); -- lccn contains a space, set an error message
end
 
return externallinkidexternal_link_id({link = handler.link, label = handler.label,
prefix=handler.prefix,id=lccn,separator=handler.separator, encode=handler.encode}) .. err_cat;
end
Line 606 ⟶ 850:
contains only digits and is less than test_limit; the value in local variable test_limit will need to be updated periodically as more PMIDs are issued.
]]
local function pmid(id)
local test_limit = 30000000; -- update this value as PMIDs approach
local handler = cfg.id_handlers['PMID'];
Line 612 ⟶ 856:
if id:match("[^%d]") then -- if PMID has anything but digits
err_cat = ' ' .. seterrorset_error( 'bad_pmid' ); -- set an error message
else -- PMID is only digits
local id_num = tonumber(id); -- convert id to a number for range testing
if 1 > id_num or test_limit < id_num then -- if PMID is outside test limit boundaries
err_cat = ' ' .. seterrorset_error( 'bad_pmid' ); -- set an error message
end
end
return externallinkidexternal_link_id({link = handler.link, label = handler.label,
prefix=handler.prefix,id=id,separator=handler.separator, encode=handler.encode}) .. err_cat;
end
Line 628 ⟶ 872:
in the future, returns true; otherwise, returns false because the embargo has expired or |embargo= not set in this cite.
]]
local function is_embargoed(embargo)
if is_set(embargo) then
local lang = mw.getContentLanguage();
Line 652 ⟶ 896:
than test_limit; the value in local variable test_limit will need to be updated periodically as more PMCs are issued.
]]
local function pmc(id, embargo)
local test_limit = 5000000; -- update this value as PMCs approach
local handler = cfg.id_handlers['PMC'];
Line 660 ⟶ 904:
 
if id:match("[^%d]") then -- if PMC has anything but digits
err_cat = ' ' .. seterrorset_error( 'bad_pmc' ); -- set an error message
else -- PMC is only digits
local id_num = tonumber(id); -- convert id to a number for range testing
if 1 > id_num or test_limit < id_num then -- if PMC is outside test limit boundaries
err_cat = ' ' .. seterrorset_error( 'bad_pmc' ); -- set an error message
end
end
Line 671 ⟶ 915:
text="[[" .. handler.link .. "|" .. handler.label .. "]]:" .. handler.separator .. id .. err_cat; --still embargoed so no external link
else
text = externallinkidexternal_link_id({link = handler.link, label = handler.label, --no embargo date, ok to link to article
prefix=handler.prefix,id=id,separator=handler.separator, encode=handler.encode}) .. err_cat;
end
Line 689 ⟶ 933:
-- and terminal punctuation may not be technically correct but it appears, that in practice these characters are rarely if ever used in doi names.
 
local function doi(id, inactive)
local cat = ""
local handler = cfg.id_handlers['DOI'];
Line 704 ⟶ 948:
inactive = " (" .. cfg.messages['inactive'] .. " " .. inactive .. ")"
else
text = externallinkidexternal_link_id({link = handler.link, label = handler.label,
prefix=handler.prefix,id=id,separator=handler.separator, encode=handler.encode})
inactive = ""
Line 710 ⟶ 954:
 
if nil == id:match("^10%.[^%s–]-/[^%s–]-[^%.,]$") then -- doi must begin with '10.', must contain a fwd slash, must not contain spaces or endashes, and must not end with period or comma
cat = ' ' .. seterrorset_error( 'bad_doi' );
end
return text .. inactive .. cat
Line 716 ⟶ 960:
 
-- Formats an OpenLibrary link, and checks for associated errors.
local function openlibrary(id)
local code = id:match("^%d+([AMW])$"); -- only digits followed by 'A', 'M', or 'W'
local handler = cfg.id_handlers['OL'];
 
if ( code == "A" ) then
return externallinkidexternal_link_id({link=handler.link, label=handler.label,
prefix="http://openlibrary.org/authors/OL",id=id, separator=handler.separator,
encode = handler.encode})
elseif ( code == "M" ) then
return externallinkidexternal_link_id({link=handler.link, label=handler.label,
prefix="http://openlibrary.org/books/OL",id=id, separator=handler.separator,
encode = handler.encode})
elseif ( code == "W" ) then
return externallinkidexternal_link_id({link=handler.link, label=handler.label,
prefix= "http://openlibrary.org/works/OL",id=id, separator=handler.separator,
encode = handler.encode})
else
return externallinkidexternal_link_id({link=handler.link, label=handler.label,
prefix= "http://openlibrary.org/OL",id=id, separator=handler.separator,
encode = handler.encode}) ..
' ' .. seterrorset_error( 'bad_ol' );
end
end
 
--[[
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
digits with a space. When that condition occurred, the resulting link looked like this:
 
|issn=0819 4327 gives: [http://www.worldcat.org/issn/0819 4327 0819 4327] -- can't have spaces in an external link
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
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
error message. The issn is always displayed with a hyphen, even if the issn was given as a single group of 8 digits.
]]
function issn(id)
local issn_copy = id; -- save a copy of unadulterated issn; use this version for display if issn does not validate
local handler = cfg.id_handlers['ISSN'];
local text;
local valid_issn = true;
 
id=id:gsub( "[%s-–]", "" ); -- strip spaces, hyphens, and endashes from the issn
 
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
valid_issn=false; -- wrong length or improper character
else
valid_issn=is_valid_isxn(id, 8); -- validate issn
end
 
if true == valid_issn then
id = string.sub( id, 1, 4 ) .. "-" .. string.sub( id, 5 ); -- if valid, display correctly formatted version
else
id = issn_copy; -- if not valid, use the show the invalid issn with error message
end
text = externallinkid({link = handler.link, label = handler.label,
prefix=handler.prefix,id=id,separator=handler.separator, encode=handler.encode})
if false == valid_issn then
text = text .. ' ' .. seterror( 'bad_issn' ) -- add an error message if the issn is invalid
end
return text
end
 
--[[--------------------------< M E S S A G E _ I D >----------------------------------------------------------
Line 787 ⟶ 992:
]]
 
local function message_id (id)
local handler = cfg.id_handlers['USENETID'];
 
text = externallinkidexternal_link_id({link = handler.link, label = handler.label,
prefix=handler.prefix,id=id,separator=handler.separator, encode=handler.encode})
if not id:match('^.+@.+$') or not id:match('^[^<].*[^>]$')then -- doesn't have '@' or has one or first or last character is '< or '>'
text = text .. ' ' .. seterrorset_error( 'bad_message_id' ) -- add an error message if the message id is invalid
end
Line 808 ⟶ 1,013:
 
]]
local function set_titletype(cite_class, title_type)
if is_set(title_type) then
if "none" == title_type then
Line 821 ⟶ 1,026:
elseif "mailinglist" == cite_class then -- if this citation is cite mailing list
return "Mailing list"; -- display mailing list annotation
 
elseif "map" == cite_class then -- if this citation is cite map
return "Map"; -- display map annotation
 
elseif "podcast" == cite_class then -- if this citation is cite podcast
Line 837 ⟶ 1,045:
return "Thesis"; -- display simple thesis annotation (without |degree= modification)
end
end
 
--[[
Determines whether a URL string is valid
 
At present the only check is whether the string appears to
be prefixed with a URI scheme. It is not determined whether
the URI scheme is valid or whether the URL is otherwise well
formed.
]]
function checkurl( url_str )
-- Protocol-relative or URL scheme
return url_str:sub(1,2) == "//" or url_str:match( "^[^/]*:" ) ~= nil;
end
 
-- Removes irrelevant text and dashes from ISBN number
-- Similar to that used for Special:BookSources
local function cleanisbnclean_isbn( isbn_str )
return isbn_str:gsub( "[^-0-9X]", "" );
end
Line 863 ⟶ 1,058:
string.gsub() treat their pattern and replace strings as patterns, not literal strings.
]]
local function escape_lua_magic_chars (argument)
argument = argument:gsub("%%", "%%%%"); -- replace % with %%
argument = argument:gsub("([%^%$%(%)%.%[%]%*%+%-%?])", "%%%1"); -- replace all other lua magic pattern characters
Line 876 ⟶ 1,071:
 
]]
local function strip_apostrophe_markup (argument)
if not is_set (argument) then return argument; end
 
Line 903 ⟶ 1,098:
]]
 
local function make_coins_title (title, script)
if is_set (title) then
title = strip_apostrophe_markup (title); -- strip any apostrophe markup
Line 926 ⟶ 1,121:
 
]]
local function get_coins_pages (pages)
local pattern;
if not is_set (pages) then return pages; end -- if no page numbers then we're done
Line 940 ⟶ 1,135:
pages = pages:gsub("&%w+;", "-" ); -- and replace html entities (&ndash; etc.) with hyphens; do we need to replace numerical entities like &#32; and the like?
return pages;
end
 
--[[
ISBN-10 and ISSN validator code calculates checksum across all isbn/issn digits including the check digit. ISBN-13 is checked in checkisbn().
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,
spaces and other non-isxn characters.
]]
function is_valid_isxn (isxn_str, len)
local temp = 0;
isxn_str = { isxn_str:byte(1, len) }; -- make a table of bytes
len = len+1; -- adjust to be a loop counter
for i, v in ipairs( isxn_str ) do -- loop through all of the bytes and calculate the checksum
if v == string.byte( "X" ) then -- if checkdigit is X
temp = temp + 10*( len - i ); -- it represents 10 decimal
else
temp = temp + tonumber( string.char(v) )*(len-i);
end
end
return temp % 11 == 0; -- returns true if calculation result is zero
end
 
-- Determines whether an ISBN string is valid
function checkisbn( isbn_str )
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
isbn_str = isbn_str:gsub( "-", "" ):gsub( " ", "" ); -- remove hyphens and spaces
local len = isbn_str:len();
if len ~= 10 and len ~= 13 then
return false;
end
 
if len == 10 then
if isbn_str:match( "^%d*X?$" ) == nil then return false; end
return is_valid_isxn(isbn_str, 10);
else
local temp = 0;
if isbn_str:match( "^97[89]%d*$" ) == nil then return false; end -- isbn13 begins with 978 or 979
isbn_str = { isbn_str:byte(1, len) };
for i, v in ipairs( isbn_str ) do
temp = temp + (3 - 2*(i % 2)) * tonumber( string.char(v) );
end
return temp % 10 == 0;
end
end
 
-- Gets the display text for a wikilink like [[A|B]] or [[B]] gives B
local function removewikilinkremove_wiki_link( str )
return (str:gsub( "%[%[([^%[%]]*)%]%]", function(l)
return l:gsub( "^[^|]*|(.*)$", "%1" ):gsub("^%s*(.-)%s*$", "%1");
end));
end
 
-- Escape sequences for content that will be used for URL descriptions
function safeforurl( str )
if str:match( "%[%[.-%]%]" ) ~= nil then
table.insert( z.message_tail, { seterror( 'wikilink_in_url', {}, true ) } );
end
return str:gsub( '[%[%]\n]', {
['['] = '&#91;',
[']'] = '&#93;',
['\n'] = ' ' } );
end
 
-- Converts a hyphen to a dash
local function hyphentodashhyphen_to_dash( str )
if not is_set(str) or str:match( "[%[%]{}<>]" ) ~= nil then
return str;
Line 1,012 ⟶ 1,152:
end
 
--[[--------------------------< S A F E _ J O I N >------------------------------------------------------------
-- Protects a string that will be wrapped in wiki italic markup '' ... ''
function safeforitalics( str )
--[[ Note: We cannot use <i> for italics, as the expected behavior for
italics specified by ''...'' in the title is that they will be inverted
(i.e. unitalicized) in the resulting references. In addition, <i> and ''
tend to interact poorly under Mediawiki's HTML tidy. ]]
if not is_set(str) then
return str;
else
if str:sub(1,1) == "'" then str = "<span />" .. str; end
if str:sub(-1,-1) == "'" then str = str .. "<span />"; end
-- Remove newlines as they break italics.
return str:gsub( '\n', ' ' );
end
end
 
--[[--------------------------< S A F E J O I N >--------------------------------------------------------------
 
Joins a sequence of strings together while checking for duplicate separation characters.
 
]]
local function safejoinsafe_join( tbl, duplicate_char )
--[[
Note: we use string functions here, rather than ustring functions.
Line 1,107 ⟶ 1,229:
end
 
 
-- Attempts to convert names to initials.
--[[--------------------------< I S _ G O O D _ V A N C _ N A M E >--------------------------------------------
function reducetoinitials(first)
 
For Vancouver Style, author/editor names are supposed to be rendered in Latin (read ASCII) characters. When a name
uses characters that contain diacritical marks, those characters are to converted to the corresponding Latin character.
When a name is written using a non-Latin alphabet or logogram, that name is to be transliterated into Latin characters.
These things are not currently possible in this module so are left to the editor to do. This module can, however, check
the content of |lastn= and |firstn= to see if the names contain non-Latin (non-ASCII) characters and emit an error message
when such characters are located.
 
Allow |lastn= to contain ASCII characters, hyphens, spaces, and apostrophes. (http://www.ncbi.nlm.nih.gov/books/NBK7271/box/A35029/)
Allow |firstn= to contain ASCII characters, hyphens, spaces, apostrophes, and periods
]]
 
local function is_good_vanc_name (last, first)
if last:find ("[^%a%-%'%s]") or first:find ("[^%a%-%'%s%.]") then
if true ~= Page_in_vanc_error_cat then -- if we haven't been here before then set a sticky flag
Page_in_vanc_error_cat=true; -- so that if there are more than one error the category is added only once
table.insert( z.message_tail, { set_error( 'vancouver', {}, true ) } );
end
return false; -- not a string of latin characters; Vancouver required Romanization
end;
return true;
end
 
 
--[[--------------------------< R E D U C E _ T O _ I N I T I A L S >------------------------------------------
 
Attempts to convert names to initials in support of |name-list-format=vanc.
 
Names in |firstn= may be separated by spaces or hyphens, or for initials, a period. See http://www.ncbi.nlm.nih.gov/books/NBK7271/box/A35062/.
 
Vancouver style requires family rank designations (Jr, II, III, etc) to be rendered as Jr, 2nd, 3rd, etc. This form is not
currently supported by this code so correctly formed names like Smith JL 2nd are converted to Smith J2. See http://www.ncbi.nlm.nih.gov/books/NBK7271/box/A35085/.
 
]]
 
local function reduce_to_initials(first)
if first:match("^%u%u$") then return first end; -- when first contains just two upper-case letters, nothing to do
local initials = {}
local i = 0; -- counter for number of initials
for word in string.gmatch(first, "[^%Ss%.%-]+") do -- names separated by spaces, hyphens, or periods
table.insert(initials, string.sub(word,1,1)) -- Vancouver format does not include full stops.
i = i + 1; -- bump the counter
if 2 <= i then break; end -- only two initials allowed in Vancouver system; if 2, quit
end
return table.concat(initials) -- Vancouver format does not include spaces.
end
 
--[[--------------------------< L I S T _ P E O P L E >-------------------------------------------------------
-- Formats a list of people (e.g. authors / editors)
Formats a list of people (e.g. authors / editors)
function listpeople(control, people)
]]
local function list_people(control, people, etal)
local sep;
local namesep;
Line 1,127 ⟶ 1,288:
local lastauthoramp = control.lastauthoramp;
local text = {}
 
local etal = false;
if 'vanc' == format then -- Vancouver-like author/editor name styling?
sep = ','; -- name-list separator between authors is a comma
Line 1,160 ⟶ 1,320:
local first = person.first
if is_set(first) then
if ( "vanc" == format ) then -- firstif =vancouver reducetoinitials(first) endformat
one = one:gsub ('%.', ''); -- remove periods from surnames (http://www.ncbi.nlm.nih.gov/books/NBK7271/box/A35029/)
if is_good_vanc_name (one, first) then -- and name is all Latin characters
first = reduce_to_initials(first) -- attempt to convert first name(s) to initials
end
end
one = one .. namesep .. first
end
Line 1,168 ⟶ 1,333:
 
if is_set(person.link) and ((nil ~= person.link:find("//")) or (nil ~= person.link:find("[%[%]]"))) then
one = one .. " " .. seterrorset_error( 'bad_authorlink' ) end -- url or wikilink in author link;
end
table.insert( text, one )
Line 1,192 ⟶ 1,357:
end
 
--[[--------------------------< A N C H O R _ I D >--------------------------------------------------------------
Generates a CITEREF anchor ID if we have at least one name or a date. Otherwise returns an empty string.
 
]]
 
local function anchoridanchor_id( options )
local id = table.concat( options ); -- concatenate names and year for CITEREF id
if is_set (id) then -- if concatenation is not an empty string
Line 1,206 ⟶ 1,371:
end
 
--[[--------------------------< E X T R A C T _ N A M E S >----------------------------------------------------
--[[
Gets name list from the input arguments
 
Line 1,215 ⟶ 1,380:
This function emits an error message when there is a |firstn= without a matching |lastn=. When there are 'holes' in the list of last names, |last1= and |last3=
are present but |last2= is missing, an error message is emitted. |lastn= is not required to have a matching |firstn=.
 
When an author or editor parameter contains some form of 'et al.', the 'et al.' is stripped from the parameter and a flag (etal) returned
that will cause list_people() to add the static 'et al.' text from Module:Citation/CS1/Configuration. This keeps 'et al.' out of the
template's metadata. When this occurs, the page is added to a maintenance category.
 
]]
 
function extractnames(args, list_name)
local function extract_names(args, list_name)
local names = {}; -- table of names
local last; -- individual name components
Line 1,225 ⟶ 1,396:
local n = 1; -- output table indexer
local count = 0; -- used to count the number of times we haven't found a |last= (or alias for authors, |editor-last or alias for editors)
local etal=false; -- return value set to true when we find some form of et al. in an author parameter
local pattern = ",? *'*[Ee][Tt] *[Aa][Ll][%.']*$" -- variations on the 'et al' theme
local err_msg_list_name = list_name:match ("(%w+)List") .. 's list'; -- modify AuthorList or EditorList for use in error messages if necessary
 
while true do
last = selectoneselect_one( args, cfg.aliases[list_name .. '-Last'], 'redundant_parameters', i ); -- search through args for name components beginning at 1
first = selectoneselect_one( args, cfg.aliases[list_name .. '-First'], 'redundant_parameters', i );
link = selectoneselect_one( args, cfg.aliases[list_name .. '-Link'], 'redundant_parameters', i );
mask = selectoneselect_one( args, cfg.aliases[list_name .. '-Mask'], 'redundant_parameters', i );
 
local name = tostring(last);
if name:match (pattern) then -- varients on et al.
last = name:gsub (pattern, ''); -- if found, remove
etal = true;
end
name = tostring(first);
if name:match (pattern) then -- varients on et al.
first = name:gsub (pattern, ''); -- if found, remove
etal = true;
end
 
if first and not last then -- if there is a firstn without a matching lastn
table.insert( z.message_tail, { seterrorset_error( 'first_missing_last', {err_msg_list_name, i}, true ) } ); -- add this error message
elseif not first and not last then -- if both firstn and lastn aren't found, are we done?
count = count + 1; -- number of times we haven't found last and first
Line 1,245 ⟶ 1,429:
n = n + 1; -- point to next location in the names table
if 1 == count then -- if the previous name was missing
table.insert( z.message_tail, { seterrorset_error( 'missing_name', {err_msg_list_name, i-1}, true ) } ); -- add this error message
end
count = 0; -- reset the counter, we're looking for two consecutive missing names
Line 1,251 ⟶ 1,435:
i = i + 1; -- point to next args location
end
return names; -- all done, return our list of names
if true == etal then
table.insert( z.maintenance_cats, 'CS1 maint: Explicit use of et al.'); -- add to maint category
end
return names, etal; -- all done, return our list of names
end
 
-- Populates ID table from arguments using configuration settings
local function extractidsextract_ids( args )
local id_list = {};
for k, v in pairs( cfg.id_handlers ) do
v = selectoneselect_one( args, v.parameters, 'redundant_parameters' );
if is_set(v) then id_list[k] = v; end
end
Line 1,264 ⟶ 1,452:
end
 
--[[--------------------------< B U I L D _ I D _ L I S T >--------------------------------------------------------
Takes a table of IDs and turns it into a table of formatted ID outputs.
 
]]
local function buildidlistbuild_id_list( id_list, options )
local new_list, handler = {};
 
Line 1,278 ⟶ 1,466:
if handler.mode == 'external' then
table.insert( new_list, {handler.label, externallinkidexternal_link_id( handler ) } );
elseif handler.mode == 'internal' then
table.insert( new_list, {handler.label, internallinkidinternal_link_id( handler ) } );
elseif handler.mode ~= 'manual' then
error( cfg.messages['unknown_ID_mode'] );
Line 1,300 ⟶ 1,488:
table.insert( new_list, {handler.label, issn( v ) } );
elseif k == 'ISBN' then
local ISBN = internallinkidinternal_link_id( handler );
if not checkisbncheck_isbn( v ) and not is_set(options.IgnoreISBN) then
ISBN = ISBN .. seterrorset_error( 'bad_isbn', {}, false, " ", "" );
end
table.insert( new_list, {handler.label, ISBN } );
Line 1,324 ⟶ 1,512:
end
-- Chooses one matching parameter from a list of parameters to consider
-- Generates an error if more than one match is present.
function selectone( args, possible, error_condition, index )
local value = nil;
local selected = '';
local error_list = {};
if index ~= nil then index = tostring(index); end
-- Handle special case of "#" replaced by empty string
if index == '1' then
for _, v in ipairs( possible ) do
v = v:gsub( "#", "" );
if is_set(args[v]) then
if value ~= nil and selected ~= v then
table.insert( error_list, v );
else
value = args[v];
selected = v;
end
end
end
end
for _, v in ipairs( possible ) do
if index ~= nil then
v = v:gsub( "#", index );
end
if is_set(args[v]) then
if value ~= nil and selected ~= v then
table.insert( error_list, v );
else
value = args[v];
selected = v;
end
end
end
if #error_list > 0 then
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);
table.insert( z.message_tail, { seterror( error_condition, {error_str}, true ) } );
end
return value, selected;
end
 
-- COinS metadata (see <http://ocoins.info/>) allows automated tools to parse
-- the citation information.
local function COinS(data)
if 'table' ~= type(data) or nil == next(data) then
return '';
Line 1,393 ⟶ 1,526:
__newindex = function(self, key, value)
if is_set(value) then
rawset( self, #self+1, table.concat{ key, '=', mw.uri.encode( removewikilinkremove_wiki_link( value ) ) } );
end
end
Line 1,425 ⟶ 1,558:
for k, v in pairs( data.ID_list ) do
local id, value = cfg.id_handlers[k].COinS;
if k == 'ISBN' then value = cleanisbnclean_isbn( v ); else value = v; end
if string.sub( id or "", 1, 4 ) == 'info' then
OCinSoutput["rft_id"] = table.concat{ id, "/", v };
Line 1,479 ⟶ 1,612:
]]
 
local function get_iso639_code (lang)
if 'norwegian' == lang:lower() then -- special case related to Wikimedia remap of code 'no' at Extension:CLDR
return 'Norwegian', 'no'; -- Make sure rendered version is properly capitalized
Line 1,517 ⟶ 1,650:
]]
 
local function language_parameter (lang, namespace)
local code; -- the ISO639-1 two character code
local name; -- the language name
Line 1,553 ⟶ 1,686:
]]
 
local function get_settings_from_cite_class (ps, ref, cite_class)
local sep;
if (cite_class == "citation") then -- for citation templates (CS2)
Line 1,580 ⟶ 1,713:
]]
 
local function set_style (mode, ps, ref, cite_class)
local sep;
if is_set (mode) then
Line 1,597 ⟶ 1,730:
end
else -- anything but cs1 or cs2
table.insert( z.message_tail, { seterrorset_error( 'invalid_param_val', {'mode', mode}, true ) } ); -- add error message
sep, ps, ref = get_settings_from_cite_class (ps, ref, cite_class); -- get settings based on the template's CitationClass
end
Line 1,615 ⟶ 1,748:
formatting.
]]
local function citation0( config, args)
--[[
Load Input Parameters
Line 1,631 ⟶ 1,764:
-- define different field names for the same underlying things.
local Authors = A['Authors'];
local author_etal;
local a = extractnames( args, 'AuthorList' );
local a, author_etal = extract_names( args, 'AuthorList' );
 
local Coauthors = A['Coauthors'];
local Others = A['Others'];
local Editors = A['Editors'];
local editor_etal;
local e = extractnames( args, 'EditorList' );
local e, editor_etal = extract_names( args, 'EditorList' );
 
local NameListFormat = A['NameListFormat']; -- replaces |author-format= and |editor-format=
if is_set (NameListFormat) and ('vanc' ~= NameListFormat) then -- only accepted value for this parameter is 'vanc'
table.insert( z.message_tail, { seterrorset_error( 'invalid_param_val', {'name-list-format', NameListFormat}, true ) } ); -- not vanc so add error message
NameListFormat = ''; -- set to empty string
end
Line 1,677 ⟶ 1,812:
local Position = '';
local Page = A['Page'];
local Pages = hyphentodashhyphen_to_dash( A['Pages'] );
local At = A['At'];
 
Line 1,701 ⟶ 1,836:
local Embargo = A['Embargo'];
 
local ID_list = extractidsextract_ids( args );
 
local Quote = A['Quote'];
Line 1,734 ⟶ 1,869:
--check this page to see if it is in one of the namespaces that cs1 is not supposed to add to the error categories.
if not is_set(no_tracking_cats) then -- ignore if we are already not going to categorize this page
if inArrayin_array (this_page.nsText, cfg.uncategorized_namespaces) then
no_tracking_cats = "true"; -- set no_tracking_cats
end
Line 1,742 ⟶ 1,877:
if is_set(Page) then
if is_set(Pages) or is_set(At) then
Page = Page .. " " .. seterrorset_error('extra_pages'); -- add error message
Pages = ''; -- unset the others
At = '';
Line 1,748 ⟶ 1,883:
elseif is_set(Pages) then
if is_set(At) then
Pages = Pages .. " " .. seterrorset_error('extra_pages'); -- add error messages
At = ''; -- unset
end
Line 1,804 ⟶ 1,939:
Issue = ""; -- unset Issue so that "number" isn't duplicated in the rendered citation or COinS metadata
else -- can't use ID so emit error message
ID = ID .. " " .. seterrorset_error('redundant_parameters', '<code>&#124;id=</code> and <code>&#124;number=</code>');
end
end
Line 1,906 ⟶ 2,041:
 
-- legacy: promote concatenation of |month=, and |year= to Date if Date not set; or, promote PublicationDate to Date if neither Date nor Year are set.
if not is_set (Date) then
if is_set (Year) then
table.insert( z.maintenance_cats, "CS1 maint: Date and year"); -- add to maint category
end
else
Date = Year; -- promote Year to Date
Year = nil; -- make nil so Year as empty string isn't used for CITEREF
Line 1,925 ⟶ 2,056:
 
if PublicationDate == Date then PublicationDate = ''; end -- if PublicationDate is same as Date, don't display in rendered citation
 
 
--[[
Line 1,933 ⟶ 2,063:
Date validation supporting code is in Module:Citation/CS1/Date_validation
]]
do -- create defined block to contain local variables error_message and mismatch
anchor_year, COinS_date, error_message = dates({['accessdate']=AccessDate, ['airdate']=AirDate, ['archivedate']=ArchiveDate, ['date']=Date, ['doi_brokendate']=DoiBroken,
local error_message = '';
['embargo']=Embargo, ['laydate']=LayDate, ['publicationdate']=PublicationDate, ['year']=Year});
 
if is_set(error_message) then
anchor_year, COinS_date, error_message = dates({['accessdate']=AccessDate, ['airdate']=AirDate, ['archivedate']=ArchiveDate, ['date']=Date, ['doi_brokendate']=DoiBroken,
table.insert( z.message_tail, { seterror( 'bad_date', {error_message}, true ) } ); -- add this error message
['embargo']=Embargo, ['laydate']=LayDate, ['publicationdate']=PublicationDate, ['year']=Year});
end
 
if is_set (Year) and is_set (Date) then -- both |date= and |year= not normally needed;
local mismatch = year_date_check (Year, Date)
if 0 == mismatch then -- |year= does not match a year-value in |date=
if is_set (error_message) then -- if there is already an error message
error_message = error_message .. ', '; -- tack on this additional message
end
error_message = error_message .. '&#124;year= / &#124;date= mismatch';
elseif 1 == mismatch then -- |year= matches year-value in |date=
table.insert( z.maintenance_cats, 'CS1 maint: Date and year'); -- add to maint category
end
end
 
if is_set(error_message) then
table.insert( z.message_tail, { set_error( 'bad_date', {error_message}, true ) } ); -- add this error message
end
end -- end of do
 
-- At this point fields may be nil if they weren't specified in the template use. We can use that fact.
-- Test if citation has no title
if not is_set(Title) and
-- not is_set(Periodical) and -- not a title
-- not is_set(Conference) and -- not a title
not is_set(TransTitle) and
not is_set(ScriptTitle) then
table.insert( z.message_tail, { seterrorset_error( 'citation_missing_title', {}, true ) } );
end
if 'none' == Title and is_set(Periodical) and not (( config.CitationClass == "encyclopaedia" ) or ( config.CitationClass == "citation" and is_set (Encyclopedia))) then -- special case
Title = ''; -- set title to empty string
table.insert( z.maintenance_cats, "'CS1 maint: Untitled periodical"'); -- add to maint category
end
 
Line 1,986 ⟶ 2,131:
['RawPage'] = this_page.prefixedText,
};
--[[Why is this here? Why are we mapping Title to Chapter when Periodical is set?
if is_set(Periodical) and not is_set(Chapter) and is_set(Title) then
Chapter = Title;
ChapterLink = TitleLink;
TransChapter = TransTitle;
Title = '';
TitleLink = '';
TransTitle = '';
end
]]
 
-- special case for cite newsgroup. Do this after COinS because we are modifying Publishername to include some static text
if 'newsgroup' == config.CitationClass then
if is_set (PublisherName) then
PublisherName = '[[Newsgroup]]:&nbsp;' .. externallinkexternal_link( 'news:' .. PublisherName, PublisherName );
end
end
Line 2,014 ⟶ 2,149:
if is_set (Maximum) then
if Maximum >= #a then -- if display-authors value greater than or equal to number of authors
table.insert( z.maintenance_cats, "'CS1 maint: display-authors"'); -- add maintenance category because display-authors parameter may be removed
end
else
Line 2,033 ⟶ 2,168:
end
Authors = listpeoplelist_people(control, a, author_etal)
end
 
if not is_set(Authors) and is_set(Coauthors) then -- coauthors aren't displayed if one of authors=, authorn=, or lastn= isn't specified
table.insert( z.message_tail, { seterrorset_error('coauthors_missing_author', {}, true) } ); -- emit error message
end
 
Line 2,046 ⟶ 2,181:
if not is_set(Maximum) and #e == 4 then
Maximum = 3;
table.insert( z.message_tail, { seterrorset_error('implict_etal_editor', {}, true) } );
elseif not is_set(Maximum) then
Maximum = #e + 1;
Line 2,058 ⟶ 2,193:
};
 
Editors, EditorCount = listpeoplelist_people(control, e, editor_etal);
else
EditorCount = 1;
end
 
-- cite map oddities
local Cartography = "";
local Scale = "";
if config.CitationClass == "map" then
Chapter = A['Map'];
if not is_set( Authors ) and is_set( PublisherName ) then
AuthorsChapterURL = PublisherNameA['MapURL'];
ChapterURLorigin = A:ORIGIN('MapURL');
PublisherName = "";
ChapterFormat = A['MapFormat'];
end
Cartography = A['Cartography'];
if is_set( Cartography ) then
Line 2,088 ⟶ 2,225:
-- Test if cite web or cite podcast |url= is missing or empty
if inArrayin_array(config.CitationClass, {"web","podcast", "mailinglist"}) then
table.insert( z.message_tail, { seterrorset_error( 'cite_web_url', {}, true ) } );
end
-- Test if accessdate is given without giving a URL
if is_set(AccessDate) and not is_set(ChapterURL)then -- ChapterURL may be set when the others are not set; TODO: move this to a separate test?
table.insert( z.message_tail, { seterrorset_error( 'accessdate_missing_url', {}, true ) } );
AccessDate = '';
end
Line 2,100 ⟶ 2,237:
-- Test if format is given without giving a URL
if is_set(Format) then
Format = Format .. seterrorset_error( 'format_missing_url', {'format', 'url'} );
end
end
 
--[[
-- Test if citation has no title
if not is_set(Title) and
-- not is_set(Periodical) and -- not a title
-- not is_set(Conference) and -- not a title
not is_set(TransTitle) and
not is_set(ScriptTitle) then
table.insert( z.message_tail, { seterror( 'citation_missing_title', {}, true ) } );
end
if 'none' == Title and is_set(Periodical) then -- special case
Title = ''; -- set title to empty string
table.insert( z.maintenance_cats, "CS1 maint: Untitled periodical"); -- add to maint category
end
]]
local OriginalURL;
DeadURL = DeadURL:lower(); -- used later when assembling archived text
Line 2,136 ⟶ 2,259:
end
 
if inArrayin_array(config.CitationClass, {"web","news","journal","pressrelease","podcast", "newsgroup"}) or
('citation' == config.CitationClass and is_set (Periodical) and not is_set (Encyclopedia)) then
if is_set (Chapter) or is_set (TransChapter) or is_set (ChapterURL)then -- chapter parameters not supported for these citation types
table.insert( z.message_tail, { seterrorset_error( 'chapter_ignored', {}, true ) } ); -- add error message
Chapter = ''; -- set to empty string to be safe with concatenation
TransChapter = '';
Line 2,149 ⟶ 2,272:
ChapterFormat = is_set(ChapterFormat) and " (" .. ChapterFormat .. ")" or "";
if is_set(ChapterFormat) and not is_set (ChapterURL) then -- Test if |chapter-format= is given without giving a |chapter-url=
ChapterFormat = ChapterFormat .. seterrorset_error( 'format_missing_url', {'chapter-format', 'chapter-url'} );
end
if 'map' == config.CitationClass and is_set (TitleType) then
Chapter = Chapter .. ' (' .. TitleType .. ')';
end
Chapter = Chapter .. ChapterFormat .. sepc .. ' ';
Line 2,160 ⟶ 2,286:
end
 
if inArrayin_array(config.CitationClass, {"web","news","journal","pressrelease","podcast", "newsgroup", "mailinglist"}) or
('citation' == config.CitationClass and is_set (Periodical) and not is_set (Encyclopedia)) thenor
('map' == config.CitationClass and is_set (Periodical)) then -- special case for cite map when the map is in a periodical treat as an article
Title = kern_quotes (Title); -- if necessary, separate title's leading and trailing quote marks from Module provided quote marks
Title = kern_quotes (Title); -- if necessary, separate title's leading and trailing quote marks from Module provided quote marks
Title = wrap_style ('quoted-title', Title);
Title = script_concatenate (Title, ScriptTitle); -- <bdi> tags, lang atribute, categorization, etc; must be done after title is wrapped
TransTitle= wrap_style ('trans-quoted-title', TransTitle );
elseif 'report' == config.CitationClass then -- no styling for cite report
Line 2,181 ⟶ 2,308:
TransTitle = " " .. TransTitle;
else
TransError = " " .. seterrorset_error( 'trans_missing_title' );
end
end
Line 2,189 ⟶ 2,316:
if is_set(Title) then
if not is_set(TitleLink) and is_set(URL) then
Title = externallinkexternal_link( URL, Title ) .. TransError .. Format
URL = "";
Format = "";
Line 2,203 ⟶ 2,330:
if is_set(Conference) then
if is_set(ConferenceURL) then
Conference = externallinkexternal_link( ConferenceURL, Conference );
end
Conference = sepc .. " " .. Conference
elseif is_set(ConferenceURL) then
Conference = sepc .. " " .. externallinkexternal_link( ConferenceURL, nil, ConferenceURLorigin );
end
Line 2,235 ⟶ 2,362:
if is_set(Pages) then
if is_set(Periodical) and
not inArrayin_array(config.CitationClass, {"encyclopaedia","web","book","news","podcast"}) then
Pages = ": " .. Pages;
elseif tonumber(Pages) ~= nil then
Line 2,245 ⟶ 2,372:
else
if is_set(Periodical) and
not inArrayin_array(config.CitationClass, {"encyclopaedia","web","book","news","podcast"}) then
Page = ": " .. Page;
else
Line 2,256 ⟶ 2,383:
if config.CitationClass == 'map' then
local Section = A['Section'];
local Sections = A['Sections'];
local Inset = A['Inset'];
if first_set( Pages, Page, At ) ~= nil or sepc ~= '.' then
if is_set( SectionInset ) then
SectionInset = sepc .. ", " .. wrap_msg ('sectioninset', SectionInset, trueuse_lowercase);
end
if is_set( Inset ) then
Inset = ", " .. wrap_msg ('inset', Inset, true);
end
else
if is_set( Section ) then
Section = sepc .. " " .. wrap_msg ('section', Section, use_lowercase);
if is_set( Inset ) then
Inset = ", " .. wrap_msg ('inset', Inset, true);
end
elseif is_set( Inset ) then
Inset = sepc .. " " .. wrap_msg ('inset', Inset, use_lowercase);
end
end
 
At = At .. Section .. Inset;
if is_set( Sections ) then
Section = sepc .. " " .. wrap_msg ('sections', Sections, use_lowercase);
elseif is_set( Section ) then
Section = sepc .. " " .. wrap_msg ('section', Section, use_lowercase);
end
At = At .. Inset .. Section;
end
 
Line 2,287 ⟶ 2,408:
-- handle type parameter for those CS1 citations that have default values
 
if inArrayin_array(config.CitationClass, {"AV-media-notes", "DVD-notes", "mailinglist", "map", "podcast", "pressrelease", "report", "techreport", "thesis"}) then
TitleType = set_titletype (config.CitationClass, TitleType);
if is_set(Degree) and "Thesis" == TitleType then -- special case for cite thesis
Line 2,308 ⟶ 2,429:
if ( mw.ustring.len(Volume) > 4 )
then Volume = sepc .." " .. Volume;
else Volume = " <b>" .. hyphentodashhyphen_to_dash(Volume) .. "</b>";
end
end
Line 2,322 ⟶ 2,443:
 
]]
if inArrayin_array(SubscriptionRequired:lower(), {'yes', 'true', 'y'}) then
SubscriptionRequired = sepc .. " " .. cfg.messages['subscription']; -- subscription required message
elseif inArrayin_array(RegistrationRequired:lower(), {'yes', 'true', 'y'}) then
SubscriptionRequired = sepc .. " " .. cfg.messages['registration']; -- registration required message
else
Line 2,348 ⟶ 2,469:
end
 
ID_list = buildidlistbuild_id_list( ID_list, {DoiBroken = DoiBroken, ASINTLD = ASINTLD, IgnoreISBN = IgnoreISBN, Embargo=Embargo} );
 
if is_set(URL) then
URL = " " .. externallinkexternal_link( URL, nil, URLorigin );
end
 
Line 2,365 ⟶ 2,486:
if is_set(ArchiveURL) then
if not is_set(ArchiveDate) then
ArchiveDate = seterrorset_error('archive_missing_date');
end
if "no" == DeadURL then
Line 2,371 ⟶ 2,492:
if sepc ~= "." then arch_text = arch_text:lower() end
Archived = sepc .. " " .. substitute( cfg.messages['archived-not-dead'],
{ externallinkexternal_link( ArchiveURL, arch_text ), ArchiveDate } );
if not is_set(OriginalURL) then
Archived = Archived .. " " .. seterrorset_error('archive_missing_url');
end
elseif is_set(OriginalURL) then
Line 2,379 ⟶ 2,500:
if sepc ~= "." then arch_text = arch_text:lower() end
Archived = sepc .. " " .. substitute( arch_text,
{ externallinkexternal_link( OriginalURL, cfg.messages['original'] ), ArchiveDate } );
else
local arch_text = cfg.messages['archived-missing'];
if sepc ~= "." then arch_text = arch_text:lower() end
Archived = sepc .. " " .. substitute( arch_text,
{ seterrorset_error('archive_missing_url'), ArchiveDate } );
end
else
Line 2,394 ⟶ 2,515:
if is_set(LayDate) then LayDate = " (" .. LayDate .. ")" end
if is_set(LaySource) then
LaySource = " &ndash; ''" .. safeforitalicssafe_for_italics(LaySource) .. "''";
else
LaySource = "";
end
if sepc == '.' then
Lay = sepc .. " " .. externallinkexternal_link( LayURL, cfg.messages['lay summary'] ) .. LaySource .. LayDate
else
Lay = sepc .. " " .. externallinkexternal_link( LayURL, cfg.messages['lay summary']:lower() ) .. LaySource .. LayDate
end
else
Line 2,408 ⟶ 2,529:
if is_set(Transcript) then
if is_set(TranscriptURL) then Transcript = externallinkexternal_link( TranscriptURL, Transcript ); end
elseif is_set(TranscriptURL) then
Transcript = externallinkexternal_link( TranscriptURL, nil, TranscriptURLorigin );
end
local Publisher;
if is_set(Periodical) and
not inArrayin_array(config.CitationClass, {"encyclopaedia","web","pressrelease","podcast"}) then
if is_set(PublisherName) then
if is_set(PublicationPlace) then
Line 2,481 ⟶ 2,602:
 
local tcommon
if inArrayin_array(config.CitationClass, {"journal","citation"}) and is_set(Periodical) then
if is_set(Others) then Others = Others .. sepc .. " " end
tcommon = safejoinsafe_join( {Others, Title, TitleNote, Conference, Periodical, Format, TitleType, Scale, Series,
Language, Cartography, Edition, Publisher, Agency, Volume, Issue}, sepc );
else
elseif 'map' == config.CitationClass then -- special cases for cite map
tcommon = safejoin( {Title, TitleNote, Conference, Periodical, Format, TitleType, Scale, Series, Language,
if is_set (Chapter) then -- map in a book; TitleType is part of Chapter
Volume, Issue, Others, Cartography, Edition, Publisher, Agency}, sepc );
tcommon = safe_join( {Title, Format, Edition, Scale, Series, Language, Cartography, Others, Publisher, Volume}, sepc );
elseif is_set (Periodical) then -- map in a periodical
tcommon = safe_join( {Title, TitleType, Format, Periodical, Scale, Series, Language, Cartography, Others, Publisher, Volume, Issue}, sepc );
else -- a sheet or stand-alone map
tcommon = safe_join( {Title, TitleType, Format, Edition, Scale, Series, Language, Cartography, Others, Publisher}, sepc );
end
else -- all other CS1 templates
tcommon = safe_join( {Title, TitleNote, Conference, Periodical, Format, TitleType, Series, Language,
Volume, Issue, Others, Edition, Publisher, Agency}, sepc );
end
if #ID_list > 0 then
ID_list = safejoinsafe_join( { sepc .. " ", table.concat( ID_list, sepc .. " " ), ID }, sepc );
else
ID_list = ID;
end
local idcommon = safejoinsafe_join( { ID_list, URL, Archived, AccessDate, Via, SubscriptionRequired, Lay, Quote }, sepc );
local text;
local pgtext = Position .. Page .. Pages .. At;
Line 2,534 ⟶ 2,665:
end
end
text = safejoinsafe_join( {Authors, Date, Chapter, Place, Editors, tcommon }, sepc );
text = safejoinsafe_join( {text, pgtext, idcommon}, sepc );
elseif is_set(Editors) then
if is_set(Date) then
Line 2,551 ⟶ 2,682:
end
end
text = safejoinsafe_join( {Editors, Date, Chapter, Place, tcommon}, sepc );
text = safejoinsafe_join( {text, pgtext, idcommon}, sepc );
else
if is_set(Date) then
Line 2,561 ⟶ 2,692:
end
if config.CitationClass=="journal" and is_set(Periodical) then
text = safejoinsafe_join( {Chapter, Place, tcommon}, sepc );
text = safejoinsafe_join( {text, pgtext, Date, idcommon}, sepc );
else
text = safejoinsafe_join( {Chapter, Place, tcommon, Date}, sepc );
text = safejoinsafe_join( {text, pgtext, idcommon}, sepc );
end
end
if is_set(PostScript) and PostScript ~= sepc then
text = safejoinsafe_join( {text, sepc}, sepc ); --Deals with italics, spaces, etc.
text = text:sub(1,-sepc:len()-1);
-- text = text:sub(1,-2); --Remove final separator (assumes that sepc is only one character)
end
text = safejoinsafe_join( {text, PostScript}, sepc );
 
-- Now enclose the whole thing in a <span/> element
Line 2,602 ⟶ 2,732:
end
names[ #names + 1 ] = first_set(Year, anchor_year); -- Year first for legacy citations and for YMD dates that require disambiguation
id = anchoridanchor_id(names)
end
options.id = id;
Line 2,609 ⟶ 2,739:
if string.len(text:gsub("<span[^>/]*>.-</span>", ""):gsub("%b<>","")) <= 2 then
z.error_categories = {};
text = seterrorset_error('empty_citation');
z.message_tail = {};
end
Line 2,630 ⟶ 2,760:
if is_set(v[1]) then
if i == #z.message_tail then
text = text .. errorcommenterror_comment( v[1], v[2] );
else
text = text .. errorcommenterror_comment( v[1] .. "; ", v[2] );
end
end
end
end
 
if #z.maintenance_cats ~= 0 then
text = text .. ' <span class="citation-comment" style="display:none; color:#33aa33">';
for _, v in ipairs( z.maintenance_cats ) do -- append maintenance categories
text = text .. v .. ' ([[:Category:' .. v ..'|link]])';
end
text = text .. '</span>'; -- maintenance mesages (realy just the names of the categories for now)
end
no_tracking_cats = no_tracking_cats:lower();
if inArrayin_array(no_tracking_cats, {"", "no", "false", "n"}) then
for _, v in ipairs( z.error_categories ) do
text = text .. '[[Category:' .. v ..']]';
Line 2,657 ⟶ 2,795:
function z.citation(frame)
local pframe = frame:getParent()
local validation;
if nil ~= string.find( (frame:getTitle(), 'sandbox', 1, true ) then -- did the {{#invoke:}} use sandbox version?
cfg = mw.loadData ( 'Module:Citation/CS1/Configuration/sandbox' ); -- load sandbox versions of Configuration and Whitelist and ...
whitelist = mw.loadData ( 'Module:Citation/CS1/Whitelist/sandbox' );
datesvalidation = require ('Module:Citation/CS1/Date_validation/sandbox').dates; -- ... sandbox version of date validation code
 
else -- otherwise
else -- otherwise
cfg = mw.loadData( 'Module:Citation/CS1/Configuration' ); -- load live versions of Configuration and Whitelist and ...
whitelistcfg = mw.loadData( ('Module:Citation/CS1/WhitelistConfiguration' ); -- load live versions of Configuration and Whitelist and ...
dateswhitelist = requiremw.loadData ('Module:Citation/CS1/Date_validationWhitelist').dates -- ... live version of date validation code;
validation = require ('Module:Citation/CS1/Date_validation'); -- ... live version of date validation code
end
 
dates = validation.dates; -- imported functions
year_date_check = validation.year_date_check;
 
local args = {};
local suggestions = {};
Line 2,685 ⟶ 2,828:
-- Exclude empty numbered parameters
if v:match("%S+") ~= nil then
error_text, error_state = seterrorset_error( 'text_ignored', {v}, true );
end
elseif validate( k:lower() ) then
error_text, error_state = seterrorset_error( 'parameter_ignored_suggest', {k, k:lower()}, true );
else
if #suggestions == 0 then
Line 2,694 ⟶ 2,837:
end
if suggestions[ k:lower() ] ~= nil then
error_text, error_state = seterrorset_error( 'parameter_ignored_suggest', {k, suggestions[ k:lower() ]}, true );
else
error_text, error_state = seterrorset_error( 'parameter_ignored', {k}, true );
end
end