1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
<?php
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Interwiki\ClassicInterwikiLookup;
use MediaWiki\MainConfigNames;
use MediaWiki\WikiMap\WikiMap;
use Wikimedia\ObjectCache\WANObjectCache;
/**
* @covers \MediaWiki\Interwiki\ClassicInterwikiLookup
* @group Database
*/
class ClassicInterwikiLookupTest extends MediaWikiIntegrationTestCase {
private function populateDB( $iwrows ) {
$this->db->newInsertQueryBuilder()
->insertInto( 'interwiki' )
->rows( $iwrows )
->caller( __METHOD__ )
->execute();
}
/**
* @param string[]|false $interwikiData
* @return ClassicInterwikiLookup
*/
private function getClassicInterwikiLookup( $interwikiData ): ClassicInterwikiLookup {
$services = $this->getServiceContainer();
$lang = $services->getLanguageFactory()->getLanguage( 'en' );
$config = [
MainConfigNames::InterwikiExpiry => 60 * 60,
MainConfigNames::InterwikiCache => $interwikiData,
MainConfigNames::InterwikiFallbackSite => 'en',
MainConfigNames::InterwikiScopes => 3,
MainConfigNames::InterwikiMagic => true,
MainConfigNames::VirtualDomainsMapping => [],
'wikiId' => WikiMap::getCurrentWikiId(),
];
return new ClassicInterwikiLookup(
new ServiceOptions(
ClassicInterwikiLookup::CONSTRUCTOR_OPTIONS,
$config
),
$lang,
WANObjectCache::newEmpty(),
$services->getHookContainer(),
$services->getConnectionProvider(),
$services->getLanguageNameUtils()
);
}
public function testDatabaseStorage() {
// NOTE: database setup is expensive, so we only do
// it once and run all the tests in one go.
$dewiki = [
'iw_prefix' => 'de',
'iw_url' => 'http://de.wikipedia.org/wiki/',
'iw_api' => 'http://de.wikipedia.org/w/api.php',
'iw_wikiid' => 'dewiki',
'iw_local' => 1,
'iw_trans' => 0
];
$zzwiki = [
'iw_prefix' => 'zz',
'iw_url' => 'http://zzwiki.org/wiki/',
'iw_api' => 'http://zzwiki.org/w/api.php',
'iw_wikiid' => 'zzwiki',
'iw_local' => 0,
'iw_trans' => 0
];
$this->populateDB( [ $dewiki, $zzwiki ] );
$lookup = $this->getClassicInterwikiLookup( false );
$this->assertEquals(
[ $dewiki, $zzwiki ],
$lookup->getAllPrefixes(),
'getAllPrefixes()'
);
$this->assertEquals(
[ $dewiki ],
$lookup->getAllPrefixes( true ),
'getAllPrefixes()'
);
$this->assertEquals(
[ $zzwiki ],
$lookup->getAllPrefixes( false ),
'getAllPrefixes()'
);
$this->assertTrue( $lookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
$this->assertFalse( $lookup->isValidInterwiki( 'xyz' ), 'unknown prefix is valid' );
$this->assertNull( $lookup->fetch( null ), 'no prefix' );
$this->assertFalse( $lookup->fetch( 'xyz' ), 'unknown prefix' );
$interwiki = $lookup->fetch( 'de' );
$this->assertInstanceOf( Interwiki::class, $interwiki );
$this->assertSame( $interwiki, $lookup->fetch( 'de' ), 'in-process caching' );
$this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
$this->assertSame( 'http://de.wikipedia.org/w/api.php', $interwiki->getAPI(), 'getAPI' );
$this->assertSame( 'dewiki', $interwiki->getWikiID(), 'getWikiID' );
$this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
$this->assertSame( false, $interwiki->isTranscludable(), 'isTranscludable' );
$lookup->invalidateCache( 'de' );
$this->assertNotSame( $interwiki, $lookup->fetch( 'de' ), 'invalidate cache' );
}
/**
* @param string $thisSite
* @param string[][] $local
* @param string[][] $global
*
* @return string[]
*/
private function populateHash( $thisSite, $local, $global ) {
$hash = [];
$hash[ '__sites:' . WikiMap::getCurrentWikiId() ] = $thisSite;
$globals = [];
$locals = [];
foreach ( $local as $row ) {
$prefix = $row['iw_prefix'];
$data = $row['iw_local'] . ' ' . $row['iw_url'];
$locals[] = $prefix;
$hash[ "_{$thisSite}:{$prefix}" ] = $data;
}
foreach ( $global as $row ) {
$prefix = $row['iw_prefix'];
$data = $row['iw_local'] . ' ' . $row['iw_url'];
$globals[] = $prefix;
$hash[ "__global:{$prefix}" ] = $data;
}
$hash[ '__list:__global' ] = implode( ' ', $globals );
$hash[ '__list:_' . $thisSite ] = implode( ' ', $locals );
return $hash;
}
public function testArrayStorage() {
$zzwiki = [
'iw_prefix' => 'zz',
'iw_url' => 'http://zzwiki.org/wiki/',
'iw_local' => 0
];
$dewiki = [
'iw_prefix' => 'de',
'iw_url' => 'http://de.wikipedia.org/wiki/',
'iw_local' => 1
];
$hash = $this->populateHash(
'en',
[ $dewiki ],
[ $zzwiki ]
);
$lookup = $this->getClassicInterwikiLookup( $hash );
$this->assertEquals(
[ $zzwiki, $dewiki ],
$lookup->getAllPrefixes(),
'getAllPrefixes()'
);
$this->assertTrue( $lookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
$this->assertTrue( $lookup->isValidInterwiki( 'zz' ), 'known prefix is valid' );
$interwiki = $lookup->fetch( 'de' );
$this->assertInstanceOf( Interwiki::class, $interwiki );
$this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
$this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
$interwiki = $lookup->fetch( 'zz' );
$this->assertInstanceOf( Interwiki::class, $interwiki );
$this->assertSame( 'http://zzwiki.org/wiki/', $interwiki->getURL(), 'getURL' );
$this->assertSame( false, $interwiki->isLocal(), 'isLocal' );
}
public function testGetAllPrefixes() {
$zz = [
'iw_prefix' => 'zz',
'iw_url' => 'https://azz.example.org/',
'iw_local' => 1
];
$de = [
'iw_prefix' => 'de',
'iw_url' => 'https://de.example.org/',
'iw_local' => 1
];
$azz = [
'iw_prefix' => 'azz',
'iw_url' => 'https://azz.example.org/',
'iw_local' => 1
];
$hash = $this->populateHash(
'en',
[],
[ $zz, $de, $azz ]
);
$lookup = $this->getClassicInterwikiLookup( $hash );
$this->assertEquals(
[ $zz, $de, $azz ],
$lookup->getAllPrefixes(),
'getAllPrefixes() - preserves order'
);
}
}
|