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