Module:Authority control/auxiliary

< Module:Authority control

Documentation for this module may be created at Module:Authority control/auxiliary/doc

require('strict')
local p = {}
--[[======================================================]]
--[[            Format validation functions               ]]
--[[======================================================]]
p.botanistV = function(id)
	return mw.ustring.match(id,"^[%u%l%d%. '-]+$")
end

p.validateIsni = function(id) --Validate ISNI (and ORCID) and retuns it as a 16 characters string or returns false if it's invalid. See http://support.orcid.org/knowledgebase/articles/116780-structure-of-the-orcid-identifier
	id = id:gsub( '[ %-]', '' ):upper()
	if not id:match( '^%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d[%dX]$' ) then
		return false
	end
	local total = 0
	for i = 1, 15 do
		local digit = id:byte( i ) - 48 --Get integer value
		total = (total + digit) * 2
	end
	local remainder = total % 11
	local result = (12 - remainder) % 11
	local checkdigit
	if result == 10 then
		checkdigit = 'X'
	else
		checkdigit=tostring( result )
	end
	if checkdigit ~= string.char( id:byte( 16 ) ) then
		return false
	end
	return id
end

local function splitLccn(id)
	if id:match( '^%l%l?%l?%d%d%d%d%d%d%d%d%d?%d?$' ) then
		id = id:gsub( '^(%l+)(%d+)(%d%d%d%d%d%d)$', '%1/%2/%3' )
	end
	if id:match( '^%l%l?%l?/%d%d%d?%d?/%d+$' ) then
		return mw.text.split( id, '/' )
	end
	return false
end

p.lccnV = function(id)
	local function append(str, c, length)
		while str:len() < length do
			str = c..str
		end
		return str
	end
	local parts = splitLccn(id) --e.g. n78039510
	if not parts then
		return false
	end
	local lccnType = parts[1] ~= 'sh' and 'names' or 'subjects'
	return lccnType .. '/' .. parts[1] .. parts[2] .. append( parts[3], '0', 6 )
end

p.WorldCatLCCN = function(id)
	local lccnParts = splitLccn(id)
	if lccnParts and lccnParts[1] ~= 'sh' then
		return lccnParts[1]..lccnParts[2]..'-'..lccnParts[3]
	else
		return false
	end
end

p.orcidV = function(id)
	id = p.validateIsni(id)
	if not id then
		return false
	end
	return id:sub( 1, 4 )..'-'..id:sub( 5, 8 )..'-'..id:sub( 9, 12 )..'-'..id:sub( 13, 16 )
end

p.tlsV = function(id)
	id = id:gsub(' +', '_')
	local idlen = mw.ustring.len(id)
	if idlen < 4 or idlen > 90 then
		return false
	end
	local regex = '^%u'..string.rep("[%w_',%.%-%(%)%*%/–&]", idlen - 1)..'$'
	if not mw.ustring.match(id,regex ) then
		return false
	end
	return id
end

p.worldcatidV = function(id)
	if not id:match( '^viaf%-%d+$' ) and
	   not id:match( '^lccn%-n[a-z]?[0-9%-]+$' ) and
	   not id:match( '^n[cps]%-.+$' ) then
		return false
	end
	return mw.uri.encode(id, 'PATH')
end

p.zbmathV = function(id)
	local ps = {'%l[%l%-]*', '%.%l[%l%-]*', '%.%d*'}
	return id:match( '^'..ps[1]..'$' ) -- prefix with no capture options
	   or id:match( '^'..ps[1]..ps[2]..'$' ) -- prefix with first capture option
	   or id:match( '^'..ps[1]..ps[3]..'$' ) -- prefix with second capture option
	   or id:match( '^'..ps[1]..ps[2]..ps[3]..'$' ) -- prefix and both capture options
end

--[[======================================================]]
--[[                Custom link functions                 ]]
--[[======================================================]]
p.ISILlink = function(id,label)
	if not id:match('^%D%D?%D?%D?%-.+$') then
		return false
	end
	for _,prefix in ipairs({'AT','AU','BE','CA','CH','DE','FI','FR','IT','KR','NZ','US','ZDB'}) do
		if id:match('^'..prefix..'%-') then
			return '<span class="uid">[https://w3id.org/isil/'..id..' ' .. (label or 'ISIL') .. ']</span>'
		end
	end
	return '[[International Standard Identifier for Libraries and Related Organizations|ISIL]]\n**<span class="uid">' .. id .. '</span>'
end

p.uscgLink = function(id)
	local id2 = id:match( '^[1-7]%-%d%d?%d?%d?%d?$' ) or id:match( '^[1-7]%-%d%d?%d?%d?%d?%.%d*[1-9]$' )
	if id2 then
		return '<span class="uid">[https://www.navcen.uscg.gov/pdf/lightlists/LightList%20V'..mw.ustring.sub(id2,1,1)..'.pdf '..id2..']</span>'
	else
		return false
	end
end

return p