MediaWiki  1.27.2
InterwikiTest.php
Go to the documentation of this file.
1 <?php
9 
10  public function testConstructor() {
11  $interwiki = new Interwiki(
12  'xyz',
13  'http://xyz.acme.test/wiki/$1',
14  'http://xyz.acme.test/w/api.php',
15  'xyzwiki',
16  1,
17  0
18  );
19 
20  $this->setContentLang( 'qqx' );
21 
22  $this->assertSame( '(interwiki-name-xyz)', $interwiki->getName() );
23  $this->assertSame( '(interwiki-desc-xyz)', $interwiki->getDescription() );
24  $this->assertSame( 'http://xyz.acme.test/w/api.php', $interwiki->getAPI() );
25  $this->assertSame( 'http://xyz.acme.test/wiki/$1', $interwiki->getURL() );
26  $this->assertSame( 'xyzwiki', $interwiki->getWikiID() );
27  $this->assertTrue( $interwiki->isLocal() );
28  $this->assertFalse( $interwiki->isTranscludable() );
29  }
30 
31  public function testGetUrl() {
32  $interwiki = new Interwiki(
33  'xyz',
34  'http://xyz.acme.test/wiki/$1'
35  );
36 
37  $this->assertSame( 'http://xyz.acme.test/wiki/$1', $interwiki->getURL() );
38  $this->assertSame( 'http://xyz.acme.test/wiki/Foo%26Bar', $interwiki->getURL( 'Foo&Bar' ) );
39  }
40 
42 
43  private function populateDB( $iwrows ) {
44  $dbw = wfGetDB( DB_MASTER );
45  $dbw->delete( 'interwiki', '*', __METHOD__ );
46  $dbw->insert( 'interwiki', array_values( $iwrows ), __METHOD__ );
47  $this->tablesUsed[] = 'interwiki';
48  }
49 
50  public function testDatabaseStorage() {
51  // NOTE: database setup is expensive, so we only do
52  // it once and run all the tests in one go.
53  $dewiki = [
54  'iw_prefix' => 'de',
55  'iw_url' => 'http://de.wikipedia.org/wiki/',
56  'iw_api' => 'http://de.wikipedia.org/w/api.php',
57  'iw_wikiid' => 'dewiki',
58  'iw_local' => 1,
59  'iw_trans' => 0
60  ];
61 
62  $zzwiki = [
63  'iw_prefix' => 'zz',
64  'iw_url' => 'http://zzwiki.org/wiki/',
65  'iw_api' => 'http://zzwiki.org/w/api.php',
66  'iw_wikiid' => 'zzwiki',
67  'iw_local' => 0,
68  'iw_trans' => 0
69  ];
70 
71  $this->populateDB( [ $dewiki, $zzwiki ] );
72 
74  $this->setMwGlobals( 'wgInterwikiCache', false );
75 
76  $this->assertEquals(
77  [ $dewiki, $zzwiki ],
79  'getAllPrefixes()'
80  );
81  $this->assertEquals(
82  [ $dewiki ],
84  'getAllPrefixes()'
85  );
86  $this->assertEquals(
87  [ $zzwiki ],
89  'getAllPrefixes()'
90  );
91 
92  $this->assertTrue( Interwiki::isValidInterwiki( 'de' ), 'known prefix is valid' );
93  $this->assertFalse( Interwiki::isValidInterwiki( 'xyz' ), 'unknown prefix is valid' );
94 
95  $this->assertNull( Interwiki::fetch( null ), 'no prefix' );
96  $this->assertFalse( Interwiki::fetch( 'xyz' ), 'unknown prefix' );
97 
98  $interwiki = Interwiki::fetch( 'de' );
99  $this->assertInstanceOf( 'Interwiki', $interwiki );
100  $this->assertSame( $interwiki, Interwiki::fetch( 'de' ), 'in-process caching' );
101 
102  $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
103  $this->assertSame( 'http://de.wikipedia.org/w/api.php', $interwiki->getAPI(), 'getAPI' );
104  $this->assertSame( 'dewiki', $interwiki->getWikiID(), 'getWikiID' );
105  $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
106  $this->assertSame( false, $interwiki->isTranscludable(), 'isTranscludable' );
107 
109  $this->assertNotSame( $interwiki, Interwiki::fetch( 'de' ), 'invalidate cache' );
110  }
111 
119  private function populateHash( $thisSite, $local, $global ) {
120  $hash = [];
121  $hash[ '__sites:' . wfWikiID() ] = $thisSite;
122 
123  $globals = [];
124  $locals = [];
125 
126  foreach ( $local as $row ) {
127  $prefix = $row['iw_prefix'];
128  $data = $row['iw_local'] . ' ' . $row['iw_url'];
129  $locals[] = $prefix;
130  $hash[ "_{$thisSite}:{$prefix}" ] = $data;
131  }
132 
133  foreach ( $global as $row ) {
134  $prefix = $row['iw_prefix'];
135  $data = $row['iw_local'] . ' ' . $row['iw_url'];
136  $globals[] = $prefix;
137  $hash[ "__global:{$prefix}" ] = $data;
138  }
139 
140  $hash[ '__list:__global' ] = implode( ' ', $globals );
141  $hash[ '__list:_' . $thisSite ] = implode( ' ', $locals );
142 
143  return $hash;
144  }
145 
146  private function populateCDB( $thisSite, $local, $global ) {
147  $cdbFile = tempnam( wfTempDir(), 'MW-ClassicInterwikiLookupTest-' ) . '.cdb';
148  $cdb = CdbWriter::open( $cdbFile );
149 
150  $hash = $this->populateHash( $thisSite, $local, $global );
151 
152  foreach ( $hash as $key => $value ) {
153  $cdb->set( $key, $value );
154  }
155 
156  $cdb->close();
157  return $cdbFile;
158  }
159 
160  public function testCDBStorage() {
161  // NOTE: CDB setup is expensive, so we only do
162  // it once and run all the tests in one go.
163 
164  $dewiki = [
165  'iw_prefix' => 'de',
166  'iw_url' => 'http://de.wikipedia.org/wiki/',
167  'iw_local' => 1
168  ];
169 
170  $zzwiki = [
171  'iw_prefix' => 'zz',
172  'iw_url' => 'http://zzwiki.org/wiki/',
173  'iw_local' => 0
174  ];
175 
176  $cdbFile = $this->populateCDB(
177  'en',
178  [ $dewiki ],
179  [ $zzwiki ]
180  );
181 
183  $this->setMwGlobals( 'wgInterwikiCache', $cdbFile );
184 
185  $this->assertEquals(
186  [ $dewiki, $zzwiki ],
188  'getAllPrefixes()'
189  );
190 
191  $this->assertTrue( Interwiki::isValidInterwiki( 'de' ), 'known prefix is valid' );
192  $this->assertTrue( Interwiki::isValidInterwiki( 'zz' ), 'known prefix is valid' );
193 
194  $interwiki = Interwiki::fetch( 'de' );
195  $this->assertInstanceOf( 'Interwiki', $interwiki );
196 
197  $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
198  $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
199 
200  $interwiki = Interwiki::fetch( 'zz' );
201  $this->assertInstanceOf( 'Interwiki', $interwiki );
202 
203  $this->assertSame( 'http://zzwiki.org/wiki/', $interwiki->getURL(), 'getURL' );
204  $this->assertSame( false, $interwiki->isLocal(), 'isLocal' );
205 
206  // cleanup temp file
207  unlink( $cdbFile );
208  }
209 
210  public function testArrayStorage() {
211  $dewiki = [
212  'iw_prefix' => 'de',
213  'iw_url' => 'http://de.wikipedia.org/wiki/',
214  'iw_local' => 1
215  ];
216 
217  $zzwiki = [
218  'iw_prefix' => 'zz',
219  'iw_url' => 'http://zzwiki.org/wiki/',
220  'iw_local' => 0
221  ];
222 
223  $cdbData = $this->populateHash(
224  'en',
225  [ $dewiki ],
226  [ $zzwiki ]
227  );
228 
230  $this->setMwGlobals( 'wgInterwikiCache', $cdbData );
231 
232  $this->assertEquals(
233  [ $dewiki, $zzwiki ],
235  'getAllPrefixes()'
236  );
237 
238  $this->assertTrue( Interwiki::isValidInterwiki( 'de' ), 'known prefix is valid' );
239  $this->assertTrue( Interwiki::isValidInterwiki( 'zz' ), 'known prefix is valid' );
240 
241  $interwiki = Interwiki::fetch( 'de' );
242  $this->assertInstanceOf( 'Interwiki', $interwiki );
243 
244  $this->assertSame( 'http://de.wikipedia.org/wiki/', $interwiki->getURL(), 'getURL' );
245  $this->assertSame( true, $interwiki->isLocal(), 'isLocal' );
246 
247  $interwiki = Interwiki::fetch( 'zz' );
248  $this->assertInstanceOf( 'Interwiki', $interwiki );
249 
250  $this->assertSame( 'http://zzwiki.org/wiki/', $interwiki->getURL(), 'getURL' );
251  $this->assertSame( false, $interwiki->isLocal(), 'isLocal' );
252  }
253 
254 }
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
populateCDB($thisSite, $local, $global)
static resetLocalCache()
Resets locally cached Interwiki objects.
Definition: Interwiki.php:122
$value
populateDB($iwrows)
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.
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:85
The interwiki class All information is loaded on creation when called by Interwiki::fetch( $prefix )...
Definition: Interwiki.php:31
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:392
const DB_MASTER
Definition: Defines.php:47
setMwGlobals($pairs, $value=null)
static invalidateCache($prefix)
Purge the cache (local and persistent) for an interwiki prefix.
Definition: Interwiki.php:131
static isValidInterwiki($prefix)
Check whether an interwiki prefix exists.
Definition: Interwiki.php:73