Use setContentLang() instead of setMwGlobals()
[lhc/web/wiklou.git] / tests / phpunit / maintenance / backup_PageTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Maintenance;
4
5 use DumpBackup;
6 use Title;
7 use WikiExporter;
8 use WikiPage;
9
10 /**
11 * Tests for page dumps of BackupDumper
12 *
13 * @group Database
14 * @group Dump
15 * @covers BackupDumper
16 */
17 class BackupDumperPageTest extends DumpTestCase {
18
19 // We'll add several pages, revision and texts. The following variables hold the
20 // corresponding ids.
21 private $pageId1, $pageId2, $pageId3, $pageId4;
22 private $pageTitle1, $pageTitle2, $pageTitle3, $pageTitle4;
23 private $revId1_1, $textId1_1;
24 private $revId2_1, $textId2_1, $revId2_2, $textId2_2;
25 private $revId2_3, $textId2_3, $revId2_4, $textId2_4;
26 private $revId3_1, $textId3_1, $revId3_2, $textId3_2;
27 private $revId4_1, $textId4_1;
28 private $namespace, $talk_namespace;
29
30 function addDBData() {
31 // be sure, titles created here using english namespace names
32 $this->setContentLang( 'en' );
33
34 $this->tablesUsed[] = 'page';
35 $this->tablesUsed[] = 'revision';
36 $this->tablesUsed[] = 'ip_changes';
37 $this->tablesUsed[] = 'text';
38
39 try {
40 $this->namespace = $this->getDefaultWikitextNS();
41 $this->talk_namespace = NS_TALK;
42
43 if ( $this->namespace === $this->talk_namespace ) {
44 // @todo work around this.
45 throw new MWException( "The default wikitext namespace is the talk namespace. "
46 . " We can't currently deal with that." );
47 }
48
49 $this->pageTitle1 = Title::newFromText( 'BackupDumperTestP1', $this->namespace );
50 $page = WikiPage::factory( $this->pageTitle1 );
51 list( $this->revId1_1, $this->textId1_1 ) = $this->addRevision( $page,
52 "BackupDumperTestP1Text1", "BackupDumperTestP1Summary1" );
53 $this->pageId1 = $page->getId();
54
55 $this->pageTitle2 = Title::newFromText( 'BackupDumperTestP2', $this->namespace );
56 $page = WikiPage::factory( $this->pageTitle2 );
57 list( $this->revId2_1, $this->textId2_1 ) = $this->addRevision( $page,
58 "BackupDumperTestP2Text1", "BackupDumperTestP2Summary1" );
59 list( $this->revId2_2, $this->textId2_2 ) = $this->addRevision( $page,
60 "BackupDumperTestP2Text2", "BackupDumperTestP2Summary2" );
61 list( $this->revId2_3, $this->textId2_3 ) = $this->addRevision( $page,
62 "BackupDumperTestP2Text3", "BackupDumperTestP2Summary3" );
63 list( $this->revId2_4, $this->textId2_4 ) = $this->addRevision( $page,
64 "BackupDumperTestP2Text4 some additional Text ",
65 "BackupDumperTestP2Summary4 extra " );
66 $this->pageId2 = $page->getId();
67
68 $this->pageTitle3 = Title::newFromText( 'BackupDumperTestP3', $this->namespace );
69 $page = WikiPage::factory( $this->pageTitle3 );
70 list( $this->revId3_1, $this->textId3_1 ) = $this->addRevision( $page,
71 "BackupDumperTestP3Text1", "BackupDumperTestP2Summary1" );
72 list( $this->revId3_2, $this->textId3_2 ) = $this->addRevision( $page,
73 "BackupDumperTestP3Text2", "BackupDumperTestP2Summary2" );
74 $this->pageId3 = $page->getId();
75 $page->doDeleteArticle( "Testing ;)" );
76
77 $this->pageTitle4 = Title::newFromText( 'BackupDumperTestP1', $this->talk_namespace );
78 $page = WikiPage::factory( $this->pageTitle4 );
79 list( $this->revId4_1, $this->textId4_1 ) = $this->addRevision( $page,
80 "Talk about BackupDumperTestP1 Text1",
81 "Talk BackupDumperTestP1 Summary1" );
82 $this->pageId4 = $page->getId();
83 } catch ( Exception $e ) {
84 // We'd love to pass $e directly. However, ... see
85 // documentation of exceptionFromAddDBData in
86 // DumpTestCase
87 $this->exceptionFromAddDBData = $e;
88 }
89 }
90
91 protected function setUp() {
92 parent::setUp();
93
94 // Since we will restrict dumping by page ranges (to allow
95 // working tests, even if the db gets prepopulated by a base
96 // class), we have to assert, that the page id are consecutively
97 // increasing
98 $this->assertEquals(
99 [ $this->pageId2, $this->pageId3, $this->pageId4 ],
100 [ $this->pageId1 + 1, $this->pageId2 + 1, $this->pageId3 + 1 ],
101 "Page ids increasing without holes" );
102 }
103
104 function testFullTextPlain() {
105 // Preparing the dump
106 $fname = $this->getNewTempFile();
107
108 $dumper = new DumpBackup();
109 $dumper->loadWithArgv( [ '--full', '--quiet', '--output', 'file:' . $fname ] );
110 $dumper->startId = $this->pageId1;
111 $dumper->endId = $this->pageId4 + 1;
112 $dumper->setDB( $this->db );
113
114 // Performing the dump
115 $dumper->execute();
116
117 // Checking the dumped data
118 $this->assertDumpStart( $fname );
119
120 // Page 1
121 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
122 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
123 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87",
124 "BackupDumperTestP1Text1" );
125 $this->assertPageEnd();
126
127 // Page 2
128 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
129 $this->assertRevision( $this->revId2_1, "BackupDumperTestP2Summary1",
130 $this->textId2_1, 23, "jprywrymfhysqllua29tj3sc7z39dl2",
131 "BackupDumperTestP2Text1" );
132 $this->assertRevision( $this->revId2_2, "BackupDumperTestP2Summary2",
133 $this->textId2_2, 23, "b7vj5ks32po5m1z1t1br4o7scdwwy95",
134 "BackupDumperTestP2Text2", $this->revId2_1 );
135 $this->assertRevision( $this->revId2_3, "BackupDumperTestP2Summary3",
136 $this->textId2_3, 23, "jfunqmh1ssfb8rs43r19w98k28gg56r",
137 "BackupDumperTestP2Text3", $this->revId2_2 );
138 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
139 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv",
140 "BackupDumperTestP2Text4 some additional Text", $this->revId2_3 );
141 $this->assertPageEnd();
142
143 // Page 3
144 // -> Page is marked deleted. Hence not visible
145
146 // Page 4
147 $this->assertPageStart(
148 $this->pageId4,
149 $this->talk_namespace,
150 $this->pageTitle4->getPrefixedText()
151 );
152 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
153 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe",
154 "Talk about BackupDumperTestP1 Text1" );
155 $this->assertPageEnd();
156
157 $this->assertDumpEnd();
158 }
159
160 function testFullStubPlain() {
161 // Preparing the dump
162 $fname = $this->getNewTempFile();
163
164 $dumper = new DumpBackup();
165 $dumper->loadWithArgv( [ '--full', '--quiet', '--output', 'file:' . $fname, '--stub' ] );
166 $dumper->startId = $this->pageId1;
167 $dumper->endId = $this->pageId4 + 1;
168 $dumper->setDB( $this->db );
169
170 // Performing the dump
171 $dumper->execute();
172
173 // Checking the dumped data
174 $this->assertDumpStart( $fname );
175
176 // Page 1
177 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
178 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
179 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
180 $this->assertPageEnd();
181
182 // Page 2
183 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
184 $this->assertRevision( $this->revId2_1, "BackupDumperTestP2Summary1",
185 $this->textId2_1, 23, "jprywrymfhysqllua29tj3sc7z39dl2" );
186 $this->assertRevision( $this->revId2_2, "BackupDumperTestP2Summary2",
187 $this->textId2_2, 23, "b7vj5ks32po5m1z1t1br4o7scdwwy95", false, $this->revId2_1 );
188 $this->assertRevision( $this->revId2_3, "BackupDumperTestP2Summary3",
189 $this->textId2_3, 23, "jfunqmh1ssfb8rs43r19w98k28gg56r", false, $this->revId2_2 );
190 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
191 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
192 $this->assertPageEnd();
193
194 // Page 3
195 // -> Page is marked deleted. Hence not visible
196
197 // Page 4
198 $this->assertPageStart(
199 $this->pageId4,
200 $this->talk_namespace,
201 $this->pageTitle4->getPrefixedText()
202 );
203 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
204 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
205 $this->assertPageEnd();
206
207 $this->assertDumpEnd();
208 }
209
210 function testCurrentStubPlain() {
211 // Preparing the dump
212 $fname = $this->getNewTempFile();
213
214 $dumper = new DumpBackup( [ '--output', 'file:' . $fname ] );
215 $dumper->startId = $this->pageId1;
216 $dumper->endId = $this->pageId4 + 1;
217 $dumper->reporting = false;
218 $dumper->setDB( $this->db );
219
220 // Performing the dump
221 $dumper->dump( WikiExporter::CURRENT, WikiExporter::STUB );
222
223 // Checking the dumped data
224 $this->assertDumpStart( $fname );
225
226 // Page 1
227 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
228 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
229 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
230 $this->assertPageEnd();
231
232 // Page 2
233 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
234 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
235 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
236 $this->assertPageEnd();
237
238 // Page 3
239 // -> Page is marked deleted. Hence not visible
240
241 // Page 4
242 $this->assertPageStart(
243 $this->pageId4,
244 $this->talk_namespace,
245 $this->pageTitle4->getPrefixedText()
246 );
247 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
248 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
249 $this->assertPageEnd();
250
251 $this->assertDumpEnd();
252 }
253
254 function testCurrentStubGzip() {
255 $this->checkHasGzip();
256
257 // Preparing the dump
258 $fname = $this->getNewTempFile();
259
260 $dumper = new DumpBackup( [ '--output', 'gzip:' . $fname ] );
261 $dumper->startId = $this->pageId1;
262 $dumper->endId = $this->pageId4 + 1;
263 $dumper->reporting = false;
264 $dumper->setDB( $this->db );
265
266 // Performing the dump
267 $dumper->dump( WikiExporter::CURRENT, WikiExporter::STUB );
268
269 // Checking the dumped data
270 $this->gunzip( $fname );
271 $this->assertDumpStart( $fname );
272
273 // Page 1
274 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
275 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
276 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
277 $this->assertPageEnd();
278
279 // Page 2
280 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
281 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
282 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
283 $this->assertPageEnd();
284
285 // Page 3
286 // -> Page is marked deleted. Hence not visible
287
288 // Page 4
289 $this->assertPageStart(
290 $this->pageId4,
291 $this->talk_namespace,
292 $this->pageTitle4->getPrefixedText()
293 );
294 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
295 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
296 $this->assertPageEnd();
297
298 $this->assertDumpEnd();
299 }
300
301 /**
302 * xmldumps-backup typically performs a single dump that that writes
303 * out three files
304 * - gzipped stubs of everything (meta-history)
305 * - gzipped stubs of latest revisions of all pages (meta-current)
306 * - gzipped stubs of latest revisions of all pages of namespage 0
307 * (articles)
308 *
309 * We reproduce such a setup with our mini fixture, although we omit
310 * chunks, and all the other gimmicks of xmldumps-backup.
311 */
312 function testXmlDumpsBackupUseCase() {
313 $this->checkHasGzip();
314
315 $fnameMetaHistory = $this->getNewTempFile();
316 $fnameMetaCurrent = $this->getNewTempFile();
317 $fnameArticles = $this->getNewTempFile();
318
319 $dumper = new DumpBackup( [ "--full", "--stub", "--output=gzip:" . $fnameMetaHistory,
320 "--output=gzip:" . $fnameMetaCurrent, "--filter=latest",
321 "--output=gzip:" . $fnameArticles, "--filter=latest",
322 "--filter=notalk", "--filter=namespace:!NS_USER",
323 "--reporting=1000" ] );
324 $dumper->startId = $this->pageId1;
325 $dumper->endId = $this->pageId4 + 1;
326 $dumper->setDB( $this->db );
327
328 // xmldumps-backup uses reporting. We will not check the exact reported
329 // message, as they are dependent on the processing power of the used
330 // computer. We only check that reporting does not crash the dumping
331 // and that something is reported
332 $dumper->stderr = fopen( 'php://output', 'a' );
333 if ( $dumper->stderr === false ) {
334 $this->fail( "Could not open stream for stderr" );
335 }
336
337 // Performing the dump
338 $dumper->dump( WikiExporter::FULL, WikiExporter::STUB );
339
340 $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
341
342 // Checking meta-history -------------------------------------------------
343
344 $this->gunzip( $fnameMetaHistory );
345 $this->assertDumpStart( $fnameMetaHistory );
346
347 // Page 1
348 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
349 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
350 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
351 $this->assertPageEnd();
352
353 // Page 2
354 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
355 $this->assertRevision( $this->revId2_1, "BackupDumperTestP2Summary1",
356 $this->textId2_1, 23, "jprywrymfhysqllua29tj3sc7z39dl2" );
357 $this->assertRevision( $this->revId2_2, "BackupDumperTestP2Summary2",
358 $this->textId2_2, 23, "b7vj5ks32po5m1z1t1br4o7scdwwy95", false, $this->revId2_1 );
359 $this->assertRevision( $this->revId2_3, "BackupDumperTestP2Summary3",
360 $this->textId2_3, 23, "jfunqmh1ssfb8rs43r19w98k28gg56r", false, $this->revId2_2 );
361 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
362 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
363 $this->assertPageEnd();
364
365 // Page 3
366 // -> Page is marked deleted. Hence not visible
367
368 // Page 4
369 $this->assertPageStart(
370 $this->pageId4,
371 $this->talk_namespace,
372 $this->pageTitle4->getPrefixedText()
373 );
374 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
375 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
376 $this->assertPageEnd();
377
378 $this->assertDumpEnd();
379
380 // Checking meta-current -------------------------------------------------
381
382 $this->gunzip( $fnameMetaCurrent );
383 $this->assertDumpStart( $fnameMetaCurrent );
384
385 // Page 1
386 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
387 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
388 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
389 $this->assertPageEnd();
390
391 // Page 2
392 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
393 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
394 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
395 $this->assertPageEnd();
396
397 // Page 3
398 // -> Page is marked deleted. Hence not visible
399
400 // Page 4
401 $this->assertPageStart(
402 $this->pageId4,
403 $this->talk_namespace,
404 $this->pageTitle4->getPrefixedText()
405 );
406 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
407 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
408 $this->assertPageEnd();
409
410 $this->assertDumpEnd();
411
412 // Checking articles -------------------------------------------------
413
414 $this->gunzip( $fnameArticles );
415 $this->assertDumpStart( $fnameArticles );
416
417 // Page 1
418 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
419 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
420 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
421 $this->assertPageEnd();
422
423 // Page 2
424 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
425 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
426 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
427 $this->assertPageEnd();
428
429 // Page 3
430 // -> Page is marked deleted. Hence not visible
431
432 // Page 4
433 // -> Page is not in $this->namespace. Hence not visible
434
435 $this->assertDumpEnd();
436
437 $this->expectETAOutput();
438 }
439 }