Moved the bulk of dbsource() to Database.php. Added support for updating wikis with...
[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 wfDie( $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 return true;
118 }
119
120 function getText( $id ) {
121 if( isset( $this->prefetch ) ) {
122 $text = $this->prefetch->prefetch( $this->thisPage, $this->thisRev );
123 if( !is_null( $text ) )
124 return $text;
125 }
126 $id = intval( $id );
127 $row = $this->db->selectRow( 'text',
128 array( 'old_text', 'old_flags' ),
129 array( 'old_id' => $id ),
130 'TextPassDumper::getText' );
131 $text = Revision::getRevisionText( $row );
132 $stripped = str_replace( "\r", "", $text );
133 $normalized = UtfNormal::cleanUp( $stripped );
134 return $normalized;
135 }
136
137 function startElement( $parser, $name, $attribs ) {
138 $this->clearOpenElement( null );
139 $this->lastName = $name;
140
141 if( $name == 'revision' ) {
142 $this->state = $name;
143 $this->egress->writeOpenPage( null, $this->buffer );
144 $this->buffer = "";
145 } elseif( $name == 'page' ) {
146 $this->state = $name;
147 if( $this->atStart ) {
148 $this->egress->writeOpenStream( $this->buffer );
149 $this->buffer = "";
150 $this->atStart = false;
151 }
152 }
153
154 if( $name == "text" && isset( $attribs['id'] ) ) {
155 $text = $this->getText( $attribs['id'] );
156 $this->openElement = array( $name, array( 'xml:space' => 'preserve' ) );
157 if( strlen( $text ) > 0 ) {
158 $this->characterData( $parser, $text );
159 }
160 } else {
161 $this->openElement = array( $name, $attribs );
162 }
163 }
164
165 function endElement( $parser, $name ) {
166 if( $this->openElement ) {
167 $this->clearOpenElement( "" );
168 } else {
169 $this->buffer .= "</$name>";
170 }
171
172 if( $name == 'revision' ) {
173 $this->egress->writeRevision( null, $this->buffer );
174 $this->buffer = "";
175 $this->thisRev = "";
176 } elseif( $name == 'page' ) {
177 $this->egress->writeClosePage( $this->buffer );
178 $this->buffer = "";
179 $this->thisPage = "";
180 } elseif( $name == 'mediawiki' ) {
181 $this->egress->writeCloseStream( $this->buffer );
182 $this->buffer = "";
183 }
184 }
185
186 function characterData( $parser, $data ) {
187 $this->clearOpenElement( null );
188 if( $this->lastName == "id" ) {
189 if( $this->state == "revision" ) {
190 $this->thisRev .= $data;
191 } elseif( $this->state == "page" ) {
192 $this->thisPage .= $data;
193 }
194 }
195 $this->buffer .= htmlspecialchars( $data );
196 }
197
198 function clearOpenElement( $style ) {
199 if( $this->openElement ) {
200 $this->buffer .= wfElement( $this->openElement[0], $this->openElement[1], $style );
201 $this->openElement = false;
202 }
203 }
204 }
205
206
207 $dumper = new TextPassDumper( $argv );
208 if( isset( $options['server'] ) ) {
209 $dumper->server = $options['server'];
210 }
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 END
233 );
234 }
235
236 ?>