Module:Naval Vessel Register URL
Summary
This module generates links to ships in the Naval Vessel Register (nvr.navy.mil) database. Intended to be used by:
NVR have, over time, adopted several different conventions to link to a ship's data base on the ship's hull classification symbol.
To get round 'new' conventions, this version of the module adopts a brute-force mechanism by using a local database that maps a ship's hull classification symbol to its associated NVR ship-data identifier. The database is two large Lua tables maintained at Module:Naval Vessel Register URL/data. Previous versions of this module and its associated data module ignored T- prefixes that are part of several hull designators. As of the 2025-07-16 version, this module does not ignore the prefix; if NVR uses the T- prefix, this module expects to see that prefix in its |id= parameter.
Using this module from templates
nvr_url_make
This function returns a link to a ship's page at the Naval Vessel Register website.
Usage:
{{#invoke:Naval Vessel Register URL|nvr_url_make |id=|title=}}{{#invoke:Naval Vessel Register URL|nvr_url_make}}— uses the calling template's parameters
Parameters:
- 1 or id — the ship's case-insensitive hull classification symbol in one of four forms:
- SSBN-659 – the preferred format because it matches the format for hull classification symbols generally used for US Navy ships throughout Wikipedia
- SSBN 659 – the form used in the NVR
- SSBN659
- SSBN_659
- There are exceptions. There are three ships listed at the NVR website that do not have hull classification symbols. These are: USS Constitution, USS Maine, and USS Texas. For these three ships, use the ship's name for this parameter. The code word OLDIRON, previously used to identify Constitution is no longer supported.
- 2 or title — A title or label for the link.
Examples:
{{#invoke:Naval Vessel Register URL|nvr_url_make|DDG_1000}}produces https://www.nvr.navy.mil/nvr/getHull.htm?shipId=5450{{#invoke:Naval Vessel Register URL|nvr_url_make|constitution}}produces https://www.nvr.navy.mil/nvr/getHull.htm?shipId=1315{{#invoke:Naval Vessel Register URL|nvr_url_make|maine|USS ''Maine''}}produces USS Maine
This function can also return a link to a service craft in the Naval Vessel Register.
Usage:
{{#invoke:Naval Vessel Register URL|nvr_url_make|id=|title=}}{{#invoke:Naval Vessel Register URL|nvr_url_make}}— uses the calling template's parameters
Parameters:
- 1 or id — the ship's case-insensitive hull classification symbol in one of four forms:
- YTB-760 – the preferred format because it matches the format for hull classification symbols generally used for US Navy ships throughout Wikipedia
- YTB 760 – the form used in the NVR
- YTB760
- YTB_760
- There are exceptions. NVR has separate pages for some single and some multiple sections of some floating drydocks. For these, this module adopts a convention similarly used by NVR where each section is distinguished by a letter designator; that letter must be appended to the hull classification symbol used in this parameter; see the examples
- 2 or title — A title or label for the link.
Examples:
{{#invoke:Naval Vessel Register URL|nvr_url_make|AFDB-7F}}produces https://www.nvr.navy.mil/nvr/getHull.htm?shipId=1549{{#invoke:Naval Vessel Register URL|nvr_url_make|ytb-760|''Natick''}}produces Natick
--[[
This module generates links to ships in the Naval Vessel Register (nvr.navy.mil) database.
It is used by Template:Naval Vessel Register URL and Template:Naval Vessel Register service craft URL
Please do not modify this code without applying the changes first at Module:Naval Vessel Register URL/sandbox and testing
at Module:Naval Vessel Register URL/sandbox/testcases and Module talk:Naval Vessel Register URL/sandbox/testcases.
Authors and maintainers:
* User:RP88
]]
require('strict')
local get_args = require ('Module:Arguments').getArgs;
local data_t = mw.loadData ('Module:Naval Vessel Register URL/data');
--[[--------------------------< M A K E _ E R R O R _ M E S S A G E >------------------------------------------
General purpose error message function to render error messages and categorization
]]
local function make_error_message (output, prefix, item, suffix, srv_craft)
local category = ''; -- for concatenation
if 0 == mw.title.getCurrentTitle().namespace then -- article namespace
category = '[[Category:WPSHIPS:Template_errors]]'; -- categorize only from article namespace
end
table.insert (output, '<span style="font-size:100%" class="error">')
table.insert (output, prefix);
table.insert (output, item);
table.insert (output, suffix);
if srv_craft then
table.insert (output, ' ([[Template:Naval Vessel Register service craft URL#Error messages|help]])</span>');
else
table.insert (output, ' ([[Template:Naval Vessel Register URL#Error messages|help]])</span>');
end
table.insert (output, category);
return
end
--[[--------------------------< _ N V R _ U R L _M A K E >-----------------------------------------------------
common function to return an NVR url when given
]]
local function _nvr_url_make (args_t)
local output = {};
if (not args_t[1]) and (not args_t['id']) then
make_error_message (output, 'required parameter missing', '', '');
return table.concat (output);
end
local hull = args_t['id'] or args_t[1];
local hull_designator; -- for use with |name= parameter rendering
local hull_prefix;
local title = args_t["title"] or args_t[2];
hull = mw.text.trim (hull):upper():gsub ('[ _%-]+', '-'); -- no leading/trailing whitespace, uppercase; convert spaces, underscores, hyphens to hyphens
local patterns_t = { -- patterns for various NVR hull classification
{'^(T)%-?(%u+)%-?(%d+)$', '%1-%2-%3', '^T%-%u+'}, -- for certain auxilliaries that have a 'T-' prefix
{'^(%u+)%-?(%d+)$', '%1-%2', '^%u+'}, -- most common hull designator format
{'^(AFDB)%-?(%d+%u?)$', '%1-%2', '^%u+'}, -- special case for AFDB drydock sections
{'^AFSB%-?%(I%)%-?15', 'AFSB-(I)-15', 'AFSB'}, -- special case for Ponce (AFSB-(I)-15)
};
for _, pattern_t in ipairs (patterns_t) do -- spin through the patterns_t sequence
if hull:match (pattern_t[1]) then -- if a match
hull = hull:gsub (pattern_t[1], pattern_t[2]); -- ensure proper format
hull_prefix = hull:match (pattern_t[3]); -- and extract the alpha hull prefix (SSBN from SSBN-659)
break;
end
end
if not hull_prefix then
if ({['CONSTITUTION'] = true, ['MAINE'] = true, ['TEXAS'] = true})[hull] then -- special case for these three ships which do not have standard hull designators in NVR
hull_prefix = hull; -- so use ship name to index into data table
else
make_error_message (output, 'malformed hull classification symbol: ', hull, '');
return table.concat (output);
end
end
hull_designator = hull; -- copy for use with |name= parameter
if not data_t[hull_prefix] then -- is there a group for the hull number?
make_error_message (output, 'unable to find group: [\'', hull_prefix, '\'] in [[Module:Naval Vessel Register URL/data]]');
return table.concat (output);
end
if not data_t[hull_prefix][hull] then -- is there a hull number
make_error_message (output, 'unable to find hull classification symbol: [\'', hull, '\'] in [[Module:Naval Vessel Register URL/data]]');
return table.concat (output);
end
local nvr_id = data_t[hull_prefix][hull][1]; -- try to fetch nvr id
if not title and (args_t['name'] and '' ~= args_t['name']) then
title = data_t[hull_prefix][hull][2];
if not title or '' == title then
title = nil; -- ensure
elseif 'no name' == title:lower() then -- new construction capital ships, and many service craft
title = title .. ' (' .. hull_designator .. ')';
elseif 'nh' == args_t['name'] then -- special keyword to render name and hull designator
title = '\'\'' .. data_t[hull_prefix][hull][2] .. '\'\' (' .. hull_designator .. ')';
else
title = '\'\'' .. data_t[hull_prefix][hull][2] .. '\'\''; -- just the name
end
end
if (nil ~= nvr_id) and ('' ~= nvr_id) then -- there appears to be an identifier, so use it
if title then -- if there is a title then make an external link from it
table.insert (output, '['); -- opening bracket
end
table.insert (output, 'https://www.nvr.navy.mil/nvr/getHull.htm?shipId='); -- create the url
table.insert (output, nvr_id);
if title then
table.insert (output, ' '); -- required space
table.insert (output, title); -- title
table.insert (output, ']'); -- and closing bracket
end
else -- no identifier
make_error_message (output, 'no identifier for hull classification symbol: [\'', hull, '\'] in [[Module:Naval Vessel Register URL/data]]');
end
return table.concat (output); -- and done
end
--[[--------------------------< N V R _ U R L _M A K E >-------------------------------------------------------
{{#invoke:Naval Vessel Register URL|nvr_url_make|1=|title=}}
Parameters:
{{{1}}} or |id=: required; the ship's hull designator: T-ARC-7, SSBN-658, YTB-760, etc
{{{2}}} or |title=: optional; a title or label for the link
]]
local function nvr_url_make (frame)
local args_t = get_args (frame);
return _nvr_url_make (args_t)
end
--[[--------------------------< E X P O R T S >----------------------------------------------------------------
]]
return {
nvr_url_make = nvr_url_make, -- entry point when called from a template
_nvr_url_make = _nvr_url_make, -- entry point when called from a module
}