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' );
31 class TextPassDumper
extends BackupDumper
{
33 var $input = "php://stdin";
34 var $history = MW_EXPORT_FULL
;
37 # This shouldn't happen if on console... ;)
38 header( 'Content-type: text/html; charset=UTF-8' );
40 # Notice messages will foul up your XML output even if they're
41 # relatively harmless.
42 // ini_set( 'display_errors', false );
44 $this->initProgress( $this->history
);
46 $this->db
=& $this->backupDb();
48 $this->egress
= new ExportProgressFilter( $this->sink
, $this );
50 $input = fopen( $this->input
, "rt" );
51 $result = $this->readDump( $input );
53 if( WikiError
::isError( $result ) ) {
54 wfDie( $result->getMessage() );
57 $this->report( true );
60 function processOption( $opt, $val, $param ) {
61 $url = $this->processFileOpt( $val, $param );
65 require_once 'maintenance/backupPrefetch.inc';
66 $this->prefetch
= new BaseDump( $url );
72 $this->history
= MW_EXPORT_CURRENT
;
75 $this->history
= MW_EXPORT_FULL
;
80 function processFileOpt( $val, $param ) {
85 return "compress.zlib://$param";
87 return "compress.bzip2://$param";
93 function readDump( $input ) {
95 $this->openElement
= false;
96 $this->atStart
= true;
102 $parser = xml_parser_create( "UTF-8" );
103 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
105 xml_set_element_handler( $parser, array( &$this, 'startElement' ), array( &$this, 'endElement' ) );
106 xml_set_character_data_handler( $parser, array( &$this, 'characterData' ) );
108 $offset = 0; // for context extraction on error reporting
109 $bufferSize = 512 * 1024;
111 $chunk = fread( $input, $bufferSize );
112 if( !xml_parse( $parser, $chunk, feof( $input ) ) ) {
113 wfDebug( "TextDumpPass::readDump encountered XML parsing error\n" );
114 return new WikiXmlError( $parser, 'XML import parse failure', $chunk, $offset );
116 $offset +
= strlen( $chunk );
117 } while( $chunk !== false && !feof( $input ) );
118 xml_parser_free( $parser );
123 function getText( $id ) {
124 if( isset( $this->prefetch
) ) {
125 $text = $this->prefetch
->prefetch( $this->thisPage
, $this->thisRev
);
126 if( !is_null( $text ) )
130 $row = $this->db
->selectRow( 'text',
131 array( 'old_text', 'old_flags' ),
132 array( 'old_id' => $id ),
133 'TextPassDumper::getText' );
134 $text = Revision
::getRevisionText( $row );
135 $stripped = str_replace( "\r", "", $text );
136 $normalized = UtfNormal
::cleanUp( $stripped );
140 function startElement( $parser, $name, $attribs ) {
141 $this->clearOpenElement( null );
142 $this->lastName
= $name;
144 if( $name == 'revision' ) {
145 $this->state
= $name;
146 $this->egress
->writeOpenPage( null, $this->buffer
);
148 } elseif( $name == 'page' ) {
149 $this->state
= $name;
150 if( $this->atStart
) {
151 $this->egress
->writeOpenStream( $this->buffer
);
153 $this->atStart
= false;
157 if( $name == "text" && isset( $attribs['id'] ) ) {
158 $text = $this->getText( $attribs['id'] );
159 $this->openElement
= array( $name, array( 'xml:space' => 'preserve' ) );
160 if( strlen( $text ) > 0 ) {
161 $this->characterData( $parser, $text );
164 $this->openElement
= array( $name, $attribs );
168 function endElement( $parser, $name ) {
169 if( $this->openElement
) {
170 $this->clearOpenElement( "" );
172 $this->buffer
.= "</$name>";
175 if( $name == 'revision' ) {
176 $this->egress
->writeRevision( null, $this->buffer
);
179 } elseif( $name == 'page' ) {
180 $this->egress
->writeClosePage( $this->buffer
);
182 $this->thisPage
= "";
183 } elseif( $name == 'mediawiki' ) {
184 $this->egress
->writeCloseStream( $this->buffer
);
189 function characterData( $parser, $data ) {
190 $this->clearOpenElement( null );
191 if( $this->lastName
== "id" ) {
192 if( $this->state
== "revision" ) {
193 $this->thisRev
.= $data;
194 } elseif( $this->state
== "page" ) {
195 $this->thisPage
.= $data;
198 $this->buffer
.= htmlspecialchars( $data );
201 function clearOpenElement( $style ) {
202 if( $this->openElement
) {
203 $this->buffer
.= wfElement( $this->openElement
[0], $this->openElement
[1], $style );
204 $this->openElement
= false;
210 $dumper = new TextPassDumper( $argv );
215 $dumper->progress( <<<END
216 This script postprocesses XML dumps from dumpBackup.php to add
217 page text which was stubbed out (using --stub).
219 XML input is accepted on stdin.
220 XML output is sent to stdout; progress reports are sent to stderr.
222 Usage: php dumpTextPass.php [<options>]
224 --stub=<type>:<file> To load a compressed stub dump instead of stdin
225 --prefetch=<type>:<file> Use a prior dump file as a text source, to save
226 pressure on the database.
227 (Requires PHP 5.0+ and the XMLReader PECL extension)
228 --quiet Don't dump status reports to stderr.
229 --report=n Report position and speed after every n pages processed.
231 --server=h Force reading from MySQL server h
232 --current Base ETA on number of pages in database instead of all revisions