backupPrefetchTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace MediaWiki\Tests\Maintenance;
  3. use BaseDump;
  4. use MediaWikiTestCase;
  5. /**
  6. * Tests for BaseDump
  7. *
  8. * @group Dump
  9. * @covers BaseDump
  10. */
  11. class BaseDumpTest extends MediaWikiTestCase {
  12. /**
  13. * @var BaseDump The BaseDump instance used within a test.
  14. *
  15. * If set, this BaseDump gets automatically closed in tearDown.
  16. */
  17. private $dump = null;
  18. protected function tearDown() {
  19. if ( $this->dump !== null ) {
  20. $this->dump->close();
  21. }
  22. // T39458, parent teardown need to be done after closing the
  23. // dump or it might cause some permissions errors.
  24. parent::tearDown();
  25. }
  26. /**
  27. * asserts that a prefetch yields an expected string
  28. *
  29. * @param string|null $expected The exepcted result of the prefetch
  30. * @param int $page The page number to prefetch the text for
  31. * @param int $revision The revision number to prefetch the text for
  32. */
  33. private function assertPrefetchEquals( $expected, $page, $revision ) {
  34. $this->assertEquals( $expected, $this->dump->prefetch( $page, $revision ),
  35. "Prefetch of page $page revision $revision" );
  36. }
  37. function testSequential() {
  38. $fname = $this->setUpPrefetch();
  39. $this->dump = new BaseDump( $fname );
  40. $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
  41. $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
  42. $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
  43. $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
  44. }
  45. function testSynchronizeRevisionMissToRevision() {
  46. $fname = $this->setUpPrefetch();
  47. $this->dump = new BaseDump( $fname );
  48. $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
  49. $this->assertPrefetchEquals( null, 2, 3 );
  50. $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
  51. }
  52. function testSynchronizeRevisionMissToPage() {
  53. $fname = $this->setUpPrefetch();
  54. $this->dump = new BaseDump( $fname );
  55. $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
  56. $this->assertPrefetchEquals( null, 2, 40 );
  57. $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
  58. }
  59. function testSynchronizePageMiss() {
  60. $fname = $this->setUpPrefetch();
  61. $this->dump = new BaseDump( $fname );
  62. $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
  63. $this->assertPrefetchEquals( null, 3, 40 );
  64. $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
  65. }
  66. function testPageMissAtEnd() {
  67. $fname = $this->setUpPrefetch();
  68. $this->dump = new BaseDump( $fname );
  69. $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
  70. $this->assertPrefetchEquals( null, 6, 40 );
  71. }
  72. function testRevisionMissAtEnd() {
  73. $fname = $this->setUpPrefetch();
  74. $this->dump = new BaseDump( $fname );
  75. $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
  76. $this->assertPrefetchEquals( null, 4, 40 );
  77. }
  78. function testSynchronizePageMissAtStart() {
  79. $fname = $this->setUpPrefetch();
  80. $this->dump = new BaseDump( $fname );
  81. $this->assertPrefetchEquals( null, 0, 2 );
  82. $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
  83. }
  84. function testSynchronizeRevisionMissAtStart() {
  85. $fname = $this->setUpPrefetch();
  86. $this->dump = new BaseDump( $fname );
  87. $this->assertPrefetchEquals( null, 1, -2 );
  88. $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
  89. }
  90. function testSequentialAcrossFiles() {
  91. $fname1 = $this->setUpPrefetch( [ 1 ] );
  92. $fname2 = $this->setUpPrefetch( [ 2, 4 ] );
  93. $this->dump = new BaseDump( $fname1 . ";" . $fname2 );
  94. $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
  95. $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
  96. $this->assertPrefetchEquals( "BackupDumperTestP2Text4 some additional Text", 2, 5 );
  97. $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
  98. }
  99. function testSynchronizeSkipAcrossFile() {
  100. $fname1 = $this->setUpPrefetch( [ 1 ] );
  101. $fname2 = $this->setUpPrefetch( [ 2 ] );
  102. $fname3 = $this->setUpPrefetch( [ 4 ] );
  103. $this->dump = new BaseDump( $fname1 . ";" . $fname2 . ";" . $fname3 );
  104. $this->assertPrefetchEquals( "BackupDumperTestP1Text1", 1, 1 );
  105. $this->assertPrefetchEquals( "Talk about BackupDumperTestP1 Text1", 4, 8 );
  106. }
  107. function testSynchronizeMissInWholeFirstFile() {
  108. $fname1 = $this->setUpPrefetch( [ 1 ] );
  109. $fname2 = $this->setUpPrefetch( [ 2 ] );
  110. $this->dump = new BaseDump( $fname1 . ";" . $fname2 );
  111. $this->assertPrefetchEquals( "BackupDumperTestP2Text1", 2, 2 );
  112. }
  113. /**
  114. * Constructs a temporary file that can be used for prefetching
  115. *
  116. * The temporary file is removed by DumpBackup upon tearDown.
  117. *
  118. * @param array $requested_pages The indices of the page parts that should
  119. * go into the prefetch file. 1,2,4 are available.
  120. * @return string The file name of the created temporary file
  121. */
  122. private function setUpPrefetch( $requested_pages = [ 1, 2, 4 ] ) {
  123. // The file name, where we store the prepared prefetch file
  124. $fname = $this->getNewTempFile();
  125. // The header of every prefetch file
  126. // phpcs:ignore Generic.Files.LineLength
  127. $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">
  128. <siteinfo>
  129. <sitename>wikisvn</sitename>
  130. <base>http://localhost/wiki-svn/index.php/Main_Page</base>
  131. <generator>MediaWiki 1.21alpha</generator>
  132. <case>first-letter</case>
  133. <namespaces>
  134. <namespace key="-2" case="first-letter">Media</namespace>
  135. <namespace key="-1" case="first-letter">Special</namespace>
  136. <namespace key="0" case="first-letter" />
  137. <namespace key="1" case="first-letter">Talk</namespace>
  138. <namespace key="2" case="first-letter">User</namespace>
  139. <namespace key="3" case="first-letter">User talk</namespace>
  140. <namespace key="4" case="first-letter">Wikisvn</namespace>
  141. <namespace key="5" case="first-letter">Wikisvn talk</namespace>
  142. <namespace key="6" case="first-letter">File</namespace>
  143. <namespace key="7" case="first-letter">File talk</namespace>
  144. <namespace key="8" case="first-letter">MediaWiki</namespace>
  145. <namespace key="9" case="first-letter">MediaWiki talk</namespace>
  146. <namespace key="10" case="first-letter">Template</namespace>
  147. <namespace key="11" case="first-letter">Template talk</namespace>
  148. <namespace key="12" case="first-letter">Help</namespace>
  149. <namespace key="13" case="first-letter">Help talk</namespace>
  150. <namespace key="14" case="first-letter">Category</namespace>
  151. <namespace key="15" case="first-letter">Category talk</namespace>
  152. </namespaces>
  153. </siteinfo>
  154. ';
  155. // An array holding the pages that are available for prefetch
  156. $available_pages = [];
  157. // Simple plain page
  158. $available_pages[1] = ' <page>
  159. <title>BackupDumperTestP1</title>
  160. <ns>0</ns>
  161. <id>1</id>
  162. <revision>
  163. <id>1</id>
  164. <timestamp>2012-04-01T16:46:05Z</timestamp>
  165. <contributor>
  166. <ip>127.0.0.1</ip>
  167. </contributor>
  168. <comment>BackupDumperTestP1Summary1</comment>
  169. <sha1>0bolhl6ol7i6x0e7yq91gxgaan39j87</sha1>
  170. <text xml:space="preserve">BackupDumperTestP1Text1</text>
  171. <model name="wikitext">1</model>
  172. <format mime="text/x-wiki">1</format>
  173. </revision>
  174. </page>
  175. ';
  176. // Page with more than one revisions. Hole in rev ids.
  177. $available_pages[2] = ' <page>
  178. <title>BackupDumperTestP2</title>
  179. <ns>0</ns>
  180. <id>2</id>
  181. <revision>
  182. <id>2</id>
  183. <timestamp>2012-04-01T16:46:05Z</timestamp>
  184. <contributor>
  185. <ip>127.0.0.1</ip>
  186. </contributor>
  187. <comment>BackupDumperTestP2Summary1</comment>
  188. <sha1>jprywrymfhysqllua29tj3sc7z39dl2</sha1>
  189. <text xml:space="preserve">BackupDumperTestP2Text1</text>
  190. <model name="wikitext">1</model>
  191. <format mime="text/x-wiki">1</format>
  192. </revision>
  193. <revision>
  194. <id>5</id>
  195. <parentid>2</parentid>
  196. <timestamp>2012-04-01T16:46:05Z</timestamp>
  197. <contributor>
  198. <ip>127.0.0.1</ip>
  199. </contributor>
  200. <comment>BackupDumperTestP2Summary4 extra</comment>
  201. <sha1>6o1ciaxa6pybnqprmungwofc4lv00wv</sha1>
  202. <text xml:space="preserve">BackupDumperTestP2Text4 some additional Text</text>
  203. <model name="wikitext">1</model>
  204. <format mime="text/x-wiki">1</format>
  205. </revision>
  206. </page>
  207. ';
  208. // Page with id higher than previous id + 1
  209. $available_pages[4] = ' <page>
  210. <title>Talk:BackupDumperTestP1</title>
  211. <ns>1</ns>
  212. <id>4</id>
  213. <revision>
  214. <id>8</id>
  215. <timestamp>2012-04-01T16:46:05Z</timestamp>
  216. <contributor>
  217. <ip>127.0.0.1</ip>
  218. </contributor>
  219. <comment>Talk BackupDumperTestP1 Summary1</comment>
  220. <sha1>nktofwzd0tl192k3zfepmlzxoax1lpe</sha1>
  221. <model name="wikitext">1</model>
  222. <format mime="text/x-wiki">1</format>
  223. <text xml:space="preserve">Talk about BackupDumperTestP1 Text1</text>
  224. </revision>
  225. </page>
  226. ';
  227. // The common ending for all files
  228. $tail = '</mediawiki>
  229. ';
  230. // Putting together the content of the prefetch files
  231. $content = $header;
  232. foreach ( $requested_pages as $i ) {
  233. $this->assertTrue( array_key_exists( $i, $available_pages ),
  234. "Check for availability of requested page " . $i );
  235. $content .= $available_pages[$i];
  236. }
  237. $content .= $tail;
  238. $this->assertEquals( strlen( $content ), file_put_contents(
  239. $fname, $content ), "Length of prepared prefetch" );
  240. return $fname;
  241. }
  242. }