Modul:UrlCheck: Unterschied zwischen den Versionen

K
30 Versionen von wikivoyage:Modul:UrlCheck importiert
K (+: in pattern)
K (30 Versionen von wikivoyage:Modul:UrlCheck importiert)
 
(17 dazwischenliegende Versionen von 3 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
-- documentation
-- module variable and administration
local UrlCheck = {
local uc = {
suite  = 'vCard',
moduleInterface = {
serial = '2020-03-28',
suite  = 'UrlCheck',
item  = 40849609
serial = '2023-01-17',
item  = 40849609
}
}
}
-- module import
local ui = mw.loadData( 'Module:UrlCheck/i18n')
-- module variable
local uc = {}


function uc.ip4( address )
function uc.ip4( address )
local parts = { address:match( '(%d+)%.(%d+)%.(%d+)%.(%d+)' ) }, value
local parts = { address:match( '(%d+)%.(%d+)%.(%d+)%.(%d+)' ) }
local value
if #parts == 4 then
if #parts == 4 then
for _, value in pairs( parts ) do
for _, value in pairs( parts ) do
Zeile 25: Zeile 22:
end
end


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


local s = mw.text.trim( url ), count
local s = mw.text.trim( url )
if s == '' then
if s == '' then
return 3
return 3
Zeile 43: Zeile 40:


-- protocol
-- protocol
local count
s, count = s:gsub( '^https?://', '' )
s, count = s:gsub( '^https?://', '' )
if count == 0 then
if count == 0 then
Zeile 51: Zeile 49:
end
end


local user = '', at
local user = ''
local password = ''
local password = ''
local host = ''
local host = ''
Zeile 59: Zeile 57:


-- split path from host
-- split path from host
at = s:find( '/' )
local at = s:find( '/' )
if at then
if at then
aPath = s:sub( at + 1, #s )
aPath = s:sub( at + 1, #s )
Zeile 69: Zeile 67:


-- path check
-- path check
if aPath ~= '' and
if not skipPathCheck and aPath ~= '' then
not mw.ustring.match( aPath, '^[-A-Za-z0-9¡-ÿ_.,~%%+&:;#*?!=()@/]*$' ) then
if not aPath:match( '^[-A-Za-z0-9_.,~%%%+&:;#*?!=()@/\128-\255]*$' ) then
return 23
return 23
end
end
end


Zeile 80: Zeile 79:


-- user and password
-- user and password
_, count = s:gsub( '@', '@' )
s, count = s:gsub( '@', '@' )
if count > 1 then
if count > 1 then
return 9
return 9
Zeile 91: Zeile 90:
end
end


_,count = user:gsub( ':', ':' )
user, count = user:gsub( ':', ':' )
if count > 1 then
if count > 1 then
return 11
return 11
Zeile 112: Zeile 111:


-- host and port
-- host and port
_, count = host:gsub( ':', ':' )
host, count = host:gsub( ':', ':' )
if count > 1 then
if count > 1 then
return 15
return 15
Zeile 145: Zeile 144:
if uc.ip4( host ) then -- is ip4 address
if uc.ip4( host ) then -- is ip4 address
return 2
return 2
elseif not mw.ustring.match( host, '^[%w%.%-]+%.%a%a+$' ) then
elseif not mw.ustring.match( host, '^[ะ-๏%w%.%-]+%.%a%a+$' ) then
-- Thai diacritical marks ะ (0E30) - ๏ (0E4F)
return 22
return 22
elseif not host:match( '^[%w%.%-]+%.%a%a+$' ) then
elseif not host:match( '^[%w%.%-]+%.%a%a+$' ) then
Zeile 152: Zeile 152:


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


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


local result = uc.isUrl( args.url )
local result = uc.isUrl( args.url, false )
if args.show:lower() == 'msg' then
if args.show:lower() == 'msg' then
local ui = mw.loadData( 'Module:UrlCheck/i18n')
if ui[ result ] then
if ui[ result ] then
return ui[ result ]
return ui[ result ]
Zeile 168: Zeile 180:
end
end
return result
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
end


return uc
return uc