From: Brad Jorsch Date: Wed, 6 Jul 2016 18:01:03 +0000 (-0400) Subject: Improve WikiMap::getWikiReferenceFromWgConf() X-Git-Tag: 1.31.0-rc.0~6240^2 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/banques/ajouter.php?a=commitdiff_plain;h=010410265a5505b44a273bb119d8fac7f1fa7739;p=lhc%2Fweb%2Fwiklou.git 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 --- 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' ], ]; }