MediaWiki  master
ClassicInterwikiLookupTest.php
Go to the documentation of this file.
1 <?php
9 
10  private function populateDB( $iwrows ) {
11  $dbw = wfGetDB( DB_MASTER );
12  $dbw->delete( 'interwiki', '*', __METHOD__ );
13  $dbw->insert( 'interwiki', array_values( $iwrows ), __METHOD__ );
14  $this->tablesUsed[] = 'interwiki';
15  }
16 
17  public function testDatabaseStorage() {
18  // NOTE: database setup is expensive, so we only do
19  // it once and run all the tests in one go.
20  $dewiki = [
21  'iw_prefix' => 'de',
22  'iw_url' => 'http://de.wikipedia.org/wiki/',
23  'iw_api' => 'http://de.wikipedia.org/w/api.php',
24  'iw_wikiid' => 'dewiki',
25  'iw_local' => 1,
26  'iw_trans' => 0
27  ];
28 
29  $zzwiki = [
30  'iw_prefix' => 'zz',
31  'iw_url' => 'http://zzwiki.org/wiki/',
32  'iw_api' => 'http://zzwiki.org/w/api.php',
33  'iw_wikiid' => 'zzwiki',
34  'iw_local' => 0,
35  'iw_trans' => 0
36  ];
37 
38  $this->populateDB( [ $dewiki, $zzwiki ] );
39  $lookup = new \MediaWiki\Interwiki\ClassicInterwikiLookup(
40  Language::factory( 'en' ),
42  60 * 60,
43  false,
44  3,
45  'en'
46  );
47 
48  $this->assertEquals(
49  [ $dewiki, $zzwiki ],
50  $lookup->getAllPrefixes(),
51  'getAllPrefixes()'
52  );
53  $this->assertEquals(
54  [ $dewiki ],
55  $lookup->getAllPrefixes( true ),
56  'getAllPrefixes()'
57  );
58  $this->assertEquals(
59  [ $zzwiki ],
60  $lookup->getAllPrefixes( false ),
61  'getAllPrefixes()'
62  );
63 
64  $this->assertTrue( $lookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
65  $this->assertFalse( $lookup->isValidInterwiki( 'xyz' ), 'unknown prefix is valid' );
66 
67  $this->assertNull( $lookup->fetch( null ), 'no prefix' );
68  $this->assertFalse( $lookup->fetch( 'xyz' ), 'unknown prefix' );
69 
70  $interwiki = $lookup->fetch( 'de' );
71  $this->assertInstanceOf( Interwiki::class, $interwiki );
72  $this->assertSame( $interwiki, $lookup->fetch( 'de' ), 'in-process caching' );
73 
74  $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
75  $this->assertSame( 'http://de.wikipedia.org/w/api.php', $interwiki->getAPI(), 'getAPI' );
76  $this->assertSame( 'dewiki', $interwiki->getWikiID(), 'getWikiID' );
77  $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
78  $this->assertSame( false, $interwiki->isTranscludable(), 'isTranscludable' );
79 
80  $lookup->invalidateCache( 'de' );
81  $this->assertNotSame( $interwiki, $lookup->fetch( 'de' ), 'invalidate cache' );
82  }
83 
91  private function populateHash( $thisSite, $local, $global ) {
92  $hash = [];
93  $hash[ '__sites:' . wfWikiID() ] = $thisSite;
94 
95  $globals = [];
96  $locals = [];
97 
98  foreach ( $local as $row ) {
99  $prefix = $row['iw_prefix'];
100  $data = $row['iw_local'] . ' ' . $row['iw_url'];
101  $locals[] = $prefix;
102  $hash[ "_{$thisSite}:{$prefix}" ] = $data;
103  }
104 
105  foreach ( $global as $row ) {
106  $prefix = $row['iw_prefix'];
107  $data = $row['iw_local'] . ' ' . $row['iw_url'];
108  $globals[] = $prefix;
109  $hash[ "__global:{$prefix}" ] = $data;
110  }
111 
112  $hash[ '__list:__global' ] = implode( ' ', $globals );
113  $hash[ '__list:_' . $thisSite ] = implode( ' ', $locals );
114 
115  return $hash;
116  }
117 
118  private function populateCDB( $thisSite, $local, $global ) {
119  $cdbFile = tempnam( wfTempDir(), 'MW-ClassicInterwikiLookupTest-' ) . '.cdb';
120  $cdb = \Cdb\Writer::open( $cdbFile );
121 
122  $hash = $this->populateHash( $thisSite, $local, $global );
123 
124  foreach ( $hash as $key => $value ) {
125  $cdb->set( $key, $value );
126  }
127 
128  $cdb->close();
129  return $cdbFile;
130  }
131 
132  public function testCDBStorage() {
133  // NOTE: CDB setup is expensive, so we only do
134  // it once and run all the tests in one go.
135 
136  $zzwiki = [
137  'iw_prefix' => 'zz',
138  'iw_url' => 'http://zzwiki.org/wiki/',
139  'iw_local' => 0
140  ];
141 
142  $dewiki = [
143  'iw_prefix' => 'de',
144  'iw_url' => 'http://de.wikipedia.org/wiki/',
145  'iw_local' => 1
146  ];
147 
148  $cdbFile = $this->populateCDB(
149  'en',
150  [ $dewiki ],
151  [ $zzwiki ]
152  );
153  $lookup = new \MediaWiki\Interwiki\ClassicInterwikiLookup(
154  Language::factory( 'en' ),
156  60 * 60,
157  $cdbFile,
158  3,
159  'en'
160  );
161 
162  $this->assertEquals(
163  [ $zzwiki, $dewiki ],
164  $lookup->getAllPrefixes(),
165  'getAllPrefixes()'
166  );
167 
168  $this->assertTrue( $lookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
169  $this->assertTrue( $lookup->isValidInterwiki( 'zz' ), 'known prefix is valid' );
170 
171  $interwiki = $lookup->fetch( 'de' );
172  $this->assertInstanceOf( Interwiki::class, $interwiki );
173 
174  $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
175  $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
176 
177  $interwiki = $lookup->fetch( 'zz' );
178  $this->assertInstanceOf( Interwiki::class, $interwiki );
179 
180  $this->assertSame( 'http://zzwiki.org/wiki/', $interwiki->getURL(), 'getURL' );
181  $this->assertSame( false, $interwiki->isLocal(), 'isLocal' );
182 
183  // cleanup temp file
184  unlink( $cdbFile );
185  }
186 
187  public function testArrayStorage() {
188  $zzwiki = [
189  'iw_prefix' => 'zz',
190  'iw_url' => 'http://zzwiki.org/wiki/',
191  'iw_local' => 0
192  ];
193  $dewiki = [
194  'iw_prefix' => 'de',
195  'iw_url' => 'http://de.wikipedia.org/wiki/',
196  'iw_local' => 1
197  ];
198 
199  $hash = $this->populateHash(
200  'en',
201  [ $dewiki ],
202  [ $zzwiki ]
203  );
204  $lookup = new \MediaWiki\Interwiki\ClassicInterwikiLookup(
205  Language::factory( 'en' ),
207  60 * 60,
208  $hash,
209  3,
210  'en'
211  );
212 
213  $this->assertEquals(
214  [ $zzwiki, $dewiki ],
215  $lookup->getAllPrefixes(),
216  'getAllPrefixes()'
217  );
218 
219  $this->assertTrue( $lookup->isValidInterwiki( 'de' ), 'known prefix is valid' );
220  $this->assertTrue( $lookup->isValidInterwiki( 'zz' ), 'known prefix is valid' );
221 
222  $interwiki = $lookup->fetch( 'de' );
223  $this->assertInstanceOf( Interwiki::class, $interwiki );
224 
225  $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
226  $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
227 
228  $interwiki = $lookup->fetch( 'zz' );
229  $this->assertInstanceOf( Interwiki::class, $interwiki );
230 
231  $this->assertSame( 'http://zzwiki.org/wiki/', $interwiki->getURL(), 'getURL' );
232  $this->assertSame( false, $interwiki->isLocal(), 'isLocal' );
233  }
234 
235  public function testGetAllPrefixes() {
236  $zz = [
237  'iw_prefix' => 'zz',
238  'iw_url' => 'https://azz.example.org/',
239  'iw_local' => 1
240  ];
241  $de = [
242  'iw_prefix' => 'de',
243  'iw_url' => 'https://de.example.org/',
244  'iw_local' => 1
245  ];
246  $azz = [
247  'iw_prefix' => 'azz',
248  'iw_url' => 'https://azz.example.org/',
249  'iw_local' => 1
250  ];
251 
252  $hash = $this->populateHash(
253  'en',
254  [],
255  [ $zz, $de, $azz ]
256  );
257  $lookup = new \MediaWiki\Interwiki\ClassicInterwikiLookup(
258  Language::factory( 'en' ),
260  60 * 60,
261  $hash,
262  3,
263  'en'
264  );
265 
266  $this->assertEquals(
267  [ $zz, $de, $azz ],
268  $lookup->getAllPrefixes(),
269  'getAllPrefixes() - preserves order'
270  );
271  }
272 
273 }
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
$value
const DB_MASTER
Definition: defines.php:26
static newEmpty()
Get an instance that wraps EmptyBagOStuff.
wfTempDir()
Tries to get the system directory for temporary files.
populateCDB( $thisSite, $local, $global)
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:216
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
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
Definition: distributors.txt:9
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:35
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MediaWiki\Interwiki\ClassicInterwikiLookup.
populateHash( $thisSite, $local, $global)