Module:Naval Vessel Register URL

--[[  
 
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
	}

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.