Módulo:LinkPhone

De Wikiviajes, la guía libre de viajes
Ir a la navegación Ir a la búsqueda
Icono de documentación de módulo Documentación del módulo[ver] [editar] [historial] [purgar]

Version check

Designación de la versión en Wikidata: 2021-05-12 Ok!

Uso

This module provides functions to link to phone numbers. Fax numbers are shown, but not linked. Comments must be added in brackets after the phone number.

Maintenance categories

Examples

Text Code Ergebnis
+49 123 2 567.890 App. 5 {{#invoke:Phone|linkPhone|+49 123 2 567.890 App. 5}} +49 123 2 567.890 App. 5
+49 123 2 567.890 App. 5 {{#invoke:Phone|linkPhone|+49 123 2 567.890 App. 5|isFax=true}} +49 123 2 567.890 App. 5
+49 123 / 2 567.890 {{#invoke:Phone|linkPhone|+49 123 / 2 567.890}} +49 123 / 2 567.890 Category:Contacto: El teléfono contiene una barra (/) El teléfono contiene una barra (/)
++49 (123) 2 56 78 90 ext. 34 (Bar) {{#invoke:Phone|linkPhone|++49 (123) 2 56 78 90 ext. 34 (Bar)}} +49 (123) 2 56 78 90 ext. 34 (Bar)
++49 0123 2 56 78 90 ext. 34 (Bar) {{#invoke:Phone|linkPhone|++49 0123 2 56 78 90 ext. 34 (Bar)|cc=+49}} +49 (0)123 2 56 78 90 ext. 34 (Bar)
+49 (0)123 2 56 78 90;ext=34 {{#invoke:Phone|linkPhone|1=+49 (0)123 2 56 78 90;ext=34}} +49 (0)123 2 56 78 90 ext. 34
+49 (123) 2 56 78 90 Bar {{#invoke:Phone|linkPhone|+49 (123) 2 56 78 90 Bar}} +49 (123) 2 56 78 90 Bar Category:Contacto: Formato de teléfono inválido Formato de teléfono inválido
+49 (0)123 VOYAGE {{#invoke:Phone|linkPhone|+''49'' (0)123 VOYAGE}} +49 (0)123 VOYAGE
(0)123 256 78 90 {{#invoke:Phone|linkPhone|(0)123 256 78 90}} (0)123 256 78 90 Category:Contacto: Teléfono sin código númerico del país Teléfono sin código númerico del país
(0)123 256 78 90 {{#invoke:Phone|linkPhone|(0)123 256 78 90|cc=+49}} (0)123 256 78 90
123 256 78 90 (Bar) {{#invoke:Phone|linkPhone|123 256 78 90 (Bar)|cc=+49}} 123 256 78 90 (Bar) Category:Contacto: Formato de teléfono inválido Formato de teléfono inválido
+49 (123) 2 56 78 90 (Lobby Bar), 0049 (123) 2 56 78 90 (Oasis Restaurant) {{#invoke:Phone|linkPhone|+49 (123) 2 56 78 90 (Vestíbulo y bar), 0049 (123) 2 56 78 91 (Restaurante)}} +49 (123) 2 56 78 90 (Vestíbulo y bar), +49 (123) 2 56 78 91 (Restaurante)
+49 (123) 2 56 78 90 oder +49 (123) 2 56 78 91 {{#invoke:Phone|linkPhone|+49 (123) 2 56 78 90 y +49 (123) 2 56 78 91}} +49 (123) 2 56 78 90, +49 (123) 2 56 78 91
(212) 307 4100 (USA) {{#invoke:Phone|linkPhone|(212) 307 4100 (USA)|cc=+1}} (212) 307 4100 (USA)
+49-345-1234567 {{#invoke:Phone|linkPhone|+49-345-1234567|format=true}} +49 (0)345 123 45 67
+20-92-1234567 {{#invoke:Phone|linkPhone|+20-92-1234567|format=true|size=4}} +20 (0)92 123 4567
0800 12 34 56 (gebührenfrei) {{#invoke:Phone|linkPhone|0800 12 34 56 (gratis)|isTollfree=true}} 0800 12 34 56 (gratis )
112 {{#invoke:Phone|linkPhone| 112 }} 112 Category:Contacto: Teléfono sin código númerico del país Teléfono sin código númerico del país
112 {{#invoke:Phone|linkPhone| 112 | cc=+49}} 112
112 {{#invoke:Phone|linkPhone| 112 (Emergencia) }} 112 (Emergencia)
0900 12 34 56 (teure Servicenummer) {{#invoke:Phone|linkPhone|0900 12 34 56 (Número de servicio caro)}} 0900 12 34 56 (Número de servicio caro)
Esta documentación está transcluida desde Módulo:LinkPhone/doc.
Los editores pueden experimentar en la zona de pruebas (crear) y en los casos de prueba (crear) del módulo.
Por favor, añade las categorías en la subpágina de documentación. Subpáginas de este módulo.

-- documentation
local LinkPhone = {
	suite  = 'LinkPhone',
	serial = '2021-05-12',
	item   = 16354802
}

-- module import
require( 'Module:No globals' )
local li = require( 'Module:Link utilities/i18n' )
local lu = require( 'Module:Link utilities' )

-- module variable and administration
local lp = {
	moduleInterface = LinkPhone
}

local function formatNumber( number, size )
	if not li.formattingWikidata then
		return number
	end

	local pos, first, last, newLast, country, localCode

	number = number:gsub( '-', ' ' )
	_, pos = number:find( '.* ' ) -- find last space
	if size > 0 and pos then
		first = number:sub( 1, pos )
		last = number:sub( pos + 1, #number )
		newLast = ''
		if tonumber( last ) then -- inserting additional spaces
			while ( #last > size + 1 ) do
				if newLast == '' then
					newLast = last:sub( -size )
				else
					newLast = last:sub( -size ) .. ' ' .. newLast
				end
				last = last:sub( 1, #last - size )
			end
			if newLast ~= '' then
				last = last .. ' ' .. newLast
			end
		end
		pos,_ = first:find( ' ' )
		if li.addZeros and pos and ( pos ~= #first ) then
			country = first:sub( 1, pos - 1 )
			localCode = first:sub( pos + 1, #first )
			if not li.noZero[ country ] then
				localCode = localCode:gsub( '[%(%)]', '' )
				if localCode:sub( 1, 1 ) == '0' then
					localCode = '(0)' .. localCode:sub( 2, #localCode ) 
				else
					localCode = '(0)' .. localCode
				end
				first = country .. ' ' .. localCode
			end
		end
		number = first .. last
	end
	return number
end

local function checkNumberMatch( key, number )
	local ar = li.exceptions[ key ]
	if not ar then
		return false
	end
	for i = 1, #ar, 1 do
		if number:find( ar[ i ] ) then
			return true
		end
	end
	return false
end

-- handle a single phone number s
function lp.linkPhoneNumber( s, args, isDemo )
	local number = mw.text.trim( s )
	if number == '' then
		return ''
	end
	local catPrefix = isDemo and ' [[:Category:' or '[[Category:'

	local ext = ''
	local extraCats = ''
	local comment, t

	number, comment = lu.extractComment( number )
	number = number:gsub( '^00+', '+' )
		:gsub( '^%+%++', '+' )

	-- extract extension
	for _, extension in ipairs( li.extensions ) do
		t = mw.ustring.gsub( number, '^.*(' .. extension .. ')$', '%1' )
		if t ~= number then
			ext = t
			number = mw.ustring.gsub( number, '[%s%c]*(' .. extension .. ')$', '' )
		end
	end
	if ext ~= '' then
		ext = ext:gsub( 'ext%s*=', 'ext. ' ) -- RFC 3966
	end

	-- formatting phone numbers retrieved from Wikidata
	if args.format then
		number = formatNumber( number, args.size )
	end

	if li.addZeros and args.cc ~= '+39' and args.cc ~= '+378' and not number:find( '^00' ) then
		number = number:gsub( '^0', '(0)' )
	end
	if li.addZeros and args.cc ~= '' and args.cc ~= '+39' and args.cc ~= '+378' then
		number = number:gsub( '^(%' .. args.cc .. ')( *)0', '%1%2(0)' )
	end

	-- plain is number for link
	local plain = number
	-- check if slashes are used
	if plain:find( '/', 1, true ) then
		extraCats = catPrefix .. li.categories.withSlash
	end

	-- remove delimiters -.()/' and spaces
	-- including thin space
	plain = mw.ustring.gsub( plain, "[  '/%.%-%)]", '' )
	args.cc = args.cc:gsub( '%-', '' )
	local exception = false
	-- handling country code including ++49, 0049 etc.
	if plain:sub( 1, 1 ) == '+' then
		plain = plain:gsub( '%(0', '' ) -- zero in parenthesis
			:gsub( '%(', '' )
	else
		plain = plain:gsub( '%(0', '0' )
		if comment ~= '' and checkNumberMatch( 'service', comment ) then
			exception = true
			number = number:gsub( '[%(%)]', '' )
		elseif args.isTollfree and checkNumberMatch( 'tollfree', plain ) then
			exception = true
			number = number:gsub( '[%(%)]', '' )
		elseif checkNumberMatch( args.cc, plain ) then
			exception = true
		elseif args.cc ~= '' then
			if li.noZero[ args.cc ] then
				plain = args.cc .. plain:gsub( '^%(', '' )
			elseif plain:sub( 1, 1 ) == '0' then
				plain = args.cc .. plain:gsub( '^0', '' )
			else
				return s .. catPrefix .. li.categories.invalid
			end
		else
			return s .. catPrefix .. li.categories.noCC
		end
	end

	-- minimum 5 characters including country code
	if not exception and #plain < 5 then
		return s .. catPrefix .. li.categories.invalid
	end

	-- lower case letters for numbers are not allowed
	if plain:find( '%l' ) then
		return s .. catPrefix .. li.categories.invalid
	elseif plain:find( '%u' ) then
		-- substitude letters
		plain = plain:gsub( '[A-C]', '2' )
			:gsub( '[D-F]', '3' )
			:gsub( '[G-I]', '4' )
			:gsub( '[J-L]', '5' )
			:gsub( '[M-O]', '6' )
			:gsub( '[P-S]', '7' )
			:gsub( '[T-V]', '8' )
			:gsub( '[W-Z]', '9' )
	end

	-- remove zero from local area code
	if args.cc ~= '' and args.cc ~= '+39' and args.cc ~= '+378' then
		plain = plain:gsub( args.cc .. '0', args.cc )
	end

	-- final test
	if not exception and not plain:match( '^%+%d+$' ) then
		return s .. catPrefix .. li.categories.invalid
	end

	-- assemble number, link, ext, comment, and categories
	if args.isFax then
		t = '<span class="listing-phone-number'
		if exception then
			t = t .. ' listing-phone-exception" title="' .. li.texts.onlyDomestic
		end
		t = t .. '" data-phone="' .. plain .. '">' .. number .. '</span>'
	else
		t = '<span class="plainlinks nourlexpansion listing-phone-number'
		if exception then
			t = t .. ' listing-phone-exception" title="' .. li.texts.onlyDomestic
		end
		t = t .. '" data-phone="' .. plain .. '">[tel:' .. plain .. ' ' .. number .. ']</span>'
	end
	if ext ~= '' then
		t = t .. ' ' .. ext
	end
	if comment ~= '' then
		t = t .. ' ' .. comment
	end
	return t .. extraCats
end

function lp.linkPhoneNumbers( args )
	local addNum = li.addNum
	if args.isFax then
		addNum = li.addNumFax
	end
	args.cc = args.cc:gsub( '^00', '+' ):gsub( '^%+%++', '+' )

	local ns     = mw.title.getCurrentTitle().namespace
	local isDemo = ns == 10 or ns == 828

	-- split separate numbers
	local items = lu.splitItems( args.phone, li.delimiters )

	-- analyse phone numbers
	local result = ''
	local i = 0
	local s
	for j, item in ipairs( items ) do
		s = lp.linkPhoneNumber( item, args, isDemo )
		if s ~= '' then
			if result == '' then
				result = s
			else
				if i == addNum then
					result = result .. '<span class="listing-add-contact">'
				end
				result = result .. ', ' .. s
			end
			i = i + 1
		end
	end
	if i > addNum then
		result = result .. '</span>'
	end
	return result;
end

function lp.getTrunkPrefix( cc )
	return li.noZero[ cc ] or '0'
end

return lp