backupPrefetch.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * Helper class for the --prefetch option of dumpTextPass.php
  4. *
  5. * Copyright © 2005 Brion Vibber <brion@pobox.com>
  6. * https://www.mediawiki.org/
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. *
  23. * @file
  24. * @ingroup Maintenance
  25. */
  26. /**
  27. * Readahead helper for making large MediaWiki data dumps;
  28. * reads in a previous XML dump to sequentially prefetch text
  29. * records already normalized and decompressed.
  30. *
  31. * This can save load on the external database servers, hopefully.
  32. *
  33. * Assumes that dumps will be recorded in the canonical order:
  34. * - ascending by page_id
  35. * - ascending by rev_id within each page
  36. * - text contents are immutable and should not change once
  37. * recorded, so the previous dump is a reliable source
  38. *
  39. * @ingroup Maintenance
  40. */
  41. class BaseDump {
  42. protected $reader = null;
  43. protected $atEnd = false;
  44. protected $atPageEnd = false;
  45. protected $lastPage = 0;
  46. protected $lastRev = 0;
  47. protected $infiles = null;
  48. public function __construct( $infile ) {
  49. $this->infiles = explode( ';', $infile );
  50. $this->reader = new XMLReader();
  51. $infile = array_shift( $this->infiles );
  52. if ( defined( 'LIBXML_PARSEHUGE' ) ) {
  53. $this->reader->open( $infile, null, LIBXML_PARSEHUGE );
  54. } else {
  55. $this->reader->open( $infile );
  56. }
  57. }
  58. /**
  59. * Attempts to fetch the text of a particular page revision
  60. * from the dump stream. May return null if the page is
  61. * unavailable.
  62. *
  63. * @param int $page ID number of page to read
  64. * @param int $rev ID number of revision to read
  65. * @return string|null
  66. */
  67. function prefetch( $page, $rev ) {
  68. $page = intval( $page );
  69. $rev = intval( $rev );
  70. while ( $this->lastPage < $page && !$this->atEnd ) {
  71. $this->debug( "BaseDump::prefetch at page $this->lastPage, looking for $page" );
  72. $this->nextPage();
  73. }
  74. if ( $this->lastPage > $page || $this->atEnd ) {
  75. $this->debug( "BaseDump::prefetch already past page $page "
  76. . "looking for rev $rev [$this->lastPage, $this->lastRev]" );
  77. return null;
  78. }
  79. while ( $this->lastRev < $rev && !$this->atEnd && !$this->atPageEnd ) {
  80. $this->debug( "BaseDump::prefetch at page $this->lastPage, rev $this->lastRev, "
  81. . "looking for $page, $rev" );
  82. $this->nextRev();
  83. }
  84. if ( $this->lastRev == $rev && !$this->atEnd ) {
  85. $this->debug( "BaseDump::prefetch hit on $page, $rev [$this->lastPage, $this->lastRev]" );
  86. return $this->nextText();
  87. } else {
  88. $this->debug( "BaseDump::prefetch already past rev $rev on page $page "
  89. . "[$this->lastPage, $this->lastRev]" );
  90. return null;
  91. }
  92. }
  93. function debug( $str ) {
  94. wfDebug( $str . "\n" );
  95. // global $dumper;
  96. // $dumper->progress( $str );
  97. }
  98. /**
  99. * @access private
  100. */
  101. function nextPage() {
  102. if ( $this->skipTo( 'page', 'mediawiki' ) ) {
  103. if ( $this->skipTo( 'id' ) ) {
  104. $this->lastPage = intval( $this->nodeContents() );
  105. $this->lastRev = 0;
  106. $this->atPageEnd = false;
  107. }
  108. } else {
  109. $this->close();
  110. if ( count( $this->infiles ) ) {
  111. $infile = array_shift( $this->infiles );
  112. $this->reader->open( $infile );
  113. $this->atEnd = false;
  114. }
  115. }
  116. }
  117. /**
  118. * @access private
  119. */
  120. function nextRev() {
  121. if ( $this->skipTo( 'revision' ) ) {
  122. if ( $this->skipTo( 'id' ) ) {
  123. $this->lastRev = intval( $this->nodeContents() );
  124. }
  125. } else {
  126. $this->atPageEnd = true;
  127. }
  128. }
  129. /**
  130. * @access private
  131. * @return string
  132. */
  133. function nextText() {
  134. $this->skipTo( 'text' );
  135. return strval( $this->nodeContents() );
  136. }
  137. /**
  138. * @access private
  139. * @param string $name
  140. * @param string $parent
  141. * @return bool|null
  142. */
  143. function skipTo( $name, $parent = 'page' ) {
  144. if ( $this->atEnd ) {
  145. return false;
  146. }
  147. while ( $this->reader->read() ) {
  148. if ( $this->reader->nodeType == XMLReader::ELEMENT
  149. && $this->reader->name == $name
  150. ) {
  151. return true;
  152. }
  153. if ( $this->reader->nodeType == XMLReader::END_ELEMENT
  154. && $this->reader->name == $parent
  155. ) {
  156. $this->debug( "BaseDump::skipTo found </$parent> searching for <$name>" );
  157. return false;
  158. }
  159. }
  160. return $this->close();
  161. }
  162. /**
  163. * Shouldn't something like this be built-in to XMLReader?
  164. * Fetches text contents of the current element, assuming
  165. * no sub-elements or such scary things.
  166. *
  167. * @return string
  168. * @access private
  169. */
  170. function nodeContents() {
  171. if ( $this->atEnd ) {
  172. return null;
  173. }
  174. if ( $this->reader->isEmptyElement ) {
  175. return "";
  176. }
  177. $buffer = "";
  178. while ( $this->reader->read() ) {
  179. switch ( $this->reader->nodeType ) {
  180. case XMLReader::TEXT:
  181. // case XMLReader::WHITESPACE:
  182. case XMLReader::SIGNIFICANT_WHITESPACE:
  183. $buffer .= $this->reader->value;
  184. break;
  185. case XMLReader::END_ELEMENT:
  186. return $buffer;
  187. }
  188. }
  189. return $this->close();
  190. }
  191. /**
  192. * @access private
  193. * @return null
  194. */
  195. function close() {
  196. $this->reader->close();
  197. $this->atEnd = true;
  198. return null;
  199. }
  200. }