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
{
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->startTime
= wfTime();
46 $this->db
=& $this->backupDb();
47 $this->maxCount
= $this->db
->selectField( 'page', 'MAX(page_id)', '', 'BackupDumper::dump' );
48 $this->startTime
= wfTime();
50 $this->egress
= new ExportProgressFilter( $this->sink
, $this );
52 $input = fopen( "php://stdin", "rt" );
53 $result = $this->readDump( $input );
55 if( WikiError
::isError( $result ) ) {
56 $this->progress( $result->getMessage() );
59 $this->report( true );
62 function processOption( $opt, $val, $param ) {
63 if( $opt == 'prefetch' ) {
64 require_once 'maintenance/backupPrefetch.inc';
70 $filename = "compress.gzip://$param";
73 $filename = "compress.bzip2://$param";
78 $this->prefetch
= new BaseDump( $filename );
82 function readDump( $input ) {
84 $this->openElement
= false;
85 $this->atStart
= true;
91 $parser = xml_parser_create( "UTF-8" );
92 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
94 xml_set_element_handler( $parser, array( &$this, 'startElement' ), array( &$this, 'endElement' ) );
95 xml_set_character_data_handler( $parser, array( &$this, 'characterData' ) );
97 $offset = 0; // for context extraction on error reporting
98 $bufferSize = 512 * 1024;
100 $chunk = fread( $input, $bufferSize );
101 if( !xml_parse( $parser, $chunk, feof( $input ) ) ) {
102 wfDebug( "TextDumpPass::readDump encountered XML parsing error\n" );
103 return new WikiXmlError( $parser, 'XML import parse failure', $chunk, $offset );
105 $offset +
= strlen( $chunk );
106 } while( $chunk !== false && !feof( $input ) );
107 xml_parser_free( $parser );
110 function getText( $id ) {
111 if( isset( $this->prefetch
) ) {
112 $text = $this->prefetch
->prefetch( $this->thisPage
, $this->thisRev
);
113 if( !is_null( $text ) )
117 $row = $this->db
->selectRow( 'text',
118 array( 'old_text', 'old_flags' ),
119 array( 'old_id' => $id ),
120 'TextPassDumper::getText' );
121 $text = Revision
::getRevisionText( $row );
122 $stripped = str_replace( "\r", "", $text );
123 $normalized = UtfNormal
::cleanUp( $stripped );
127 function startElement( $parser, $name, $attribs ) {
128 $this->clearOpenElement( null );
129 $this->lastName
= $name;
131 if( $name == 'revision' ) {
132 $this->state
= $name;
133 $this->egress
->writeOpenPage( null, $this->buffer
);
135 } elseif( $name == 'page' ) {
136 $this->state
= $name;
137 if( $this->atStart
) {
138 $this->egress
->writeOpenStream( $this->buffer
);
140 $this->atStart
= false;
144 if( $name == "text" && isset( $attribs['id'] ) ) {
145 $text = $this->getText( $attribs['id'] );
146 $this->openElement
= array( $name, array( 'xml:space' => 'preserve' ) );
147 if( strlen( $text ) > 0 ) {
148 $this->characterData( $parser, $text );
151 $this->openElement
= array( $name, $attribs );
155 function endElement( $parser, $name ) {
156 if( $this->openElement
) {
157 $this->clearOpenElement( "" );
159 $this->buffer
.= "</$name>";
162 if( $name == 'revision' ) {
163 $this->egress
->writeRevision( null, $this->buffer
);
166 } elseif( $name == 'page' ) {
167 $this->egress
->writeClosePage( $this->buffer
);
169 $this->thisPage
= "";
170 } elseif( $name == 'mediawiki' ) {
171 $this->egress
->writeCloseStream( $this->buffer
);
176 function characterData( $parser, $data ) {
177 $this->clearOpenElement( null );
178 if( $this->lastName
== "id" ) {
179 if( $this->state
== "revision" ) {
180 $this->thisRev
.= $data;
181 } elseif( $this->state
== "page" ) {
182 $this->thisPage
.= $data;
185 $this->buffer
.= htmlspecialchars( $data );
188 function clearOpenElement( $style ) {
189 if( $this->openElement
) {
190 $this->buffer
.= wfElement( $this->openElement
[0], $this->openElement
[1], $style );
191 $this->openElement
= false;
197 $dumper = new TextPassDumper( $argv );
198 if( isset( $options['server'] ) ) {
199 $dumper->server
= $options['server'];
205 $dumper->progress( <<<END
206 This script postprocesses XML dumps from dumpBackup.php to add
207 page text which was stubbed out (using --stub).
209 XML input is accepted on stdin.
210 XML output is sent to stdout; progress reports are sent to stderr.
212 Usage: php dumpTextPass.php [<options>]
214 --prefetch <file> Use a prior dump file as a text source where possible.
215 (Requires PHP 5.0+ and the XMLReader PECL extension)
216 --quiet Don't dump status reports to stderr.
217 --report=n Report position and speed after every n pages processed.
219 --server=h Force reading from MySQL server h