From 010410265a5505b44a273bb119d8fac7f1fa7739 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Wed, 6 Jul 2016 14:01:03 -0400 Subject: [PATCH] Improve WikiMap::getWikiReferenceFromWgConf() 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 | 5 +++++ includes/WikiMap.php | 8 ++++++++ tests/phpunit/includes/WikiMapTest.php | 3 +++ 3 files changed, 16 insertions(+) diff --git a/includes/SiteConfiguration.php b/includes/SiteConfiguration.php index 1a92fb236a..4c048f7aa3 100644 --- a/includes/SiteConfiguration.php +++ b/includes/SiteConfiguration.php @@ -108,6 +108,11 @@ * 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' ); diff --git a/includes/WikiMap.php b/includes/WikiMap.php index cf97984ce1..37f85ea27c 100644 --- a/includes/WikiMap.php +++ b/includes/WikiMap.php @@ -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 ); } diff --git a/tests/phpunit/includes/WikiMapTest.php b/tests/phpunit/includes/WikiMapTest.php index 4e22e3c7d6..12878b37ed 100644 --- a/tests/phpunit/includes/WikiMapTest.php +++ b/tests/phpunit/includes/WikiMapTest.php @@ -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' ], ]; } -- 2.20.1