195a42d1bd436732b478eab3ded4af1656606aa8
[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
37 function dump() {
38 # This shouldn't happen if on console... ;)
39 header( 'Content-type: text/html; charset=UTF-8' );
40
41 # Notice messages will foul up your XML output even if they're
42 # relatively harmless.
43 // ini_set( 'display_errors', false );
44
45 $this->startTime = wfTime();
46
47 $this->db =& $this->backupDb();
48 $this->maxCount = $this->db->selectField( 'page', 'MAX(page_id)', '', 'BackupDumper::dump' );
49 $this->startTime = wfTime();
50
51 $this->egress = new ExportProgressFilter( $this->sink, $this );
52
53 $input = fopen( $this->input, "rt" );
54 $result = $this->readDump( $input );
55
56 if( WikiError::isError( $result ) ) {
57 $this->progress( $result->getMessage() );
58 }
59
60 $this->report( true );
61 }
62
63 function processOption( $opt, $val, $param ) {
64 $url = $this->processFileOpt( $val, $param );
65
66 switch( $opt ) {
67 case 'prefetch':
68 require_once 'maintenance/backupPrefetch.inc';
69 $this->prefetch = new BaseDump( $url );
70 break;
71 case 'stub':
72 $this->input = $url;
73 break;
74 }
75 }
76
77 function processFileOpt( $val, $param ) {
78 switch( $val ) {
79 case "file":
80 return $param;
81 case "gzip":
82 return "compress.zlib://$param";
83 case "bzip2":
84 return "compress.bzip2://$param";
85 default:
86 return $val;
87 }
88 }
89
90 function readDump( $input ) {
91 $this->buffer = "";
92 $this->openElement = false;
93 $this->atStart = true;
94 $this->state = "";
95 $this->lastName = "";
96 $this->thisPage = 0;
97 $this->thisRev = 0;
98
99 $parser = xml_parser_create( "UTF-8" );
100 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
101
102 xml_set_element_handler( $parser, array( &$this, 'startElement' ), array( &$this, 'endElement' ) );
103 xml_set_character_data_handler( $parser, array( &$this, 'characterData' ) );
104
105 $offset = 0; // for context extraction on error reporting
106 $bufferSize = 512 * 1024;
107 do {
108 $chunk = fread( $input, $bufferSize );
109 if( !xml_parse( $parser, $chunk, feof( $input ) ) ) {
110 wfDebug( "TextDumpPass::readDump encountered XML parsing error\n" );
111 return new WikiXmlError( $parser, 'XML import parse failure', $chunk, $offset );
112 }
113 $offset += strlen( $chunk );
114 } while( $chunk !== false && !feof( $input ) );
115 xml_parser_free( $parser );
116 }
117
118 function getText( $id ) {
119 if( isset( $this->prefetch ) ) {
120 $text = $this->prefetch->prefetch( $this->thisPage, $this->thisRev );
121 if( !is_null( $text ) )
122 return $text;
123 }
124 $id = intval( $id );
125 $row = $this->db->selectRow( 'text',
126 array( 'old_text', 'old_flags' ),
127 array( 'old_id' => $id ),
128 'TextPassDumper::getText' );
129 $text = Revision::getRevisionText( $row );
130 $stripped = str_replace( "\r", "", $text );
131 $normalized = UtfNormal::cleanUp( $stripped );
132 return $normalized;
133 }
134
135 function startElement( $parser, $name, $attribs ) {
136 $this->clearOpenElement( null );
137 $this->lastName = $name;
138
139 if( $name == 'revision' ) {
140 $this->state = $name;
141 $this->egress->writeOpenPage( null, $this->buffer );
142 $this->buffer = "";
143 } elseif( $name == 'page' ) {
144 $this->state = $name;
145 if( $this->atStart ) {
146 $this->egress->writeOpenStream( $this->buffer );
147 $this->buffer = "";
148 $this->atStart = false;
149 }
150 }
151
152 if( $name == "text" && isset( $attribs['id'] ) ) {
153 $text = $this->getText( $attribs['id'] );
154 $this->openElement = array( $name, array( 'xml:space' => 'preserve' ) );
155 if( strlen( $text ) > 0 ) {
156 $this->characterData( $parser, $text );
157 }
158 } else {
159 $this->openElement = array( $name, $attribs );
160 }
161 }
162
163 function endElement( $parser, $name ) {
164 if( $this->openElement ) {
165 $this->clearOpenElement( "" );
166 } else {
167 $this->buffer .= "</$name>";
168 }
169
170 if( $name == 'revision' ) {
171 $this->egress->writeRevision( null, $this->buffer );
172 $this->buffer = "";
173 $this->thisRev = "";
174 } elseif( $name == 'page' ) {
175 $this->egress->writeClosePage( $this->buffer );
176 $this->buffer = "";
177 $this->thisPage = "";
178 } elseif( $name == 'mediawiki' ) {
179 $this->egress->writeCloseStream( $this->buffer );
180 $this->buffer = "";
181 }
182 }
183
184 function characterData( $parser, $data ) {
185 $this->clearOpenElement( null );
186 if( $this->lastName == "id" ) {
187 if( $this->state == "revision" ) {
188 $this->thisRev .= $data;
189 } elseif( $this->state == "page" ) {
190 $this->thisPage .= $data;
191 }
192 }
193 $this->buffer .= htmlspecialchars( $data );
194 }
195
196 function clearOpenElement( $style ) {
197 if( $this->openElement ) {
198 $this->buffer .= wfElement( $this->openElement[0], $this->openElement[1], $style );
199 $this->openElement = false;
200 }
201 }
202 }
203
204
205 $dumper = new TextPassDumper( $argv );
206 if( isset( $options['server'] ) ) {
207 $dumper->server = $options['server'];
208 }
209
210 if( true ) {
211 $dumper->dump();
212 } else {
213 $dumper->progress( <<<END
214 This script postprocesses XML dumps from dumpBackup.php to add
215 page text which was stubbed out (using --stub).
216
217 XML input is accepted on stdin.
218 XML output is sent to stdout; progress reports are sent to stderr.
219
220 Usage: php dumpTextPass.php [<options>]
221 Options:
222 --stub=<type>:<file> To load a compressed stub dump instead of stdin
223 --prefetch=<type>:<file> Use a prior dump file as a text source, to save
224 pressure on the database.
225 (Requires PHP 5.0+ and the XMLReader PECL extension)
226 --quiet Don't dump status reports to stderr.
227 --report=n Report position and speed after every n pages processed.
228 (Default: 100)
229 --server=h Force reading from MySQL server h
230 END
231 );
232 }
233
234 ?>