3 use MediaWiki\Config\ServiceOptions
;
4 use MediaWiki\Interwiki\ClassicInterwikiLookup
;
5 use MediaWiki\MainConfigNames
;
6 use MediaWiki\WikiMap\WikiMap
;
7 use Wikimedia\ObjectCache\WANObjectCache
;
10 * @covers \MediaWiki\Interwiki\ClassicInterwikiLookup
13 class ClassicInterwikiLookupTest
extends MediaWikiIntegrationTestCase
{
15 private function populateDB( $iwrows ) {
16 $this->db
->newInsertQueryBuilder()
17 ->insertInto( 'interwiki' )
19 ->caller( __METHOD__
)
24 * @param string[]|false $interwikiData
25 * @return ClassicInterwikiLookup
27 private function getClassicInterwikiLookup( $interwikiData ): ClassicInterwikiLookup
{
28 $services = $this->getServiceContainer();
29 $lang = $services->getLanguageFactory()->getLanguage( 'en' );
31 MainConfigNames
::InterwikiExpiry
=> 60 * 60,
32 MainConfigNames
::InterwikiCache
=> $interwikiData,
33 MainConfigNames
::InterwikiFallbackSite
=> 'en',
34 MainConfigNames
::InterwikiScopes
=> 3,
35 MainConfigNames
::InterwikiMagic
=> true,
36 MainConfigNames
::VirtualDomainsMapping
=> [],
37 'wikiId' => WikiMap
::getCurrentWikiId(),
40 return new ClassicInterwikiLookup(
42 ClassicInterwikiLookup
::CONSTRUCTOR_OPTIONS
,
46 WANObjectCache
::newEmpty(),
47 $services->getHookContainer(),
48 $services->getConnectionProvider(),
49 $services->getLanguageNameUtils()
53 public function testDatabaseStorage() {
54 // NOTE: database setup is expensive, so we only do
55 // it once and run all the tests in one go.
58 'iw_url' => 'http://de.wikipedia.org/wiki/',
59 'iw_api' => 'http://de.wikipedia.org/w/api.php',
60 'iw_wikiid' => 'dewiki',
67 'iw_url' => 'http://zzwiki.org/wiki/',
68 'iw_api' => 'http://zzwiki.org/w/api.php',
69 'iw_wikiid' => 'zzwiki',
74 $this->populateDB( [ $dewiki, $zzwiki ] );
75 $lookup = $this->getClassicInterwikiLookup( false );
79 $lookup->getAllPrefixes(),
84 $lookup->getAllPrefixes( true ),
89 $lookup->getAllPrefixes( false ),
93 $this->assertTrue( $lookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
94 $this->assertFalse( $lookup->isValidInterwiki( 'xyz' ), 'unknown prefix is valid' );
96 $this->assertNull( $lookup->fetch( null ), 'no prefix' );
97 $this->assertFalse( $lookup->fetch( 'xyz' ), 'unknown prefix' );
99 $interwiki = $lookup->fetch( 'de' );
100 $this->assertInstanceOf( Interwiki
::class, $interwiki );
101 $this->assertSame( $interwiki, $lookup->fetch( 'de' ), 'in-process caching' );
103 $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
104 $this->assertSame( 'http://de.wikipedia.org/w/api.php', $interwiki->getAPI(), 'getAPI' );
105 $this->assertSame( 'dewiki', $interwiki->getWikiID(), 'getWikiID' );
106 $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
107 $this->assertSame( false, $interwiki->isTranscludable(), 'isTranscludable' );
109 $lookup->invalidateCache( 'de' );
110 $this->assertNotSame( $interwiki, $lookup->fetch( 'de' ), 'invalidate cache' );
114 * @param string $thisSite
115 * @param string[][] $local
116 * @param string[][] $global
120 private function populateHash( $thisSite, $local, $global ) {
122 $hash[ '__sites:' . WikiMap
::getCurrentWikiId() ] = $thisSite;
127 foreach ( $local as $row ) {
128 $prefix = $row['iw_prefix'];
129 $data = $row['iw_local'] . ' ' . $row['iw_url'];
131 $hash[ "_{$thisSite}:{$prefix}" ] = $data;
134 foreach ( $global as $row ) {
135 $prefix = $row['iw_prefix'];
136 $data = $row['iw_local'] . ' ' . $row['iw_url'];
137 $globals[] = $prefix;
138 $hash[ "__global:{$prefix}" ] = $data;
141 $hash[ '__list:__global' ] = implode( ' ', $globals );
142 $hash[ '__list:_' . $thisSite ] = implode( ' ', $locals );
147 public function testArrayStorage() {
150 'iw_url' => 'http://zzwiki.org/wiki/',
155 'iw_url' => 'http://de.wikipedia.org/wiki/',
159 $hash = $this->populateHash(
164 $lookup = $this->getClassicInterwikiLookup( $hash );
167 [ $zzwiki, $dewiki ],
168 $lookup->getAllPrefixes(),
172 $this->assertTrue( $lookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
173 $this->assertTrue( $lookup->isValidInterwiki( 'zz' ), 'known prefix is valid' );
175 $interwiki = $lookup->fetch( 'de' );
176 $this->assertInstanceOf( Interwiki
::class, $interwiki );
178 $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
179 $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
181 $interwiki = $lookup->fetch( 'zz' );
182 $this->assertInstanceOf( Interwiki
::class, $interwiki );
184 $this->assertSame( 'http://zzwiki.org/wiki/', $interwiki->getURL(), 'getURL' );
185 $this->assertSame( false, $interwiki->isLocal(), 'isLocal' );
188 public function testGetAllPrefixes() {
191 'iw_url' => 'https://azz.example.org/',
196 'iw_url' => 'https://de.example.org/',
200 'iw_prefix' => 'azz',
201 'iw_url' => 'https://azz.example.org/',
205 $hash = $this->populateHash(
210 $lookup = $this->getClassicInterwikiLookup( $hash );
214 $lookup->getAllPrefixes(),
215 'getAllPrefixes() - preserves order'