869ba807158d6bad56decff7451f1383fa2a6208
[lhc/web/wiklou.git] / maintenance / backupPrefetch.inc
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 /**
28 * Readahead helper for making large MediaWiki data dumps;
29 * reads in a previous XML dump to sequentially prefetch text
30 * records already normalized and decompressed.
31 *
32 * This can save load on the external database servers, hopefully.
33 *
34 * Assumes that dumps will be recorded in the canonical order:
35 * - ascending by page_id
36 * - ascending by rev_id within each page
37 * - text contents are immutable and should not change once
38 * recorded, so the previous dump is a reliable source
39 *
40 * @ingroup Maintenance
41 */
42 class BaseDump {
43 protected $reader = null;
44 protected $atEnd = false;
45 protected $atPageEnd = false;
46 protected $lastPage = 0;
47 protected $lastRev = 0;
48 protected $infiles = null;
49
50 public function __construct( $infile ) {
51 $this->infiles = explode( ';', $infile );
52 $this->reader = new XMLReader();
53 $infile = array_shift( $this->infiles );
54 if ( defined( 'LIBXML_PARSEHUGE' ) ) {
55 $this->reader->open( $infile, null, LIBXML_PARSEHUGE );
56 }
57 else {
58 $this->reader->open( $infile );
59 }
60 }
61
62 /**
63 * Attempts to fetch the text of a particular page revision
64 * from the dump stream. May return null if the page is
65 * unavailable.
66 *
67 * @param int $page ID number of page to read
68 * @param int $rev ID number of revision to read
69 * @return string|null
70 */
71 function prefetch( $page, $rev ) {
72 $page = intval( $page );
73 $rev = intval( $rev );
74 while ( $this->lastPage < $page && !$this->atEnd ) {
75 $this->debug( "BaseDump::prefetch at page $this->lastPage, looking for $page" );
76 $this->nextPage();
77 }
78 if ( $this->lastPage > $page || $this->atEnd ) {
79 $this->debug( "BaseDump::prefetch already past page $page "
80 . "looking for rev $rev [$this->lastPage, $this->lastRev]" );
81 return null;
82 }
83 while ( $this->lastRev < $rev && !$this->atEnd && !$this->atPageEnd ) {
84 $this->debug( "BaseDump::prefetch at page $this->lastPage, rev $this->lastRev, "
85 . "looking for $page, $rev" );
86 $this->nextRev();
87 }
88 if ( $this->lastRev == $rev && !$this->atEnd ) {
89 $this->debug( "BaseDump::prefetch hit on $page, $rev [$this->lastPage, $this->lastRev]" );
90 return $this->nextText();
91 } else {
92 $this->debug( "BaseDump::prefetch already past rev $rev on page $page "
93 . "[$this->lastPage, $this->lastRev]" );
94 return null;
95 }
96 }
97
98 function debug( $str ) {
99 wfDebug( $str . "\n" );
100 // global $dumper;
101 // $dumper->progress( $str );
102 }
103
104 /**
105 * @access private
106 */
107 function nextPage() {
108 if ( $this->skipTo( 'page', 'mediawiki' ) ) {
109 if ( $this->skipTo( 'id' ) ) {
110 $this->lastPage = intval( $this->nodeContents() );
111 $this->lastRev = 0;
112 $this->atPageEnd = false;
113 }
114 } else {
115 $this->close();
116 if ( count( $this->infiles ) ) {
117 $infile = array_shift( $this->infiles );
118 $this->reader->open( $infile );
119 $this->atEnd = false;
120 }
121 }
122 }
123
124 /**
125 * @access private
126 */
127 function nextRev() {
128 if ( $this->skipTo( 'revision' ) ) {
129 if ( $this->skipTo( 'id' ) ) {
130 $this->lastRev = intval( $this->nodeContents() );
131 }
132 } else {
133 $this->atPageEnd = true;
134 }
135 }
136
137 /**
138 * @access private
139 * @return string
140 */
141 function nextText() {
142 $this->skipTo( 'text' );
143 return strval( $this->nodeContents() );
144 }
145
146 /**
147 * @access private
148 * @param string $name
149 * @param string $parent
150 * @return bool|null
151 */
152 function skipTo( $name, $parent = 'page' ) {
153 if ( $this->atEnd ) {
154 return false;
155 }
156 while ( $this->reader->read() ) {
157 if ( $this->reader->nodeType == XMLReader::ELEMENT &&
158 $this->reader->name == $name ) {
159 return true;
160 }
161 if ( $this->reader->nodeType == XMLReader::END_ELEMENT &&
162 $this->reader->name == $parent ) {
163 $this->debug( "BaseDump::skipTo found </$parent> searching for <$name>" );
164 return false;
165 }
166 }
167 return $this->close();
168 }
169
170 /**
171 * Shouldn't something like this be built-in to XMLReader?
172 * Fetches text contents of the current element, assuming
173 * no sub-elements or such scary things.
174 *
175 * @return string
176 * @access private
177 */
178 function nodeContents() {
179 if ( $this->atEnd ) {
180 return null;
181 }
182 if ( $this->reader->isEmptyElement ) {
183 return "";
184 }
185 $buffer = "";
186 while ( $this->reader->read() ) {
187 switch ( $this->reader->nodeType ) {
188 case XMLReader::TEXT:
189 // case XMLReader::WHITESPACE:
190 case XMLReader::SIGNIFICANT_WHITESPACE:
191 $buffer .= $this->reader->value;
192 break;
193 case XMLReader::END_ELEMENT:
194 return $buffer;
195 }
196 }
197 return $this->close();
198 }
199
200 /**
201 * @access private
202 * @return null
203 */
204 function close() {
205 $this->reader->close();
206 $this->atEnd = true;
207 return null;
208 }
209 }