From ca85f200998f988fd630658e05b87e1ab530551c Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Mon, 28 Aug 2017 10:32:49 -0400 Subject: [PATCH] Make mw.uri.encode 'WIKI' mode match core {{urlencode:}} The core {{urlencode:}} parser function doesn't encode various characters in WIKI mode that it does in other modes. mw.uri.encode should match that. Bug: T174239 Change-Id: I2be0811cf39c02c5c0ad3433e4b0ef9030350e24 --- engines/LuaCommon/lualib/mw.uri.lua | 13 ++++++++++++- tests/phpunit/engines/LuaCommon/UriLibraryTests.lua | 12 ++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/engines/LuaCommon/lualib/mw.uri.lua b/engines/LuaCommon/lualib/mw.uri.lua index 6d01b054..ff00eae8 100644 --- a/engines/LuaCommon/lualib/mw.uri.lua +++ b/engines/LuaCommon/lualib/mw.uri.lua @@ -35,6 +35,17 @@ local function rawencode( s, space ) return ret 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 ret = string.gsub( s, '%%(%x%x)', function ( hex ) return string.char( tonumber( hex, 16 ) ) @@ -51,7 +62,7 @@ function uri.encode( s, enctype ) elseif enctype == 'PATH' then return rawencode( s, '%20' ) elseif enctype == 'WIKI' then - return rawencode( s, '_' ) + return wikiencode( s ) else error( "bad argument #2 to 'encode' (expected QUERY, PATH, or WIKI)", 2 ) end diff --git a/tests/phpunit/engines/LuaCommon/UriLibraryTests.lua b/tests/phpunit/engines/LuaCommon/UriLibraryTests.lua index c01fa2e2..7bf68c93 100644 --- a/tests/phpunit/engines/LuaCommon/UriLibraryTests.lua +++ b/tests/phpunit/engines/LuaCommon/UriLibraryTests.lua @@ -18,16 +18,16 @@ local tests = { expect = { '__foo+b%C3%A1r+%2B+baz__' } }, { name = 'uri.encode QUERY', func = mw.uri.encode, - args = { '__foo b\195\161r + baz__', 'QUERY' }, - expect = { '__foo+b%C3%A1r+%2B+baz__' } + args = { '__foo b\195\161r + /baz/__', 'QUERY' }, + expect = { '__foo+b%C3%A1r+%2B+%2Fbaz%2F__' } }, { name = 'uri.encode PATH', func = mw.uri.encode, - args = { '__foo b\195\161r + baz__', 'PATH' }, - expect = { '__foo%20b%C3%A1r%20%2B%20baz__' } + args = { '__foo b\195\161r + /baz/__', 'PATH' }, + expect = { '__foo%20b%C3%A1r%20%2B%20%2Fbaz%2F__' } }, { name = 'uri.encode WIKI', func = mw.uri.encode, - args = { '__foo b\195\161r + baz__', 'WIKI' }, - expect = { '__foo_b%C3%A1r_%2B_baz__' } + args = { '__foo b\195\161r + /baz/__', 'WIKI' }, + expect = { '__foo_b%C3%A1r_%2B_/baz/__' } }, { name = 'uri.decode', func = mw.uri.decode,