Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / includes / export / BaseDump.php
blobca1f87fe6109a6f208aad8b105e776eb12f30b4d
1 <?php
2 /**
3 * Helper class for the --prefetch option of dumpTextPass.php
5 * Copyright © 2005 Brooke Vibber <bvibber@wikimedia.org>
6 * https://www.mediawiki.org/
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.
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.
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
23 * @file
24 * @ingroup Maintenance
27 use MediaWiki\Revision\SlotRecord;
29 /**
30 * Readahead helper for making large MediaWiki data dumps;
31 * reads in a previous XML dump to sequentially prefetch text
32 * records already normalized and decompressed.
34 * This can save load on the external database servers, hopefully.
36 * Assumes that dumps will be recorded in the canonical order:
37 * - ascending by page_id
38 * - ascending by rev_id within each page
39 * - text contents are immutable and should not change once
40 * recorded, so the previous dump is a reliable source
42 * @ingroup Maintenance
44 class BaseDump {
45 /** @var XMLReader|null */
46 protected $reader = null;
47 /** @var bool */
48 protected $atEnd = false;
49 /** @var bool */
50 protected $atPageEnd = false;
51 /** @var int */
52 protected $lastPage = 0;
53 /** @var int */
54 protected $lastRev = 0;
55 /** @var string[]|null */
56 protected $infiles = null;
58 /**
59 * @param string $infile
61 public function __construct( $infile ) {
62 $this->infiles = explode( ';', $infile );
63 $this->reader = new XMLReader();
64 $infile = array_shift( $this->infiles );
65 if ( !$this->reader->open( $infile, null, LIBXML_PARSEHUGE ) ) {
66 $this->debug( __METHOD__ . ' was unable to open xml' );
67 $this->atEnd = true;
71 /**
72 * Attempts to fetch the text of a particular page revision
73 * from the dump stream. May return null if the page is
74 * unavailable.
76 * @param int $page ID number of page to read
77 * @param int $rev ID number of revision to read
78 * @param string $slot Role name of the slot to read
79 * @return string|null
81 public function prefetch( $page, $rev, $slot = SlotRecord::MAIN ) {
82 $page = intval( $page );
83 $rev = intval( $rev );
84 while ( $this->lastPage < $page && !$this->atEnd ) {
85 $this->debug( "BaseDump::prefetch at page $this->lastPage, looking for $page" );
86 $this->nextPage();
88 if ( $this->lastPage > $page || $this->atEnd ) {
89 $this->debug( "BaseDump::prefetch already past page $page or failed to open/read input file, "
90 . "looking for rev $rev [$this->lastPage, $this->lastRev]" );
92 return null;
94 while ( $this->lastRev < $rev && !$this->atEnd && !$this->atPageEnd ) {
95 $this->debug( "BaseDump::prefetch at page $this->lastPage, rev $this->lastRev, "
96 . "looking for $page, $rev" );
97 $this->nextRev();
99 if ( $this->lastRev == $rev && !$this->atEnd ) {
100 $this->debug( "BaseDump::prefetch hit on $page, $rev [$this->lastPage, $this->lastRev]" );
102 if ( $slot !== SlotRecord::MAIN ) {
103 $lastSlot = SlotRecord::MAIN;
104 while ( $lastSlot !== $slot ) {
105 if ( !$this->skipTo( 'content', 'revision' ) ||
106 !$this->skipTo( 'role', 'revision' )
108 return null;
110 $lastSlot = $this->nodeContents();
114 return $this->nextText();
115 } else {
116 $this->debug( "BaseDump::prefetch already past rev $rev on page $page "
117 . "[$this->lastPage, $this->lastRev]" );
119 return null;
124 * @param string $str
126 protected function debug( $str ) {
127 wfDebug( $str );
128 // global $dumper;
129 // $dumper->progress( $str );
132 private function nextPage() {
133 if ( $this->skipTo( 'page', 'mediawiki' ) ) {
134 if ( $this->skipTo( 'id' ) ) {
135 $this->lastPage = intval( $this->nodeContents() );
136 $this->lastRev = 0;
137 $this->atPageEnd = false;
139 } else {
140 $this->close();
141 if ( count( $this->infiles ) ) {
142 $infile = array_shift( $this->infiles );
143 if ( !$this->reader->open( $infile, null, LIBXML_PARSEHUGE ) ) {
144 $this->debug( __METHOD__ . ' was unable to open xml' );
145 $this->atEnd = true;
146 } else {
147 $this->atEnd = false;
153 private function nextRev() {
154 if ( $this->skipTo( 'revision' ) ) {
155 if ( $this->skipTo( 'id' ) ) {
156 $this->lastRev = intval( $this->nodeContents() );
158 } else {
159 $this->atPageEnd = true;
164 * @return string|null
166 private function nextText() {
167 if ( !$this->skipTo( 'text', 'revision' ) ) {
168 return null;
171 return strval( $this->nodeContents() );
175 * @param string $name
176 * @param string $parent
177 * @return bool|null
179 private function skipTo( $name, $parent = 'page' ) {
180 if ( $this->atEnd ) {
181 return false;
183 while ( $this->reader->read() ) {
184 if ( $this->reader->nodeType == XMLReader::ELEMENT
185 && $this->reader->name == $name
187 return true;
189 if ( $this->reader->nodeType == XMLReader::END_ELEMENT
190 && $this->reader->name == $parent
192 $this->debug( "BaseDump::skipTo found </$parent> searching for <$name>" );
194 return false;
198 return $this->close();
202 * Shouldn't something like this be built-in to XMLReader?
203 * Fetches text contents of the current element, assuming
204 * no sub-elements or such scary things.
206 * @return string|null
208 private function nodeContents() {
209 if ( $this->atEnd ) {
210 return null;
212 if ( $this->reader->isEmptyElement ) {
213 return "";
215 $buffer = "";
216 while ( $this->reader->read() ) {
217 switch ( $this->reader->nodeType ) {
218 case XMLReader::TEXT:
219 // case XMLReader::WHITESPACE:
220 case XMLReader::SIGNIFICANT_WHITESPACE:
221 $buffer .= $this->reader->value;
222 break;
223 case XMLReader::END_ELEMENT:
224 return $buffer;
228 return $this->close();
232 * @return null
234 public function close() {
235 $this->reader->close();
236 $this->atEnd = true;
238 return null;