MediaWiki  REL1_22
backupPrefetchTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 require_once __DIR__ . "/../../../maintenance/backupPrefetch.inc";
00004 
00010 class BaseDumpTest extends MediaWikiTestCase {
00011 
00017     private $dump = null;
00018 
00019     protected function tearDown() {
00020         if ( $this->dump !== null ) {
00021             $this->dump->close();
00022         }
00023 
00024         // Bug 37458, parent teardown need to be done after closing the
00025         // dump or it might cause some permissions errors.
00026         parent::tearDown();
00027     }
00028 
00036     private function assertPrefetchEquals( $expected, $page, $revision ) {
00037         $this->assertEquals( $expected, $this->dump->prefetch( $page, $revision ),
00038             "Prefetch of page $page revision $revision" );
00039     }
00040 
00041     function testSequential() {
00042         $fname = $this->setUpPrefetch();
00043         $this->dump = new BaseDump( $fname );
00044 
00045         $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
00046         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00047         $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
00048         $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
00049     }
00050 
00051     function testSynchronizeRevisionMissToRevision() {
00052         $fname = $this->setUpPrefetch();
00053         $this->dump = new BaseDump( $fname );
00054 
00055         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00056         $this->assertPrefetchEquals( null, 2, 3 );
00057         $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
00058     }
00059 
00060     function testSynchronizeRevisionMissToPage() {
00061         $fname = $this->setUpPrefetch();
00062         $this->dump = new BaseDump( $fname );
00063 
00064         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00065         $this->assertPrefetchEquals( null, 2, 40 );
00066         $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
00067     }
00068 
00069     function testSynchronizePageMiss() {
00070         $fname = $this->setUpPrefetch();
00071         $this->dump = new BaseDump( $fname );
00072 
00073         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00074         $this->assertPrefetchEquals( null, 3, 40 );
00075         $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
00076     }
00077 
00078     function testPageMissAtEnd() {
00079         $fname = $this->setUpPrefetch();
00080         $this->dump = new BaseDump( $fname );
00081 
00082         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00083         $this->assertPrefetchEquals( null, 6, 40 );
00084     }
00085 
00086     function testRevisionMissAtEnd() {
00087         $fname = $this->setUpPrefetch();
00088         $this->dump = new BaseDump( $fname );
00089 
00090         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00091         $this->assertPrefetchEquals( null, 4, 40 );
00092     }
00093 
00094     function testSynchronizePageMissAtStart() {
00095         $fname = $this->setUpPrefetch();
00096         $this->dump = new BaseDump( $fname );
00097 
00098         $this->assertPrefetchEquals( null, 0, 2 );
00099         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00100     }
00101 
00102     function testSynchronizeRevisionMissAtStart() {
00103         $fname = $this->setUpPrefetch();
00104         $this->dump = new BaseDump( $fname );
00105 
00106         $this->assertPrefetchEquals( null, 1, -2 );
00107         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00108     }
00109 
00110     function testSequentialAcrossFiles() {
00111         $fname1 = $this->setUpPrefetch( array( 1 ) );
00112         $fname2 = $this->setUpPrefetch( array( 2, 4 ) );
00113         $this->dump = new BaseDump( $fname1 . ";" . $fname2 );
00114 
00115         $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
00116         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00117         $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
00118         $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
00119     }
00120 
00121     function testSynchronizeSkipAcrossFile() {
00122         $fname1 = $this->setUpPrefetch( array( 1 ) );
00123         $fname2 = $this->setUpPrefetch( array( 2 ) );
00124         $fname3 = $this->setUpPrefetch( array( 4 ) );
00125         $this->dump = new BaseDump( $fname1 . ";" . $fname2 . ";" . $fname3 );
00126 
00127         $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
00128         $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
00129     }
00130 
00131     function testSynchronizeMissInWholeFirstFile() {
00132         $fname1 = $this->setUpPrefetch( array( 1 ) );
00133         $fname2 = $this->setUpPrefetch( array( 2 ) );
00134         $this->dump = new BaseDump( $fname1 . ";" . $fname2 );
00135 
00136         $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
00137     }
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 }