3 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
4 * http://www.mediawiki.org/
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.
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.
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
22 * @subpackage SpecialPage
25 $originalDir = getcwd();
27 require_once( 'commandLine.inc' );
28 require_once( 'SpecialExport.php' );
29 require_once( 'maintenance/backup.inc' );
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.
36 class SevenZipStream
{
39 private function stripPath( $path ) {
40 $prefix = 'mediawiki.compress.7z://';
41 return substr( $path, strlen( $prefix ) );
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';
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';
58 $this->stream
= popen( $command, $mode );
59 return ($this->stream
!== false);
62 function url_stat( $path, $flags ) {
63 return stat( $this->stripPath( $path ) );
66 // This is all so lame; there should be a default class we can extend
68 function stream_close() {
69 return fclose( $this->stream
);
72 function stream_flush() {
73 return fflush( $this->stream
);
76 function stream_read( $count ) {
77 return fread( $this->stream
, $count );
80 function stream_write( $data ) {
81 return fwrite( $this->stream
, $data );
84 function stream_tell() {
85 return ftell( $this->stream
);
88 function stream_eof() {
89 return feof( $this->stream
);
92 function stream_seek( $offset, $whence ) {
93 return fseek( $this->stream
, $offset, $whence );
96 stream_wrapper_register( 'mediawiki.compress.7z', 'SevenZipStream' );
99 class TextPassDumper
extends BackupDumper
{
100 var $prefetch = null;
101 var $input = "php://stdin";
102 var $history = WikiExporter
::FULL
;
104 var $prefetchCount = 0;
107 var $maxFailures = 200;
108 var $failureTimeout = 5; // Seconds to sleep after db failure
111 # This shouldn't happen if on console... ;)
112 header( 'Content-type: text/html; charset=UTF-8' );
114 # Notice messages will foul up your XML output even if they're
115 # relatively harmless.
116 // ini_set( 'display_errors', false );
118 $this->initProgress( $this->history
);
120 $this->db
=& $this->backupDb();
122 $this->egress
= new ExportProgressFilter( $this->sink
, $this );
124 $input = fopen( $this->input
, "rt" );
125 $result = $this->readDump( $input );
127 if( WikiError
::isError( $result ) ) {
128 wfDie( $result->getMessage() );
131 $this->report( true );
134 function processOption( $opt, $val, $param ) {
135 $url = $this->processFileOpt( $val, $param );
139 require_once 'maintenance/backupPrefetch.inc';
140 $this->prefetch
= new BaseDump( $url );
146 $this->history
= WikiExporter
::CURRENT
;
149 $this->history
= WikiExporter
::FULL
;
154 function processFileOpt( $val, $param ) {
159 return "compress.zlib://$param";
161 return "compress.bzip2://$param";
163 return "mediawiki.compress.7z://$param";
170 * Overridden to include prefetch ratio if enabled.
172 function showReport() {
173 if( !$this->prefetch
) {
174 return parent
::showReport();
177 if( $this->reporting
) {
178 $delta = wfTime() - $this->startTime
;
179 $now = wfTimestamp( TS_DB
);
181 $rate = $this->pageCount
/ $delta;
182 $revrate = $this->revCount
/ $delta;
183 $portion = $this->revCount
/ $this->maxCount
;
184 $eta = $this->startTime +
$delta / $portion;
185 $etats = wfTimestamp( TS_DB
, intval( $eta ) );
186 $fetchrate = 100.0 * $this->prefetchCount
/ $this->fetchCount
;
193 $this->progress( sprintf( "%s: %s %d pages (%0.3f/sec), %d revs (%0.3f/sec), %0.1f%% prefetched, ETA %s [max %d]",
194 $now, wfWikiID(), $this->pageCount
, $rate, $this->revCount
, $revrate, $fetchrate, $etats, $this->maxCount
) );
198 function readDump( $input ) {
200 $this->openElement
= false;
201 $this->atStart
= true;
203 $this->lastName
= "";
207 $parser = xml_parser_create( "UTF-8" );
208 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
210 xml_set_element_handler( $parser, array( &$this, 'startElement' ), array( &$this, 'endElement' ) );
211 xml_set_character_data_handler( $parser, array( &$this, 'characterData' ) );
213 $offset = 0; // for context extraction on error reporting
214 $bufferSize = 512 * 1024;
216 $chunk = fread( $input, $bufferSize );
217 if( !xml_parse( $parser, $chunk, feof( $input ) ) ) {
218 wfDebug( "TextDumpPass::readDump encountered XML parsing error\n" );
219 return new WikiXmlError( $parser, 'XML import parse failure', $chunk, $offset );
221 $offset +
= strlen( $chunk );
222 } while( $chunk !== false && !feof( $input ) );
223 xml_parser_free( $parser );
228 function getText( $id ) {
230 if( isset( $this->prefetch
) ) {
231 $text = $this->prefetch
->prefetch( $this->thisPage
, $this->thisRev
);
232 if( $text === null ) {
233 // Entry missing from prefetch dump
234 } elseif( $text === "" ) {
235 // Blank entries may indicate that the prior dump was broken.
236 // To be safe, reload it.
238 $this->prefetchCount++
;
244 return $this->doGetText( $id );
245 } catch (DBQueryError
$ex) {
247 if( $this->failures
> $this->maxFailures
) {
250 $this->progress( "Database failure $this->failures " .
251 "of allowed $this->maxFailures! " .
252 "Pausing $this->failureTimeout seconds..." );
253 sleep( $this->failureTimeout
);
260 * May throw a database error if, say, the server dies during query.
262 private function doGetText( $id ) {
264 $row = $this->db
->selectRow( 'text',
265 array( 'old_text', 'old_flags' ),
266 array( 'old_id' => $id ),
267 'TextPassDumper::getText' );
268 $text = Revision
::getRevisionText( $row );
269 $stripped = str_replace( "\r", "", $text );
270 $normalized = UtfNormal
::cleanUp( $stripped );
274 function startElement( $parser, $name, $attribs ) {
275 $this->clearOpenElement( null );
276 $this->lastName
= $name;
278 if( $name == 'revision' ) {
279 $this->state
= $name;
280 $this->egress
->writeOpenPage( null, $this->buffer
);
282 } elseif( $name == 'page' ) {
283 $this->state
= $name;
284 if( $this->atStart
) {
285 $this->egress
->writeOpenStream( $this->buffer
);
287 $this->atStart
= false;
291 if( $name == "text" && isset( $attribs['id'] ) ) {
292 $text = $this->getText( $attribs['id'] );
293 $this->openElement
= array( $name, array( 'xml:space' => 'preserve' ) );
294 if( strlen( $text ) > 0 ) {
295 $this->characterData( $parser, $text );
298 $this->openElement
= array( $name, $attribs );
302 function endElement( $parser, $name ) {
303 if( $this->openElement
) {
304 $this->clearOpenElement( "" );
306 $this->buffer
.= "</$name>";
309 if( $name == 'revision' ) {
310 $this->egress
->writeRevision( null, $this->buffer
);
313 } elseif( $name == 'page' ) {
314 $this->egress
->writeClosePage( $this->buffer
);
316 $this->thisPage
= "";
317 } elseif( $name == 'mediawiki' ) {
318 $this->egress
->writeCloseStream( $this->buffer
);
323 function characterData( $parser, $data ) {
324 $this->clearOpenElement( null );
325 if( $this->lastName
== "id" ) {
326 if( $this->state
== "revision" ) {
327 $this->thisRev
.= $data;
328 } elseif( $this->state
== "page" ) {
329 $this->thisPage
.= $data;
332 $this->buffer
.= htmlspecialchars( $data );
335 function clearOpenElement( $style ) {
336 if( $this->openElement
) {
337 $this->buffer
.= wfElement( $this->openElement
[0], $this->openElement
[1], $style );
338 $this->openElement
= false;
344 $dumper = new TextPassDumper( $argv );
349 $dumper->progress( <<<END
350 This script postprocesses XML dumps from dumpBackup.php to add
351 page text which was stubbed out (using --stub).
353 XML input is accepted on stdin.
354 XML output is sent to stdout; progress reports are sent to stderr.
356 Usage: php dumpTextPass.php [<options>]
358 --stub=<type>:<file> To load a compressed stub dump instead of stdin
359 --prefetch=<type>:<file> Use a prior dump file as a text source, to save
360 pressure on the database.
361 (Requires PHP 5.0+ and the XMLReader PECL extension)
362 --quiet Don't dump status reports to stderr.
363 --report=n Report position and speed after every n pages processed.
365 --server=h Force reading from MySQL server h
366 --current Base ETA on number of pages in database instead of all revisions