Zum Inhalt springen

Modul:UrlCheck

Aus Wikivoyage
Dokumentation für das Modul UrlCheck[Ansicht] [Bearbeiten] [Versionsgeschichte] [Aktualisieren]

Version

Versionsbezeichnung auf Wikidata: keine Version verfügbar

Funktion

Das Modul stellt Funktionen zur Überprüfung von Internetadressen bereit. Sie werden üblicherweise in anderen Modulen oder Vorlagen verwendet.

Verwendung in anderen Modulen

Dieses Modul ist notwendig für die Ausführung folgender Module. Bei Anpassungen sollte die Funktionstüchtigkeit der folgenden Module geprüft werden. Benutze dazu auch diese Tracking-Kategorie um Fehler zu finden, die sich dann auf Artikel auswirken:

Beispiele

function uc.checkUrl( frame )
  • Mögliche Parameter:
    • |url= URL, die überprüft werden soll.
    • |show= msg Anstelle der Fehlernummern wird der Fehler im Wortlaut ausgegeben.
Text Code Ergebnis Beschreibung
http://xyz.hotel.com:8080 {{#invoke:UrlCheck|checkUrl|url=http://xyz.hotel.com:8080}} 0 Keinen Fehler entdeckt
http://xyz.hotel.com/index.html?p1=A&p2=B#ressource {{#invoke:UrlCheck|checkUrl|url=http://xyz.hotel.com/index.html?p1=A&p2=B#ressource}} 0 Keinen Fehler entdeckt
//xyz.hotel.com {{#invoke:UrlCheck|checkUrl|url=//xyz.hotel.com}} 0 Keinen Fehler entdeckt
xyz.hotel.com {{#invoke:UrlCheck|checkUrl|url=xyz.hotel.com}} 6 Fehlendes oder falsches Protokoll
https://max:[email protected] {{#invoke:UrlCheck|checkUrl|url=https://max:[email protected]}} 0 Keinen Fehler entdeckt
http://www.müller.com {{#invoke:UrlCheck|checkUrl|url=http://www.müller.com}} 1 Host mit Nicht-ASCII-Zeichen
http://150.150.150.150/index.html {{#invoke:UrlCheck|checkUrl|url=http://150.150.150.150/index.html}} 2 Host ist eine IP-Adresse
http://150.150.150/index.html {{#invoke:UrlCheck|checkUrl|url=http://150.150.150/index.html}} 22 Ungültige Host-Struktur
http://my.weekend.co.il/צימר_עזוז/ {{#invoke:UrlCheck|checkUrl|url=http://my.weekend.co.il/צימר_עזוז/}} 0 Keinen Fehler entdeckt
http://my.hotel.de/a'b|c/ {{#invoke:UrlCheck|checkUrl|url=http://my.hotel.de/a'b|c/}} 23 Ungültige Pfad-Struktur

Weitere Funktionen

function uc.ip4( address )
  • Die Funktion überprüft, ob eine gültige vierstellige IP4-Internetadresse vorliegt.
  • Ergebnis: Integer-Zahl. true: Adresse ist korrekt, false: Angabe ist keine gültige IP4-Internetadresse.
function uc.isUrl( url, skipPathCheck )
  • Die Funktion überprüft, ob eine URL formal syntaktisch korrekt ist. Die Bezeichnungen der Top-Level-Domänen werden noch nicht überprüft. Die formelle Prüfung der Pfadangaben in der URL kann mit skipPathCheck unterdrückt werden.
  • Ergebnis: Integer-Zahl. 0: URL ist wohl formal korrekt. > 0: URL ist fehlerhaft.
function uc.uriEncodePath( url )
function uc.encodePath( frame )
  • Die Funktion liefert die URL mit URI-enkodierten Pfad über einen #invoke-Aufruf.
  • Mögliche Parameter:
    • |url= URL, die enkodiert werden soll.

Benötigte weitere Module

Dieses Modul benötigt folgende weitere Module: UrlCheck/i18n
Hinweise
-- documentation
local UrlCheck = {
	suite  = 'vCard',
	serial = '2020-03-31',
	item   = 40849609
}

-- module import
local ui = mw.loadData( 'Module:UrlCheck/i18n')

-- module variable
local uc = {}

function uc.ip4( address )
	local parts = { address:match( '(%d+)%.(%d+)%.(%d+)%.(%d+)' ) }, value
	if #parts == 4 then
		for _, value in pairs( parts ) do
			if tonumber( value ) < 0 or tonumber( value ) > 255 then
				return false
			end
		end
		return true -- ok
	end
	return false
end

function uc.isUrl( url, skipPathCheck )
	-- return codes 0 through 2 reserved
	if not url or type( url ) ~= 'string' then
		return 3
	end

	local s = mw.text.trim( url ), count, i
	if s == '' then
		return 3
	elseif #s > 2048 then -- limitation because of search engines or IE
		return 4
	elseif s:find( '%s' ) or s:find( '%c' ) or s:match( '^%.' ) then
		return 5
	end
	
	-- https://max:[email protected]:8080/index.html?p1=A&p2=B#ressource

	-- protocol
	s, count = s:gsub( '^https?://', '' )
	if count == 0 then
		s, count = s:gsub( '^//', '' )
	end
	if count == 0 then -- missing or wrong protocol
		return 6
	end

	local user = '', at
	local password = ''
	local host = ''
	local port = ''
	local aPath = ''
	local topLevel = ''

	-- split path from host
	at = s:find( '/' )
	if at then
		aPath = s:sub( at + 1, #s )
		s = s:sub( 1, at - 1 )
		if not s then
			return 7
		end
	end

	-- path check
	if not skipPathCheck and aPath ~= '' then
		-- replacing utf-8 characters > \x7F
		for i = 1, #aPath, 1 do
			if aPath:byte( i ) > 127 then
				aPath = aPath:sub( 1, i - 1 ) .. 'a' .. aPath:sub( i + 1 )
			end
		end
		if not aPath:match( '^[-A-Za-z0-9_.,~%%%+&:;#*?!=()@/]*$' ) then
			return 23
		end
	end

	if s:find( '%.%.' ) or s:find( '%.@' ) or s:find( '@[%.%-]' ) or s:find( '%-%.' )
		or s:find( '%.%-' ) or s:find( '%./' ) or s:find( '/%.' ) then
		return 8
	end

	-- user and password
	_, count = s:gsub( '@', '@' )
	if count > 1 then
		return 9
	elseif count == 1 then
		at = s:find( '@' )
		user = s:sub( 1, at - 1 )
		host = s:sub( at + 1, #s )
		if not user or not s then
			return 10
		end

		_,count = user:gsub( ':', ':' )
		if count > 1 then
			return 11
		elseif count == 1 then
			at = user:find( ':' )
			password = user:sub( at + 1, #user )
			user = user:sub( 1, at - 1 )
			if not user or not password then
				return 12
			elseif #user > 64 then
				return 13
			end
		end
	else
		host = s
	end
	if host == '' then
		return 14
	end

	-- host and port
	_, count = host:gsub( ':', ':' )
	if count > 1 then
		return 15
	elseif count == 1 then
		at = host:find( ':' )
		port = host:sub( at + 1, #host )
		host = host:sub( 1, at - 1 )
		if not host or not port then
			return 16
		elseif not port:match( '^[1-9]%d*$' ) or tonumber( port ) > 65535 then
			return 17
		end
	end

	-- handle host part
	if #host > 253 then
		return 18
	end

	-- getting top-level domain
	at = host:match( '^.*()%.' ) -- find last dot
	if not at then
		return 19
	end
	topLevel = host:sub( at + 1, #host )
	if not topLevel then
		return 20
	end

	-- future: check of top-level domain

	if uc.ip4( host ) then -- is ip4 address
		return 2
	elseif not mw.ustring.match( host, '^[%w%.%-]+%.%a%a+$' ) then
		return 22
	elseif not host:match( '^[%w%.%-]+%.%a%a+$' ) then
		return 1 -- matches domain only in UTF 8 mode
	end

	return 0
end

function uc.uriEncodePath( url )
	local domain, aPath
	local at,_ = url:find( '[^/]/[^/]' )
	if at then
		domain = url:sub( 1, at + 1 )
		aPath = url:sub( at + 2, #url )
		aPath = mw.uri.encode( aPath, 'PATH' )
		url = domain .. aPath
	end
	return url
end

function uc.checkUrl( frame )
	local args = frame.args
	local pArgs = frame:getParent().args
	args.url   = args.url or pArgs.url or ''
	args.show  = args.show or pArgs.show or ''

	local result = uc.isUrl( args.url, false )
	if args.show:lower() == 'msg' then
		if ui[ result ] then
			return ui[ result ]
		else
			return ui.unknown
		end
	end
	return result
end

function uc.encodePath( frame )
	local args = frame.args
	local pArgs = frame:getParent().args
	args.url   = args.url or args[ 1 ] or pArgs.url or pArgs[ 1 ] or ''
	return uc.uriEncodePath( args.url )
end

return uc