Mr. Stradivarius has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/199911

Change subject: Standardise URI query fields with multiple values
......................................................................

Standardise URI query fields with multiple values

This patch standardises the handling of URI query fields with multiple
values in the mw.uri library. This involves two things:

1. Altering mw.uri.buildQueryString to produce foo[0]=bar&foo[1]=baz,
instead of foo=bar&foo=baz. (The square brackets are HTML-encoded.)
2. For query arrays passed from Lua to PHP, normalise array values to
start from 0, not 1.

Bug: T93059
Change-Id: I3ed9529deb7a63dbabdc58d7afb3b0cea9307a7f
---
M engines/LuaCommon/UriLibrary.php
M engines/LuaCommon/lualib/mw.uri.lua
M tests/engines/LuaCommon/UriLibraryTests.lua
3 files changed, 47 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Scribunto 
refs/changes/11/199911/1

diff --git a/engines/LuaCommon/UriLibrary.php b/engines/LuaCommon/UriLibrary.php
index 20012a3..06129fa 100644
--- a/engines/LuaCommon/UriLibrary.php
+++ b/engines/LuaCommon/UriLibrary.php
@@ -21,6 +21,16 @@
        }
 
        private function getUrl( $func, $page, $query ) {
+               # If a query array has arrays as values, make them zero-based.
+               if( gettype( $query ) === 'array' ) {
+                       $query = array_map( function( $val ) {
+                               if( gettype( $val ) === 'array' ) {
+                                       $val = array_merge( array(), $val );
+                               }
+                               return $val;
+                       }, $query );
+               }
+
                $title = Title::newFromText( $page );
                if( !$title ) {
                        $title = Title::newFromURL( urldecode( $page ) );
diff --git a/engines/LuaCommon/lualib/mw.uri.lua 
b/engines/LuaCommon/lualib/mw.uri.lua
index 184ec4a..8074280 100644
--- a/engines/LuaCommon/lualib/mw.uri.lua
+++ b/engines/LuaCommon/lualib/mw.uri.lua
@@ -310,9 +310,15 @@
                if type( v ) ~= 'table' then
                        v = { v }
                end
-               for i = 1, #v do
+               local n = #v
+               for i = 1, n do
                        t[#t+1] = '&'
                        t[#t+1] = rawencode( k, '+' )
+                       if n > 1 then
+                               t[#t+1] = rawencode( '[' )
+                               t[#t+1] = i - 1 -- Subtract 1 as Lua arrays 
start from 1, not 0.
+                               t[#t+1] = rawencode( ']' )
+                       end
                        if v[i] then
                                t[#t+1] = '='
                                t[#t+1] = rawencode( v[i], '+' )
diff --git a/tests/engines/LuaCommon/UriLibraryTests.lua 
b/tests/engines/LuaCommon/UriLibraryTests.lua
index c01fa2e..e26d8cf 100644
--- a/tests/engines/LuaCommon/UriLibraryTests.lua
+++ b/tests/engines/LuaCommon/UriLibraryTests.lua
@@ -66,16 +66,30 @@
                                  bar = false,
                                  baz = { '1', '2' },
                          },
-                         queryString = 'foo=1&bar&baz=1&baz=2',
+                         queryString = 'foo=1&bar&baz%5B0%5D=1&baz%5B1%5D=2',
                          fragment = 'fragment',
-                         relativePath = '/test?foo=1&bar&baz=1&baz=2#fragment',
+                         relativePath = 
'/test?foo=1&bar&baz%5B0%5D=1&baz%5B1%5D=2#fragment',
                  },
          },
        },
 
-       { name = 'uri.new', func = mw.uri.new, type = 'ToString',
+       { name = 'uri.new with tostring', func = mw.uri.new, type = 'ToString',
          args = { 'http://www.example.com/test?foo=1&bar&baz=1&baz=2#fragment' 
},
-         expect = { 
'http://www.example.com/test?foo=1&bar&baz=1&baz=2#fragment' },
+         expect = { 
'http://www.example.com/test?foo=1&bar&baz%5B0%5D=1&baz%5B1%5D=2#fragment' },
+       },
+
+       { name = 'uri.new with duplicate query params', func = mw.uri.new, type 
= 'ToString',
+         args = {
+                 {
+                         protocol = 'http',
+                         host = 'www.example.com',
+                         path = '/test',
+                         query = {
+                                 foo = { 'bar', 'baz' },
+                         },
+                 },
+         },
+         expect = { 
'http://www.example.com/test?foo%5B0%5D=bar&foo%5B1%5D=baz' },
        },
 
        { name = 'uri.localUrl( Example )', func = mw.uri.localUrl, type = 
'ToString',
@@ -90,6 +104,10 @@
          args = { 'Example', { action = 'edit' } },
          expect = { '/w/index.php?title=Example&action=edit' },
        },
+       { name = 'uri.localUrl with duplicate query params', func = 
mw.uri.localUrl, type = 'ToString',
+         args = { 'Example', { foo = { 'bar', 'baz' } } },
+         expect = { '/w/index.php?title=Example&foo%5B0%5D=bar&foo%5B1%5D=baz' 
},
+       },
 
        { name = 'uri.fullUrl( Example )', func = mw.uri.fullUrl, type = 
'ToString',
          args = { 'Example' },
@@ -103,6 +121,10 @@
          args = { 'Example', { action = 'edit' } },
          expect = { '//wiki.local/w/index.php?title=Example&action=edit' },
        },
+       { name = 'uri.fullUrl with duplicate query params', func = 
mw.uri.fullUrl, type = 'ToString',
+         args = { 'Example', { foo = { 'bar', 'baz' } } },
+         expect = { 
'//wiki.local/w/index.php?title=Example&foo%5B0%5D=bar&foo%5B1%5D=baz' },
+       },
 
        { name = 'uri.canonicalUrl( Example )', func = mw.uri.canonicalUrl, 
type = 'ToString',
          args = { 'Example' },
@@ -116,6 +138,10 @@
          args = { 'Example', { action = 'edit' } },
          expect = { 'http://wiki.local/w/index.php?title=Example&action=edit' 
},
        },
+       { name = 'uri.canonicalUrl with duplicate query params', func = 
mw.uri.canonicalUrl, type = 'ToString',
+         args = { 'Example', { foo = { 'bar', 'baz' } } },
+         expect = { 
'http://wiki.local/w/index.php?title=Example&foo%5B0%5D=bar&foo%5B1%5D=baz' },
+       },
 
        { name = 'uri.new with empty query string', func = mw.uri.new, type = 
'ToString',
          args = { 'http://wiki.local/w/index.php?' },

-- 
To view, visit https://gerrit.wikimedia.org/r/199911
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3ed9529deb7a63dbabdc58d7afb3b0cea9307a7f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Scribunto
Gerrit-Branch: master
Gerrit-Owner: Mr. Stradivarius <misterst...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to