mirror of
https://github.com/gnosygnu/xowa.git
synced 2025-06-13 12:54:14 +00:00
Scribunto: Synchronize mw.uri.lua file with changes for uri.encode(s, 'WIKI') [#504]
This commit is contained in:
parent
9da8b4dde8
commit
9b51a7c660
@ -1,3 +1,4 @@
|
|||||||
|
-- SYNC:2019-06-29;https://github.com/wikimedia/mediawiki-extensions-Scribunto/blob/master/includes/engines/LuaCommon/lualib/mw.uri.lua
|
||||||
local uri = {}
|
local uri = {}
|
||||||
local urifuncs = {}
|
local urifuncs = {}
|
||||||
local urimt = {}
|
local urimt = {}
|
||||||
@ -36,6 +37,17 @@ local function rawencode( s, space )
|
|||||||
return ret
|
return ret
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function wikiencode( s )
|
||||||
|
local ret = string.gsub( s, '([^a-zA-Z0-9!$()*,./:;@~_-])', function ( c )
|
||||||
|
if c == ' ' then
|
||||||
|
return '_'
|
||||||
|
else
|
||||||
|
return string.format( '%%%02X', string.byte( c, 1, 1 ) )
|
||||||
|
end
|
||||||
|
end );
|
||||||
|
return ret
|
||||||
|
end
|
||||||
|
|
||||||
local function rawdecode( s )
|
local function rawdecode( s )
|
||||||
local ret = string.gsub( s, '%%(%x%x)', function ( hex )
|
local ret = string.gsub( s, '%%(%x%x)', function ( hex )
|
||||||
return string.char( tonumber( hex, 16 ) )
|
return string.char( tonumber( hex, 16 ) )
|
||||||
@ -52,7 +64,7 @@ function uri.encode( s, enctype )
|
|||||||
elseif enctype == 'PATH' then
|
elseif enctype == 'PATH' then
|
||||||
return rawencode( s, '%20' )
|
return rawencode( s, '%20' )
|
||||||
elseif enctype == 'WIKI' then
|
elseif enctype == 'WIKI' then
|
||||||
return rawencode( s, '_' )
|
return wikiencode( s )
|
||||||
else
|
else
|
||||||
error( "bad argument #2 to 'encode' (expected QUERY, PATH, or WIKI)", 2 )
|
error( "bad argument #2 to 'encode' (expected QUERY, PATH, or WIKI)", 2 )
|
||||||
end
|
end
|
||||||
@ -181,7 +193,12 @@ function uri.validate( obj )
|
|||||||
if obj.host then
|
if obj.host then
|
||||||
if type( obj.host ) ~= 'string' then
|
if type( obj.host ) ~= 'string' then
|
||||||
err[#err+1] = '.host must be a string, not ' .. type( obj.host )
|
err[#err+1] = '.host must be a string, not ' .. type( obj.host )
|
||||||
elseif not string.match( obj.host, '^[^:/?#]*$' ) then
|
elseif not (
|
||||||
|
-- Normal syntax
|
||||||
|
string.match( obj.host, '^[^:/?#]*$' ) or
|
||||||
|
-- IP-literal syntax
|
||||||
|
string.match( obj.host, '^%[[^/?#%[%]@]+%]$' )
|
||||||
|
) then
|
||||||
err[#err+1] = 'invalid .host'
|
err[#err+1] = 'invalid .host'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -290,7 +307,7 @@ function uri.parseQueryString( s, i, j )
|
|||||||
v = rawdecode( string.sub( s, eq + 1, amp - 1 ) )
|
v = rawdecode( string.sub( s, eq + 1, amp - 1 ) )
|
||||||
end
|
end
|
||||||
if qs[k] then
|
if qs[k] then
|
||||||
if type( qs[k] ) ~= table then
|
if type( qs[k] ) ~= 'table' then
|
||||||
qs[k] = { qs[k], v }
|
qs[k] = { qs[k], v }
|
||||||
else
|
else
|
||||||
table.insert( qs[k], v )
|
table.insert( qs[k], v )
|
||||||
@ -440,15 +457,27 @@ function urimt:__newindex( key, value )
|
|||||||
local host, port = nil, nil
|
local host, port = nil, nil
|
||||||
if value then
|
if value then
|
||||||
checkTypeForIndex( key, value, 'string' )
|
checkTypeForIndex( key, value, 'string' )
|
||||||
local i = string.find( value, ':', 1, true )
|
|
||||||
if i then
|
-- IP-literal syntax, with and without a port
|
||||||
host = string.sub( value, 1, i - 1 )
|
host, port = string.match( value, '^(%[[^/?#%[%]@]+%]):(%d+)$' )
|
||||||
port = tonumber( string.sub( value, i + 1 ) )
|
if port then
|
||||||
if not port then
|
port = tonumber( port )
|
||||||
error( string.format( "Invalid port in '%s'", value ), 2 )
|
end
|
||||||
|
if not host then
|
||||||
|
host = string.match( value, '^(%[[^/?#%[%]@]+%])$' )
|
||||||
|
end
|
||||||
|
-- Normal syntax
|
||||||
|
if not host then
|
||||||
|
local i = string.find( value, ':', 1, true )
|
||||||
|
if i then
|
||||||
|
host = string.sub( value, 1, i - 1 )
|
||||||
|
port = tonumber( string.sub( value, i + 1 ) )
|
||||||
|
if not port then
|
||||||
|
error( string.format( "Invalid port in '%s'", value ), 2 )
|
||||||
|
end
|
||||||
|
else
|
||||||
|
host = value
|
||||||
end
|
end
|
||||||
else
|
|
||||||
host = value
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rawset( self, 'host', host )
|
rawset( self, 'host', host )
|
||||||
|
Loading…
Reference in New Issue
Block a user