*/
/**
- * Helper tools for dealing with other locally-hosted wikis
+ * Helper tools for dealing with other wikis.
*/
class WikiMap {
* @return WikiReference|null WikiReference object or null if the wiki was not found
*/
public static function getWiki( $wikiID ) {
+ $wikiReference = self::getWikiReferenceFromWgConf( $wikiID );
+ if ( $wikiReference ) {
+ return $wikiReference;
+ }
+
+ // Try sites, if $wgConf failed
+ return self::getWikiWikiReferenceFromSites( $wikiID );
+ }
+
+ /**
+ * @param string $wikiID
+ * @return WikiReference|null WikiReference object or null if the wiki was not found
+ */
+ private static function getWikiReferenceFromWgConf( $wikiID ) {
global $wgConf;
$wgConf->loadFullData();
return new WikiReference( $canonicalServer, $path, $server );
}
+ /**
+ * @param string $wikiID
+ * @return WikiReference|null WikiReference object or null if the wiki was not found
+ */
+ private static function getWikiWikiReferenceFromSites( $wikiID ) {
+ static $siteStore = null;
+ if ( !$siteStore ) {
+ // Replace once T114471 got fixed and don't do the caching here.
+ $siteStore = SiteSQLStore::newInstance();
+ }
+
+ $site = $siteStore->getSite( $wikiID );
+
+ if ( !$site instanceof MediaWikiSite ) {
+ // Abort if not a MediaWikiSite, as this is about Wikis
+ return null;
+ }
+
+ $urlParts = wfParseUrl( $site->getPageUrl() );
+ if ( $urlParts === false || !isset( $urlParts['path'] ) || !isset( $urlParts['host'] ) ) {
+ // We can't create a meaningful WikiReference without URLs
+ return null;
+ }
+
+ // XXX: Check whether path contains a $1?
+ $path = $urlParts['path'];
+ if ( isset( $urlParts['query'] ) ) {
+ $path .= '?' . $urlParts['query'];
+ }
+
+ $canonicalServer = isset( $urlParts['scheme'] ) ? $urlParts['scheme'] : 'http';
+ $canonicalServer .= '://' . $urlParts['host'];
+
+ return new WikiReference( $canonicalServer, $path );
+ }
+
/**
* Convenience to get the wiki's display name
*
/**
* @covers WikiMap
+ *
+ * @group Database
*/
-
class WikiMapTest extends MediaWikiLangTestCase {
public function setUp() {
$this->setMwGlobals( array(
'wgConf' => $conf,
) );
+
+ TestSites::insertIntoDb();
}
public function provideGetWiki() {
+ // As provided by $wgConf
$enwiki = new WikiReference( 'http://en.example.org', '/w/$1' );
$ruwiki = new WikiReference( '//ru.example.org', '/wiki/$1' );
+ // Created from site objects
+ $nlwiki = new WikiReference( 'https://nl.wikipedia.org', '/wiki/$1' );
+ // enwiktionary doesn't have an interwiki id, thus this falls back to minor = lang code
+ $enwiktionary = new WikiReference( 'https://en.wiktionary.org', '/wiki/$1' );
+
return array(
- 'unknown' => array( false, 'xyzzy' ),
- 'enwiki' => array( $enwiki, 'enwiki' ),
- 'ruwiki' => array( $ruwiki, 'ruwiki' ),
+ 'unknown' => array( null, 'xyzzy' ),
+ 'enwiki (wgConf)' => array( $enwiki, 'enwiki' ),
+ 'ruwiki (wgConf)' => array( $ruwiki, 'ruwiki' ),
+ 'nlwiki (sites)' => array( $nlwiki, 'nlwiki', false ),
+ 'enwiktionary (sites)' => array( $enwiktionary, 'enwiktionary', false ),
+ 'non MediaWiki site' => array( null, 'spam', false ),
);
}
/**
* @dataProvider provideGetWiki
*/
- public function testGetWiki( $expected, $wikiId ) {
+ public function testGetWiki( $expected, $wikiId, $useWgConf = true ) {
+ if ( !$useWgConf ) {
+ $this->setMwGlobals( array(
+ 'wgConf' => new SiteConfiguration(),
+ ) );
+ }
+
$this->assertEquals( $expected, WikiMap::getWiki( $wikiId ) );
}
'unknown' => array( 'xyzzy', 'xyzzy' ),
'enwiki' => array( 'en.example.org', 'enwiki' ),
'ruwiki' => array( 'ru.example.org', 'ruwiki' ),
+ 'enwiktionary (sites)' => array( 'en.wiktionary.org', 'enwiktionary' ),
);
}
'Фу',
'вар'
),
+ 'enwiktionary (sites)' => array(
+ '<a class="external" rel="nofollow" ' .
+ 'href="https://en.wiktionary.org/wiki/Kitten">Kittens!</a>',
+ 'enwiktionary',
+ 'Kitten',
+ 'Kittens!'
+ ),
);
}
'Фу',
'вар'
),
+ 'enwiktionary (sites)' => array(
+ '<a class="external" rel="nofollow" ' .
+ 'href="https://en.wiktionary.org/wiki/User:Dummy">Whatever</a>',
+ 'enwiktionary',
+ 'Dummy',
+ 'Whatever'
+ ),
);
}
return array(
'unknown' => array( false, 'xyzzy', 'Foo' ),
'enwiki' => array( 'http://en.example.org/w/Foo', 'enwiki', 'Foo' ),
+ 'enwiktionary (sites)' => array(
+ 'https://en.wiktionary.org/wiki/Testme',
+ 'enwiktionary',
+ 'Testme'
+ ),
'ruwiki with fragment' => array(
'//ru.example.org/wiki/%D0%A4%D1%83#%D0%B2%D0%B0%D1%80',
'ruwiki',