fc8c15eede18febaf039a2f9073e5a10eb3bbdce
[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 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( !is_null( $text ) )
127 return $text;
128 }
129 $id = intval( $id );
130 $row = $this->db->selectRow( 'text',
131 array( 'old_text', 'old_flags' ),
132 array( 'old_id' => $id ),
133 'TextPassDumper::getText' );
134 $text = Revision::getRevisionText( $row );
135 $stripped = str_replace( "\r", "", $text );
136 $normalized = UtfNormal::cleanUp( $stripped );
137 return $normalized;
138 }
139
140 function startElement( $parser, $name, $attribs ) {
141 $this->clearOpenElement( null );
142 $this->lastName = $name;
143
144 if( $name == 'revision' ) {
145 $this->state = $name;
146 $this->egress->writeOpenPage( null, $this->buffer );
147 $this->buffer = "";
148 } elseif( $name == 'page' ) {
149 $this->state = $name;
150 if( $this->atStart ) {
151 $this->egress->writeOpenStream( $this->buffer );
152 $this->buffer = "";
153 $this->atStart = false;
154 }
155 }
156
157 if( $name == "text" && isset( $attribs['id'] ) ) {
158 $text = $this->getText( $attribs['id'] );
159 $this->openElement = array( $name, array( 'xml:space' => 'preserve' ) );
160 if( strlen( $text ) > 0 ) {
161 $this->characterData( $parser, $text );
162 }
163 } else {
164 $this->openElement = array( $name, $attribs );
165 }
166 }
167
168 function endElement( $parser, $name ) {
169 if( $this->openElement ) {
170 $this->clearOpenElement( "" );
171 } else {
172 $this->buffer .= "</$name>";
173 }
174
175 if( $name == 'revision' ) {
176 $this->egress->writeRevision( null, $this->buffer );
177 $this->buffer = "";
178 $this->thisRev = "";
179 } elseif( $name == 'page' ) {
180 $this->egress->writeClosePage( $this->buffer );
181 $this->buffer = "";
182 $this->thisPage = "";
183 } elseif( $name == 'mediawiki' ) {
184 $this->egress->writeCloseStream( $this->buffer );
185 $this->buffer = "";
186 }
187 }
188
189 function characterData( $parser, $data ) {
190 $this->clearOpenElement( null );
191 if( $this->lastName == "id" ) {
192 if( $this->state == "revision" ) {
193 $this->thisRev .= $data;
194 } elseif( $this->state == "page" ) {
195 $this->thisPage .= $data;
196 }
197 }
198 $this->buffer .= htmlspecialchars( $data );
199 }
200
201 function clearOpenElement( $style ) {
202 if( $this->openElement ) {
203 $this->buffer .= wfElement( $this->openElement[0], $this->openElement[1], $style );
204 $this->openElement = false;
205 }
206 }
207 }
208
209
210 $dumper = new TextPassDumper( $argv );
211
212 if( true ) {
213 $dumper->dump();
214 } else {
215 $dumper->progress( <<<END
216 This script postprocesses XML dumps from dumpBackup.php to add
217 page text which was stubbed out (using --stub).
218
219 XML input is accepted on stdin.
220 XML output is sent to stdout; progress reports are sent to stderr.
221
222 Usage: php dumpTextPass.php [<options>]
223 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.
230 (Default: 100)
231 --server=h Force reading from MySQL server h
232 --current Base ETA on number of pages in database instead of all revisions
233 END
234 );
235 }
236
237 ?>