Difference between revisions of "Module:NVR"
blackwiki>Jackmcbarn (private functions come first, and make them actually be private) |
blackwiki>RP88 (eliminate id param) |
||
| Line 11: | Line 11: | ||
]] | ]] | ||
| − | |||
| − | |||
local p = {} | local p = {} | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
-- ======================================= | -- ======================================= | ||
| Line 57: | Line 24: | ||
Usage: | Usage: | ||
| − | {{#invoke:NVR|MakeShipLink| | + | {{#invoke:NVR|MakeShipLink|1=|title=}} |
{{#invoke:NVR|MakeShipLink}} - uses the caller's parameters | {{#invoke:NVR|MakeShipLink}} - uses the caller's parameters | ||
Parameters | Parameters | ||
| − | 1 | + | 1: The 'file name' portion of the url path (typically the ship's hull designation) without the .HTM/.HTML extension. |
2, title: A title or label for the link. | 2, title: A title or label for the link. | ||
]] | ]] | ||
| Line 71: | Line 38: | ||
end | end | ||
| − | local nvrid = | + | local nvrid = args[1] or ''; |
| − | + | local title = args["title"] or args[2] or ''; | |
| − | |||
| − | |||
| − | |||
| − | local title = | ||
local output = p._MakeShipLink(nvrid, title) | local output = p._MakeShipLink(nvrid, title) | ||
| Line 89: | Line 52: | ||
Usage: | Usage: | ||
| − | {{#invoke:NVR|MakeServiceShipLink| | + | {{#invoke:NVR|MakeServiceShipLink|1=|title=}} |
{{#invoke:NVR|MakeServiceShipLink}} - uses the caller's parameters | {{#invoke:NVR|MakeServiceShipLink}} - uses the caller's parameters | ||
Parameters | Parameters | ||
| − | 1 | + | 1: The 'file name' portion of the url path (typically the ship's hull designation) without the .HTM/.HTML extension. |
2, title: A title or label for the link. | 2, title: A title or label for the link. | ||
]] | ]] | ||
| Line 103: | Line 66: | ||
end | end | ||
| − | local nvrid = | + | local nvrid = args[1] or ''; |
| − | local title = | + | local title = args["title"] or args[2] or ''; |
local output = p._MakeServiceShipLink(nvrid, title) | local output = p._MakeServiceShipLink(nvrid, title) | ||
| Line 174: | Line 137: | ||
return output | return output | ||
end | end | ||
| + | |||
| + | |||
| + | -- ======================================= | ||
| + | -- === Private Functions ================= | ||
| + | -- ======================================= | ||
| + | |||
| + | --[[ | ||
| + | Generate link to NVR database. | ||
| + | ]] | ||
| + | function nvr_link( nvrid, oldid, supportship, title ) | ||
| + | |||
| + | local link = '' | ||
| + | local url_str = '' | ||
| + | |||
| + | if oldid then | ||
| + | if supportship then | ||
| + | url_str = 'http://www.nvr.navy.mil/NVRSERVICECRAFT/DETAILS/' .. nvrid .. '.HTM' | ||
| + | else | ||
| + | url_str = 'http://www.nvr.navy.mil/NVRSHIPS/DETAILS/' .. nvrid .. '.HTM' | ||
| + | end | ||
| + | else | ||
| + | url_str = 'http://www.nvr.navy.mil/SHIPDETAILS/SHIPSDETAIL_' .. nvrid .. '.HTML' | ||
| + | end | ||
| + | |||
| + | if (title == '') then | ||
| + | link = url_str | ||
| + | else | ||
| + | link = '[' .. url_str .. ' ' .. title .. ']' | ||
| + | end | ||
| + | |||
| + | return link | ||
| + | end | ||
| + | |||
return p | return p | ||
Revision as of 00:31, 28 September 2016
Summary
This module generates links to ships in the Naval Vessel Register (nvr.navy.mil) database. Intended to be used by:
{{NVR url}}{{NVR SC url}}
NVR have adopted a file naming convention based on a ship's hull classification symbol with an appended underscore and some number of digits. The extra digits do not seem to follow an identifiable pattern so the former rules-based system used here no longer works.
To get round the 'new' file format, 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 file name. The database is two large Lua tables maintained at Module:NVR/data.
Using this module from templates
MakeShipLink
This function returns a link to a ship's page at the Naval Vessel Register website.
Usage:
{{#invoke:NVR|MakeShipLink|id=|title=}}{{#invoke:NVR|MakeShipLink}}— uses the calling template's parameters
Parameters:
- 1 or id — the ship's case-insensitive hull classification symbol in one of five 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
- SSBN659
- SSBN_659
- SSBN_659_1635 – the NVR file name format as of September 2017
- 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:NVR|MakeShipLink|DDG_1000}}produces http://www.nvr.navy.mil/SHIPDETAILS/SHIPSDETAIL_DDG_1000.HTML{{#invoke:NVR|MakeShipLink|constitution}}produces http://www.nvr.navy.mil/SHIPDETAILS/SHIPSDETAIL_CONSTITUTION.HTML{{#invoke:NVR|MakeShipLink|maine|USS ''Maine''}}produces USS Maine
MakeServiceCraftLink
This function returns a link to a service craft in the Naval Vessel Register.
Usage:
{{#invoke:NVR|MakeServiceCraftLink|id=|title=}}{{#invoke:NVR|MakeServiceCraftLink}}— uses the calling template's parameters
Parameters: Parameters:
- 1 or id — the ship's case-insensitive hull classification symbol in one of five 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
- YTB760
- YTB_760
- YTB_760_5292 – the NVR file name format as of September 2017
- 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:NVR|MakeServiceCraftLink|AFDB-7F}}produces Script error: The function "MakeServiceCraftLink" does not exist.{{#invoke:NVR|MakeServiceCraftLink|ytb-760}}produces Script error: The function "MakeServiceCraftLink" does not exist.
--[[
This module generates links to ships in the Naval Vessel Register (nvr.navy.mil) database.
It is used by Template:NVR_url and Template:NVR_SC_url
Please do not modify this code without applying the changes first at Module:NVR/sandbox and testing
at Module:NVR/sandbox/testcases and Module talk:NVR/sandbox/testcases.
Authors and maintainers:
* User:RP88
]]
local p = {}
-- =======================================
-- === Public Functions ==================
-- =======================================
--[[
MakeShipLink
This function returns a link to a ship in the Naval Vessel Register.
Usage:
{{#invoke:NVR|MakeShipLink|1=|title=}}
{{#invoke:NVR|MakeShipLink}} - uses the caller's parameters
Parameters
1: The 'file name' portion of the url path (typically the ship's hull designation) without the .HTM/.HTML extension.
2, title: A title or label for the link.
]]
function p.MakeShipLink( frame )
-- if no argument provided than check parent template/module args
local args = frame.args
if args[1]==nil then
args = frame:getParent().args
end
local nvrid = args[1] or '';
local title = args["title"] or args[2] or '';
local output = p._MakeShipLink(nvrid, title)
return output
end
--[[
MakeServiceShipLink
This function returns a link to a service ship in the Naval Vessel Register.
Usage:
{{#invoke:NVR|MakeServiceShipLink|1=|title=}}
{{#invoke:NVR|MakeServiceShipLink}} - uses the caller's parameters
Parameters
1: The 'file name' portion of the url path (typically the ship's hull designation) without the .HTM/.HTML extension.
2, title: A title or label for the link.
]]
function p.MakeServiceShipLink( frame )
-- if no argument provided than check parent template/module args
local args = frame.args
if args[1]==nil then
args = frame:getParent().args
end
local nvrid = args[1] or '';
local title = args["title"] or args[2] or '';
local output = p._MakeServiceShipLink(nvrid, title)
return output
end
--[[
This function returns a link to a ship in the Naval Vessel Register.
Parameters
nvrid: The 'file name' portion of the url path (typically the ship's hull designation) without the .HTM/.HTML extension, as a string.
title: Title for link, set to '' for a bare link without a title.
]]
function p._MakeShipLink( nvrid, title )
local output = ''
-- if the id appears to be an old id, convert it to a new id, if possible.
nvrid = mw.ustring.upper(nvrid)
if (nvrid == 'MAINE') then
nvrid = '939'
elseif (nvrid == 'MARCOS') then
nvrid = '940'
elseif (nvrid == 'OLDIRON') then
nvrid = '1315'
elseif (nvrid == 'AFSB15') then
nvrid = 'AFSB_(I)_15'
elseif (mw.ustring.find(nvrid, "^[A-Z]+[0-9]+$") ~= nil) then
nvrid = mw.ustring.gsub(nvrid, "^([A-Z]+)([0-9]+)$", "%1_%2", 1);
end
output = nvr_link(nvrid, false, false, title)
return output
end
--[[
This function returns a link to a service ship in the Naval Vessel Register.
Parameters
nvrid: The 'file name' portion of the url path (typically the ship's hull designation) without the .HTM/.HTML extension, as a string.
title: Title for link, set to '' for a bare link without a title.
]]
function p._MakeServiceShipLink( nvrid, title )
local output = ''
local oldid = false
-- if the id appears to be an old id, convert it to a new id, if possible.
nvrid = mw.ustring.upper(nvrid)
if (nvrid == 'AFDB7_1') then
nvrid = 'AFDB_7'
elseif (nvrid == 'AFDB1_1') then
oldid = true
elseif (nvrid == 'AFDB1_3') then
oldid = true
elseif (nvrid == 'AFDB1_4') then
oldid = true
elseif (nvrid == 'AFDB7_2') then
oldid = true
elseif (mw.ustring.find(nvrid, "^[A-Z]+[0-9]+$") ~= nil) then
nvrid = mw.ustring.gsub(nvrid, "^([A-Z]+)([0-9]+)$", "%1_%2", 1);
end
output = nvr_link(nvrid, oldid, true, title)
return output
end
-- =======================================
-- === Private Functions =================
-- =======================================
--[[
Generate link to NVR database.
]]
function nvr_link( nvrid, oldid, supportship, title )
local link = ''
local url_str = ''
if oldid then
if supportship then
url_str = 'http://www.nvr.navy.mil/NVRSERVICECRAFT/DETAILS/' .. nvrid .. '.HTM'
else
url_str = 'http://www.nvr.navy.mil/NVRSHIPS/DETAILS/' .. nvrid .. '.HTM'
end
else
url_str = 'http://www.nvr.navy.mil/SHIPDETAILS/SHIPSDETAIL_' .. nvrid .. '.HTML'
end
if (title == '') then
link = url_str
else
link = '[' .. url_str .. ' ' .. title .. ']'
end
return link
end
return p