Support using gzip and bzip2 decompression wrappers on the prefetch XML
[lhc/web/wiklou.git] / maintenance / dumpTextPass.php
1 <?php
2 /**
3 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
4 * http://www.mediawiki.org/
5 *
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.
10 *
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.
15 *
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
20 *
21 * @package MediaWiki
22 * @subpackage SpecialPage
23 */
24
25 $originalDir = getcwd();
26
27 $optionsWithArgs = array( 'server', 'pagelist', 'start', 'end' );
28
29 require_once( 'commandLine.inc' );
30 require_once( 'SpecialExport.php' );
31 require_once( 'maintenance/backup.inc' );
32
33 class TextPassDumper extends BackupDumper {
34 var $prefetch = null;
35
36 function dump() {
37 # This shouldn't happen if on console... ;)
38 header( 'Content-type: text/html; charset=UTF-8' );
39
40 # Notice messages will foul up your XML output even if they're
41 # relatively harmless.
42 // ini_set( 'display_errors', false );
43
44 $this->startTime = wfTime();
45
46 $this->db =& wfGetDB( DB_SLAVE );
47 $this->maxCount = $this->db->selectField( 'page', 'MAX(page_id)', '', 'BackupDumper::dump' );
48 $this->startTime = wfTime();
49
50 $this->egress = new ExportProgressFilter( $this->sink, $this );
51
52 $input = fopen( "php://stdin", "rt" );
53 $result = $this->readDump( $input );
54
55 if( WikiError::isError( $result ) ) {
56 $this->progress( $result->getMessage() );
57 }
58
59 $this->report( true );
60 }
61
62 function processOption( $opt, $val, $param ) {
63 if( $opt == 'prefetch' ) {
64 require_once 'maintenance/backupPrefetch.inc';
65 switch( $val ) {
66 case "file":
67 $filename = $param;
68 break;
69 case "gzip":
70 $filename = "compress.gzip://$param";
71 break;
72 case "bzip2":
73 $filename = "compress.bzip2://$param";
74 break;
75 default:
76 $filename = $val;
77 }
78 $this->prefetch = new BaseDump( $filename );
79 }
80 }
81
82 function readDump( $input ) {
83 $this->buffer = "";
84 $this->openElement = false;
85 $this->atStart = true;
86 $this->state = "";
87 $this->lastName = "";
88 $this->thisPage = 0;
89 $this->thisRev = 0;
90
91 $parser = xml_parser_create( "UTF-8" );
92 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
93
94 xml_set_element_handler( $parser, array( &$this, 'startElement' ), array( &$this, 'endElement' ) );
95 xml_set_character_data_handler( $parser, array( &$this, 'characterData' ) );
96
97 $offset = 0; // for context extraction on error reporting
98 $bufferSize = 512 * 1024;
99 do {
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 );
104 }
105 $offset += strlen( $chunk );
106 } while( $chunk !== false && !feof( $input ) );
107 xml_parser_free( $parser );
108 }
109
110 function getText( $id ) {
111 if( isset( $this->prefetch ) ) {
112 $text = $this->prefetch->prefetch( $this->thisPage, $this->thisRev );
113 if( !is_null( $text ) )
114 return $text;
115 }
116 $id = intval( $id );
117 $row = $this->db->selectRow( 'text',
118 array( 'old_text', 'old_flags' ),
119 array( 'old_id' => $id ),
120 'TextPassDumper::getText' );
121 return UtfNormal::cleanUp( strval( Revision::getRevisionText( $row ) ) );
122 }
123
124 function startElement( $parser, $name, $attribs ) {
125 $this->clearOpenElement( null );
126 $this->lastName = $name;
127
128 if( $name == 'revision' ) {
129 $this->state = $name;
130 $this->egress->writeOpenPage( null, $this->buffer );
131 $this->buffer = "";
132 } elseif( $name == 'page' ) {
133 $this->state = $name;
134 if( $this->atStart ) {
135 $this->egress->writeOpenStream( $this->buffer );
136 $this->buffer = "";
137 $this->atStart = false;
138 }
139 }
140
141 if( $name == "text" && isset( $attribs['id'] ) ) {
142 $text = $this->getText( $attribs['id'] );
143 $this->openElement = array( $name, array( 'xml:space' => 'preserve' ) );
144 if( strlen( $text ) > 0 ) {
145 $this->characterData( $parser, $text );
146 }
147 } else {
148 $this->openElement = array( $name, $attribs );
149 }
150 }
151
152 function endElement( $parser, $name ) {
153 if( $this->openElement ) {
154 $this->clearOpenElement( "" );
155 } else {
156 $this->buffer .= "</$name>";
157 }
158
159 if( $name == 'revision' ) {
160 $this->egress->writeRevision( null, $this->buffer );
161 $this->buffer = "";
162 } elseif( $name == 'page' ) {
163 $this->egress->writeClosePage( $this->buffer );
164 $this->buffer = "";
165 } elseif( $name == 'mediawiki' ) {
166 $this->egress->writeCloseStream( $this->buffer );
167 $this->buffer = "";
168 }
169 }
170
171 function characterData( $parser, $data ) {
172 $this->clearOpenElement( null );
173 if( $this->lastName == "id" ) {
174 if( $this->state == "revision" ) {
175 $this->thisRev = intval( $data );
176 } elseif( $this->state == "page" ) {
177 $this->thisPage = intval( $data );
178 }
179 }
180 $this->buffer .= htmlspecialchars( $data );
181 }
182
183 function clearOpenElement( $style ) {
184 if( $this->openElement ) {
185 $this->buffer .= wfElement( $this->openElement[0], $this->openElement[1], $style );
186 $this->openElement = false;
187 }
188 }
189 }
190
191
192 $dumper = new TextPassDumper( $argv );
193
194 if( true ) {
195 $dumper->dump();
196 } else {
197 $dumper->progress( <<<END
198 This script postprocesses XML dumps from dumpBackup.php to add
199 page text which was stubbed out (using --stub).
200
201 XML input is accepted on stdin.
202 XML output is sent to stdout; progress reports are sent to stderr.
203
204 Usage: php dumpTextPass.php [<options>]
205 Options:
206 --prefetch <file> Use a prior dump file as a text source where possible.
207 (Requires PHP 5.0+ and the XMLReader PECL extension)
208 --quiet Don't dump status reports to stderr.
209 --report=n Report position and speed after every n pages processed.
210 (Default: 100)
211 END
212 );
213 }
214
215 ?>