Merge "Fix self link issues about titles in different or mixed variants"
[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
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
99 function testFullTextPlain () {
100 // Preparing the dump
101 $fname = $this->getNewTempFile();
102 $dumper = new BackupDumper( array ( "--output=file:" . $fname ) );
103 $dumper->startId = $this->pageId1;
104 $dumper->endId = $this->pageId4 + 1;
105 $dumper->reporting = false;
106 $dumper->setDb( $this->db );
107
108 // Performing the dump
109 $dumper->dump( WikiExporter::FULL, WikiExporter::TEXT );
110
111 // Checking the dumped data
112 $this->assertDumpStart( $fname );
113
114 // Page 1
115 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
116 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
117 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87",
118 "BackupDumperTestP1Text1" );
119 $this->assertPageEnd();
120
121 // Page 2
122 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
123 $this->assertRevision( $this->revId2_1, "BackupDumperTestP2Summary1",
124 $this->textId2_1, 23, "jprywrymfhysqllua29tj3sc7z39dl2",
125 "BackupDumperTestP2Text1" );
126 $this->assertRevision( $this->revId2_2, "BackupDumperTestP2Summary2",
127 $this->textId2_2, 23, "b7vj5ks32po5m1z1t1br4o7scdwwy95",
128 "BackupDumperTestP2Text2", $this->revId2_1 );
129 $this->assertRevision( $this->revId2_3, "BackupDumperTestP2Summary3",
130 $this->textId2_3, 23, "jfunqmh1ssfb8rs43r19w98k28gg56r",
131 "BackupDumperTestP2Text3", $this->revId2_2 );
132 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
133 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv",
134 "BackupDumperTestP2Text4 some additional Text", $this->revId2_3 );
135 $this->assertPageEnd();
136
137 // Page 3
138 // -> Page is marked deleted. Hence not visible
139
140 // Page 4
141 $this->assertPageStart( $this->pageId4, $this->talk_namespace, $this->pageTitle4->getPrefixedText() );
142 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
143 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe",
144 "Talk about BackupDumperTestP1 Text1" );
145 $this->assertPageEnd();
146
147 $this->assertDumpEnd();
148 }
149
150 function testFullStubPlain () {
151 // Preparing the dump
152 $fname = $this->getNewTempFile();
153 $dumper = new BackupDumper( array ( "--output=file:" . $fname ) );
154 $dumper->startId = $this->pageId1;
155 $dumper->endId = $this->pageId4 + 1;
156 $dumper->reporting = false;
157 $dumper->setDb( $this->db );
158
159 // Performing the dump
160 $dumper->dump( WikiExporter::FULL, WikiExporter::STUB );
161
162 // Checking the dumped data
163 $this->assertDumpStart( $fname );
164
165 // Page 1
166 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
167 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
168 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
169 $this->assertPageEnd();
170
171 // Page 2
172 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
173 $this->assertRevision( $this->revId2_1, "BackupDumperTestP2Summary1",
174 $this->textId2_1, 23, "jprywrymfhysqllua29tj3sc7z39dl2" );
175 $this->assertRevision( $this->revId2_2, "BackupDumperTestP2Summary2",
176 $this->textId2_2, 23, "b7vj5ks32po5m1z1t1br4o7scdwwy95", false, $this->revId2_1 );
177 $this->assertRevision( $this->revId2_3, "BackupDumperTestP2Summary3",
178 $this->textId2_3, 23, "jfunqmh1ssfb8rs43r19w98k28gg56r", false, $this->revId2_2 );
179 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
180 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
181 $this->assertPageEnd();
182
183 // Page 3
184 // -> Page is marked deleted. Hence not visible
185
186 // Page 4
187 $this->assertPageStart( $this->pageId4, $this->talk_namespace, $this->pageTitle4->getPrefixedText() );
188 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
189 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
190 $this->assertPageEnd();
191
192 $this->assertDumpEnd();
193 }
194
195 function testCurrentStubPlain () {
196 // Preparing the dump
197 $fname = $this->getNewTempFile();
198 $dumper = new BackupDumper( array ( "--output=file:" . $fname ) );
199 $dumper->startId = $this->pageId1;
200 $dumper->endId = $this->pageId4 + 1;
201 $dumper->reporting = false;
202 $dumper->setDb( $this->db );
203
204 // Performing the dump
205 $dumper->dump( WikiExporter::CURRENT, WikiExporter::STUB );
206
207 // Checking the dumped data
208 $this->assertDumpStart( $fname );
209
210 // Page 1
211 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
212 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
213 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
214 $this->assertPageEnd();
215
216 // Page 2
217 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
218 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
219 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
220 $this->assertPageEnd();
221
222 // Page 3
223 // -> Page is marked deleted. Hence not visible
224
225 // Page 4
226 $this->assertPageStart( $this->pageId4, $this->talk_namespace, $this->pageTitle4->getPrefixedText() );
227 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
228 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
229 $this->assertPageEnd();
230
231 $this->assertDumpEnd();
232 }
233
234 function testCurrentStubGzip () {
235 $this->checkHasGzip();
236
237 // Preparing the dump
238 $fname = $this->getNewTempFile();
239 $dumper = new BackupDumper( array ( "--output=gzip:" . $fname ) );
240 $dumper->startId = $this->pageId1;
241 $dumper->endId = $this->pageId4 + 1;
242 $dumper->reporting = false;
243 $dumper->setDb( $this->db );
244
245 // Performing the dump
246 $dumper->dump( WikiExporter::CURRENT, WikiExporter::STUB );
247
248 // Checking the dumped data
249 $this->gunzip( $fname );
250 $this->assertDumpStart( $fname );
251
252 // Page 1
253 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
254 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
255 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
256 $this->assertPageEnd();
257
258 // Page 2
259 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
260 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
261 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
262 $this->assertPageEnd();
263
264 // Page 3
265 // -> Page is marked deleted. Hence not visible
266
267 // Page 4
268 $this->assertPageStart( $this->pageId4, $this->talk_namespace, $this->pageTitle4->getPrefixedText() );
269 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
270 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
271 $this->assertPageEnd();
272
273 $this->assertDumpEnd();
274 }
275
276
277
278 function testXmlDumpsBackupUseCase () {
279 // xmldumps-backup typically performs a single dump that that writes
280 // out three files
281 // * gzipped stubs of everything (meta-history)
282 // * gzipped stubs of latest revisions of all pages (meta-current)
283 // * gzipped stubs of latest revisions of all pages of namespage 0
284 // (articles)
285 //
286 // We reproduce such a setup with our mini fixture, although we omit
287 // chunks, and all the other gimmicks of xmldumps-backup.
288 //
289 $this->checkHasGzip();
290
291 $fnameMetaHistory = $this->getNewTempFile();
292 $fnameMetaCurrent = $this->getNewTempFile();
293 $fnameArticles = $this->getNewTempFile();
294
295 $dumper = new BackupDumper( array ( "--output=gzip:" . $fnameMetaHistory,
296 "--output=gzip:" . $fnameMetaCurrent, "--filter=latest",
297 "--output=gzip:" . $fnameArticles, "--filter=latest",
298 "--filter=notalk", "--filter=namespace:!NS_USER",
299 "--reporting=1000" ) );
300 $dumper->startId = $this->pageId1;
301 $dumper->endId = $this->pageId4 + 1;
302 $dumper->setDb( $this->db );
303
304 // xmldumps-backup uses reporting. We will not check the exact reported
305 // message, as they are dependent on the processing power of the used
306 // computer. We only check that reporting does not crash the dumping
307 // and that something is reported
308 $dumper->stderr = fopen( 'php://output', 'a' );
309 if ( $dumper->stderr === false ) {
310 $this->fail( "Could not open stream for stderr" );
311 }
312
313 // Performing the dump
314 $dumper->dump( WikiExporter::FULL, WikiExporter::STUB );
315
316 $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
317
318 // Checking meta-history -------------------------------------------------
319
320 $this->gunzip( $fnameMetaHistory );
321 $this->assertDumpStart( $fnameMetaHistory );
322
323 // Page 1
324 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
325 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
326 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
327 $this->assertPageEnd();
328
329 // Page 2
330 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
331 $this->assertRevision( $this->revId2_1, "BackupDumperTestP2Summary1",
332 $this->textId2_1, 23, "jprywrymfhysqllua29tj3sc7z39dl2" );
333 $this->assertRevision( $this->revId2_2, "BackupDumperTestP2Summary2",
334 $this->textId2_2, 23, "b7vj5ks32po5m1z1t1br4o7scdwwy95", false, $this->revId2_1 );
335 $this->assertRevision( $this->revId2_3, "BackupDumperTestP2Summary3",
336 $this->textId2_3, 23, "jfunqmh1ssfb8rs43r19w98k28gg56r", false, $this->revId2_2 );
337 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
338 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
339 $this->assertPageEnd();
340
341 // Page 3
342 // -> Page is marked deleted. Hence not visible
343
344 // Page 4
345 $this->assertPageStart( $this->pageId4, $this->talk_namespace, $this->pageTitle4->getPrefixedText() );
346 $this->assertRevision( $this->revId4_1, "Talk BackupDumperTestP1 Summary1",
347 $this->textId4_1, 35, "nktofwzd0tl192k3zfepmlzxoax1lpe" );
348 $this->assertPageEnd();
349
350 $this->assertDumpEnd();
351
352 // Checking meta-current -------------------------------------------------
353
354 $this->gunzip( $fnameMetaCurrent );
355 $this->assertDumpStart( $fnameMetaCurrent );
356
357 // Page 1
358 $this->assertPageStart( $this->pageId1, $this->namespace, $this->pageTitle1->getPrefixedText() );
359 $this->assertRevision( $this->revId1_1, "BackupDumperTestP1Summary1",
360 $this->textId1_1, 23, "0bolhl6ol7i6x0e7yq91gxgaan39j87" );
361 $this->assertPageEnd();
362
363 // Page 2
364 $this->assertPageStart( $this->pageId2, $this->namespace, $this->pageTitle2->getPrefixedText() );
365 $this->assertRevision( $this->revId2_4, "BackupDumperTestP2Summary4 extra",
366 $this->textId2_4, 44, "6o1ciaxa6pybnqprmungwofc4lv00wv", false, $this->revId2_3 );
367 $this->assertPageEnd();
368
369 // Page 3
370 // -> Page is marked deleted. Hence not visible
371
372 // Page 4
373 $this->assertPageStart( $this->pageId4, $this->talk_namespace, $this->pageTitle4->getPrefixedText() );
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 articles -------------------------------------------------
381
382 $this->gunzip( $fnameArticles );
383 $this->assertDumpStart( $fnameArticles );
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 // -> Page is not in $this->namespace. Hence not visible
402
403 $this->assertDumpEnd();
404
405 $this->expectETAOutput();
406 }
407
408
409
410 }