Anomie has uploaded a new change for review.

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


Change subject: Properly handle spaces/underscores in mw.site.namespaces
......................................................................

Properly handle spaces/underscores in mw.site.namespaces

The PHP call that makes mw.site.namespaces work case-insensitively
doesn't handle non-standard spaces/underscores. So standardize them
before the call.

Bug: 56216
Change-Id: I4758478b126858fb581614f64eb15472f42fef51
---
M engines/LuaCommon/SiteLibrary.php
M tests/engines/LuaCommon/SiteLibraryTests.lua
2 files changed, 46 insertions(+), 9 deletions(-)


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

diff --git a/engines/LuaCommon/SiteLibrary.php 
b/engines/LuaCommon/SiteLibrary.php
index 12dd58b..35958f4 100644
--- a/engines/LuaCommon/SiteLibrary.php
+++ b/engines/LuaCommon/SiteLibrary.php
@@ -126,6 +126,8 @@
        public function getNsIndex( $name = null ) {
                global $wgContLang;
                $this->checkType( 'getNsIndex', 1, $name, 'string' );
+               // PHP call is case-insensitive but chokes on non-standard 
spaces/underscores.
+               $name = trim( preg_replace( '/[\s_]+/', '_', $name ), '_' );
                return array( $wgContLang->getNsIndex( $name ) );
        }
 }
diff --git a/tests/engines/LuaCommon/SiteLibraryTests.lua 
b/tests/engines/LuaCommon/SiteLibraryTests.lua
index 308554e..2806f29 100644
--- a/tests/engines/LuaCommon/SiteLibraryTests.lua
+++ b/tests/engines/LuaCommon/SiteLibraryTests.lua
@@ -1,7 +1,17 @@
 local testframework = require 'Module:TestFramework'
 
-local function identity( ... )
-       return ...
+local function nsTest( ... )
+       local args = { ... }
+       local t = mw.site.namespaces
+       local path = 'mw.site.namespaces'
+       for i = 1, #args do
+               t = t[args[i]]
+               path = path .. string.format( '[%q]', args[i] )
+               if t == nil then
+                       error( path .. ' is nil!' )
+               end
+       end
+       return t
 end
 
 return testframework.getTestProvider( {
@@ -39,37 +49,62 @@
        },
 
        { name = 'Project namespace by number',
-         func = identity, args = { mw.site.namespaces[4].canonicalName },
+         func = nsTest, args = { 4, 'canonicalName' },
          expect = { 'Project' }
        },
 
        { name = 'Project namespace by name',
-         func = identity, args = { mw.site.namespaces.Project.id },
+         func = nsTest, args = { 'Project', 'id' },
          expect = { 4 }
        },
 
        { name = 'Project namespace by name (2)',
-         func = identity, args = { mw.site.namespaces.PrOjEcT.canonicalName },
+         func = nsTest, args = { 'PrOjEcT', 'canonicalName' },
          expect = { 'Project' }
        },
 
        { name = 'Project namespace subject is itself',
-         func = identity, args = { 
mw.site.namespaces.Project.subject.canonicalName },
+         func = nsTest, args = { 'Project', 'subject', 'canonicalName' },
          expect = { 'Project' }
        },
 
        { name = 'Project talk namespace via Project',
-         func = identity, args = { 
mw.site.namespaces.Project.talk.canonicalName },
+         func = nsTest, args = { 'Project', 'talk', 'canonicalName' },
          expect = { 'Project talk' }
        },
 
        { name = 'Project namespace via Project talk',
-         func = identity, args = { 
mw.site.namespaces.Project_talk.subject.canonicalName },
+         func = nsTest, args = { 'Project_talk', 'subject', 'canonicalName' },
          expect = { 'Project' }
        },
 
        { name = 'Project talk namespace via Project (associated)',
-         func = identity, args = { 
mw.site.namespaces.Project.associated.canonicalName },
+         func = nsTest, args = { 'Project', 'associated', 'canonicalName' },
          expect = { 'Project talk' }
        },
+
+       { name = 'Project talk namespace by name (standard caps, no 
underscores)',
+         func = nsTest, args = { 'Project talk', 'id' },
+         expect = { 5 }
+       },
+
+       { name = 'Project talk namespace by name (standard caps, underscores)',
+         func = nsTest, args = { 'Project_talk', 'id' },
+         expect = { 5 }
+       },
+
+       { name = 'Project talk namespace by name (odd caps, no underscores)',
+         func = nsTest, args = { 'pRoJeCt tAlK', 'id' },
+         expect = { 5 }
+       },
+
+       { name = 'Project talk namespace by name (odd caps, underscores)',
+         func = nsTest, args = { 'pRoJeCt_tAlK', 'id' },
+         expect = { 5 }
+       },
+
+       { name = 'Project talk namespace by name (extraneous spaces and 
underscores)',
+         func = nsTest, args = { '_ _ _Project_ _talk_ _ _', 'id' },
+         expect = { 5 }
+       },
 } )

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4758478b126858fb581614f64eb15472f42fef51
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Scribunto
Gerrit-Branch: master
Gerrit-Owner: Anomie <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to