* Use revision rate for ETA in dump generation; it tends to be more stable
[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 var $input = "php://stdin";
36 var $history = MW_EXPORT_FULL;
37
38 function dump() {
39 # This shouldn't happen if on console... ;)
40 header( 'Content-type: text/html; charset=UTF-8' );
41
42 # Notice messages will foul up your XML output even if they're
43 # relatively harmless.
44 // ini_set( 'display_errors', false );
45
46 $this->initProgress( $this->history );
47
48 $this->db =& $this->backupDb();
49
50 $this->egress = new ExportProgressFilter( $this->sink, $this );
51
52 $input = fopen( $this->input, "rt" );
53 $result = $this->readDump( $input );
54
55 if( WikiError::isError( $result ) ) {
56 wfDie( $result->getMessage() );
57 }
58
59 $this->report( true );
60 }
61
62 function processOption( $opt, $val, $param ) {
63 $url = $this->processFileOpt( $val, $param );
64
65 switch( $opt ) {
66 case 'prefetch':
67 require_once 'maintenance/backupPrefetch.inc';
68 $this->prefetch = new BaseDump( $url );
69 break;
70 case 'stub':
71 $this->input = $url;
72 break;
73 case 'current':
74 $this->history = MW_EXPORT_CURRENT;
75 break;
76 case 'full':
77 $this->history = MW_EXPORT_FULL;
78 break;
79 }
80 }
81
82 function processFileOpt( $val, $param ) {
83 switch( $val ) {
84 case "file":
85 return $param;
86 case "gzip":
87 return "compress.zlib://$param";
88 case "bzip2":
89 return "compress.bzip2://$param";
90 default:
91 return $val;
92 }
93 }
94
95 function readDump( $input ) {
96 $this->buffer = "";
97 $this->openElement = false;
98 $this->atStart = true;
99 $this->state = "";
100 $this->lastName = "";
101 $this->thisPage = 0;
102 $this->thisRev = 0;
103
104 $parser = xml_parser_create( "UTF-8" );
105 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
106
107 xml_set_element_handler( $parser, array( &$this, 'startElement' ), array( &$this, 'endElement' ) );
108 xml_set_character_data_handler( $parser, array( &$this, 'characterData' ) );
109
110 $offset = 0; // for context extraction on error reporting
111 $bufferSize = 512 * 1024;
112 do {
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 );
117 }
118 $offset += strlen( $chunk );
119 } while( $chunk !== false && !feof( $input ) );
120 xml_parser_free( $parser );
121
122 return true;
123 }
124
125 function getText( $id ) {
126 if( isset( $this->prefetch ) ) {
127 $text = $this->prefetch->prefetch( $this->thisPage, $this->thisRev );
128 if( !is_null( $text ) )
129 return $text;
130 }
131 $id = intval( $id );
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 );
139 return $normalized;
140 }
141
142 function startElement( $parser, $name, $attribs ) {
143 $this->clearOpenElement( null );
144 $this->lastName = $name;
145
146 if( $name == 'revision' ) {
147 $this->state = $name;
148 $this->egress->writeOpenPage( null, $this->buffer );
149 $this->buffer = "";
150 } elseif( $name == 'page' ) {
151 $this->state = $name;
152 if( $this->atStart ) {
153 $this->egress->writeOpenStream( $this->buffer );
154 $this->buffer = "";
155 $this->atStart = false;
156 }
157 }
158
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 );
164 }
165 } else {
166 $this->openElement = array( $name, $attribs );
167 }
168 }
169
170 function endElement( $parser, $name ) {
171 if( $this->openElement ) {
172 $this->clearOpenElement( "" );
173 } else {
174 $this->buffer .= "</$name>";
175 }
176
177 if( $name == 'revision' ) {
178 $this->egress->writeRevision( null, $this->buffer );
179 $this->buffer = "";
180 $this->thisRev = "";
181 } elseif( $name == 'page' ) {
182 $this->egress->writeClosePage( $this->buffer );
183 $this->buffer = "";
184 $this->thisPage = "";
185 } elseif( $name == 'mediawiki' ) {
186 $this->egress->writeCloseStream( $this->buffer );
187 $this->buffer = "";
188 }
189 }
190
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;
198 }
199 }
200 $this->buffer .= htmlspecialchars( $data );
201 }
202
203 function clearOpenElement( $style ) {
204 if( $this->openElement ) {
205 $this->buffer .= wfElement( $this->openElement[0], $this->openElement[1], $style );
206 $this->openElement = false;
207 }
208 }
209 }
210
211
212 $dumper = new TextPassDumper( $argv );
213 if( isset( $options['server'] ) ) {
214 $dumper->server = $options['server'];
215 }
216
217 if( true ) {
218 $dumper->dump();
219 } else {
220 $dumper->progress( <<<END
221 This script postprocesses XML dumps from dumpBackup.php to add
222 page text which was stubbed out (using --stub).
223
224 XML input is accepted on stdin.
225 XML output is sent to stdout; progress reports are sent to stderr.
226
227 Usage: php dumpTextPass.php [<options>]
228 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.
235 (Default: 100)
236 --server=h Force reading from MySQL server h
237 --current Base ETA on number of pages in database instead of all revisions
238 END
239 );
240 }
241
242 ?>