* Use double quoted consistently on attributes in linker output; preparing for new...
[mediawiki.git] / maintenance / dumpTextPass.php
blob78367c0b107930a5dbe0d61186c1bf17f13fd3c4
1 <?php
2 /**
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
21 * @package MediaWiki
22 * @subpackage SpecialPage
25 $originalDir = getcwd();
27 require_once( 'commandLine.inc' );
28 require_once( 'SpecialExport.php' );
29 require_once( 'maintenance/backup.inc' );
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.
36 class SevenZipStream {
37 var $stream;
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';
49 } else {
50 return false;
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 = MW_EXPORT_FULL;
103 var $fetchCount = 0;
104 var $prefetchCount = 0;
106 function dump() {
107 # This shouldn't happen if on console... ;)
108 header( 'Content-type: text/html; charset=UTF-8' );
110 # Notice messages will foul up your XML output even if they're
111 # relatively harmless.
112 // ini_set( 'display_errors', false );
114 $this->initProgress( $this->history );
116 $this->db =& $this->backupDb();
118 $this->egress = new ExportProgressFilter( $this->sink, $this );
120 $input = fopen( $this->input, "rt" );
121 $result = $this->readDump( $input );
123 if( WikiError::isError( $result ) ) {
124 wfDie( $result->getMessage() );
127 $this->report( true );
130 function processOption( $opt, $val, $param ) {
131 $url = $this->processFileOpt( $val, $param );
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;
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;
166 * Overridden to include prefetch ratio if enabled.
168 function showReport() {
169 if( !$this->prefetch ) {
170 return parent::showReport();
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 = '-';
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 ) );
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;
204 $parser = xml_parser_create( "UTF-8" );
205 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
207 xml_set_element_handler( $parser, array( &$this, 'startElement' ), array( &$this, 'endElement' ) );
208 xml_set_character_data_handler( $parser, array( &$this, 'characterData' ) );
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 );
218 $offset += strlen( $chunk );
219 } while( $chunk !== false && !feof( $input ) );
220 xml_parser_free( $parser );
222 return true;
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;
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;
250 function startElement( $parser, $name, $attribs ) {
251 $this->clearOpenElement( null );
252 $this->lastName = $name;
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;
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 );
273 } else {
274 $this->openElement = array( $name, $attribs );
278 function endElement( $parser, $name ) {
279 if( $this->openElement ) {
280 $this->clearOpenElement( "" );
281 } else {
282 $this->buffer .= "</$name>";
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 = "";
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;
308 $this->buffer .= htmlspecialchars( $data );
311 function clearOpenElement( $style ) {
312 if( $this->openElement ) {
313 $this->buffer .= wfElement( $this->openElement[0], $this->openElement[1], $style );
314 $this->openElement = false;
320 $dumper = new TextPassDumper( $argv );
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).
329 XML input is accepted on stdin.
330 XML output is sent to stdout; progress reports are sent to stderr.
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