3 namespace MediaWiki\Tests\Maintenance
;
14 class BaseDumpTest
extends MediaWikiTestCase
{
17 * @var BaseDump The BaseDump instance used within a test.
19 * If set, this BaseDump gets automatically closed in tearDown.
23 protected function tearDown() {
24 if ( $this->dump
!== null ) {
28 // T39458, parent teardown need to be done after closing the
29 // dump or it might cause some permissions errors.
34 * asserts that a prefetch yields an expected string
36 * @param string|null $expected The exepcted result of the prefetch
37 * @param int $page The page number to prefetch the text for
38 * @param int $revision The revision number to prefetch the text for
40 private function assertPrefetchEquals( $expected, $page, $revision ) {
41 $this->assertEquals( $expected, $this->dump
->prefetch( $page, $revision ),
42 "Prefetch of page $page revision $revision" );
45 function testSequential() {
46 $fname = $this->setUpPrefetch();
47 $this->dump
= new BaseDump( $fname );
49 $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
50 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
51 $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
52 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
55 function testSynchronizeRevisionMissToRevision() {
56 $fname = $this->setUpPrefetch();
57 $this->dump
= new BaseDump( $fname );
59 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
60 $this->assertPrefetchEquals( null, 2, 3 );
61 $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
64 function testSynchronizeRevisionMissToPage() {
65 $fname = $this->setUpPrefetch();
66 $this->dump
= new BaseDump( $fname );
68 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
69 $this->assertPrefetchEquals( null, 2, 40 );
70 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
73 function testSynchronizePageMiss() {
74 $fname = $this->setUpPrefetch();
75 $this->dump
= new BaseDump( $fname );
77 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
78 $this->assertPrefetchEquals( null, 3, 40 );
79 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
82 function testPageMissAtEnd() {
83 $fname = $this->setUpPrefetch();
84 $this->dump
= new BaseDump( $fname );
86 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
87 $this->assertPrefetchEquals( null, 6, 40 );
90 function testRevisionMissAtEnd() {
91 $fname = $this->setUpPrefetch();
92 $this->dump
= new BaseDump( $fname );
94 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
95 $this->assertPrefetchEquals( null, 4, 40 );
98 function testSynchronizePageMissAtStart() {
99 $fname = $this->setUpPrefetch();
100 $this->dump
= new BaseDump( $fname );
102 $this->assertPrefetchEquals( null, 0, 2 );
103 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
106 function testSynchronizeRevisionMissAtStart() {
107 $fname = $this->setUpPrefetch();
108 $this->dump
= new BaseDump( $fname );
110 $this->assertPrefetchEquals( null, 1, -2 );
111 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
114 function testSequentialAcrossFiles() {
115 $fname1 = $this->setUpPrefetch( [ 1 ] );
116 $fname2 = $this->setUpPrefetch( [ 2, 4 ] );
117 $this->dump
= new BaseDump( $fname1 . ";" . $fname2 );
119 $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
120 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
121 $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
122 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
125 function testSynchronizeSkipAcrossFile() {
126 $fname1 = $this->setUpPrefetch( [ 1 ] );
127 $fname2 = $this->setUpPrefetch( [ 2 ] );
128 $fname3 = $this->setUpPrefetch( [ 4 ] );
129 $this->dump
= new BaseDump( $fname1 . ";" . $fname2 . ";" . $fname3 );
131 $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
132 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
135 function testSynchronizeMissInWholeFirstFile() {
136 $fname1 = $this->setUpPrefetch( [ 1 ] );
137 $fname2 = $this->setUpPrefetch( [ 2 ] );
138 $this->dump
= new BaseDump( $fname1 . ";" . $fname2 );
140 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
144 * Constructs a temporary file that can be used for prefetching
146 * The temporary file is removed by DumpBackup upon tearDown.
148 * @param array $requested_pages The indices of the page parts that should
149 * go into the prefetch file. 1,2,4 are available.
150 * @return string The file name of the created temporary file
152 private function setUpPrefetch( $requested_pages = [ 1, 2, 4 ] ) {
153 // The file name, where we store the prepared prefetch file
154 $fname = $this->getNewTempFile();
156 // The header of every prefetch file
157 // phpcs:ignore Generic.Files.LineLength
158 $header = '<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.7/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.7/ http://www.mediawiki.org/xml/export-0.7.xsd" version="0.7" xml:lang="en">
160 <sitename>wikisvn</sitename>
161 <base>http://localhost/wiki-svn/index.php/Main_Page</base>
162 <generator>MediaWiki 1.21alpha</generator>
163 <case>first-letter</case>
165 <namespace key="-2" case="first-letter">Media</namespace>
166 <namespace key="-1" case="first-letter">Special</namespace>
167 <namespace key="0" case="first-letter" />
168 <namespace key="1" case="first-letter">Talk</namespace>
169 <namespace key="2" case="first-letter">User</namespace>
170 <namespace key="3" case="first-letter">User talk</namespace>
171 <namespace key="4" case="first-letter">Wikisvn</namespace>
172 <namespace key="5" case="first-letter">Wikisvn talk</namespace>
173 <namespace key="6" case="first-letter">File</namespace>
174 <namespace key="7" case="first-letter">File talk</namespace>
175 <namespace key="8" case="first-letter">MediaWiki</namespace>
176 <namespace key="9" case="first-letter">MediaWiki talk</namespace>
177 <namespace key="10" case="first-letter">Template</namespace>
178 <namespace key="11" case="first-letter">Template talk</namespace>
179 <namespace key="12" case="first-letter">Help</namespace>
180 <namespace key="13" case="first-letter">Help talk</namespace>
181 <namespace key="14" case="first-letter">Category</namespace>
182 <namespace key="15" case="first-letter">Category talk</namespace>
187 // An array holding the pages that are available for prefetch
188 $available_pages = [];
191 $available_pages[1] = ' <page>
192 <title>BackupDumperTestP1</title>
197 <timestamp>2012-04-01T16:46:05Z</timestamp>
201 <comment>BackupDumperTestP1Summary1</comment>
202 <sha1>0bolhl6ol7i6x0e7yq91gxgaan39j87</sha1>
203 <text xml:space="preserve">BackupDumperTestP1Text1</text>
204 <model name="wikitext">1</model>
205 <format mime="text/x-wiki">1</format>
209 // Page with more than one revisions. Hole in rev ids.
210 $available_pages[2] = ' <page>
211 <title>BackupDumperTestP2</title>
216 <timestamp>2012-04-01T16:46:05Z</timestamp>
220 <comment>BackupDumperTestP2Summary1</comment>
221 <sha1>jprywrymfhysqllua29tj3sc7z39dl2</sha1>
222 <text xml:space="preserve">BackupDumperTestP2Text1</text>
223 <model name="wikitext">1</model>
224 <format mime="text/x-wiki">1</format>
228 <parentid>2</parentid>
229 <timestamp>2012-04-01T16:46:05Z</timestamp>
233 <comment>BackupDumperTestP2Summary4 extra</comment>
234 <sha1>6o1ciaxa6pybnqprmungwofc4lv00wv</sha1>
235 <text xml:space="preserve">BackupDumperTestP2Text4 some additional Text</text>
236 <model name="wikitext">1</model>
237 <format mime="text/x-wiki">1</format>
241 // Page with id higher than previous id + 1
242 $available_pages[4] = ' <page>
243 <title>Talk:BackupDumperTestP1</title>
248 <timestamp>2012-04-01T16:46:05Z</timestamp>
252 <comment>Talk BackupDumperTestP1 Summary1</comment>
253 <sha1>nktofwzd0tl192k3zfepmlzxoax1lpe</sha1>
254 <model name="wikitext">1</model>
255 <format mime="text/x-wiki">1</format>
256 <text xml:space="preserve">Talk about BackupDumperTestP1 Text1</text>
261 // The common ending for all files
262 $tail = '</mediawiki>
265 // Putting together the content of the prefetch files
267 foreach ( $requested_pages as $i ) {
268 $this->assertTrue( array_key_exists( $i, $available_pages ),
269 "Check for availability of requested page " . $i );
270 $content .= $available_pages[$i];
274 $this->assertEquals( strlen( $content ), file_put_contents(
275 $fname, $content ), "Length of prepared prefetch" );