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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @subpackage SpecialPage
25 $originalDir = getcwd();
27 $optionsWithArgs = array( 'server', 'pagelist', 'start', 'end' );
29 require_once( 'commandLine.inc' );
30 require_once( 'SpecialExport.php' );
31 require_once( 'maintenance/backup.inc' );
33 class TextPassDumper
extends BackupDumper
{
35 var $input = "php://stdin";
36 var $history = MW_EXPORT_FULL
;
39 # This shouldn't happen if on console... ;)
40 header( 'Content-type: text/html; charset=UTF-8' );
42 # Notice messages will foul up your XML output even if they're
43 # relatively harmless.
44 // ini_set( 'display_errors', false );
46 $this->initProgress( $this->history
);
48 $this->db
=& $this->backupDb();
50 $this->egress
= new ExportProgressFilter( $this->sink
, $this );
52 $input = fopen( $this->input
, "rt" );
53 $result = $this->readDump( $input );
55 if( WikiError
::isError( $result ) ) {
56 wfDie( $result->getMessage() );
59 $this->report( true );
62 function processOption( $opt, $val, $param ) {
63 $url = $this->processFileOpt( $val, $param );
67 require_once 'maintenance/backupPrefetch.inc';
68 $this->prefetch
= new BaseDump( $url );
74 $this->history
= MW_EXPORT_CURRENT
;
77 $this->history
= MW_EXPORT_FULL
;
82 function processFileOpt( $val, $param ) {
87 return "compress.zlib://$param";
89 return "compress.bzip2://$param";
95 function readDump( $input ) {
97 $this->openElement
= false;
98 $this->atStart
= true;
100 $this->lastName
= "";
104 $parser = xml_parser_create( "UTF-8" );
105 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
107 xml_set_element_handler( $parser, array( &$this, 'startElement' ), array( &$this, 'endElement' ) );
108 xml_set_character_data_handler( $parser, array( &$this, 'characterData' ) );
110 $offset = 0; // for context extraction on error reporting
111 $bufferSize = 512 * 1024;
113 $chunk = fread( $input, $bufferSize );
114 if( !xml_parse( $parser, $chunk, feof( $input ) ) ) {
115 wfDebug( "TextDumpPass::readDump encountered XML parsing error\n" );
116 return new WikiXmlError( $parser, 'XML import parse failure', $chunk, $offset );
118 $offset +
= strlen( $chunk );
119 } while( $chunk !== false && !feof( $input ) );
120 xml_parser_free( $parser );
125 function getText( $id ) {
126 if( isset( $this->prefetch
) ) {
127 $text = $this->prefetch
->prefetch( $this->thisPage
, $this->thisRev
);
128 if( !is_null( $text ) )
132 $row = $this->db
->selectRow( 'text',
133 array( 'old_text', 'old_flags' ),
134 array( 'old_id' => $id ),
135 'TextPassDumper::getText' );
136 $text = Revision
::getRevisionText( $row );
137 $stripped = str_replace( "\r", "", $text );
138 $normalized = UtfNormal
::cleanUp( $stripped );
142 function startElement( $parser, $name, $attribs ) {
143 $this->clearOpenElement( null );
144 $this->lastName
= $name;
146 if( $name == 'revision' ) {
147 $this->state
= $name;
148 $this->egress
->writeOpenPage( null, $this->buffer
);
150 } elseif( $name == 'page' ) {
151 $this->state
= $name;
152 if( $this->atStart
) {
153 $this->egress
->writeOpenStream( $this->buffer
);
155 $this->atStart
= false;
159 if( $name == "text" && isset( $attribs['id'] ) ) {
160 $text = $this->getText( $attribs['id'] );
161 $this->openElement
= array( $name, array( 'xml:space' => 'preserve' ) );
162 if( strlen( $text ) > 0 ) {
163 $this->characterData( $parser, $text );
166 $this->openElement
= array( $name, $attribs );
170 function endElement( $parser, $name ) {
171 if( $this->openElement
) {
172 $this->clearOpenElement( "" );
174 $this->buffer
.= "</$name>";
177 if( $name == 'revision' ) {
178 $this->egress
->writeRevision( null, $this->buffer
);
181 } elseif( $name == 'page' ) {
182 $this->egress
->writeClosePage( $this->buffer
);
184 $this->thisPage
= "";
185 } elseif( $name == 'mediawiki' ) {
186 $this->egress
->writeCloseStream( $this->buffer
);
191 function characterData( $parser, $data ) {
192 $this->clearOpenElement( null );
193 if( $this->lastName
== "id" ) {
194 if( $this->state
== "revision" ) {
195 $this->thisRev
.= $data;
196 } elseif( $this->state
== "page" ) {
197 $this->thisPage
.= $data;
200 $this->buffer
.= htmlspecialchars( $data );
203 function clearOpenElement( $style ) {
204 if( $this->openElement
) {
205 $this->buffer
.= wfElement( $this->openElement
[0], $this->openElement
[1], $style );
206 $this->openElement
= false;
212 $dumper = new TextPassDumper( $argv );
213 if( isset( $options['server'] ) ) {
214 $dumper->server
= $options['server'];
220 $dumper->progress( <<<END
221 This script postprocesses XML dumps from dumpBackup.php to add
222 page text which was stubbed out (using --stub).
224 XML input is accepted on stdin.
225 XML output is sent to stdout; progress reports are sent to stderr.
227 Usage: php dumpTextPass.php [<options>]
229 --stub=<type>:<file> To load a compressed stub dump instead of stdin
230 --prefetch=<type>:<file> Use a prior dump file as a text source, to save
231 pressure on the database.
232 (Requires PHP 5.0+ and the XMLReader PECL extension)
233 --quiet Don't dump status reports to stderr.
234 --report=n Report position and speed after every n pages processed.
236 --server=h Force reading from MySQL server h
237 --current Base ETA on number of pages in database instead of all revisions