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