3 namespace MediaWiki\Tests\Maintenance
;
6 use MediaWiki\MediaWikiServices
;
11 use Wikimedia\Rdbms\IDatabase
;
12 use Wikimedia\Rdbms\LoadBalancer
;
16 * Tests for page dumps of BackupDumper
20 * @covers BackupDumper
22 class BackupDumperPageTest
extends DumpTestCase
{
24 // We'll add several pages, revision and texts. The following variables hold the
26 private $pageId1, $pageId2, $pageId3, $pageId4;
27 private $pageTitle1, $pageTitle2, $pageTitle3, $pageTitle4;
28 private $revId1_1, $textId1_1;
29 private $revId2_1, $textId2_1, $revId2_2, $textId2_2;
30 private $revId2_3, $textId2_3, $revId2_4, $textId2_4;
31 private $revId3_1, $textId3_1, $revId3_2, $textId3_2;
32 private $revId4_1, $textId4_1;
33 private $namespace, $talk_namespace;
36 * @var LoadBalancer|null
38 private $streamingLoadBalancer = null;
40 function addDBData() {
41 // be sure, titles created here using english namespace names
42 $this->setContentLang( 'en' );
44 $this->tablesUsed
[] = 'page';
45 $this->tablesUsed
[] = 'revision';
46 $this->tablesUsed
[] = 'ip_changes';
47 $this->tablesUsed
[] = 'text';
50 $this->namespace = $this->getDefaultWikitextNS();
51 $this->talk_namespace
= NS_TALK
;
53 if ( $this->namespace === $this->talk_namespace
) {
54 // @todo work around this.
55 throw new MWException( "The default wikitext namespace is the talk namespace. "
56 . " We can't currently deal with that." );
59 $this->pageTitle1
= Title
::newFromText( 'BackupDumperTestP1', $this->namespace );
60 $page = WikiPage
::factory( $this->pageTitle1
);
61 list( $this->revId1_1
, $this->textId1_1
) = $this->addRevision( $page,
62 "BackupDumperTestP1Text1", "BackupDumperTestP1Summary1" );
63 $this->pageId1
= $page->getId();
65 $this->pageTitle2
= Title
::newFromText( 'BackupDumperTestP2', $this->namespace );
66 $page = WikiPage
::factory( $this->pageTitle2
);
67 list( $this->revId2_1
, $this->textId2_1
) = $this->addRevision( $page,
68 "BackupDumperTestP2Text1", "BackupDumperTestP2Summary1" );
69 list( $this->revId2_2
, $this->textId2_2
) = $this->addRevision( $page,
70 "BackupDumperTestP2Text2", "BackupDumperTestP2Summary2" );
71 list( $this->revId2_3
, $this->textId2_3
) = $this->addRevision( $page,
72 "BackupDumperTestP2Text3", "BackupDumperTestP2Summary3" );
73 list( $this->revId2_4
, $this->textId2_4
) = $this->addRevision( $page,
74 "BackupDumperTestP2Text4 some additional Text ",
75 "BackupDumperTestP2Summary4 extra " );
76 $this->pageId2
= $page->getId();
78 $this->pageTitle3
= Title
::newFromText( 'BackupDumperTestP3', $this->namespace );
79 $page = WikiPage
::factory( $this->pageTitle3
);
80 list( $this->revId3_1
, $this->textId3_1
) = $this->addRevision( $page,
81 "BackupDumperTestP3Text1", "BackupDumperTestP2Summary1" );
82 list( $this->revId3_2
, $this->textId3_2
) = $this->addRevision( $page,
83 "BackupDumperTestP3Text2", "BackupDumperTestP2Summary2" );
84 $this->pageId3
= $page->getId();
85 $page->doDeleteArticle( "Testing ;)" );
87 $this->pageTitle4
= Title
::newFromText( 'BackupDumperTestP1', $this->talk_namespace
);
88 $page = WikiPage
::factory( $this->pageTitle4
);
89 list( $this->revId4_1
, $this->textId4_1
) = $this->addRevision( $page,
90 "Talk about BackupDumperTestP1 Text1",
91 "Talk BackupDumperTestP1 Summary1" );
92 $this->pageId4
= $page->getId();
93 } catch ( Exception
$e ) {
94 // We'd love to pass $e directly. However, ... see
95 // documentation of exceptionFromAddDBData in
97 $this->exceptionFromAddDBData
= $e;
101 protected function setUp() {
104 // Since we will restrict dumping by page ranges (to allow
105 // working tests, even if the db gets prepopulated by a base
106 // class), we have to assert, that the page id are consecutively
109 [ $this->pageId2
, $this->pageId3
, $this->pageId4
],
110 [ $this->pageId1 +
1, $this->pageId2 +
1, $this->pageId3 +
1 ],
111 "Page ids increasing without holes" );
114 function tearDown() {
117 if ( isset( $this->streamingLoadBalancer
) ) {
118 $this->streamingLoadBalancer
->closeAll();
123 * Returns a new database connection which is separate from the conenctions returned
124 * by the default LoadBalancer instance.
128 private function newStreamingDBConnection() {
129 // Create a *new* LoadBalancer, so no connections are shared
130 if ( !$this->streamingLoadBalancer
) {
131 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
133 $this->streamingLoadBalancer
= $lbFactory->newMainLB();
136 $db = $this->streamingLoadBalancer
->getConnection( DB_REPLICA
);
138 // Make sure the DB connection has the fake table clones and the fake table prefix
139 MediaWikiTestCase
::setupDatabaseWithTestPrefix( $db );
141 // Make sure the DB connection has all the test data
142 $this->copyTestData( $this->db
, $db );
149 * @param int $startId
154 private function newDumpBackup( $argv, $startId, $endId ) {
155 $dumper = new DumpBackup( $argv );
156 $dumper->startId
= $startId;
157 $dumper->endId
= $endId;
158 $dumper->reporting
= false;
160 // NOTE: The copyTestData() method used by newStreamingDBConnection()
161 // doesn't work with SQLite (T217607).
162 // But DatabaseSqlite doesn't support streaming anyway, so just skip that part.
163 if ( $this->db
->getType() === 'sqlite' ) {
164 $dumper->setDB( $this->db
);
166 $dumper->setDB( $this->newStreamingDBConnection() );
172 function testFullTextPlain() {
173 // Preparing the dump
174 $fname = $this->getNewTempFile();
176 $dumper = $this->newDumpBackup(
177 [ '--full', '--quiet', '--output', 'file:' . $fname ],
182 // Performing the dump
185 // Checking the dumped data
186 $this->assertDumpStart( $fname );
189 $this->assertPageStart( $this->pageId1
, $this->namespace, $this->pageTitle1
->getPrefixedText() );
190 $this->assertRevision( $this->revId1_1
, "BackupDumperTestP1Summary1",
191 $this->textId1_1
, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87",
192 "BackupDumperTestP1Text1" );
193 $this->assertPageEnd();
196 $this->assertPageStart( $this->pageId2
, $this->namespace, $this->pageTitle2
->getPrefixedText() );
197 $this->assertRevision( $this->revId2_1
, "BackupDumperTestP2Summary1",
198 $this->textId2_1
, 23, "jprywrymfhysqllua29tj3sc7z39dl2",
199 "BackupDumperTestP2Text1" );
200 $this->assertRevision( $this->revId2_2
, "BackupDumperTestP2Summary2",
201 $this->textId2_2
, 23, "b7vj5ks32po5m1z1t1br4o7scdwwy95",
202 "BackupDumperTestP2Text2", $this->revId2_1
);
203 $this->assertRevision( $this->revId2_3
, "BackupDumperTestP2Summary3",
204 $this->textId2_3
, 23, "jfunqmh1ssfb8rs43r19w98k28gg56r",
205 "BackupDumperTestP2Text3", $this->revId2_2
);
206 $this->assertRevision( $this->revId2_4
, "BackupDumperTestP2Summary4 extra",
207 $this->textId2_4
, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv",
208 "BackupDumperTestP2Text4 some additional Text", $this->revId2_3
);
209 $this->assertPageEnd();
212 // -> Page is marked deleted. Hence not visible
215 $this->assertPageStart(
217 $this->talk_namespace
,
218 $this->pageTitle4
->getPrefixedText()
220 $this->assertRevision( $this->revId4_1
, "Talk BackupDumperTestP1 Summary1",
221 $this->textId4_1
, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe",
222 "Talk about BackupDumperTestP1 Text1" );
223 $this->assertPageEnd();
225 $this->assertDumpEnd();
228 function testFullStubPlain() {
229 // Preparing the dump
230 $fname = $this->getNewTempFile();
232 $dumper = $this->newDumpBackup(
233 [ '--full', '--quiet', '--output', 'file:' . $fname, '--stub' ],
238 // Performing the dump
241 // Checking the dumped data
242 $this->assertDumpStart( $fname );
245 $this->assertPageStart( $this->pageId1
, $this->namespace, $this->pageTitle1
->getPrefixedText() );
246 $this->assertRevision( $this->revId1_1
, "BackupDumperTestP1Summary1",
247 $this->textId1_1
, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
248 $this->assertPageEnd();
251 $this->assertPageStart( $this->pageId2
, $this->namespace, $this->pageTitle2
->getPrefixedText() );
252 $this->assertRevision( $this->revId2_1
, "BackupDumperTestP2Summary1",
253 $this->textId2_1
, 23, "jprywrymfhysqllua29tj3sc7z39dl2" );
254 $this->assertRevision( $this->revId2_2
, "BackupDumperTestP2Summary2",
255 $this->textId2_2
, 23, "b7vj5ks32po5m1z1t1br4o7scdwwy95", false, $this->revId2_1
);
256 $this->assertRevision( $this->revId2_3
, "BackupDumperTestP2Summary3",
257 $this->textId2_3
, 23, "jfunqmh1ssfb8rs43r19w98k28gg56r", false, $this->revId2_2
);
258 $this->assertRevision( $this->revId2_4
, "BackupDumperTestP2Summary4 extra",
259 $this->textId2_4
, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3
);
260 $this->assertPageEnd();
263 // -> Page is marked deleted. Hence not visible
266 $this->assertPageStart(
268 $this->talk_namespace
,
269 $this->pageTitle4
->getPrefixedText()
271 $this->assertRevision( $this->revId4_1
, "Talk BackupDumperTestP1 Summary1",
272 $this->textId4_1
, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
273 $this->assertPageEnd();
275 $this->assertDumpEnd();
278 function testCurrentStubPlain() {
279 // Preparing the dump
280 $fname = $this->getNewTempFile();
282 $dumper = $this->newDumpBackup(
283 [ '--output', 'file:' . $fname ],
288 // Performing the dump
289 $dumper->dump( WikiExporter
::CURRENT
, WikiExporter
::STUB
);
291 // Checking the dumped data
292 $this->assertDumpStart( $fname );
295 $this->assertPageStart( $this->pageId1
, $this->namespace, $this->pageTitle1
->getPrefixedText() );
296 $this->assertRevision( $this->revId1_1
, "BackupDumperTestP1Summary1",
297 $this->textId1_1
, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
298 $this->assertPageEnd();
301 $this->assertPageStart( $this->pageId2
, $this->namespace, $this->pageTitle2
->getPrefixedText() );
302 $this->assertRevision( $this->revId2_4
, "BackupDumperTestP2Summary4 extra",
303 $this->textId2_4
, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3
);
304 $this->assertPageEnd();
307 // -> Page is marked deleted. Hence not visible
310 $this->assertPageStart(
312 $this->talk_namespace
,
313 $this->pageTitle4
->getPrefixedText()
315 $this->assertRevision( $this->revId4_1
, "Talk BackupDumperTestP1 Summary1",
316 $this->textId4_1
, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
317 $this->assertPageEnd();
319 $this->assertDumpEnd();
322 function testCurrentStubGzip() {
323 $this->checkHasGzip();
325 // Preparing the dump
326 $fname = $this->getNewTempFile();
328 $dumper = $this->newDumpBackup(
329 [ '--output', 'gzip:' . $fname ],
334 // Performing the dump
335 $dumper->dump( WikiExporter
::CURRENT
, WikiExporter
::STUB
);
337 // Checking the dumped data
338 $this->gunzip( $fname );
339 $this->assertDumpStart( $fname );
342 $this->assertPageStart( $this->pageId1
, $this->namespace, $this->pageTitle1
->getPrefixedText() );
343 $this->assertRevision( $this->revId1_1
, "BackupDumperTestP1Summary1",
344 $this->textId1_1
, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
345 $this->assertPageEnd();
348 $this->assertPageStart( $this->pageId2
, $this->namespace, $this->pageTitle2
->getPrefixedText() );
349 $this->assertRevision( $this->revId2_4
, "BackupDumperTestP2Summary4 extra",
350 $this->textId2_4
, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3
);
351 $this->assertPageEnd();
354 // -> Page is marked deleted. Hence not visible
357 $this->assertPageStart(
359 $this->talk_namespace
,
360 $this->pageTitle4
->getPrefixedText()
362 $this->assertRevision( $this->revId4_1
, "Talk BackupDumperTestP1 Summary1",
363 $this->textId4_1
, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
364 $this->assertPageEnd();
366 $this->assertDumpEnd();
370 * xmldumps-backup typically performs a single dump that that writes
372 * - gzipped stubs of everything (meta-history)
373 * - gzipped stubs of latest revisions of all pages (meta-current)
374 * - gzipped stubs of latest revisions of all pages of namespage 0
377 * We reproduce such a setup with our mini fixture, although we omit
378 * chunks, and all the other gimmicks of xmldumps-backup.
380 function testXmlDumpsBackupUseCase() {
381 $this->checkHasGzip();
383 $fnameMetaHistory = $this->getNewTempFile();
384 $fnameMetaCurrent = $this->getNewTempFile();
385 $fnameArticles = $this->getNewTempFile();
387 $dumper = $this->newDumpBackup(
388 [ "--full", "--stub", "--output=gzip:" . $fnameMetaHistory,
389 "--output=gzip:" . $fnameMetaCurrent, "--filter=latest",
390 "--output=gzip:" . $fnameArticles, "--filter=latest",
391 "--filter=notalk", "--filter=namespace:!NS_USER",
397 $dumper->reporting
= true;
399 // xmldumps-backup uses reporting. We will not check the exact reported
400 // message, as they are dependent on the processing power of the used
401 // computer. We only check that reporting does not crash the dumping
402 // and that something is reported
403 $dumper->stderr
= fopen( 'php://output', 'a' );
404 if ( $dumper->stderr
=== false ) {
405 $this->fail( "Could not open stream for stderr" );
408 // Performing the dump
409 $dumper->dump( WikiExporter
::FULL
, WikiExporter
::STUB
);
411 $this->assertTrue( fclose( $dumper->stderr
), "Closing stderr handle" );
413 // Checking meta-history -------------------------------------------------
415 $this->gunzip( $fnameMetaHistory );
416 $this->assertDumpStart( $fnameMetaHistory );
419 $this->assertPageStart( $this->pageId1
, $this->namespace, $this->pageTitle1
->getPrefixedText() );
420 $this->assertRevision( $this->revId1_1
, "BackupDumperTestP1Summary1",
421 $this->textId1_1
, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
422 $this->assertPageEnd();
425 $this->assertPageStart( $this->pageId2
, $this->namespace, $this->pageTitle2
->getPrefixedText() );
426 $this->assertRevision( $this->revId2_1
, "BackupDumperTestP2Summary1",
427 $this->textId2_1
, 23, "jprywrymfhysqllua29tj3sc7z39dl2" );
428 $this->assertRevision( $this->revId2_2
, "BackupDumperTestP2Summary2",
429 $this->textId2_2
, 23, "b7vj5ks32po5m1z1t1br4o7scdwwy95", false, $this->revId2_1
);
430 $this->assertRevision( $this->revId2_3
, "BackupDumperTestP2Summary3",
431 $this->textId2_3
, 23, "jfunqmh1ssfb8rs43r19w98k28gg56r", false, $this->revId2_2
);
432 $this->assertRevision( $this->revId2_4
, "BackupDumperTestP2Summary4 extra",
433 $this->textId2_4
, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3
);
434 $this->assertPageEnd();
437 // -> Page is marked deleted. Hence not visible
440 $this->assertPageStart(
442 $this->talk_namespace
,
443 $this->pageTitle4
->getPrefixedText()
445 $this->assertRevision( $this->revId4_1
, "Talk BackupDumperTestP1 Summary1",
446 $this->textId4_1
, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
447 $this->assertPageEnd();
449 $this->assertDumpEnd();
451 // Checking meta-current -------------------------------------------------
453 $this->gunzip( $fnameMetaCurrent );
454 $this->assertDumpStart( $fnameMetaCurrent );
457 $this->assertPageStart( $this->pageId1
, $this->namespace, $this->pageTitle1
->getPrefixedText() );
458 $this->assertRevision( $this->revId1_1
, "BackupDumperTestP1Summary1",
459 $this->textId1_1
, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
460 $this->assertPageEnd();
463 $this->assertPageStart( $this->pageId2
, $this->namespace, $this->pageTitle2
->getPrefixedText() );
464 $this->assertRevision( $this->revId2_4
, "BackupDumperTestP2Summary4 extra",
465 $this->textId2_4
, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3
);
466 $this->assertPageEnd();
469 // -> Page is marked deleted. Hence not visible
472 $this->assertPageStart(
474 $this->talk_namespace
,
475 $this->pageTitle4
->getPrefixedText()
477 $this->assertRevision( $this->revId4_1
, "Talk BackupDumperTestP1 Summary1",
478 $this->textId4_1
, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
479 $this->assertPageEnd();
481 $this->assertDumpEnd();
483 // Checking articles -------------------------------------------------
485 $this->gunzip( $fnameArticles );
486 $this->assertDumpStart( $fnameArticles );
489 $this->assertPageStart( $this->pageId1
, $this->namespace, $this->pageTitle1
->getPrefixedText() );
490 $this->assertRevision( $this->revId1_1
, "BackupDumperTestP1Summary1",
491 $this->textId1_1
, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
492 $this->assertPageEnd();
495 $this->assertPageStart( $this->pageId2
, $this->namespace, $this->pageTitle2
->getPrefixedText() );
496 $this->assertRevision( $this->revId2_4
, "BackupDumperTestP2Summary4 extra",
497 $this->textId2_4
, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3
);
498 $this->assertPageEnd();
501 // -> Page is marked deleted. Hence not visible
504 // -> Page is not in $this->namespace. Hence not visible
506 $this->assertDumpEnd();
508 $this->expectETAOutput();