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