MediaWiki  REL1_23
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         $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">
00155   <siteinfo>
00156     <sitename>wikisvn</sitename>
00157     <base>http://localhost/wiki-svn/index.php/Main_Page</base>
00158     <generator>MediaWiki 1.21alpha</generator>
00159     <case>first-letter</case>
00160     <namespaces>
00161       <namespace key="-2" case="first-letter">Media</namespace>
00162       <namespace key="-1" case="first-letter">Special</namespace>
00163       <namespace key="0" case="first-letter" />
00164       <namespace key="1" case="first-letter">Talk</namespace>
00165       <namespace key="2" case="first-letter">User</namespace>
00166       <namespace key="3" case="first-letter">User talk</namespace>
00167       <namespace key="4" case="first-letter">Wikisvn</namespace>
00168       <namespace key="5" case="first-letter">Wikisvn talk</namespace>
00169       <namespace key="6" case="first-letter">File</namespace>
00170       <namespace key="7" case="first-letter">File talk</namespace>
00171       <namespace key="8" case="first-letter">MediaWiki</namespace>
00172       <namespace key="9" case="first-letter">MediaWiki talk</namespace>
00173       <namespace key="10" case="first-letter">Template</namespace>
00174       <namespace key="11" case="first-letter">Template talk</namespace>
00175       <namespace key="12" case="first-letter">Help</namespace>
00176       <namespace key="13" case="first-letter">Help talk</namespace>
00177       <namespace key="14" case="first-letter">Category</namespace>
00178       <namespace key="15" case="first-letter">Category talk</namespace>
00179     </namespaces>
00180   </siteinfo>
00181 ';
00182 
00183         // An array holding the pages that are available for prefetch
00184         $available_pages = array();
00185 
00186         // Simple plain page
00187         $available_pages[1] = '  <page>
00188     <title>BackupDumperTestP1</title>
00189     <ns>0</ns>
00190     <id>1</id>
00191     <revision>
00192       <id>1</id>
00193       <timestamp>2012-04-01T16:46:05Z</timestamp>
00194       <contributor>
00195         <ip>127.0.0.1</ip>
00196       </contributor>
00197       <comment>BackupDumperTestP1Summary1</comment>
00198       <sha1>0bolhl6ol7i6x0e7yq91gxgaan39j87</sha1>
00199       <text xml:space="preserve">BackupDumperTestP1Text1</text>
00200       <model name="wikitext">1</model>
00201       <format mime="text/x-wiki">1</format>
00202     </revision>
00203   </page>
00204 ';
00205         // Page with more than one revisions. Hole in rev ids.
00206         $available_pages[2] = '  <page>
00207     <title>BackupDumperTestP2</title>
00208     <ns>0</ns>
00209     <id>2</id>
00210     <revision>
00211       <id>2</id>
00212       <timestamp>2012-04-01T16:46:05Z</timestamp>
00213       <contributor>
00214         <ip>127.0.0.1</ip>
00215       </contributor>
00216       <comment>BackupDumperTestP2Summary1</comment>
00217       <sha1>jprywrymfhysqllua29tj3sc7z39dl2</sha1>
00218       <text xml:space="preserve">BackupDumperTestP2Text1</text>
00219       <model name="wikitext">1</model>
00220       <format mime="text/x-wiki">1</format>
00221     </revision>
00222     <revision>
00223       <id>5</id>
00224       <parentid>2</parentid>
00225       <timestamp>2012-04-01T16:46:05Z</timestamp>
00226       <contributor>
00227         <ip>127.0.0.1</ip>
00228       </contributor>
00229       <comment>BackupDumperTestP2Summary4 extra</comment>
00230       <sha1>6o1ciaxa6pybnqprmungwofc4lv00wv</sha1>
00231       <text xml:space="preserve">BackupDumperTestP2Text4 some additional Text</text>
00232       <model name="wikitext">1</model>
00233       <format mime="text/x-wiki">1</format>
00234     </revision>
00235   </page>
00236 ';
00237         // Page with id higher than previous id + 1
00238         $available_pages[4] = '  <page>
00239     <title>Talk:BackupDumperTestP1</title>
00240     <ns>1</ns>
00241     <id>4</id>
00242     <revision>
00243       <id>8</id>
00244       <timestamp>2012-04-01T16:46:05Z</timestamp>
00245       <contributor>
00246         <ip>127.0.0.1</ip>
00247       </contributor>
00248       <comment>Talk BackupDumperTestP1 Summary1</comment>
00249       <sha1>nktofwzd0tl192k3zfepmlzxoax1lpe</sha1>
00250       <model name="wikitext">1</model>
00251       <format mime="text/x-wiki">1</format>
00252       <text xml:space="preserve">Talk about BackupDumperTestP1 Text1</text>
00253     </revision>
00254   </page>
00255 ';
00256 
00257         // The common ending for all files
00258         $tail = '</mediawiki>
00259 ';
00260 
00261         // Putting together the content of the prefetch files
00262         $content = $header;
00263         foreach ( $requested_pages as $i ) {
00264             $this->assertTrue( array_key_exists( $i, $available_pages ),
00265                 "Check for availability of requested page " . $i );
00266             $content .= $available_pages[$i];
00267         }
00268         $content .= $tail;
00269 
00270         $this->assertEquals( strlen( $content ), file_put_contents(
00271             $fname, $content ), "Length of prepared prefetch" );
00272 
00273         return $fname;
00274     }
00275 }