0e2e1453ea2edfd4eca9b0e6df43965477137048
9 class BaseDumpTest
extends MediaWikiTestCase
{
12 * @var BaseDump The BaseDump instance used within a test.
14 * If set, this BaseDump gets automatically closed in tearDown.
18 protected function tearDown() {
19 if ( $this->dump
!== null ) {
23 // T39458, parent teardown need to be done after closing the
24 // dump or it might cause some permissions errors.
29 * asserts that a prefetch yields an expected string
31 * @param string|null $expected The exepcted result of the prefetch
32 * @param int $page The page number to prefetch the text for
33 * @param int $revision The revision number to prefetch the text for
35 private function assertPrefetchEquals( $expected, $page, $revision ) {
36 $this->assertEquals( $expected, $this->dump
->prefetch( $page, $revision ),
37 "Prefetch of page $page revision $revision" );
40 function testSequential() {
41 $fname = $this->setUpPrefetch();
42 $this->dump
= new BaseDump( $fname );
44 $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
45 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
46 $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
47 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
50 function testSynchronizeRevisionMissToRevision() {
51 $fname = $this->setUpPrefetch();
52 $this->dump
= new BaseDump( $fname );
54 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
55 $this->assertPrefetchEquals( null, 2, 3 );
56 $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
59 function testSynchronizeRevisionMissToPage() {
60 $fname = $this->setUpPrefetch();
61 $this->dump
= new BaseDump( $fname );
63 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
64 $this->assertPrefetchEquals( null, 2, 40 );
65 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
68 function testSynchronizePageMiss() {
69 $fname = $this->setUpPrefetch();
70 $this->dump
= new BaseDump( $fname );
72 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
73 $this->assertPrefetchEquals( null, 3, 40 );
74 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
77 function testPageMissAtEnd() {
78 $fname = $this->setUpPrefetch();
79 $this->dump
= new BaseDump( $fname );
81 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
82 $this->assertPrefetchEquals( null, 6, 40 );
85 function testRevisionMissAtEnd() {
86 $fname = $this->setUpPrefetch();
87 $this->dump
= new BaseDump( $fname );
89 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
90 $this->assertPrefetchEquals( null, 4, 40 );
93 function testSynchronizePageMissAtStart() {
94 $fname = $this->setUpPrefetch();
95 $this->dump
= new BaseDump( $fname );
97 $this->assertPrefetchEquals( null, 0, 2 );
98 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
101 function testSynchronizeRevisionMissAtStart() {
102 $fname = $this->setUpPrefetch();
103 $this->dump
= new BaseDump( $fname );
105 $this->assertPrefetchEquals( null, 1, -2 );
106 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
109 function testSequentialAcrossFiles() {
110 $fname1 = $this->setUpPrefetch( [ 1 ] );
111 $fname2 = $this->setUpPrefetch( [ 2, 4 ] );
112 $this->dump
= new BaseDump( $fname1 . ";" . $fname2 );
114 $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
115 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
116 $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
117 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
120 function testSynchronizeSkipAcrossFile() {
121 $fname1 = $this->setUpPrefetch( [ 1 ] );
122 $fname2 = $this->setUpPrefetch( [ 2 ] );
123 $fname3 = $this->setUpPrefetch( [ 4 ] );
124 $this->dump
= new BaseDump( $fname1 . ";" . $fname2 . ";" . $fname3 );
126 $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
127 $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
130 function testSynchronizeMissInWholeFirstFile() {
131 $fname1 = $this->setUpPrefetch( [ 1 ] );
132 $fname2 = $this->setUpPrefetch( [ 2 ] );
133 $this->dump
= new BaseDump( $fname1 . ";" . $fname2 );
135 $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
139 * Constructs a temporary file that can be used for prefetching
141 * The temporary file is removed by DumpBackup upon tearDown.
143 * @param array $requested_pages The indices of the page parts that should
144 * go into the prefetch file. 1,2,4 are available.
145 * @return string The file name of the created temporary file
147 private function setUpPrefetch( $requested_pages = [ 1, 2, 4 ] ) {
148 // The file name, where we store the prepared prefetch file
149 $fname = $this->getNewTempFile();
151 // The header of every prefetch file
152 // phpcs:ignore Generic.Files.LineLength
153 $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">
155 <sitename>wikisvn</sitename>
156 <base>http://localhost/wiki-svn/index.php/Main_Page</base>
157 <generator>MediaWiki 1.21alpha</generator>
158 <case>first-letter</case>
160 <namespace key="-2" case="first-letter">Media</namespace>
161 <namespace key="-1" case="first-letter">Special</namespace>
162 <namespace key="0" case="first-letter" />
163 <namespace key="1" case="first-letter">Talk</namespace>
164 <namespace key="2" case="first-letter">User</namespace>
165 <namespace key="3" case="first-letter">User talk</namespace>
166 <namespace key="4" case="first-letter">Wikisvn</namespace>
167 <namespace key="5" case="first-letter">Wikisvn talk</namespace>
168 <namespace key="6" case="first-letter">File</namespace>
169 <namespace key="7" case="first-letter">File talk</namespace>
170 <namespace key="8" case="first-letter">MediaWiki</namespace>
171 <namespace key="9" case="first-letter">MediaWiki talk</namespace>
172 <namespace key="10" case="first-letter">Template</namespace>
173 <namespace key="11" case="first-letter">Template talk</namespace>
174 <namespace key="12" case="first-letter">Help</namespace>
175 <namespace key="13" case="first-letter">Help talk</namespace>
176 <namespace key="14" case="first-letter">Category</namespace>
177 <namespace key="15" case="first-letter">Category talk</namespace>
182 // An array holding the pages that are available for prefetch
183 $available_pages = [];
186 $available_pages[1] = ' <page>
187 <title>BackupDumperTestP1</title>
192 <timestamp>2012-04-01T16:46:05Z</timestamp>
196 <comment>BackupDumperTestP1Summary1</comment>
197 <sha1>0bolhl6ol7i6x0e7yq91gxgaan39j87</sha1>
198 <text xml:space="preserve">BackupDumperTestP1Text1</text>
199 <model name="wikitext">1</model>
200 <format mime="text/x-wiki">1</format>
204 // Page with more than one revisions. Hole in rev ids.
205 $available_pages[2] = ' <page>
206 <title>BackupDumperTestP2</title>
211 <timestamp>2012-04-01T16:46:05Z</timestamp>
215 <comment>BackupDumperTestP2Summary1</comment>
216 <sha1>jprywrymfhysqllua29tj3sc7z39dl2</sha1>
217 <text xml:space="preserve">BackupDumperTestP2Text1</text>
218 <model name="wikitext">1</model>
219 <format mime="text/x-wiki">1</format>
223 <parentid>2</parentid>
224 <timestamp>2012-04-01T16:46:05Z</timestamp>
228 <comment>BackupDumperTestP2Summary4 extra</comment>
229 <sha1>6o1ciaxa6pybnqprmungwofc4lv00wv</sha1>
230 <text xml:space="preserve">BackupDumperTestP2Text4 some additional Text</text>
231 <model name="wikitext">1</model>
232 <format mime="text/x-wiki">1</format>
236 // Page with id higher than previous id + 1
237 $available_pages[4] = ' <page>
238 <title>Talk:BackupDumperTestP1</title>
243 <timestamp>2012-04-01T16:46:05Z</timestamp>
247 <comment>Talk BackupDumperTestP1 Summary1</comment>
248 <sha1>nktofwzd0tl192k3zfepmlzxoax1lpe</sha1>
249 <model name="wikitext">1</model>
250 <format mime="text/x-wiki">1</format>
251 <text xml:space="preserve">Talk about BackupDumperTestP1 Text1</text>
256 // The common ending for all files
257 $tail = '</mediawiki>
260 // Putting together the content of the prefetch files
262 foreach ( $requested_pages as $i ) {
263 $this->assertTrue( array_key_exists( $i, $available_pages ),
264 "Check for availability of requested page " . $i );
265 $content .= $available_pages[$i];
269 $this->assertEquals( strlen( $content ), file_put_contents(
270 $fname, $content ), "Length of prepared prefetch" );