'' instead of '-' to disable nav items
[lhc/web/wiklou.git] / includes / SpecialImport.php
1 <?php
2 # Copyright (C) 2003 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 function wfSpecialImport( $page = "" ) {
21 global $wgOut, $wgLang, $wgRequest, $wgTitle;
22
23 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
24 $importer = new WikiImporter;
25 if( $importer->setupFromUpload( "xmlimport" ) ) {
26 $importer->setRevisionHandler( "wfImportRevision" );
27 if( $importer->doImport() ) {
28 # Success!
29 $wgOut->addHTML( "<p>" . wfMsg( "importsuccess" ) . "</p>" );
30 } else {
31 $wgOut->addHTML( "<p>" . wfMsg( "importfailed",
32 htmlspecialchars( $importer->getError() ) ) . "</p>" );
33 }
34 } else {
35 $wgOut->addWikiText( htmlspecialchars( $importer->getError() ) );
36 }
37 }
38
39 $wgOut->addWikiText( "<p>" . wfMsg( "importtext" ) . "</p>" );
40 $action = $wgTitle->escapeLocalUrl();
41 $wgOut->addHTML( "
42 <form enctype='multipart/form-data' method='post' action=\"$action\">
43 <input type='hidden' name='action' value='submit' />
44 <input type='hidden' name='MAX_FILE_SIZE' value='200000' />
45 <input type='file' name='xmlimport' value='' size='40' /><br />
46 <input type='submit' value='" . htmlspecialchars( wfMsg( "uploadbtn" ) ) . "'/>
47 </form>
48 " );
49 }
50
51 function wfImportRevision( $revision ) {
52 $fname = "wfImportRevision";
53
54 # Sneak a single revision into place
55 $ns = $revision->title->getNamespace();
56 $t = wfStrencode( $revision->title->getDBkey() );
57 $text = wfStrencode( $revision->text );
58 $ts = wfStrencode( $revision->timestamp );
59 $its = wfStrencode( wfInvertTimestamp( $revision->timestamp ) ) ;
60 $minor = 0; # ??
61 $flags = "";
62
63 # Make sure it doesn't already exist
64 $res = wfQuery( "SELECT COUNT(*) FROM old WHERE old_namespace=$ns AND old_title='$t' AND old_timestamp='$ts'", DB_WRITE, $fname );
65 $numrows = wfNumRows( $res );
66 wfFreeResult( $res );
67 if( $numrows > 0 ) {
68 return "gaaah";
69 }
70
71 $res = wfQuery( "INSERT INTO old " .
72 "(old_namespace,old_title,old_text,old_comment,old_user,old_user_text," .
73 "old_timestamp,inverse_timestamp,old_minor_edit,old_flags) " .
74 "VALUES ($ns,'$t','$text','$comment',$user,'$user_text','$ts','$its',$minor,'$flags')",
75 DB_WRITE, $fname );
76
77 return true;
78 }
79
80 class WikiRevision {
81 var $title = NULL;
82 var $timestamp = "20010115000000";
83 var $user = 0;
84 var $user_text = "";
85 var $text = "";
86
87 function setTitle( $text ) {
88 $text = $this->fixEncoding( $text );
89 $this->title = Title::newFromText( $text );
90 }
91
92 function setTimestamp( $ts ) {
93 # 2003-08-05T18:30:02Z
94 $this->timestamp = preg_replace( '/^(....)-(..)-(..)T(..):(..):(..)Z$/', '$1$2$3$4$5$6', $ts );
95 }
96
97 function setUsername( $user ) {
98 $this->user_text = $this->fixEncoding( $user );
99 }
100
101 function setUserIP( $ip ) {
102 $this->user_text = $this->fixEncoding( $ip );
103 }
104
105 function setText( $text ) {
106 $this->text = $this->fixEncoding( $text );
107 }
108
109 function fixEncoding( $data ) {
110 global $wgLang, $wgInputEncoding;
111
112 if( strcasecmp( $wgInputEncoding, "utf-8" ) == 0 ) {
113 return $data;
114 } else {
115 return $wgLang->iconv( "utf-8", $wgInputEncoding, $data );
116 }
117 }
118
119 function getTitle() {
120 return $this->title;
121 }
122
123 function getTimestamp() {
124 return $this->timestamp;
125 }
126
127 function getUser() {
128 return $this->user_text;
129 }
130
131 function getText() {
132 return $this->text;
133 }
134 }
135
136 class WikiImporter {
137 var $mSource = NULL;
138 var $mError = "";
139 var $mXmlError = XML_ERROR_NONE;
140 var $mRevisionHandler = NULL;
141 var $lastfield;
142
143 function WikiImporter() {
144 $this->setRevisionHandler( array( &$this, "defaultRevisionHandler" ) );
145 }
146
147 function setError( $err ) {
148 $this->mError = $err;
149 return false;
150 }
151
152 function getError() {
153 if( $this->mXmlError == XML_ERROR_NONE ) {
154 return $this->mError;
155 } else {
156 return xml_error_string( $this->mXmlError );
157 }
158 }
159
160 function throwXmlError( $err ) {
161 $this->debug( "FAILURE: $err" );
162 }
163
164 function setupFromFile( $filename ) {
165 $this->mSource = file_get_contents( $filename );
166 return true;
167 }
168
169 function setupFromUpload( $fieldname = "xmlimport" ) {
170 global $wgOut;
171
172 $upload =& $_FILES[$fieldname];
173
174 if( !isset( $upload ) ) {
175 return $this->setError( wfMsg( "importnofile" ) );
176 }
177 if( !empty( $upload['error'] ) ) {
178 return $this->setError( wfMsg( "importuploaderror", $upload['error'] ) );
179 }
180 $fname = $upload['tmp_name'];
181 if( is_uploaded_file( $fname ) ) {
182 return $this->setupFromFile( $fname );
183 } else {
184 return $this->setError( wfMsg( "importnofile" ) );
185 }
186 }
187
188 function setupFromURL( $url ) {
189 # FIXME
190 wfDebugDieBacktrace( "Not yet implemented." );
191 }
192
193 function doImport() {
194 if( empty( $this->mSource ) ) {
195 return $this->setError( wfMsg( "importnotext" ) );
196 }
197
198 $parser = xml_parser_create( "UTF-8" );
199
200 # case folding violates XML standard, turn it off
201 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
202
203 xml_set_object( $parser, &$this );
204 xml_set_element_handler( $parser, "in_start", "" );
205
206 if( !xml_parse( $parser, $this->mSource, true ) ) {
207 # return error message
208 $this->mXmlError = xml_get_error_code( $parser );
209 xml_parser_free( $parser );
210 return false;
211 }
212 xml_parser_free( $parser );
213
214 return true;
215 }
216
217 function debug( $data ) {
218 # global $wgOut;
219 # $wgOut->addHTML( htmlspecialchars( $data ) . "<br>\n" );
220 }
221
222 function setRevisionHandler( $functionref ) {
223 $this->mRevisionHandler = $functionref;
224 }
225
226 function defaultRevisionHandler( $revision ) {
227 $this->debug( "Got revision:" );
228 if( is_object( $revision->title ) ) {
229 $this->debug( "-- Title: " . $revision->title->getPrefixedText() );
230 } else {
231 $this->debug( "-- Title: <invalid>" );
232 }
233 $this->debug( "-- User: " . $revision->user_text );
234 $this->debug( "-- Timestamp: " . $revision->timestamp );
235 $this->debug( "-- Text: " . $revision->text );
236 }
237
238
239
240 # XML parser callbacks from here out -- beware!
241 function donothing( $parser, $x, $y="" ) {
242 #$this->debug( "donothing" );
243 }
244
245 function in_start( $parser, $name, $attribs ) {
246 $this->debug( "in_start $name" );
247 if( $name != "mediawiki" ) {
248 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
249 }
250 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
251 }
252
253 function in_mediawiki( $parser, $name, $attribs ) {
254 $this->debug( "in_mediawiki $name" );
255 if( $name != "page" ) {
256 return $this->throwXMLerror( "Expected <page>, got <$name>" );
257 }
258 xml_set_element_handler( $parser, "in_page", "out_page" );
259 }
260 function out_mediawiki( $parser, $name ) {
261 $this->debug( "out_mediawiki $name" );
262 if( $name != "mediawiki" ) {
263 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
264 }
265 xml_set_element_handler( $parser, "donothing", "donothing" );
266 }
267
268 function in_page( $parser, $name, $attribs ) {
269 $this->debug( "in_page $name" );
270 switch( $name ) {
271 case "id":
272 case "title":
273 case "restrictions":
274 $this->appendfield = $name;
275 $this->parenttag = "page";
276 xml_set_element_handler( $parser, "in_nothing", "out_append" );
277 xml_set_character_data_handler( $parser, "char_append" );
278 break;
279 case "revision":
280 $this->workRevision = new WikiRevision;
281 $this->workRevision->setTitle( $this->workTitle );
282 xml_set_element_handler( $parser, "in_revision", "out_revision" );
283 break;
284 default:
285 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
286 }
287 }
288
289 function out_page( $parser, $name ) {
290 $this->debug( "out_page $name" );
291 if( $name != "page" ) {
292 return $this->throwXMLerror( "Expected </page>, got </$name>" );
293 }
294 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
295
296 $this->workTitle = NULL;
297 $this->workRevision = NULL;
298 }
299
300 function in_nothing( $parser, $name, $attribs ) {
301 $this->debug( "in_nothing $name" );
302 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
303 }
304 function char_append( $parser, $data ) {
305 $this->debug( "char_append '$data'" );
306 $this->appenddata .= $data;
307 }
308 function out_append( $parser, $name ) {
309 $this->debug( "out_append $name" );
310 if( $name != $this->appendfield ) {
311 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
312 }
313 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
314 xml_set_character_data_handler( $parser, "donothing" );
315 switch( $this->appendfield ) {
316 case "title":
317 $this->workTitle = $this->appenddata;
318 break;
319 case "text":
320 $this->workRevision->setText( $this->appenddata );
321 break;
322 case "username":
323 $this->workRevision->setUsername( $this->appenddata );
324 break;
325 case "ip":
326 $this->workRevision->setUserIP( $this->appenddata );
327 break;
328 case "timestamp":
329 $this->workRevision->setTimestamp( $this->appenddata );
330 break;
331 default;
332 $this->debug( "Bad append: {$this->appendfield}" );
333 }
334 $this->appendfield = "";
335 $this->appenddata = "";
336 }
337
338 function in_revision( $parser, $name, $attribs ) {
339 $this->debug( "in_revision $name" );
340 switch( $name ) {
341 case "id":
342 case "timestamp":
343 case "comment":
344 case "text":
345 $this->parenttag = "revision";
346 $this->appendfield = $name;
347 xml_set_element_handler( $parser, "in_nothing", "out_append" );
348 xml_set_character_data_handler( $parser, "char_append" );
349 break;
350 case "contributor":
351 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
352 break;
353 default:
354 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
355 }
356 }
357 function out_revision( $parser, $name ) {
358 $this->debug( "out_revision $name" );
359 if( $name != "revision" ) {
360 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
361 }
362 xml_set_element_handler( $parser, "in_page", "out_page" );
363
364 call_user_func( $this->mRevisionHandler, $this->workRevision );
365 }
366
367 function in_contributor( $parser, $name, $attribs ) {
368 $this->debug( "in_contributor $name" );
369 switch( $name ) {
370 case "username":
371 case "ip":
372 $this->parenttag = "contributor";
373 $this->appendfield = $name;
374 xml_set_element_handler( $parser, "in_nothing", "out_append" );
375 xml_set_character_data_handler( $parser, "char_append" );
376 break;
377 default:
378 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
379 }
380 }
381
382 function out_contributor( $parser, $name ) {
383 $this->debug( "out_contributor $name" );
384 if( $name != "contributor" ) {
385 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
386 }
387 xml_set_element_handler( $parser, "in_revision", "out_revision" );
388 }
389 }
390
391
392 ?>