Improve WikiMap::getWikiReferenceFromWgConf()
authorBrad Jorsch <bjorsch@wikimedia.org>
Wed, 6 Jul 2016 18:01:03 +0000 (14:01 -0400)
committerBrad Jorsch <bjorsch@wikimedia.org>
Wed, 6 Jul 2016 18:20:07 +0000 (14:20 -0400)
If we don't have a valid canonical server and path to pass, there's no
point in returning a WikiReference that will fail in strange and unusal
ways.

This also documents that $wgServer/$wgCanonicalServer and $wgArticlePath
are required in SiteConfiguration.

Change-Id: Ib08011e9f1d0817a5d1bb165aba6b424785eaa6a

includes/SiteConfiguration.php
includes/WikiMap.php
tests/phpunit/includes/WikiMapTest.php

index 1a92fb2..4c048f7 100644 (file)
  * extract( $globals );
  * @endcode
  *
+ * @note For WikiMap to function, the configuration must define string values for
+ *  $wgServer (or $wgCanonicalServer) and $wgArticlePath, even if these are the
+ *  same for all wikis or can be correctly determined by the logic in
+ *  Setup.php.
+ *
  * @todo Give examples for,
  * suffixes:
  * $conf->suffixes = array( 'wiki' );
index cf97984..37f85ea 100644 (file)
@@ -65,6 +65,14 @@ class WikiMap {
 
                $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
                        [ 'lang' => $minor, 'site' => $major ] );
+
+               // If we don't have a canonical server or a path containing $1, the
+               // WikiReference isn't going to function properly. Just return null in
+               // that case.
+               if ( !is_string( $canonicalServer ) || !is_string( $path ) || strpos( $path, '$1' ) === false ) {
+                       return null;
+               }
+
                return new WikiReference( $canonicalServer, $path, $server );
        }
 
index 4e22e3c..12878b3 100644 (file)
@@ -15,6 +15,7 @@ class WikiMapTest extends MediaWikiLangTestCase {
                        'wgServer' => [
                                'enwiki' => 'http://en.example.org',
                                'ruwiki' => '//ru.example.org',
+                               'nopathwiki' => '//nopath.example.org',
                        ],
                        'wgArticlePath' => [
                                'enwiki' => '/w/$1',
@@ -46,6 +47,8 @@ class WikiMapTest extends MediaWikiLangTestCase {
                        'nlwiki (sites)' => [ $nlwiki, 'nlwiki', false ],
                        'enwiktionary (sites)' => [ $enwiktionary, 'enwiktionary', false ],
                        'non MediaWiki site' => [ null, 'spam', false ],
+                       'boguswiki' => [ null, 'boguswiki' ],
+                       'nopathwiki' => [ null, 'nopathwiki' ],
                ];
        }