MediaWiki REL1_31
RevisionStoreTest.php
Go to the documentation of this file.
1<?php
2
4
6use Language;
12use Title;
16
18
26 private function getRevisionStore(
27 $loadBalancer = null,
28 $blobStore = null,
29 $WANObjectCache = null
30 ) {
31 return new RevisionStore(
32 $loadBalancer ? $loadBalancer : $this->getMockLoadBalancer(),
33 $blobStore ? $blobStore : $this->getMockSqlBlobStore(),
34 $WANObjectCache ? $WANObjectCache : $this->getHashWANObjectCache(),
35 MediaWikiServices::getInstance()->getCommentStore(),
36 MediaWikiServices::getInstance()->getActorMigration()
37 );
38 }
39
43 private function getMockLoadBalancer() {
44 return $this->getMockBuilder( LoadBalancer::class )
45 ->disableOriginalConstructor()->getMock();
46 }
47
51 private function getMockDatabase() {
52 return $this->getMockBuilder( Database::class )
53 ->disableOriginalConstructor()->getMock();
54 }
55
59 private function getMockSqlBlobStore() {
60 return $this->getMockBuilder( SqlBlobStore::class )
61 ->disableOriginalConstructor()->getMock();
62 }
63
64 private function getHashWANObjectCache() {
65 return new WANObjectCache( [ 'cache' => new \HashBagOStuff() ] );
66 }
67
72 public function testGetSetContentHandlerDb() {
73 $store = $this->getRevisionStore();
74 $this->assertTrue( $store->getContentHandlerUseDB() );
75 $store->setContentHandlerUseDB( false );
76 $this->assertFalse( $store->getContentHandlerUseDB() );
77 $store->setContentHandlerUseDB( true );
78 $this->assertTrue( $store->getContentHandlerUseDB() );
79 }
80
81 private function getDefaultQueryFields() {
82 return [
83 'rev_id',
84 'rev_page',
85 'rev_text_id',
86 'rev_timestamp',
87 'rev_minor_edit',
88 'rev_deleted',
89 'rev_len',
90 'rev_parent_id',
91 'rev_sha1',
92 ];
93 }
94
95 private function getCommentQueryFields() {
96 return [
97 'rev_comment_text' => 'rev_comment',
98 'rev_comment_data' => 'NULL',
99 'rev_comment_cid' => 'NULL',
100 ];
101 }
102
103 private function getActorQueryFields() {
104 return [
105 'rev_user' => 'rev_user',
106 'rev_user_text' => 'rev_user_text',
107 'rev_actor' => 'NULL',
108 ];
109 }
110
111 private function getContentHandlerQueryFields() {
112 return [
113 'rev_content_format',
114 'rev_content_model',
115 ];
116 }
117
118 public function provideGetQueryInfo() {
119 yield [
120 true,
121 [],
122 [
123 'tables' => [ 'revision' ],
124 'fields' => array_merge(
125 $this->getDefaultQueryFields(),
126 $this->getCommentQueryFields(),
127 $this->getActorQueryFields(),
129 ),
130 'joins' => [],
131 ]
132 ];
133 yield [
134 false,
135 [],
136 [
137 'tables' => [ 'revision' ],
138 'fields' => array_merge(
139 $this->getDefaultQueryFields(),
140 $this->getCommentQueryFields(),
141 $this->getActorQueryFields()
142 ),
143 'joins' => [],
144 ]
145 ];
146 yield [
147 false,
148 [ 'page' ],
149 [
150 'tables' => [ 'revision', 'page' ],
151 'fields' => array_merge(
152 $this->getDefaultQueryFields(),
153 $this->getCommentQueryFields(),
154 $this->getActorQueryFields(),
155 [
156 'page_namespace',
157 'page_title',
158 'page_id',
159 'page_latest',
160 'page_is_redirect',
161 'page_len',
162 ]
163 ),
164 'joins' => [
165 'page' => [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
166 ],
167 ]
168 ];
169 yield [
170 false,
171 [ 'user' ],
172 [
173 'tables' => [ 'revision', 'user' ],
174 'fields' => array_merge(
175 $this->getDefaultQueryFields(),
176 $this->getCommentQueryFields(),
177 $this->getActorQueryFields(),
178 [
179 'user_name',
180 ]
181 ),
182 'joins' => [
183 'user' => [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
184 ],
185 ]
186 ];
187 yield [
188 false,
189 [ 'text' ],
190 [
191 'tables' => [ 'revision', 'text' ],
192 'fields' => array_merge(
193 $this->getDefaultQueryFields(),
194 $this->getCommentQueryFields(),
195 $this->getActorQueryFields(),
196 [
197 'old_text',
198 'old_flags',
199 ]
200 ),
201 'joins' => [
202 'text' => [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ],
203 ],
204 ]
205 ];
206 yield [
207 true,
208 [ 'page', 'user', 'text' ],
209 [
210 'tables' => [ 'revision', 'page', 'user', 'text' ],
211 'fields' => array_merge(
212 $this->getDefaultQueryFields(),
213 $this->getCommentQueryFields(),
214 $this->getActorQueryFields(),
216 [
217 'page_namespace',
218 'page_title',
219 'page_id',
220 'page_latest',
221 'page_is_redirect',
222 'page_len',
223 'user_name',
224 'old_text',
225 'old_flags',
226 ]
227 ),
228 'joins' => [
229 'page' => [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
230 'user' => [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
231 'text' => [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ],
232 ],
233 ]
234 ];
235 }
236
241 public function testGetQueryInfo( $contentHandlerUseDb, $options, $expected ) {
242 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
243 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD );
244 $this->overrideMwServices();
245 $store = $this->getRevisionStore();
246 $store->setContentHandlerUseDB( $contentHandlerUseDb );
247 $this->assertEquals( $expected, $store->getQueryInfo( $options ) );
248 }
249
250 private function getDefaultArchiveFields() {
251 return [
252 'ar_id',
253 'ar_page_id',
254 'ar_namespace',
255 'ar_title',
256 'ar_rev_id',
257 'ar_text_id',
258 'ar_timestamp',
259 'ar_minor_edit',
260 'ar_deleted',
261 'ar_len',
262 'ar_parent_id',
263 'ar_sha1',
264 ];
265 }
266
271 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
272 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD );
273 $this->overrideMwServices();
274 $store = $this->getRevisionStore();
275 $store->setContentHandlerUseDB( true );
276 $this->assertEquals(
277 [
278 'tables' => [
279 'archive'
280 ],
281 'fields' => array_merge(
283 [
284 'ar_comment_text' => 'ar_comment',
285 'ar_comment_data' => 'NULL',
286 'ar_comment_cid' => 'NULL',
287 'ar_user_text' => 'ar_user_text',
288 'ar_user' => 'ar_user',
289 'ar_actor' => 'NULL',
290 'ar_content_format',
291 'ar_content_model',
292 ]
293 ),
294 'joins' => [],
295 ],
296 $store->getArchiveQueryInfo()
297 );
298 }
299
304 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
305 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD );
306 $this->overrideMwServices();
307 $store = $this->getRevisionStore();
308 $store->setContentHandlerUseDB( false );
309 $this->assertEquals(
310 [
311 'tables' => [
312 'archive'
313 ],
314 'fields' => array_merge(
316 [
317 'ar_comment_text' => 'ar_comment',
318 'ar_comment_data' => 'NULL',
319 'ar_comment_cid' => 'NULL',
320 'ar_user_text' => 'ar_user_text',
321 'ar_user' => 'ar_user',
322 'ar_actor' => 'NULL',
323 ]
324 ),
325 'joins' => [],
326 ],
327 $store->getArchiveQueryInfo()
328 );
329 }
330
332 $mockLoadBalancer = $this->getMockLoadBalancer();
333 // Title calls wfGetDB() so we have to set the main service
334 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
335
336 $db = $this->getMockDatabase();
337 // Title calls wfGetDB() which uses a regular Connection
338 $mockLoadBalancer->expects( $this->atLeastOnce() )
339 ->method( 'getConnection' )
340 ->willReturn( $db );
341
342 // First call to Title::newFromID, faking no result (db lag?)
343 $db->expects( $this->at( 0 ) )
344 ->method( 'selectRow' )
345 ->with(
346 'page',
347 $this->anything(),
348 [ 'page_id' => 1 ]
349 )
350 ->willReturn( (object)[
351 'page_namespace' => '1',
352 'page_title' => 'Food',
353 ] );
354
355 $store = $this->getRevisionStore( $mockLoadBalancer );
356 $title = $store->getTitle( 1, 2, RevisionStore::READ_NORMAL );
357
358 $this->assertSame( 1, $title->getNamespace() );
359 $this->assertSame( 'Food', $title->getDBkey() );
360 }
361
363 $mockLoadBalancer = $this->getMockLoadBalancer();
364 // Title calls wfGetDB() so we have to set the main service
365 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
366
367 $db = $this->getMockDatabase();
368 // Title calls wfGetDB() which uses a regular Connection
369 // Assert that the first call uses a REPLICA and the second falls back to master
370 $mockLoadBalancer->expects( $this->exactly( 2 ) )
371 ->method( 'getConnection' )
372 ->willReturn( $db );
373 // RevisionStore getTitle uses a ConnectionRef
374 $mockLoadBalancer->expects( $this->atLeastOnce() )
375 ->method( 'getConnectionRef' )
376 ->willReturn( $db );
377
378 // First call to Title::newFromID, faking no result (db lag?)
379 $db->expects( $this->at( 0 ) )
380 ->method( 'selectRow' )
381 ->with(
382 'page',
383 $this->anything(),
384 [ 'page_id' => 1 ]
385 )
386 ->willReturn( false );
387
388 // First select using rev_id, faking no result (db lag?)
389 $db->expects( $this->at( 1 ) )
390 ->method( 'selectRow' )
391 ->with(
392 [ 'revision', 'page' ],
393 $this->anything(),
394 [ 'rev_id' => 2 ]
395 )
396 ->willReturn( false );
397
398 // Second call to Title::newFromID, no result
399 $db->expects( $this->at( 2 ) )
400 ->method( 'selectRow' )
401 ->with(
402 'page',
403 $this->anything(),
404 [ 'page_id' => 1 ]
405 )
406 ->willReturn( (object)[
407 'page_namespace' => '2',
408 'page_title' => 'Foodey',
409 ] );
410
411 $store = $this->getRevisionStore( $mockLoadBalancer );
412 $title = $store->getTitle( 1, 2, RevisionStore::READ_NORMAL );
413
414 $this->assertSame( 2, $title->getNamespace() );
415 $this->assertSame( 'Foodey', $title->getDBkey() );
416 }
417
419 $mockLoadBalancer = $this->getMockLoadBalancer();
420 // Title calls wfGetDB() so we have to set the main service
421 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
422
423 $db = $this->getMockDatabase();
424 // Title calls wfGetDB() which uses a regular Connection
425 $mockLoadBalancer->expects( $this->atLeastOnce() )
426 ->method( 'getConnection' )
427 ->willReturn( $db );
428 // RevisionStore getTitle uses a ConnectionRef
429 $mockLoadBalancer->expects( $this->atLeastOnce() )
430 ->method( 'getConnectionRef' )
431 ->willReturn( $db );
432
433 // First call to Title::newFromID, faking no result (db lag?)
434 $db->expects( $this->at( 0 ) )
435 ->method( 'selectRow' )
436 ->with(
437 'page',
438 $this->anything(),
439 [ 'page_id' => 1 ]
440 )
441 ->willReturn( false );
442
443 // First select using rev_id, faking no result (db lag?)
444 $db->expects( $this->at( 1 ) )
445 ->method( 'selectRow' )
446 ->with(
447 [ 'revision', 'page' ],
448 $this->anything(),
449 [ 'rev_id' => 2 ]
450 )
451 ->willReturn( (object)[
452 'page_namespace' => '1',
453 'page_title' => 'Food2',
454 ] );
455
456 $store = $this->getRevisionStore( $mockLoadBalancer );
457 $title = $store->getTitle( 1, 2, RevisionStore::READ_NORMAL );
458
459 $this->assertSame( 1, $title->getNamespace() );
460 $this->assertSame( 'Food2', $title->getDBkey() );
461 }
462
464 $mockLoadBalancer = $this->getMockLoadBalancer();
465 // Title calls wfGetDB() so we have to set the main service
466 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
467
468 $db = $this->getMockDatabase();
469 // Title calls wfGetDB() which uses a regular Connection
470 // Assert that the first call uses a REPLICA and the second falls back to master
471 $mockLoadBalancer->expects( $this->exactly( 2 ) )
472 ->method( 'getConnection' )
473 ->willReturn( $db );
474 // RevisionStore getTitle uses a ConnectionRef
475 $mockLoadBalancer->expects( $this->atLeastOnce() )
476 ->method( 'getConnectionRef' )
477 ->willReturn( $db );
478
479 // First call to Title::newFromID, faking no result (db lag?)
480 $db->expects( $this->at( 0 ) )
481 ->method( 'selectRow' )
482 ->with(
483 'page',
484 $this->anything(),
485 [ 'page_id' => 1 ]
486 )
487 ->willReturn( false );
488
489 // First select using rev_id, faking no result (db lag?)
490 $db->expects( $this->at( 1 ) )
491 ->method( 'selectRow' )
492 ->with(
493 [ 'revision', 'page' ],
494 $this->anything(),
495 [ 'rev_id' => 2 ]
496 )
497 ->willReturn( false );
498
499 // Second call to Title::newFromID, no result
500 $db->expects( $this->at( 2 ) )
501 ->method( 'selectRow' )
502 ->with(
503 'page',
504 $this->anything(),
505 [ 'page_id' => 1 ]
506 )
507 ->willReturn( false );
508
509 // Second select using rev_id, result
510 $db->expects( $this->at( 3 ) )
511 ->method( 'selectRow' )
512 ->with(
513 [ 'revision', 'page' ],
514 $this->anything(),
515 [ 'rev_id' => 2 ]
516 )
517 ->willReturn( (object)[
518 'page_namespace' => '2',
519 'page_title' => 'Foodey',
520 ] );
521
522 $store = $this->getRevisionStore( $mockLoadBalancer );
523 $title = $store->getTitle( 1, 2, RevisionStore::READ_NORMAL );
524
525 $this->assertSame( 2, $title->getNamespace() );
526 $this->assertSame( 'Foodey', $title->getDBkey() );
527 }
528
533 $mockLoadBalancer = $this->getMockLoadBalancer();
534 // Title calls wfGetDB() so we have to set the main service
535 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
536
537 $db = $this->getMockDatabase();
538 // Title calls wfGetDB() which uses a regular Connection
539 // Assert that the first call uses a REPLICA and the second falls back to master
540
541 // RevisionStore getTitle uses getConnectionRef
542 // Title::newFromID uses getConnection
543 foreach ( [ 'getConnection', 'getConnectionRef' ] as $method ) {
544 $mockLoadBalancer->expects( $this->exactly( 2 ) )
545 ->method( $method )
546 ->willReturnCallback( function ( $masterOrReplica ) use ( $db ) {
547 static $callCounter = 0;
548 $callCounter++;
549 // The first call should be to a REPLICA, and the second a MASTER.
550 if ( $callCounter === 1 ) {
551 $this->assertSame( DB_REPLICA, $masterOrReplica );
552 } elseif ( $callCounter === 2 ) {
553 $this->assertSame( DB_MASTER, $masterOrReplica );
554 }
555 return $db;
556 } );
557 }
558 // First and third call to Title::newFromID, faking no result
559 foreach ( [ 0, 2 ] as $counter ) {
560 $db->expects( $this->at( $counter ) )
561 ->method( 'selectRow' )
562 ->with(
563 'page',
564 $this->anything(),
565 [ 'page_id' => 1 ]
566 )
567 ->willReturn( false );
568 }
569
570 foreach ( [ 1, 3 ] as $counter ) {
571 $db->expects( $this->at( $counter ) )
572 ->method( 'selectRow' )
573 ->with(
574 [ 'revision', 'page' ],
575 $this->anything(),
576 [ 'rev_id' => 2 ]
577 )
578 ->willReturn( false );
579 }
580
581 $store = $this->getRevisionStore( $mockLoadBalancer );
582
583 $this->setExpectedException( RevisionAccessException::class );
584 $store->getTitle( 1, 2, RevisionStore::READ_NORMAL );
585 }
586
588 yield 'windows-1252, old_flags is empty' => [
589 'windows-1252',
590 'en',
591 [
592 'old_flags' => '',
593 'old_text' => "S\xF6me Content",
594 ],
595 'Söme Content'
596 ];
597
598 yield 'windows-1252, old_flags is null' => [
599 'windows-1252',
600 'en',
601 [
602 'old_flags' => null,
603 'old_text' => "S\xF6me Content",
604 ],
605 'Söme Content'
606 ];
607 }
608
615 public function testNewRevisionFromRow_legacyEncoding_applied( $encoding, $locale, $row, $text ) {
616 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
617
618 $blobStore = new SqlBlobStore( wfGetLB(), $cache );
619 $blobStore->setLegacyEncoding( $encoding, Language::factory( $locale ) );
620
621 $store = $this->getRevisionStore( wfGetLB(), $blobStore, $cache );
622
623 $record = $store->newRevisionFromRow(
624 $this->makeRow( $row ),
625 0,
626 Title::newFromText( __METHOD__ . '-UTPage' )
627 );
628
629 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
630 }
631
637 $row = [
638 'old_flags' => 'utf-8',
639 'old_text' => 'Söme Content',
640 ];
641
642 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
643
644 $blobStore = new SqlBlobStore( wfGetLB(), $cache );
645 $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) );
646
647 $store = $this->getRevisionStore( wfGetLB(), $blobStore, $cache );
648
649 $record = $store->newRevisionFromRow(
650 $this->makeRow( $row ),
651 0,
652 Title::newFromText( __METHOD__ . '-UTPage' )
653 );
654 $this->assertSame( 'Söme Content', $record->getContent( 'main' )->serialize() );
655 }
656
657 private function makeRow( array $array ) {
658 $row = $array + [
659 'rev_id' => 7,
660 'rev_page' => 5,
661 'rev_text_id' => 11,
662 'rev_timestamp' => '20110101000000',
663 'rev_user_text' => 'Tester',
664 'rev_user' => 17,
665 'rev_minor_edit' => 0,
666 'rev_deleted' => 0,
667 'rev_len' => 100,
668 'rev_parent_id' => 0,
669 'rev_sha1' => 'deadbeef',
670 'rev_comment_text' => 'Testing',
671 'rev_comment_data' => '{}',
672 'rev_comment_cid' => 111,
673 'rev_content_format' => CONTENT_FORMAT_TEXT,
674 'rev_content_model' => CONTENT_MODEL_TEXT,
675 'page_namespace' => 0,
676 'page_title' => 'TEST',
677 'page_id' => 5,
678 'page_latest' => 7,
679 'page_is_redirect' => 0,
680 'page_len' => 100,
681 'user_name' => 'Tester',
682 'old_is' => 13,
683 'old_text' => 'Hello World',
684 'old_flags' => 'utf-8',
685 ];
686
687 return (object)$row;
688 }
689
690}
wfGetLB( $wiki=false)
Get a load balancer object.
Simple store for keeping values in an associative array for the current process.
Internationalisation code.
Definition Language.php:35
Database $db
Primary database.
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)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
setService( $name, $object)
Sets a service, maintaining a stashed version of the previous service to be restored in tearDown.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static getInstance()
Returns the global default instance of the top level service locator.
Exception representing a failure to look up a revision.
Service for looking up page revisions.
Service for storing and loading Content objects.
testGetArchiveQueryInfo_noContentHandlerDb()
\MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
getRevisionStore( $loadBalancer=null, $blobStore=null, $WANObjectCache=null)
testGetSetContentHandlerDb()
\MediaWiki\Storage\RevisionStore::getContentHandlerUseDB \MediaWiki\Storage\RevisionStore::setContent...
testNewRevisionFromRow_legacyEncoding_applied( $encoding, $locale, $row, $text)
provideNewRevisionFromRow_legacyEncoding_applied
testGetArchiveQueryInfo_contentHandlerDb()
\MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
testNewRevisionFromRow_legacyEncoding_ignored()
\MediaWiki\Storage\RevisionStore::newRevisionFromRow \MediaWiki\Storage\RevisionStore::newRevisionFro...
testGetQueryInfo( $contentHandlerUseDb, $options, $expected)
provideGetQueryInfo \MediaWiki\Storage\RevisionStore::getQueryInfo
testGetTitle_correctFallbackAndthrowsExceptionAfterFallbacks()
\MediaWiki\Storage\RevisionStore::getTitle
Represents a title within MediaWiki.
Definition Title.php:39
Multi-datacenter aware caching interface.
Relational database abstraction object.
Definition Database.php:48
Database connection, tracking, load balancing, and transaction manager for a cluster.
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 and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same so they can t rely on Unix and must forbid reads to even standard directories like tmp lest users read each others files We cannot assume that the user has the ability to install or run any programs not written as web accessible PHP scripts Since anything that works on cheap shared hosting will work if you have shell or root access MediaWiki s design is based around catering to the lowest common denominator Although we support higher end setups as the way many things work by default is tailored toward shared hosting These defaults are unconventional from the point of view of and they certainly aren t ideal for someone who s installing MediaWiki as MediaWiki does not conform to normal Unix filesystem layout Hopefully we ll offer direct support for standard layouts in the but for now *any change to the location of files is unsupported *Moving things and leaving symlinks will *probably *not break anything
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2001
const CONTENT_FORMAT_TEXT
Definition Defines.php:266
const CONTENT_MODEL_TEXT
Definition Defines.php:248
const MIGRATION_OLD
Definition Defines.php:302
$cache
Definition mcc.php:33
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:29