MediaWiki  REL1_24
backupPrefetchTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 require_once __DIR__ . "/../../../maintenance/backupPrefetch.inc";
00004 
00011 class BaseDumpTest extends MediaWikiTestCase {
00012 
00018     private $dump = null;
00019 
00020     protected function tearDown() {
00021         if ( $this->dump !== null ) {
00022             $this->dump->close();
00023         }
00024 
00025         // Bug 37458, parent teardown need to be done after closing the
00026         // dump or it might cause some permissions errors.
00027         parent::tearDown();
00028     }
00029 
00037     private function assertPrefetchEquals( $expected, $page, $revision ) {
00038         $this->assertEquals( $expected, $this->dump->prefetch( $page, $revision ),
00039             "Prefetch of page $page revision $revision" );
00040     }
00041 
00042     function testSequential() {
00043         $fname = $this->setUpPrefetch();
00044         $this->dump = new BaseDump( $fname );
00045 
00046         $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
00047         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00048         $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
00049         $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
00050     }
00051 
00052     function testSynchronizeRevisionMissToRevision() {
00053         $fname = $this->setUpPrefetch();
00054         $this->dump = new BaseDump( $fname );
00055 
00056         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00057         $this->assertPrefetchEquals( null, 2, 3 );
00058         $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
00059     }
00060 
00061     function testSynchronizeRevisionMissToPage() {
00062         $fname = $this->setUpPrefetch();
00063         $this->dump = new BaseDump( $fname );
00064 
00065         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00066         $this->assertPrefetchEquals( null, 2, 40 );
00067         $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
00068     }
00069 
00070     function testSynchronizePageMiss() {
00071         $fname = $this->setUpPrefetch();
00072         $this->dump = new BaseDump( $fname );
00073 
00074         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00075         $this->assertPrefetchEquals( null, 3, 40 );
00076         $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
00077     }
00078 
00079     function testPageMissAtEnd() {
00080         $fname = $this->setUpPrefetch();
00081         $this->dump = new BaseDump( $fname );
00082 
00083         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00084         $this->assertPrefetchEquals( null, 6, 40 );
00085     }
00086 
00087     function testRevisionMissAtEnd() {
00088         $fname = $this->setUpPrefetch();
00089         $this->dump = new BaseDump( $fname );
00090 
00091         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00092         $this->assertPrefetchEquals( null, 4, 40 );
00093     }
00094 
00095     function testSynchronizePageMissAtStart() {
00096         $fname = $this->setUpPrefetch();
00097         $this->dump = new BaseDump( $fname );
00098 
00099         $this->assertPrefetchEquals( null, 0, 2 );
00100         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00101     }
00102 
00103     function testSynchronizeRevisionMissAtStart() {
00104         $fname = $this->setUpPrefetch();
00105         $this->dump = new BaseDump( $fname );
00106 
00107         $this->assertPrefetchEquals( null, 1, -2 );
00108         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00109     }
00110 
00111     function testSequentialAcrossFiles() {
00112         $fname1 = $this->setUpPrefetch( array( 1 ) );
00113         $fname2 = $this->setUpPrefetch( array( 2, 4 ) );
00114         $this->dump = new BaseDump( $fname1 . ";" . $fname2 );
00115 
00116         $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
00117         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00118         $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
00119         $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
00120     }
00121 
00122     function testSynchronizeSkipAcrossFile() {
00123         $fname1 = $this->setUpPrefetch( array( 1 ) );
00124         $fname2 = $this->setUpPrefetch( array( 2 ) );
00125         $fname3 = $this->setUpPrefetch( array( 4 ) );
00126         $this->dump = new BaseDump( $fname1 . ";" . $fname2 . ";" . $fname3 );
00127 
00128         $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
00129         $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
00130     }
00131 
00132     function testSynchronizeMissInWholeFirstFile() {
00133         $fname1 = $this->setUpPrefetch( array( 1 ) );
00134         $fname2 = $this->setUpPrefetch( array( 2 ) );
00135         $this->dump = new BaseDump( $fname1 . ";" . $fname2 );
00136 
00137         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00138     }
00139 
00149     private function setUpPrefetch( $requested_pages = array( 1, 2, 4 ) ) {
00150         // The file name, where we store the prepared prefetch file
00151         $fname = $this->getNewTempFile();
00152 
00153         // The header of every prefetch file
00154         // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
00155         $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">
00156   <siteinfo>
00157     <sitename>wikisvn</sitename>
00158     <base>http://localhost/wiki-svn/index.php/Main_Page</base>
00159     <generator>MediaWiki 1.21alpha</generator>
00160     <case>first-letter</case>
00161     <namespaces>
00162       <namespace key="-2" case="first-letter">Media</namespace>
00163       <namespace key="-1" case="first-letter">Special</namespace>
00164       <namespace key="0" case="first-letter" />
00165       <namespace key="1" case="first-letter">Talk</namespace>
00166       <namespace key="2" case="first-letter">User</namespace>
00167       <namespace key="3" case="first-letter">User talk</namespace>
00168       <namespace key="4" case="first-letter">Wikisvn</namespace>
00169       <namespace key="5" case="first-letter">Wikisvn talk</namespace>
00170       <namespace key="6" case="first-letter">File</namespace>
00171       <namespace key="7" case="first-letter">File talk</namespace>
00172       <namespace key="8" case="first-letter">MediaWiki</namespace>
00173       <namespace key="9" case="first-letter">MediaWiki talk</namespace>
00174       <namespace key="10" case="first-letter">Template</namespace>
00175       <namespace key="11" case="first-letter">Template talk</namespace>
00176       <namespace key="12" case="first-letter">Help</namespace>
00177       <namespace key="13" case="first-letter">Help talk</namespace>
00178       <namespace key="14" case="first-letter">Category</namespace>
00179       <namespace key="15" case="first-letter">Category talk</namespace>
00180     </namespaces>
00181   </siteinfo>
00182 ';
00183         // @codingStandardsIgnoreEnd
00184 
00185         // An array holding the pages that are available for prefetch
00186         $available_pages = array();
00187 
00188         // Simple plain page
00189         $available_pages[1] = '  <page>
00190     <title>BackupDumperTestP1</title>
00191     <ns>0</ns>
00192     <id>1</id>
00193     <revision>
00194       <id>1</id>
00195       <timestamp>2012-04-01T16:46:05Z</timestamp>
00196       <contributor>
00197         <ip>127.0.0.1</ip>
00198       </contributor>
00199       <comment>BackupDumperTestP1Summary1</comment>
00200       <sha1>0bolhl6ol7i6x0e7yq91gxgaan39j87</sha1>
00201       <text xml:space="preserve">BackupDumperTestP1Text1</text>
00202       <model name="wikitext">1</model>
00203       <format mime="text/x-wiki">1</format>
00204     </revision>
00205   </page>
00206 ';
00207         // Page with more than one revisions. Hole in rev ids.
00208         $available_pages[2] = '  <page>
00209     <title>BackupDumperTestP2</title>
00210     <ns>0</ns>
00211     <id>2</id>
00212     <revision>
00213       <id>2</id>
00214       <timestamp>2012-04-01T16:46:05Z</timestamp>
00215       <contributor>
00216         <ip>127.0.0.1</ip>
00217       </contributor>
00218       <comment>BackupDumperTestP2Summary1</comment>
00219       <sha1>jprywrymfhysqllua29tj3sc7z39dl2</sha1>
00220       <text xml:space="preserve">BackupDumperTestP2Text1</text>
00221       <model name="wikitext">1</model>
00222       <format mime="text/x-wiki">1</format>
00223     </revision>
00224     <revision>
00225       <id>5</id>
00226       <parentid>2</parentid>
00227       <timestamp>2012-04-01T16:46:05Z</timestamp>
00228       <contributor>
00229         <ip>127.0.0.1</ip>
00230       </contributor>
00231       <comment>BackupDumperTestP2Summary4 extra</comment>
00232       <sha1>6o1ciaxa6pybnqprmungwofc4lv00wv</sha1>
00233       <text xml:space="preserve">BackupDumperTestP2Text4 some additional Text</text>
00234       <model name="wikitext">1</model>
00235       <format mime="text/x-wiki">1</format>
00236     </revision>
00237   </page>
00238 ';
00239         // Page with id higher than previous id + 1
00240         $available_pages[4] = '  <page>
00241     <title>Talk:BackupDumperTestP1</title>
00242     <ns>1</ns>
00243     <id>4</id>
00244     <revision>
00245       <id>8</id>
00246       <timestamp>2012-04-01T16:46:05Z</timestamp>
00247       <contributor>
00248         <ip>127.0.0.1</ip>
00249       </contributor>
00250       <comment>Talk BackupDumperTestP1 Summary1</comment>
00251       <sha1>nktofwzd0tl192k3zfepmlzxoax1lpe</sha1>
00252       <model name="wikitext">1</model>
00253       <format mime="text/x-wiki">1</format>
00254       <text xml:space="preserve">Talk about BackupDumperTestP1 Text1</text>
00255     </revision>
00256   </page>
00257 ';
00258 
00259         // The common ending for all files
00260         $tail = '</mediawiki>
00261 ';
00262 
00263         // Putting together the content of the prefetch files
00264         $content = $header;
00265         foreach ( $requested_pages as $i ) {
00266             $this->assertTrue( array_key_exists( $i, $available_pages ),
00267                 "Check for availability of requested page " . $i );
00268             $content .= $available_pages[$i];
00269         }
00270         $content .= $tail;
00271 
00272         $this->assertEquals( strlen( $content ), file_put_contents(
00273             $fname, $content ), "Length of prepared prefetch" );
00274 
00275         return $fname;
00276     }
00277 }