* Introduce updateArticleCount maintenance script which uses a better check that...
[lhc/web/wiklou.git] / maintenance / dumpTextPass.php
1 <?php
2 /**
3 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
4 * http://www.mediawiki.org/
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @package MediaWiki
22 * @subpackage SpecialPage
23 */
24
25 $originalDir = getcwd();
26
27 require_once( 'commandLine.inc' );
28 require_once( 'SpecialExport.php' );
29 require_once( 'maintenance/backup.inc' );
30
31 /**
32 * Stream wrapper around 7za filter program.
33 * Required since we can't pass an open file resource to XMLReader->open()
34 * which is used for the text prefetch.
35 */
36 class SevenZipStream {
37 var $stream;
38
39 private function stripPath( $path ) {
40 $prefix = 'mediawiki.compress.7z://';
41 return substr( $path, strlen( $prefix ) );
42 }
43
44 function stream_open( $path, $mode, $options, &$opened_path ) {
45 if( $mode{0} == 'r' ) {
46 $options = 'e -bd -so';
47 } elseif( $mode{0} == 'w' ) {
48 $options = 'a -bd -si';
49 } else {
50 return false;
51 }
52 $arg = wfEscapeShellArg( $this->stripPath( $path ) );
53 $command = "7za $options $arg";
54 if( !wfIsWindows() ) {
55 // Suppress the stupid messages on stderr
56 $command .= ' 2>/dev/null';
57 }
58 $this->stream = popen( $command, $mode );
59 return ($this->stream !== false);
60 }
61
62 function url_stat( $path, $flags ) {
63 return stat( $this->stripPath( $path ) );
64 }
65
66 // This is all so lame; there should be a default class we can extend
67
68 function stream_close() {
69 return fclose( $this->stream );
70 }
71
72 function stream_flush() {
73 return fflush( $this->stream );
74 }
75
76 function stream_read( $count ) {
77 return fread( $this->stream, $count );
78 }
79
80 function stream_write( $data ) {
81 return fwrite( $this->stream, $data );
82 }
83
84 function stream_tell() {
85 return ftell( $this->stream );
86 }
87
88 function stream_eof() {
89 return feof( $this->stream );
90 }
91
92 function stream_seek( $offset, $whence ) {
93 return fseek( $this->stream, $offset, $whence );
94 }
95 }
96 stream_wrapper_register( 'mediawiki.compress.7z', 'SevenZipStream' );
97
98
99 class TextPassDumper extends BackupDumper {
100 var $prefetch = null;
101 var $input = "php://stdin";
102 var $history = MW_EXPORT_FULL;
103 var $fetchCount = 0;
104 var $prefetchCount = 0;
105
106 function dump() {
107 # This shouldn't happen if on console... ;)
108 header( 'Content-type: text/html; charset=UTF-8' );
109
110 # Notice messages will foul up your XML output even if they're
111 # relatively harmless.
112 // ini_set( 'display_errors', false );
113
114 $this->initProgress( $this->history );
115
116 $this->db =& $this->backupDb();
117
118 $this->egress = new ExportProgressFilter( $this->sink, $this );
119
120 $input = fopen( $this->input, "rt" );
121 $result = $this->readDump( $input );
122
123 if( WikiError::isError( $result ) ) {
124 wfDie( $result->getMessage() );
125 }
126
127 $this->report( true );
128 }
129
130 function processOption( $opt, $val, $param ) {
131 $url = $this->processFileOpt( $val, $param );
132
133 switch( $opt ) {
134 case 'prefetch':
135 require_once 'maintenance/backupPrefetch.inc';
136 $this->prefetch = new BaseDump( $url );
137 break;
138 case 'stub':
139 $this->input = $url;
140 break;
141 case 'current':
142 $this->history = MW_EXPORT_CURRENT;
143 break;
144 case 'full':
145 $this->history = MW_EXPORT_FULL;
146 break;
147 }
148 }
149
150 function processFileOpt( $val, $param ) {
151 switch( $val ) {
152 case "file":
153 return $param;
154 case "gzip":
155 return "compress.zlib://$param";
156 case "bzip2":
157 return "compress.bzip2://$param";
158 case "7zip":
159 return "mediawiki.compress.7z://$param";
160 default:
161 return $val;
162 }
163 }
164
165 /**
166 * Overridden to include prefetch ratio if enabled.
167 */
168 function showReport() {
169 if( !$this->prefetch ) {
170 return parent::showReport();
171 }
172
173 if( $this->reporting ) {
174 $delta = wfTime() - $this->startTime;
175 $now = wfTimestamp( TS_DB );
176 if( $delta ) {
177 $rate = $this->pageCount / $delta;
178 $revrate = $this->revCount / $delta;
179 $portion = $this->revCount / $this->maxCount;
180 $eta = $this->startTime + $delta / $portion;
181 $etats = wfTimestamp( TS_DB, intval( $eta ) );
182 $fetchrate = 100.0 * $this->prefetchCount / $this->fetchCount;
183 } else {
184 $rate = '-';
185 $revrate = '-';
186 $etats = '-';
187 $fetchrate = '-';
188 }
189 global $wgDBname;
190 $this->progress( sprintf( "%s: %s %d pages (%0.3f/sec), %d revs (%0.3f/sec), %0.1f%% prefetched, ETA %s [max %d]",
191 $now, $wgDBname, $this->pageCount, $rate, $this->revCount, $revrate, $fetchrate, $etats, $this->maxCount ) );
192 }
193 }
194
195 function readDump( $input ) {
196 $this->buffer = "";
197 $this->openElement = false;
198 $this->atStart = true;
199 $this->state = "";
200 $this->lastName = "";
201 $this->thisPage = 0;
202 $this->thisRev = 0;
203
204 $parser = xml_parser_create( "UTF-8" );
205 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
206
207 xml_set_element_handler( $parser, array( &$this, 'startElement' ), array( &$this, 'endElement' ) );
208 xml_set_character_data_handler( $parser, array( &$this, 'characterData' ) );
209
210 $offset = 0; // for context extraction on error reporting
211 $bufferSize = 512 * 1024;
212 do {
213 $chunk = fread( $input, $bufferSize );
214 if( !xml_parse( $parser, $chunk, feof( $input ) ) ) {
215 wfDebug( "TextDumpPass::readDump encountered XML parsing error\n" );
216 return new WikiXmlError( $parser, 'XML import parse failure', $chunk, $offset );
217 }
218 $offset += strlen( $chunk );
219 } while( $chunk !== false && !feof( $input ) );
220 xml_parser_free( $parser );
221
222 return true;
223 }
224
225 function getText( $id ) {
226 $this->fetchCount++;
227 if( isset( $this->prefetch ) ) {
228 $text = $this->prefetch->prefetch( $this->thisPage, $this->thisRev );
229 if( $text === null ) {
230 // Entry missing from prefetch dump
231 } elseif( $text === "" ) {
232 // Blank entries may indicate that the prior dump was broken.
233 // To be safe, reload it.
234 } else {
235 $this->prefetchCount++;
236 return $text;
237 }
238 }
239 $id = intval( $id );
240 $row = $this->db->selectRow( 'text',
241 array( 'old_text', 'old_flags' ),
242 array( 'old_id' => $id ),
243 'TextPassDumper::getText' );
244 $text = Revision::getRevisionText( $row );
245 $stripped = str_replace( "\r", "", $text );
246 $normalized = UtfNormal::cleanUp( $stripped );
247 return $normalized;
248 }
249
250 function startElement( $parser, $name, $attribs ) {
251 $this->clearOpenElement( null );
252 $this->lastName = $name;
253
254 if( $name == 'revision' ) {
255 $this->state = $name;
256 $this->egress->writeOpenPage( null, $this->buffer );
257 $this->buffer = "";
258 } elseif( $name == 'page' ) {
259 $this->state = $name;
260 if( $this->atStart ) {
261 $this->egress->writeOpenStream( $this->buffer );
262 $this->buffer = "";
263 $this->atStart = false;
264 }
265 }
266
267 if( $name == "text" && isset( $attribs['id'] ) ) {
268 $text = $this->getText( $attribs['id'] );
269 $this->openElement = array( $name, array( 'xml:space' => 'preserve' ) );
270 if( strlen( $text ) > 0 ) {
271 $this->characterData( $parser, $text );
272 }
273 } else {
274 $this->openElement = array( $name, $attribs );
275 }
276 }
277
278 function endElement( $parser, $name ) {
279 if( $this->openElement ) {
280 $this->clearOpenElement( "" );
281 } else {
282 $this->buffer .= "</$name>";
283 }
284
285 if( $name == 'revision' ) {
286 $this->egress->writeRevision( null, $this->buffer );
287 $this->buffer = "";
288 $this->thisRev = "";
289 } elseif( $name == 'page' ) {
290 $this->egress->writeClosePage( $this->buffer );
291 $this->buffer = "";
292 $this->thisPage = "";
293 } elseif( $name == 'mediawiki' ) {
294 $this->egress->writeCloseStream( $this->buffer );
295 $this->buffer = "";
296 }
297 }
298
299 function characterData( $parser, $data ) {
300 $this->clearOpenElement( null );
301 if( $this->lastName == "id" ) {
302 if( $this->state == "revision" ) {
303 $this->thisRev .= $data;
304 } elseif( $this->state == "page" ) {
305 $this->thisPage .= $data;
306 }
307 }
308 $this->buffer .= htmlspecialchars( $data );
309 }
310
311 function clearOpenElement( $style ) {
312 if( $this->openElement ) {
313 $this->buffer .= wfElement( $this->openElement[0], $this->openElement[1], $style );
314 $this->openElement = false;
315 }
316 }
317 }
318
319
320 $dumper = new TextPassDumper( $argv );
321
322 if( true ) {
323 $dumper->dump();
324 } else {
325 $dumper->progress( <<<END
326 This script postprocesses XML dumps from dumpBackup.php to add
327 page text which was stubbed out (using --stub).
328
329 XML input is accepted on stdin.
330 XML output is sent to stdout; progress reports are sent to stderr.
331
332 Usage: php dumpTextPass.php [<options>]
333 Options:
334 --stub=<type>:<file> To load a compressed stub dump instead of stdin
335 --prefetch=<type>:<file> Use a prior dump file as a text source, to save
336 pressure on the database.
337 (Requires PHP 5.0+ and the XMLReader PECL extension)
338 --quiet Don't dump status reports to stderr.
339 --report=n Report position and speed after every n pages processed.
340 (Default: 100)
341 --server=h Force reading from MySQL server h
342 --current Base ETA on number of pages in database instead of all revisions
343 END
344 );
345 }
346
347 ?>