* dumpTextPass now skips goes to database for entries that were blank in the
[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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @package MediaWiki
22 * @subpackage SpecialPage
23 */
24
25 $originalDir = getcwd();
26
27 require_once( 'commandLine.inc' );
28 require_once( 'SpecialExport.php' );
29 require_once( 'maintenance/backup.inc' );
30
31 class TextPassDumper extends BackupDumper {
32 var $prefetch = null;
33 var $input = "php://stdin";
34 var $history = MW_EXPORT_FULL;
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->initProgress( $this->history );
45
46 $this->db =& $this->backupDb();
47
48 $this->egress = new ExportProgressFilter( $this->sink, $this );
49
50 $input = fopen( $this->input, "rt" );
51 $result = $this->readDump( $input );
52
53 if( WikiError::isError( $result ) ) {
54 wfDie( $result->getMessage() );
55 }
56
57 $this->report( true );
58 }
59
60 function processOption( $opt, $val, $param ) {
61 $url = $this->processFileOpt( $val, $param );
62
63 switch( $opt ) {
64 case 'prefetch':
65 require_once 'maintenance/backupPrefetch.inc';
66 $this->prefetch = new BaseDump( $url );
67 break;
68 case 'stub':
69 $this->input = $url;
70 break;
71 case 'current':
72 $this->history = MW_EXPORT_CURRENT;
73 break;
74 case 'full':
75 $this->history = MW_EXPORT_FULL;
76 break;
77 }
78 }
79
80 function processFileOpt( $val, $param ) {
81 switch( $val ) {
82 case "file":
83 return $param;
84 case "gzip":
85 return "compress.zlib://$param";
86 case "bzip2":
87 return "compress.bzip2://$param";
88 default:
89 return $val;
90 }
91 }
92
93 function readDump( $input ) {
94 $this->buffer = "";
95 $this->openElement = false;
96 $this->atStart = true;
97 $this->state = "";
98 $this->lastName = "";
99 $this->thisPage = 0;
100 $this->thisRev = 0;
101
102 $parser = xml_parser_create( "UTF-8" );
103 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
104
105 xml_set_element_handler( $parser, array( &$this, 'startElement' ), array( &$this, 'endElement' ) );
106 xml_set_character_data_handler( $parser, array( &$this, 'characterData' ) );
107
108 $offset = 0; // for context extraction on error reporting
109 $bufferSize = 512 * 1024;
110 do {
111 $chunk = fread( $input, $bufferSize );
112 if( !xml_parse( $parser, $chunk, feof( $input ) ) ) {
113 wfDebug( "TextDumpPass::readDump encountered XML parsing error\n" );
114 return new WikiXmlError( $parser, 'XML import parse failure', $chunk, $offset );
115 }
116 $offset += strlen( $chunk );
117 } while( $chunk !== false && !feof( $input ) );
118 xml_parser_free( $parser );
119
120 return true;
121 }
122
123 function getText( $id ) {
124 if( isset( $this->prefetch ) ) {
125 $text = $this->prefetch->prefetch( $this->thisPage, $this->thisRev );
126 if( $text === null ) {
127 // Entry missing from prefetch dump
128 } elseif( $text === "" ) {
129 // Blank entries may indicate that the prior dump was broken.
130 // To be safe, reload it.
131 } else {
132 return $text;
133 }
134 }
135 $id = intval( $id );
136 $row = $this->db->selectRow( 'text',
137 array( 'old_text', 'old_flags' ),
138 array( 'old_id' => $id ),
139 'TextPassDumper::getText' );
140 $text = Revision::getRevisionText( $row );
141 $stripped = str_replace( "\r", "", $text );
142 $normalized = UtfNormal::cleanUp( $stripped );
143 return $normalized;
144 }
145
146 function startElement( $parser, $name, $attribs ) {
147 $this->clearOpenElement( null );
148 $this->lastName = $name;
149
150 if( $name == 'revision' ) {
151 $this->state = $name;
152 $this->egress->writeOpenPage( null, $this->buffer );
153 $this->buffer = "";
154 } elseif( $name == 'page' ) {
155 $this->state = $name;
156 if( $this->atStart ) {
157 $this->egress->writeOpenStream( $this->buffer );
158 $this->buffer = "";
159 $this->atStart = false;
160 }
161 }
162
163 if( $name == "text" && isset( $attribs['id'] ) ) {
164 $text = $this->getText( $attribs['id'] );
165 $this->openElement = array( $name, array( 'xml:space' => 'preserve' ) );
166 if( strlen( $text ) > 0 ) {
167 $this->characterData( $parser, $text );
168 }
169 } else {
170 $this->openElement = array( $name, $attribs );
171 }
172 }
173
174 function endElement( $parser, $name ) {
175 if( $this->openElement ) {
176 $this->clearOpenElement( "" );
177 } else {
178 $this->buffer .= "</$name>";
179 }
180
181 if( $name == 'revision' ) {
182 $this->egress->writeRevision( null, $this->buffer );
183 $this->buffer = "";
184 $this->thisRev = "";
185 } elseif( $name == 'page' ) {
186 $this->egress->writeClosePage( $this->buffer );
187 $this->buffer = "";
188 $this->thisPage = "";
189 } elseif( $name == 'mediawiki' ) {
190 $this->egress->writeCloseStream( $this->buffer );
191 $this->buffer = "";
192 }
193 }
194
195 function characterData( $parser, $data ) {
196 $this->clearOpenElement( null );
197 if( $this->lastName == "id" ) {
198 if( $this->state == "revision" ) {
199 $this->thisRev .= $data;
200 } elseif( $this->state == "page" ) {
201 $this->thisPage .= $data;
202 }
203 }
204 $this->buffer .= htmlspecialchars( $data );
205 }
206
207 function clearOpenElement( $style ) {
208 if( $this->openElement ) {
209 $this->buffer .= wfElement( $this->openElement[0], $this->openElement[1], $style );
210 $this->openElement = false;
211 }
212 }
213 }
214
215
216 $dumper = new TextPassDumper( $argv );
217
218 if( true ) {
219 $dumper->dump();
220 } else {
221 $dumper->progress( <<<END
222 This script postprocesses XML dumps from dumpBackup.php to add
223 page text which was stubbed out (using --stub).
224
225 XML input is accepted on stdin.
226 XML output is sent to stdout; progress reports are sent to stderr.
227
228 Usage: php dumpTextPass.php [<options>]
229 Options:
230 --stub=<type>:<file> To load a compressed stub dump instead of stdin
231 --prefetch=<type>:<file> Use a prior dump file as a text source, to save
232 pressure on the database.
233 (Requires PHP 5.0+ and the XMLReader PECL extension)
234 --quiet Don't dump status reports to stderr.
235 --report=n Report position and speed after every n pages processed.
236 (Default: 100)
237 --server=h Force reading from MySQL server h
238 --current Base ETA on number of pages in database instead of all revisions
239 END
240 );
241 }
242
243 ?>