Die Dokumentation für dieses Modul kann unter Modul:URLutil/doc erstellt werden
--[=[ URLutil: Utilities for URL etc. * getAuthority() * getHost() * getScheme() * isIP() * isIPv4() * isIPv6() * isMailAddress() Only [[dotted decimal]] notation for IPv4 supported. Does not support dotted hexadecimal, dotted octal, or single-number formats. Functions are not "local", so other modules can require this module and call them directly. We return an object with small stub functions to call the real ones so that the functions can be called from templates also. ---- Based upon w:en:Special:Permalink/542839577?title=Module:IPAddress 2013-03-01 Unit tests at :en:Module:IPAddress/tests ]=] function _getAuthority( url ) if type( url ) == "string" then local host, colon, port = mw.ustring.match( url .. "/", "^%s*%w*:?//([%w%.%%-]+)(:?)([%d]*)/" ) if isHost( host ) then host = mw.ustring.lower( host ) if colon == ":" then if port:match( "^[1-9]" ) then return ( host .. ":" .. port ) end elseif #port == 0 then return host end end end return false end -- _getAuthority() function _getHost( url ) if type( url ) == "string" then local auth = _getAuthority( url ) if type( auth ) == "string" then return mw.ustring.match( auth, "^([%w%.%%-]+):?[%d]*$" ) end end return false end -- _getHost() function _getScheme( url ) if type( url ) == "string" then local prot, colon, slashes = url:match( "^%s*([a-zA-Z]*)(:?)(//)" ) if slashes == "//" then if colon == ":" then if #prot > 2 then return prot:lower() .. "://" end elseif #prot == 0 then return "//" end end end return false end -- _getScheme() function isHost( s ) -- internal only if type( s ) == "string" then if _isIP( s ) then return s elseif type( mw.ustring.match( s, "^([%w%.%%-]+%w)%.[a-zA-Z][a-zA-Z]+$" ) ) == "string" then if mw.ustring.match( s, "^%w" ) then if mw.ustring.find( s, "..", 1, true ) then return false else return s end end end end return false end -- isHost() function _isIP( s ) return _isIPv4( s ) and "4" or _isIPv6( s ) and "6" end -- _isIP() function _isIPv4( s ) local function legal( n ) return ( tonumber( n ) or 256 ) < 256 and not n:match( "^0%d" ) end -- in lua 0 is true! if type( s ) ~= "string" then return false end local p1, p2, p3, p4 = s:match( "^%s*(%d+)%.(%d+)%.(%d+)%.(%d+)%s*$" ) return legal( p1 ) and legal( p2 ) and legal( p3 ) and legal( p4 ) end -- _isIPv4() function _isIPv6( s ) local dcolon, groups if type( s ) ~= "string" or s:len() == 0 or s:find( "[^:%x]" ) -- only colon and hex digits are legal chars or s:find( "^:[^:]" ) -- can begin or end with :: but not with single : or s:find( "[^:]:$" ) or s:find( ":::" ) then return false end s = mw.text.trim( s ) s, dcolon = s:gsub( "::", ":" ) if dcolon > 1 then return false end -- at most one :: s = s:gsub( "^:?", ":" ) -- prepend : if needed, upper s, groups = s:gsub( ":%x%x?%x?%x?", "" ) -- remove valid groups, and count them return ( ( dcolon == 1 and groups < 8 ) or ( dcolon == 0 and groups == 8 ) ) and ( s:len() == 0 or ( dcolon == 1 and s == ":" ) ) -- might be one dangling : if original ended with :: end -- _isIPv6() function _isMailAddress( s ) if type( s ) == "string" then s = mw.ustring.match( s, "^%s*[%w%.%%_-]+@([%w%.%%-]+)%s*" ) return isHost( s ) end return false end -- _isMailAddress() -- Provide template access local p = {} function p.getAuthority( frame ) return _getAuthority( frame.args[ 1 ] ) or "" end function p.getHost( frame ) return _getHost( frame.args[ 1 ] ) or "" end function p.getScheme( frame ) return _getScheme( frame.args[ 1 ] ) or "" end function p.isIP( frame ) return _isIP( frame.args[ 1 ] ) or "" end function p.isIPv4( frame ) return _isIPv4( frame.args[ 1 ] ) and "1" or "0" end function p.isIPv6( frame ) return _isIPv6( frame.args[ 1 ] ) and "1" or "0" end function p.isMailAddress( frame ) return _isMailAddress( frame.args[ 1 ] ) and "1" or "0" end return p