Modul:URLutil: Unterschied zwischen den Versionen
updates
w>PerfektesChaos (update) |
w>PerfektesChaos (updates) |
||
Zeile 1: | Zeile 1: | ||
--[=[ URLutil 2014-09- | --[=[ URLutil 2014-09-20 | ||
Utilities for URL etc. on www. | Utilities for URL etc. on www. | ||
* getAuthority() | * getAuthority() | ||
Zeile 17: | Zeile 17: | ||
* isDomain() | * isDomain() | ||
* isDomainExample() | * isDomainExample() | ||
* isDomainInt() | |||
* isHost() | * isHost() | ||
* isIP() | * isIP() | ||
Zeile 58: | Zeile 59: | ||
local getTopDomain = function ( url, mode ) | local getTopDomain = function ( url, mode ) | ||
local | local r = URLutil.getHost( url ) | ||
if | if r then | ||
local pattern = "[%w%%]+%.[ | local pattern = "[%w%%]+%.%a[%w-]*%a)$" | ||
if mode == 3 then | if mode == 3 then | ||
pattern = "[%w%%]+%." .. pattern | pattern = "[%w%%]+%." .. pattern | ||
end | end | ||
r = mw.ustring.match( "." .. r, "%.(" .. pattern ) | |||
if | if not r then | ||
r = false | |||
end | end | ||
else | |||
r = false | |||
end | end | ||
return | return r | ||
end -- getTopDomain() | end -- getTopDomain() | ||
Zeile 75: | Zeile 78: | ||
URLutil.getAuthority = function ( url ) | URLutil.getAuthority = function ( url ) | ||
local r | |||
if type( url ) == "string" then | if type( url ) == "string" then | ||
local | local colon, host, port | ||
local pattern = "^%s*%w*:?//([%w%.%%-]+)(:?)([%d]*)/" | local pattern = "^%s*%w*:?//([%w%.%%-]+)(:?)([%d]*)/" | ||
local i = | local s = mw.text.decode( url ) | ||
local i = s:find( "#", 6, true ) | |||
if i then | if i then | ||
s = | s = s:sub( 1, i - 1 ) .. "/" | ||
else | else | ||
s = | s = s .. "/" | ||
end | end | ||
host, colon, port = mw.ustring.match( s, pattern ) | host, colon, port = mw.ustring.match( s, pattern ) | ||
Zeile 89: | Zeile 94: | ||
if colon == ":" then | if colon == ":" then | ||
if port:find( "^[1-9]" ) then | if port:find( "^[1-9]" ) then | ||
r = ( host .. ":" .. port ) | |||
end | end | ||
elseif #port == 0 then | elseif #port == 0 then | ||
r = host | |||
end | end | ||
end | end | ||
else | |||
r = false | |||
end | end | ||
return | return r | ||
end -- URLutil.getAuthority() | end -- URLutil.getAuthority() | ||
Zeile 104: | Zeile 111: | ||
local r | local r | ||
if type( url ) == "string" then | if type( url ) == "string" then | ||
local i = | local s = mw.text.decode( url ) | ||
local i = s:find( "#", 1, true ) | |||
if i then | if i then | ||
r = mw.text.trim( | r = mw.text.trim( s:sub( i ) ):sub( 2 ) | ||
if type( decode ) == "string" then | if type( decode ) == "string" then | ||
local encoding = mw.text.trim( decode ) | local encoding = mw.text.trim( decode ) | ||
Zeile 133: | Zeile 141: | ||
URLutil.getHost = function ( url ) | URLutil.getHost = function ( url ) | ||
local | local r = URLutil.getAuthority( url ) | ||
if | if r then | ||
r = mw.ustring.match( r, "^([%w%.%%-]+):?[%d]*$" ) | |||
end | end | ||
return | return r | ||
end -- URLutil.getHost() | end -- URLutil.getHost() | ||
Zeile 149: | Zeile 157: | ||
r = false | r = false | ||
else | else | ||
local i = r:find( "#", 1, true ) | local i | ||
r = mw.text.decode( r ) | |||
i = r:find( "#", 1, true ) | |||
if i then | if i then | ||
if i == 1 then | if i == 1 then | ||
Zeile 290: | Zeile 300: | ||
local r | local r | ||
if type( url ) == "string" then | if type( url ) == "string" then | ||
local | local pattern = "^%s*([a-zA-Z]*)(:?)(//)" | ||
local prot, colon, slashes = url:match( pattern ) | |||
r = false | r = false | ||
if slashes == "//" then | if slashes == "//" then | ||
Zeile 310: | Zeile 321: | ||
URLutil.getTLD = function ( url ) | URLutil.getTLD = function ( url ) | ||
local | local r = URLutil.getHost( url ) | ||
if | if r then | ||
r = mw.ustring.match( r, "[%w]+%.(%a[%w-]*%a)$" ) | |||
if not r then | |||
r = false | |||
end | |||
end | end | ||
return | return r | ||
end -- URLutil.getTLD() | end -- URLutil.getTLD() | ||
Zeile 333: | Zeile 346: | ||
URLutil.isAuthority = function ( s ) | URLutil.isAuthority = function ( s ) | ||
local r | |||
if type( s ) == "string" then | if type( s ) == "string" then | ||
local | local pattern = "^%s*([%w%.%%-]+)(:?)(%d*)%s*$" | ||
local host, colon, port = mw.ustring.match( s, pattern ) | |||
if colon == ":" then | if colon == ":" then | ||
port = port:match( "^[1-9][0-9]*$" ) | port = port:match( "^[1-9][0-9]*$" ) | ||
if type( port ) ~= "string" then | if type( port ) ~= "string" then | ||
r = false | |||
end | end | ||
elseif port ~= "" then | elseif port ~= "" then | ||
r = false | |||
end | end | ||
r = URLutil.isHost( host ) | |||
else | |||
r = nil | |||
end | end | ||
return | return r | ||
end -- URLutil.isAuthority() | end -- URLutil.isAuthority() | ||
Zeile 351: | Zeile 368: | ||
URLutil.isDomain = function ( s ) | URLutil.isDomain = function ( s ) | ||
local r | |||
if type( s ) == "string" then | if type( s ) == "string" then | ||
local scan = "^%s*([%w%.%%-]+%w)%.(%a[%w-]*%a)%s*$" | |||
local scope | |||
s, scope = mw.ustring.match( s, scan ) | |||
if type( s ) == "string" then | if type( s ) == "string" then | ||
if mw.ustring.find( s, "^%w" ) then | if mw.ustring.find( s, "^%w" ) then | ||
if mw.ustring.find( s, "..", 1, true ) then | if mw.ustring.find( s, "..", 1, true ) then | ||
r = false | |||
else | else | ||
r = true | |||
end | end | ||
end | end | ||
end | end | ||
else | |||
r = nil | |||
end | end | ||
return | return r | ||
end -- URLutil.isDomain() | end -- URLutil.isDomain() | ||
Zeile 384: | Zeile 406: | ||
return r | return r | ||
end -- URLutil.isDomainExample() | end -- URLutil.isDomainExample() | ||
URLutil.isDomainInt = function ( url ) | |||
-- Internationalized Domain Name (Punycode) | |||
local r = URLutil.getHost( url ) | |||
if r then | |||
if r:match( "^[!-~]+$" ) then | |||
local s = "." .. r | |||
if s:find( ".xn--", 1, true ) then | |||
r = true | |||
else | |||
r = false | |||
end | |||
else | |||
r = true | |||
end | |||
end | |||
return r | |||
end -- URLutil.isDomainInt() | |||
Zeile 444: | Zeile 486: | ||
return ( tonumber( n ) < 256 ) | return ( tonumber( n ) < 256 ) | ||
end | end | ||
local r = false | |||
if type( s ) == "string" then | if type( s ) == "string" then | ||
local p1, p2, p3, p4 = s:match( "^%s*([1-9][0-9]?[0-9]?)%.([12]?[0-9]?[0-9])%.([12]?[0-9]?[0-9])%.([12]?[0-9]?[0-9])%s*$" ) | local p1, p2, p3, p4 = s:match( "^%s*([1-9][0-9]?[0-9]?)%.([12]?[0-9]?[0-9])%.([12]?[0-9]?[0-9])%.([12]?[0-9]?[0-9])%s*$" ) | ||
if p1 and p2 and p3 and p4 then | if p1 and p2 and p3 and p4 then | ||
r = legal( p1 ) and legal( p2 ) and legal( p3 ) and legal( p4 ) | |||
end | end | ||
end | end | ||
return | return r | ||
end -- URLutil.isIPv4() | end -- URLutil.isIPv4() | ||
Zeile 693: | Zeile 736: | ||
function p.isDomainExample( frame ) | function p.isDomainExample( frame ) | ||
return URLutil.isDomainExample( frame.args[ 1 ] ) and "1" or "" | return URLutil.isDomainExample( frame.args[ 1 ] ) and "1" or "" | ||
end | |||
function p.isDomainInt( frame ) | |||
return URLutil.isDomainInt( frame.args[ 1 ] ) and "1" or "" | |||
end | end | ||
function p.isHost( frame ) | function p.isHost( frame ) |