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";
38 # This shouldn't happen if on console... ;)
39 header( 'Content-type: text/html; charset=UTF-8' );
41 # Notice messages will foul up your XML output even if they're
42 # relatively harmless.
43 // ini_set( 'display_errors', false );
45 $this->startTime
= wfTime();
47 $this->db
=& $this->backupDb();
48 $this->maxCount
= $this->db
->selectField( 'page', 'MAX(page_id)', '', 'BackupDumper::dump' );
49 $this->startTime
= wfTime();
51 $this->egress
= new ExportProgressFilter( $this->sink
, $this );
53 $input = fopen( $this->input
, "rt" );
54 $result = $this->readDump( $input );
56 if( WikiError
::isError( $result ) ) {
57 wfDie( $result->getMessage() );
60 $this->report( true );
63 function processOption( $opt, $val, $param ) {
64 $url = $this->processFileOpt( $val, $param );
68 require_once 'maintenance/backupPrefetch.inc';
69 $this->prefetch
= new BaseDump( $url );
77 function processFileOpt( $val, $param ) {
82 return "compress.zlib://$param";
84 return "compress.bzip2://$param";
90 function readDump( $input ) {
92 $this->openElement
= false;
93 $this->atStart
= true;
99 $parser = xml_parser_create( "UTF-8" );
100 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
102 xml_set_element_handler( $parser, array( &$this, 'startElement' ), array( &$this, 'endElement' ) );
103 xml_set_character_data_handler( $parser, array( &$this, 'characterData' ) );
105 $offset = 0; // for context extraction on error reporting
106 $bufferSize = 512 * 1024;
108 $chunk = fread( $input, $bufferSize );
109 if( !xml_parse( $parser, $chunk, feof( $input ) ) ) {
110 wfDebug( "TextDumpPass::readDump encountered XML parsing error\n" );
111 return new WikiXmlError( $parser, 'XML import parse failure', $chunk, $offset );
113 $offset +
= strlen( $chunk );
114 } while( $chunk !== false && !feof( $input ) );
115 xml_parser_free( $parser );
120 function getText( $id ) {
121 if( isset( $this->prefetch
) ) {
122 $text = $this->prefetch
->prefetch( $this->thisPage
, $this->thisRev
);
123 if( !is_null( $text ) )
127 $row = $this->db
->selectRow( 'text',
128 array( 'old_text', 'old_flags' ),
129 array( 'old_id' => $id ),
130 'TextPassDumper::getText' );
131 $text = Revision
::getRevisionText( $row );
132 $stripped = str_replace( "\r", "", $text );
133 $normalized = UtfNormal
::cleanUp( $stripped );
137 function startElement( $parser, $name, $attribs ) {
138 $this->clearOpenElement( null );
139 $this->lastName
= $name;
141 if( $name == 'revision' ) {
142 $this->state
= $name;
143 $this->egress
->writeOpenPage( null, $this->buffer
);
145 } elseif( $name == 'page' ) {
146 $this->state
= $name;
147 if( $this->atStart
) {
148 $this->egress
->writeOpenStream( $this->buffer
);
150 $this->atStart
= false;
154 if( $name == "text" && isset( $attribs['id'] ) ) {
155 $text = $this->getText( $attribs['id'] );
156 $this->openElement
= array( $name, array( 'xml:space' => 'preserve' ) );
157 if( strlen( $text ) > 0 ) {
158 $this->characterData( $parser, $text );
161 $this->openElement
= array( $name, $attribs );
165 function endElement( $parser, $name ) {
166 if( $this->openElement
) {
167 $this->clearOpenElement( "" );
169 $this->buffer
.= "</$name>";
172 if( $name == 'revision' ) {
173 $this->egress
->writeRevision( null, $this->buffer
);
176 } elseif( $name == 'page' ) {
177 $this->egress
->writeClosePage( $this->buffer
);
179 $this->thisPage
= "";
180 } elseif( $name == 'mediawiki' ) {
181 $this->egress
->writeCloseStream( $this->buffer
);
186 function characterData( $parser, $data ) {
187 $this->clearOpenElement( null );
188 if( $this->lastName
== "id" ) {
189 if( $this->state
== "revision" ) {
190 $this->thisRev
.= $data;
191 } elseif( $this->state
== "page" ) {
192 $this->thisPage
.= $data;
195 $this->buffer
.= htmlspecialchars( $data );
198 function clearOpenElement( $style ) {
199 if( $this->openElement
) {
200 $this->buffer
.= wfElement( $this->openElement
[0], $this->openElement
[1], $style );
201 $this->openElement
= false;
207 $dumper = new TextPassDumper( $argv );
208 if( isset( $options['server'] ) ) {
209 $dumper->server
= $options['server'];
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