MediaWiki  1.28.0
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  $this->assertEquals(
86  [ $dewiki, $zzwiki ],
88  'getAllPrefixes()'
89  );
90  $this->assertEquals(
91  [ $dewiki ],
93  'getAllPrefixes()'
94  );
95  $this->assertEquals(
96  [ $zzwiki ],
98  'getAllPrefixes()'
99  );
100 
101  $this->assertTrue( Interwiki::isValidInterwiki( 'de' ), 'known prefix is valid' );
102  $this->assertFalse( Interwiki::isValidInterwiki( 'xyz' ), 'unknown prefix is valid' );
103 
104  $this->assertNull( Interwiki::fetch( null ), 'no prefix' );
105  $this->assertFalse( Interwiki::fetch( 'xyz' ), 'unknown prefix' );
106 
107  $interwiki = Interwiki::fetch( 'de' );
108  $this->assertInstanceOf( 'Interwiki', $interwiki );
109  $this->assertSame( $interwiki, Interwiki::fetch( 'de' ), 'in-process caching' );
110 
111  $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
112  $this->assertSame( 'http://de.wikipedia.org/w/api.php', $interwiki->getAPI(), 'getAPI' );
113  $this->assertSame( 'dewiki', $interwiki->getWikiID(), 'getWikiID' );
114  $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
115  $this->assertSame( false, $interwiki->isTranscludable(), 'isTranscludable' );
116 
118  $this->assertNotSame( $interwiki, Interwiki::fetch( 'de' ), 'invalidate cache' );
119  }
120 
128  private function populateHash( $thisSite, $local, $global ) {
129  $hash = [];
130  $hash[ '__sites:' . wfWikiID() ] = $thisSite;
131 
132  $globals = [];
133  $locals = [];
134 
135  foreach ( $local as $row ) {
136  $prefix = $row['iw_prefix'];
137  $data = $row['iw_local'] . ' ' . $row['iw_url'];
138  $locals[] = $prefix;
139  $hash[ "_{$thisSite}:{$prefix}" ] = $data;
140  }
141 
142  foreach ( $global as $row ) {
143  $prefix = $row['iw_prefix'];
144  $data = $row['iw_local'] . ' ' . $row['iw_url'];
145  $globals[] = $prefix;
146  $hash[ "__global:{$prefix}" ] = $data;
147  }
148 
149  $hash[ '__list:__global' ] = implode( ' ', $globals );
150  $hash[ '__list:_' . $thisSite ] = implode( ' ', $locals );
151 
152  return $hash;
153  }
154 
155  private function populateCDB( $thisSite, $local, $global ) {
156  $cdbFile = tempnam( wfTempDir(), 'MW-ClassicInterwikiLookupTest-' ) . '.cdb';
157  $cdb = CdbWriter::open( $cdbFile );
158 
159  $hash = $this->populateHash( $thisSite, $local, $global );
160 
161  foreach ( $hash as $key => $value ) {
162  $cdb->set( $key, $value );
163  }
164 
165  $cdb->close();
166  return $cdbFile;
167  }
168 
169  public function testCDBStorage() {
170  // NOTE: CDB setup is expensive, so we only do
171  // it once and run all the tests in one go.
172 
173  $dewiki = [
174  'iw_prefix' => 'de',
175  'iw_url' => 'http://de.wikipedia.org/wiki/',
176  'iw_local' => 1
177  ];
178 
179  $zzwiki = [
180  'iw_prefix' => 'zz',
181  'iw_url' => 'http://zzwiki.org/wiki/',
182  'iw_local' => 0
183  ];
184 
185  $cdbFile = $this->populateCDB(
186  'en',
187  [ $dewiki ],
188  [ $zzwiki ]
189  );
190 
191  $this->setWgInterwikiCache( $cdbFile );
192 
193  $this->assertEquals(
194  [ $dewiki, $zzwiki ],
196  'getAllPrefixes()'
197  );
198 
199  $this->assertTrue( Interwiki::isValidInterwiki( 'de' ), 'known prefix is valid' );
200  $this->assertTrue( Interwiki::isValidInterwiki( 'zz' ), 'known prefix is valid' );
201 
202  $interwiki = Interwiki::fetch( 'de' );
203  $this->assertInstanceOf( 'Interwiki', $interwiki );
204 
205  $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
206  $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
207 
208  $interwiki = Interwiki::fetch( 'zz' );
209  $this->assertInstanceOf( 'Interwiki', $interwiki );
210 
211  $this->assertSame( 'http://zzwiki.org/wiki/', $interwiki->getURL(), 'getURL' );
212  $this->assertSame( false, $interwiki->isLocal(), 'isLocal' );
213 
214  // cleanup temp file
215  unlink( $cdbFile );
216  }
217 
218  public function testArrayStorage() {
219  $dewiki = [
220  'iw_prefix' => 'de',
221  'iw_url' => 'http://de.wikipedia.org/wiki/',
222  'iw_local' => 1
223  ];
224 
225  $zzwiki = [
226  'iw_prefix' => 'zz',
227  'iw_url' => 'http://zzwiki.org/wiki/',
228  'iw_local' => 0
229  ];
230 
231  $cdbData = $this->populateHash(
232  'en',
233  [ $dewiki ],
234  [ $zzwiki ]
235  );
236 
237  $this->setWgInterwikiCache( $cdbData );
238 
239  $this->assertEquals(
240  [ $dewiki, $zzwiki ],
242  'getAllPrefixes()'
243  );
244 
245  $this->assertTrue( Interwiki::isValidInterwiki( 'de' ), 'known prefix is valid' );
246  $this->assertTrue( Interwiki::isValidInterwiki( 'zz' ), 'known prefix is valid' );
247 
248  $interwiki = Interwiki::fetch( 'de' );
249  $this->assertInstanceOf( 'Interwiki', $interwiki );
250 
251  $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
252  $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
253 
254  $interwiki = Interwiki::fetch( 'zz' );
255  $this->assertInstanceOf( 'Interwiki', $interwiki );
256 
257  $this->assertSame( 'http://zzwiki.org/wiki/', $interwiki->getURL(), 'getURL' );
258  $this->assertSame( false, $interwiki->isLocal(), 'isLocal' );
259  }
260 
261 }
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
populateCDB($thisSite, $local, $global)
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$value
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 MediaWikiServices
Definition: injection.txt:23
populateDB($iwrows)
const DB_MASTER
Definition: defines.php:23
setWgInterwikiCache($interwikiCache)
wfTempDir()
Tries to get the system directory for temporary files.
Interwiki.
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
overrideMwServices(Config $configOverrides=null, array $services=[])
Stashes the global instance of MediaWikiServices, and installs a new one, allowing test cases to over...
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
static fetch($prefix)
Fetch an Interwiki object.
Definition: Interwiki.php:80
Value object for representing interwiki records.
Definition: Interwiki.php:27
populateHash($thisSite, $local, $global)
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
static getAllPrefixes($local=null)
Returns all interwiki prefixes.
Definition: Interwiki.php:103
setMwGlobals($pairs, $value=null)
static invalidateCache($prefix)
Purge the cache (local and persistent) for an interwiki prefix.
Definition: Interwiki.php:90
static isValidInterwiki($prefix)
Check whether an interwiki prefix exists.
Definition: Interwiki.php:68