Jump to content

Module:Naval Vessel Register URL/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Trappist the monk (talk | contribs) at 16:38, 29 August 2017. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
--[[  
 
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
 
]]

require('Module:No globals')
local p = {}

--[[--------------------------< F O R W A R D   D E C L A R A T I O N S >--------------------------------------
]]

local ships_data = {};
local srv_craft_data = {};


--[[--------------------------< M A K E _ S H I P _ L I N K >--------------------------------------------------

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, id: The ship's hull classification symbol
    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
	local output = {};

	if (args[1]==nil) and (args["id"]==nil) then
		args = frame:getParent().args;
		if (args[1]==nil) and (args["id"]==nil) then
			return 'required parameters missing';
		end
	end
	
	local data = mw.loadData ('Module:NVR/data');
	ships_data = data.nvr_ships_id;

	local hull = args["id"] or args[1] or '';
	local title = args["title"] or args[2] or '';
	
	if '' == title then															-- to prevent external links that look like this: [1]
		title = nil;
	end
	
	hull = mw.text.trim (hull);													-- make sure that there is no leading/trailing whitespace
	hull = hull:upper();														-- and all uppercase

	local hull_prefix;
	
	if hull:match ('(%a+)%-%d+') then											-- most common case
		hull_prefix = hull:match ('(%a+)%-%d+');
	elseif hull:match ('T%-%a+') then											-- T- prefix
		hull_prefix = hull:match ('T%-(%a+)%-%d+')								-- these are grouped with ships that don't use the prefix
	elseif hull:match ('[%a+ %(%)]+%-%d+') then									-- special one-off case for AFSB (I)-15
		hull_prefix = hull:match ('([%a+ %(%)]+)%-%d+')
	elseif hull:match ('^%a+$') then											-- letters only
		hull_prefix = hull;														-- Constitution, Maine, or Texas
	else
		table.insert (output, '<span style="font-size:100%" class="error">')
		table.insert (output, 'cannot recognize hull classification symbol: [\'');
		table.insert (output, hull);
		table.insert (output, '\']</span>');
		return table.concat (output);
	end
	
	if not ships_data[hull_prefix] then											-- is there a group for the hull number?
		table.insert (output, '<span style="font-size:100%" class="error">')
		table.insert (output, 'unable to find group: [\'');
		table.insert (output, hull_prefix);
		table.insert (output, '\'] in MODULE:NVR/data nvr_ships_id</span>');
		return table.concat (output);
	end

	if not ships_data[hull_prefix][hull] then									-- is there a hull number
		table.insert (output, '<span style="font-size:100%" class="error">')
		table.insert (output, 'unable to find hull classification symbol: [\'');
		table.insert (output, hull);
		table.insert (output, '\'] in MODULE:NVR/data nvr_ships_id</span>');
		return table.concat (output);
	end

	local nvr_id = ships_data[hull_prefix][hull][1];							-- try to fetch nvr id

	if (nil ~= nvr_id) and ('' ~= nvr_id) then									-- there appears to be an identifier, so use it
		if title then
			table.insert (output, '[');
		end
		table.insert (output, 'http://www.nvr.navy.mil/SHIPDETAILS/SHIPSDETAIL_');
		table.insert (output, nvr_id);
		table.insert (output, '.HTML');
		if title then
			table.insert (output, ' ');
			table.insert (output, title);
			table.insert (output, ']');
		end
		
	else																		-- no identifier
		table.insert (output, '<span style="font-size:100%" class="error">')
		table.insert (output, 'no identifier for hull classification symbol: [\'');
		table.insert (output, hull);
		table.insert ('\'] in MODULE:NVR/data nvr_ships_id</span>');
	end
	
	return table.concat (output);
end


--[[--------------------------< M A K E _ S E R V I C E _ C R A F T _ L I N K >--------------------------------

This function returns a link to a service craft in the Naval Vessel Register.
 
Usage:
{{#invoke:NVR|MakeServiceCraftLink|1=|title=}}
{{#invoke:NVR|MakeServiceCraftLink}} - uses the caller's parameters
 
Parameters
    1, id: The 'file name' portion of the url path (typically the craft's hull designation) without the .HTM/.HTML extension. 
    2, title: A title or label for the link.
]]

function p.MakeServiceCraftLink( frame )
	-- if no argument provided than check parent template/module args
	local args = frame.args
	local output = {};

	if (args[1]==nil) and (args["id"]==nil) then
		args = frame:getParent().args;
		if (args[1]==nil) and (args["id"]==nil) then
			return 'required parameters missing';
		end
	end
	
	local data = mw.loadData ('Module:NVR/data');
	srv_craft_data = data.nvr_srv_craft_id;

	local hull = args["id"] or args[1] or '';
	local title = args["title"] or args[2] or '';
	
	if '' == title then															-- to prevent external links that look like this: [1]
		title = nil;
	end
	
	hull = mw.text.trim (hull);													-- make sure that there is no leading/trailing whitespace
	hull = hull:upper();														-- and all uppercase

	local hull_prefix;
	
	if hull:match ('(%a+)%-%d+') then											-- most common case
		hull_prefix = hull:match ('(%a+)%-%d+');
	elseif hull:match ('(%a+)%-%d+%a+') then									-- special cases for the various sections of floating dry docks
		hull_prefix = hull:match ('(%a+)%-%d+%a+');
	else
		table.insert (output, '<span style="font-size:100%" class="error">')
		table.insert (output, 'cannot recognize hull classification symbol: [\'');
		table.insert (output, hull);
		table.insert (output, '\']</span>');
		return table.concat (output);
	end
	
	if not srv_craft_data[hull_prefix] then										-- is there a group for the hull number?
		table.insert (output, '<span style="font-size:100%" class="error">')
		table.insert (output, 'unable to find group: [\'');
		table.insert (output, hull_prefix);
		table.insert (output, '\'] in MODULE:NVR/data nvr_ships_id</span>');
		return table.concat (output);
	end

	if not srv_craft_data[hull_prefix][hull] then								-- is there a hull number
		table.insert (output, '<span style="font-size:100%" class="error">')
		table.insert (output, 'unable to find hull classification symbol: [\'');
		table.insert (output, hull);
		table.insert (output, '\'] in MODULE:NVR/data nvr_ships_id</span>');
		return table.concat (output);
	end

	local nvr_id = srv_craft_data[hull_prefix][hull][1];						-- try to fetch nvr id

	if (nil ~= nvr_id) and ('' ~= nvr_id) then									-- there appears to be an identifier, so use it
		if title then
			table.insert (output, '[');
		end
		table.insert (output, 'http://www.nvr.navy.mil/SHIPDETAILS/SHIPSDETAIL_');
		table.insert (output, nvr_id);
		table.insert (output, '.HTML');
		if title then
			table.insert (output, ' ');
			table.insert (output, title);
			table.insert (output, ']');
		end
	else																		-- no identifier
		table.insert (output, '<span style="font-size:100%" class="error">')
		table.insert (output, 'no identifier for hull classification symbol: [\'');
		table.insert (output, hull);
		table.insert ('\'] in MODULE:NVR/data nvr_ships_id</span>');
	end
	
	return table.concat (output);
end

return p