MediaWiki REL1_29
InterwikiTest.php
Go to the documentation of this file.
1<?php
3
11
12 public function testConstructor() {
13 $interwiki = new Interwiki(
14 'xyz',
15 'http://xyz.acme.test/wiki/$1',
16 'http://xyz.acme.test/w/api.php',
17 'xyzwiki',
18 1,
19 0
20 );
21
22 $this->setContentLang( 'qqx' );
23
24 $this->assertSame( '(interwiki-name-xyz)', $interwiki->getName() );
25 $this->assertSame( '(interwiki-desc-xyz)', $interwiki->getDescription() );
26 $this->assertSame( 'http://xyz.acme.test/w/api.php', $interwiki->getAPI() );
27 $this->assertSame( 'http://xyz.acme.test/wiki/$1', $interwiki->getURL() );
28 $this->assertSame( 'xyzwiki', $interwiki->getWikiID() );
29 $this->assertTrue( $interwiki->isLocal() );
30 $this->assertFalse( $interwiki->isTranscludable() );
31 }
32
33 public function testGetUrl() {
34 $interwiki = new Interwiki(
35 'xyz',
36 'http://xyz.acme.test/wiki/$1'
37 );
38
39 $this->assertSame( 'http://xyz.acme.test/wiki/$1', $interwiki->getURL() );
40 $this->assertSame( 'http://xyz.acme.test/wiki/Foo%26Bar', $interwiki->getURL( 'Foo&Bar' ) );
41 }
42
44
45 private function populateDB( $iwrows ) {
46 $dbw = wfGetDB( DB_MASTER );
47 $dbw->delete( 'interwiki', '*', __METHOD__ );
48 $dbw->insert( 'interwiki', array_values( $iwrows ), __METHOD__ );
49 $this->tablesUsed[] = 'interwiki';
50 }
51
52 private function setWgInterwikiCache( $interwikiCache ) {
53 $this->overrideMwServices();
54 MediaWikiServices::getInstance()->resetServiceForTesting( 'InterwikiLookup' );
55 $this->setMwGlobals( 'wgInterwikiCache', $interwikiCache );
56 }
57
58 public function testDatabaseStorage() {
59 $this->markTestSkipped( 'Needs I37b8e8018b3 <https://gerrit.wikimedia.org/r/#/c/270555/>' );
60
61 // NOTE: database setup is expensive, so we only do
62 // it once and run all the tests in one go.
63 $dewiki = [
64 'iw_prefix' => 'de',
65 'iw_url' => 'http://de.wikipedia.org/wiki/',
66 'iw_api' => 'http://de.wikipedia.org/w/api.php',
67 'iw_wikiid' => 'dewiki',
68 'iw_local' => 1,
69 'iw_trans' => 0
70 ];
71
72 $zzwiki = [
73 'iw_prefix' => 'zz',
74 'iw_url' => 'http://zzwiki.org/wiki/',
75 'iw_api' => 'http://zzwiki.org/w/api.php',
76 'iw_wikiid' => 'zzwiki',
77 'iw_local' => 0,
78 'iw_trans' => 0
79 ];
80
81 $this->populateDB( [ $dewiki, $zzwiki ] );
82
83 $this->setWgInterwikiCache( false );
84
85 $interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
86 $this->assertEquals(
87 [ $dewiki, $zzwiki ],
88 $interwikiLookup->getAllPrefixes(),
89 'getAllPrefixes()'
90 );
91 $this->assertEquals(
92 [ $dewiki ],
93 $interwikiLookup->getAllPrefixes( true ),
94 'getAllPrefixes()'
95 );
96 $this->assertEquals(
97 [ $zzwiki ],
98 $interwikiLookup->getAllPrefixes( false ),
99 'getAllPrefixes()'
100 );
101
102 $this->assertTrue( $interwikiLookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
103 $this->assertFalse( $interwikiLookup->isValidInterwiki( 'xyz' ), 'unknown prefix is valid' );
104
105 $this->assertNull( $interwikiLookup->fetch( null ), 'no prefix' );
106 $this->assertFalse( $interwikiLookup->fetch( 'xyz' ), 'unknown prefix' );
107
108 $interwiki = $interwikiLookup->fetch( 'de' );
109 $this->assertInstanceOf( 'Interwiki', $interwiki );
110 $this->assertSame( $interwiki, $interwikiLookup->fetch( 'de' ), 'in-process caching' );
111
112 $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
113 $this->assertSame( 'http://de.wikipedia.org/w/api.php', $interwiki->getAPI(), 'getAPI' );
114 $this->assertSame( 'dewiki', $interwiki->getWikiID(), 'getWikiID' );
115 $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
116 $this->assertSame( false, $interwiki->isTranscludable(), 'isTranscludable' );
117
119 $this->assertNotSame( $interwiki, $interwikiLookup->fetch( 'de' ), 'invalidate cache' );
120 }
121
129 private function populateHash( $thisSite, $local, $global ) {
130 $hash = [];
131 $hash[ '__sites:' . wfWikiID() ] = $thisSite;
132
133 $globals = [];
134 $locals = [];
135
136 foreach ( $local as $row ) {
137 $prefix = $row['iw_prefix'];
138 $data = $row['iw_local'] . ' ' . $row['iw_url'];
139 $locals[] = $prefix;
140 $hash[ "_{$thisSite}:{$prefix}" ] = $data;
141 }
142
143 foreach ( $global as $row ) {
144 $prefix = $row['iw_prefix'];
145 $data = $row['iw_local'] . ' ' . $row['iw_url'];
146 $globals[] = $prefix;
147 $hash[ "__global:{$prefix}" ] = $data;
148 }
149
150 $hash[ '__list:__global' ] = implode( ' ', $globals );
151 $hash[ '__list:_' . $thisSite ] = implode( ' ', $locals );
152
153 return $hash;
154 }
155
156 private function populateCDB( $thisSite, $local, $global ) {
157 $cdbFile = tempnam( wfTempDir(), 'MW-ClassicInterwikiLookupTest-' ) . '.cdb';
158 $cdb = CdbWriter::open( $cdbFile );
159
160 $hash = $this->populateHash( $thisSite, $local, $global );
161
162 foreach ( $hash as $key => $value ) {
163 $cdb->set( $key, $value );
164 }
165
166 $cdb->close();
167 return $cdbFile;
168 }
169
170 public function testCDBStorage() {
171 // NOTE: CDB setup is expensive, so we only do
172 // it once and run all the tests in one go.
173
174 $dewiki = [
175 'iw_prefix' => 'de',
176 'iw_url' => 'http://de.wikipedia.org/wiki/',
177 'iw_local' => 1
178 ];
179
180 $zzwiki = [
181 'iw_prefix' => 'zz',
182 'iw_url' => 'http://zzwiki.org/wiki/',
183 'iw_local' => 0
184 ];
185
186 $cdbFile = $this->populateCDB(
187 'en',
188 [ $dewiki ],
189 [ $zzwiki ]
190 );
191
192 $this->setWgInterwikiCache( $cdbFile );
193
194 $interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
195 $this->assertEquals(
196 [ $dewiki, $zzwiki ],
197 $interwikiLookup->getAllPrefixes(),
198 'getAllPrefixes()'
199 );
200
201 $this->assertTrue( $interwikiLookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
202 $this->assertTrue( $interwikiLookup->isValidInterwiki( 'zz' ), 'known prefix is valid' );
203
204 $interwiki = $interwikiLookup->fetch( 'de' );
205 $this->assertInstanceOf( 'Interwiki', $interwiki );
206
207 $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
208 $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
209
210 $interwiki = $interwikiLookup->fetch( 'zz' );
211 $this->assertInstanceOf( 'Interwiki', $interwiki );
212
213 $this->assertSame( 'http://zzwiki.org/wiki/', $interwiki->getURL(), 'getURL' );
214 $this->assertSame( false, $interwiki->isLocal(), 'isLocal' );
215
216 // cleanup temp file
217 unlink( $cdbFile );
218 }
219
220 public function testArrayStorage() {
221 $dewiki = [
222 'iw_prefix' => 'de',
223 'iw_url' => 'http://de.wikipedia.org/wiki/',
224 'iw_local' => 1
225 ];
226
227 $zzwiki = [
228 'iw_prefix' => 'zz',
229 'iw_url' => 'http://zzwiki.org/wiki/',
230 'iw_local' => 0
231 ];
232
233 $cdbData = $this->populateHash(
234 'en',
235 [ $dewiki ],
236 [ $zzwiki ]
237 );
238
239 $this->setWgInterwikiCache( $cdbData );
240
241 $interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
242 $this->assertEquals(
243 [ $dewiki, $zzwiki ],
244 $interwikiLookup->getAllPrefixes(),
245 'getAllPrefixes()'
246 );
247
248 $this->assertTrue( $interwikiLookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
249 $this->assertTrue( $interwikiLookup->isValidInterwiki( 'zz' ), 'known prefix is valid' );
250
251 $interwiki = $interwikiLookup->fetch( 'de' );
252 $this->assertInstanceOf( 'Interwiki', $interwiki );
253
254 $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
255 $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
256
257 $interwiki = $interwikiLookup->fetch( 'zz' );
258 $this->assertInstanceOf( 'Interwiki', $interwiki );
259
260 $this->assertSame( 'http://zzwiki.org/wiki/', $interwiki->getURL(), 'getURL' );
261 $this->assertSame( false, $interwiki->isLocal(), 'isLocal' );
262 }
263
264}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfTempDir()
Tries to get the system directory for temporary files.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
populateHash( $thisSite, $local, $global)
populateCDB( $thisSite, $local, $global)
setWgInterwikiCache( $interwikiCache)
populateDB( $iwrows)
Value object for representing interwiki records.
Definition Interwiki.php:27
static invalidateCache( $prefix)
Purge the cache (local and persistent) for an interwiki prefix.
Definition Interwiki.php:90
overrideMwServices(Config $configOverrides=null, array $services=[])
Stashes the global instance of MediaWikiServices, and installs a new one, allowing test cases to over...
setMwGlobals( $pairs, $value=null)
MediaWikiServices is the service locator for the application scope of MediaWiki.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
const DB_MASTER
Definition defines.php:26