fix error display
[lhc/web/wiklou.git] / includes / SpecialImport.php
1 <?php
2 /**
3 * MediaWiki page data importer
4 * Copyright (C) 2003,2005 Brion Vibber <brion@pobox.com>
5 * http://www.mediawiki.org/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @package MediaWiki
23 * @subpackage SpecialPage
24 */
25
26 /** */
27 require_once( 'WikiError.php' );
28
29 /**
30 * Constructor
31 */
32 function wfSpecialImport( $page = '' ) {
33 global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources;
34
35 ###
36 # $wgOut->addWikiText( "Special:Import is not ready for this beta release, sorry." );
37 # return;
38 ###
39
40 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
41 switch( $wgRequest->getVal( "source" ) ) {
42 case "upload":
43 if( $wgUser->isAllowed( 'importupload' ) ) {
44 $source = ImportStreamSource::newFromUpload( "xmlimport" );
45 } else {
46 return $wgOut->permissionRequired( 'importupload' );
47 }
48 break;
49 case "interwiki":
50 $source = ImportStreamSource::newFromInterwiki(
51 $wgRequest->getVal( "interwiki" ),
52 $wgRequest->getText( "frompage" ) );
53 break;
54 default:
55 $source = new WikiError( "Unknown import source type" );
56 }
57
58 if( WikiError::isError( $source ) ) {
59 $wgOut->addWikiText( wfEscapeWikiText( $source->getMessage() ) );
60 } else {
61 $importer = new WikiImporter( $source );
62 $result = $importer->doImport();
63 if( WikiError::isError( $result ) ) {
64 $wgOut->addWikiText( wfMsg( "importfailed",
65 wfEscapeWikiText( $result->getMessage() ) ) );
66 } else {
67 # Success!
68 $wgOut->addWikiText( wfMsg( "importsuccess" ) );
69 }
70 }
71 }
72
73 $action = $wgTitle->escapeLocalUrl( 'action=submit' );
74
75 if( $wgUser->isAllowed( 'importupload' ) ) {
76 $wgOut->addWikiText( wfMsg( "importtext" ) );
77 $wgOut->addHTML( "
78 <fieldset>
79 <legend>" . wfMsgHtml('upload') . "</legend>
80 <form enctype='multipart/form-data' method='post' action=\"$action\">
81 <input type='hidden' name='action' value='submit' />
82 <input type='hidden' name='source' value='upload' />
83 <input type='hidden' name='MAX_FILE_SIZE' value='2000000' />
84 <input type='file' name='xmlimport' value='' size='30' />
85 <input type='submit' value=\"" . wfMsgHtml( "uploadbtn" ) . "\" />
86 </form>
87 </fieldset>
88 " );
89 } else {
90 if( empty( $wgImportSources ) ) {
91 $wgOut->addWikiText( wfMsg( 'importnosources' ) );
92 }
93 }
94
95 if( !empty( $wgImportSources ) ) {
96 $wgOut->addHTML( "
97 <fieldset>
98 <legend>" . wfMsgHtml('importinterwiki') . "</legend>
99 <form method='post' action=\"$action\">
100 <input type='hidden' name='action' value='submit' />
101 <input type='hidden' name='source' value='interwiki' />
102 <select name='interwiki'>
103 " );
104 foreach( $wgImportSources as $interwiki ) {
105 $iw = htmlspecialchars( $interwiki );
106 $wgOut->addHTML( "<option value=\"$iw\">$iw</option>\n" );
107 }
108 $wgOut->addHTML( "
109 </select>
110 <input name='frompage' />
111 <input type='submit' />
112 </form>
113 </fieldset>
114 " );
115 }
116 }
117
118 /**
119 *
120 * @package MediaWiki
121 * @subpackage SpecialPage
122 */
123 class WikiRevision {
124 var $title = NULL;
125 var $id = 0;
126 var $timestamp = "20010115000000";
127 var $user = 0;
128 var $user_text = "";
129 var $text = "";
130 var $comment = "";
131 var $minor = false;
132
133 function setTitle( $text ) {
134 $this->title = Title::newFromText( $text );
135 }
136
137 function setID( $id ) {
138 $this->id = $id;
139 }
140
141 function setTimestamp( $ts ) {
142 # 2003-08-05T18:30:02Z
143 $this->timestamp = wfTimestamp( TS_MW, $ts );
144 }
145
146 function setUsername( $user ) {
147 $this->user_text = $user;
148 }
149
150 function setUserIP( $ip ) {
151 $this->user_text = $ip;
152 }
153
154 function setText( $text ) {
155 $this->text = $text;
156 }
157
158 function setComment( $text ) {
159 $this->comment = $text;
160 }
161
162 function setMinor( $minor ) {
163 $this->minor = (bool)$minor;
164 }
165
166 function getTitle() {
167 return $this->title;
168 }
169
170 function getID() {
171 return $this->id;
172 }
173
174 function getTimestamp() {
175 return $this->timestamp;
176 }
177
178 function getUser() {
179 return $this->user_text;
180 }
181
182 function getText() {
183 return $this->text;
184 }
185
186 function getComment() {
187 return $this->comment;
188 }
189
190 function getMinor() {
191 return $this->minor;
192 }
193
194 function importOldRevision() {
195 $fname = "WikiImporter::importOldRevision";
196 $dbw =& wfGetDB( DB_MASTER );
197
198 # Sneak a single revision into place
199 $user = User::newFromName( $this->getUser() );
200 if( $user ) {
201 $userId = intval( $user->getId() );
202 $userText = $user->getName();
203 } else {
204 $userId = 0;
205 $userText = $this->getUser();
206 }
207
208 // avoid memory leak...?
209 $linkCache =& LinkCache::singleton();
210 $linkCache->clear();
211
212 $article = new Article( $this->title );
213 $pageId = $article->getId();
214 if( $pageId == 0 ) {
215 # must create the page...
216 $pageId = $article->insertOn( $dbw );
217 }
218
219 # FIXME: Check for exact conflicts
220 # FIXME: Use original rev_id optionally
221 # FIXME: blah blah blah
222
223 #if( $numrows > 0 ) {
224 # return wfMsg( "importhistoryconflict" );
225 #}
226
227 # Insert the row
228 $revision = new Revision( array(
229 'page' => $pageId,
230 'text' => $this->getText(),
231 'comment' => $this->getComment(),
232 'user' => $userId,
233 'user_text' => $userText,
234 'timestamp' => $this->timestamp,
235 'minor_edit' => $this->minor,
236 ) );
237 $revId = $revision->insertOn( $dbw );
238 $article->updateIfNewerOn( $dbw, $revision );
239
240 return true;
241 }
242
243 }
244
245 /**
246 *
247 * @package MediaWiki
248 * @subpackage SpecialPage
249 */
250 class WikiImporter {
251 var $mSource = null;
252 var $mPageCallback = null;
253 var $mRevisionCallback = null;
254 var $lastfield;
255
256 function WikiImporter( $source ) {
257 $this->setRevisionCallback( array( &$this, "importRevision" ) );
258 $this->mSource = $source;
259 }
260
261 function throwXmlError( $err ) {
262 $this->debug( "FAILURE: $err" );
263 wfDebug( "WikiImporter XML error: $err\n" );
264 }
265
266 # --------------
267
268 function doImport() {
269 if( empty( $this->mSource ) ) {
270 return new WikiErrorMsg( "importnotext" );
271 }
272
273 $parser = xml_parser_create( "UTF-8" );
274
275 # case folding violates XML standard, turn it off
276 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
277
278 xml_set_object( $parser, $this );
279 xml_set_element_handler( $parser, "in_start", "" );
280
281 $offset = 0; // for context extraction on error reporting
282 do {
283 $chunk = $this->mSource->readChunk();
284 if( !xml_parse( $parser, $chunk, $this->mSource->atEnd() ) ) {
285 wfDebug( "WikiImporter::doImport encountered XML parsing error\n" );
286 return new WikiXmlError( $parser, 'XML import parse failure', $chunk, $offset );
287 }
288 $offset += strlen( $chunk );
289 } while( $chunk !== false && !$this->mSource->atEnd() );
290 xml_parser_free( $parser );
291
292 return true;
293 }
294
295 function debug( $data ) {
296 #wfDebug( "IMPORT: $data\n" );
297 }
298
299 function notice( $data ) {
300 global $wgCommandLineMode;
301 if( $wgCommandLineMode ) {
302 print "$data\n";
303 } else {
304 global $wgOut;
305 $wgOut->addHTML( "<li>$data</li>\n" );
306 }
307 }
308
309 /**
310 * Sets the action to perform as each new page in the stream is reached.
311 * @param callable $callback
312 * @return callable
313 */
314 function setPageCallback( $callback ) {
315 $previous = $this->mPageCallback;
316 $this->mPageCallback = $callback;
317 return $previous;
318 }
319
320 /**
321 * Sets the action to perform as each page revision is reached.
322 * @param callable $callback
323 * @return callable
324 */
325 function setRevisionCallback( $callback ) {
326 $previous = $this->mRevisionCallback;
327 $this->mRevisionCallback = $callback;
328 return $previous;
329 }
330
331 /**
332 * Default per-revision callback, performs the import.
333 * @param WikiRevision $revision
334 * @access private
335 */
336 function importRevision( &$revision ) {
337 $dbw =& wfGetDB( DB_MASTER );
338 $dbw->deadlockLoop( array( &$revision, 'importOldRevision' ) );
339 }
340
341 /**
342 * Alternate per-revision callback, for debugging.
343 * @param WikiRevision $revision
344 * @access private
345 */
346 function debugRevisionHandler( &$revision ) {
347 $this->debug( "Got revision:" );
348 if( is_object( $revision->title ) ) {
349 $this->debug( "-- Title: " . $revision->title->getPrefixedText() );
350 } else {
351 $this->debug( "-- Title: <invalid>" );
352 }
353 $this->debug( "-- User: " . $revision->user_text );
354 $this->debug( "-- Timestamp: " . $revision->timestamp );
355 $this->debug( "-- Comment: " . $revision->comment );
356 $this->debug( "-- Text: " . $revision->text );
357 }
358
359 /**
360 * Notify the callback function when a new <page> is reached.
361 * @param Title $title
362 * @access private
363 */
364 function pageCallback( $title ) {
365 if( is_callable( $this->mPageCallback ) ) {
366 call_user_func( $this->mPageCallback, $title );
367 }
368 }
369
370
371 # XML parser callbacks from here out -- beware!
372 function donothing( $parser, $x, $y="" ) {
373 #$this->debug( "donothing" );
374 }
375
376 function in_start( $parser, $name, $attribs ) {
377 $this->debug( "in_start $name" );
378 if( $name != "mediawiki" ) {
379 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
380 }
381 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
382 }
383
384 function in_mediawiki( $parser, $name, $attribs ) {
385 $this->debug( "in_mediawiki $name" );
386 if( $name == 'siteinfo' ) {
387 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
388 } elseif( $name == 'page' ) {
389 xml_set_element_handler( $parser, "in_page", "out_page" );
390 } else {
391 return $this->throwXMLerror( "Expected <page>, got <$name>" );
392 }
393 }
394 function out_mediawiki( $parser, $name ) {
395 $this->debug( "out_mediawiki $name" );
396 if( $name != "mediawiki" ) {
397 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
398 }
399 xml_set_element_handler( $parser, "donothing", "donothing" );
400 }
401
402
403 function in_siteinfo( $parser, $name, $attribs ) {
404 // no-ops for now
405 $this->debug( "in_siteinfo $name" );
406 switch( $name ) {
407 case "sitename":
408 case "base":
409 case "generator":
410 case "case":
411 case "namespaces":
412 case "namespace":
413 break;
414 default:
415 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
416 }
417 }
418
419 function out_siteinfo( $parser, $name ) {
420 if( $name == "siteinfo" ) {
421 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
422 }
423 }
424
425
426 function in_page( $parser, $name, $attribs ) {
427 $this->debug( "in_page $name" );
428 switch( $name ) {
429 case "id":
430 case "title":
431 case "restrictions":
432 $this->appendfield = $name;
433 $this->appenddata = "";
434 $this->parenttag = "page";
435 xml_set_element_handler( $parser, "in_nothing", "out_append" );
436 xml_set_character_data_handler( $parser, "char_append" );
437 break;
438 case "revision":
439 $this->workRevision = new WikiRevision;
440 $this->workRevision->setTitle( $this->workTitle );
441 xml_set_element_handler( $parser, "in_revision", "out_revision" );
442 break;
443 default:
444 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
445 }
446 }
447
448 function out_page( $parser, $name ) {
449 $this->debug( "out_page $name" );
450 if( $name != "page" ) {
451 return $this->throwXMLerror( "Expected </page>, got </$name>" );
452 }
453 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
454
455 $this->workTitle = NULL;
456 $this->workRevision = NULL;
457 }
458
459 function in_nothing( $parser, $name, $attribs ) {
460 $this->debug( "in_nothing $name" );
461 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
462 }
463 function char_append( $parser, $data ) {
464 $this->debug( "char_append '$data'" );
465 $this->appenddata .= $data;
466 }
467 function out_append( $parser, $name ) {
468 $this->debug( "out_append $name" );
469 if( $name != $this->appendfield ) {
470 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
471 }
472 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
473 xml_set_character_data_handler( $parser, "donothing" );
474
475 switch( $this->appendfield ) {
476 case "title":
477 $this->workTitle = $this->appenddata;
478 $this->pageCallback( $this->workTitle );
479 break;
480 case "id":
481 if ( $this->parenttag == 'revision' ) {
482 $this->workRevision->setID( $this->appenddata );
483 }
484 break;
485 case "text":
486 $this->workRevision->setText( $this->appenddata );
487 break;
488 case "username":
489 $this->workRevision->setUsername( $this->appenddata );
490 break;
491 case "ip":
492 $this->workRevision->setUserIP( $this->appenddata );
493 break;
494 case "timestamp":
495 $this->workRevision->setTimestamp( $this->appenddata );
496 break;
497 case "comment":
498 $this->workRevision->setComment( $this->appenddata );
499 break;
500 case "minor":
501 $this->workRevision->setMinor( true );
502 break;
503 default:
504 $this->debug( "Bad append: {$this->appendfield}" );
505 }
506 $this->appendfield = "";
507 $this->appenddata = "";
508 }
509
510 function in_revision( $parser, $name, $attribs ) {
511 $this->debug( "in_revision $name" );
512 switch( $name ) {
513 case "id":
514 case "timestamp":
515 case "comment":
516 case "minor":
517 case "text":
518 $this->parenttag = "revision";
519 $this->appendfield = $name;
520 xml_set_element_handler( $parser, "in_nothing", "out_append" );
521 xml_set_character_data_handler( $parser, "char_append" );
522 break;
523 case "contributor":
524 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
525 break;
526 default:
527 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
528 }
529 }
530
531 function out_revision( $parser, $name ) {
532 $this->debug( "out_revision $name" );
533 if( $name != "revision" ) {
534 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
535 }
536 xml_set_element_handler( $parser, "in_page", "out_page" );
537
538 $out = call_user_func_array( $this->mRevisionCallback,
539 array( &$this->workRevision, &$this ) );
540 if( !empty( $out ) ) {
541 global $wgOut;
542 $wgOut->addHTML( "<li>" . $out . "</li>\n" );
543 }
544 }
545
546 function in_contributor( $parser, $name, $attribs ) {
547 $this->debug( "in_contributor $name" );
548 switch( $name ) {
549 case "username":
550 case "ip":
551 case "id":
552 $this->parenttag = "contributor";
553 $this->appendfield = $name;
554 xml_set_element_handler( $parser, "in_nothing", "out_append" );
555 xml_set_character_data_handler( $parser, "char_append" );
556 break;
557 default:
558 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
559 }
560 }
561
562 function out_contributor( $parser, $name ) {
563 $this->debug( "out_contributor $name" );
564 if( $name != "contributor" ) {
565 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
566 }
567 xml_set_element_handler( $parser, "in_revision", "out_revision" );
568 }
569
570 }
571
572 /** @package MediaWiki */
573 class ImportStringSource {
574 function ImportStringSource( $string ) {
575 $this->mString = $string;
576 $this->mRead = false;
577 }
578
579 function atEnd() {
580 return $this->mRead;
581 }
582
583 function readChunk() {
584 if( $this->atEnd() ) {
585 return false;
586 } else {
587 $this->mRead = true;
588 return $this->mString;
589 }
590 }
591 }
592
593 /** @package MediaWiki */
594 class ImportStreamSource {
595 function ImportStreamSource( $handle ) {
596 $this->mHandle = $handle;
597 }
598
599 function atEnd() {
600 return feof( $this->mHandle );
601 }
602
603 function readChunk() {
604 return fread( $this->mHandle, 32768 );
605 }
606
607 function newFromFile( $filename ) {
608 $file = @fopen( $filename, 'rt' );
609 if( !$file ) {
610 return new WikiError( "Couldn't open import file" );
611 }
612 return new ImportStreamSource( $file );
613 }
614
615 function newFromUpload( $fieldname = "xmlimport" ) {
616 $upload =& $_FILES[$fieldname];
617
618 if( !isset( $upload ) || !$upload['name'] ) {
619 return new WikiErrorMsg( 'importnofile' );
620 }
621 if( !empty( $upload['error'] ) ) {
622 return new WikiErrorMsg( 'importuploaderror', $upload['error'] );
623 }
624 $fname = $upload['tmp_name'];
625 if( is_uploaded_file( $fname ) ) {
626 return ImportStreamSource::newFromFile( $fname );
627 } else {
628 return new WikiErrorMsg( 'importnofile' );
629 }
630 }
631
632 function newFromURL( $url ) {
633 # fopen-wrappers are normally turned off for security.
634 ini_set( "allow_url_fopen", true );
635 $ret = ImportStreamSource::newFromFile( $url );
636 ini_set( "allow_url_fopen", false );
637 return $ret;
638 }
639
640 function newFromInterwiki( $interwiki, $page ) {
641 $base = Title::getInterwikiLink( $interwiki );
642 if( empty( $base ) ) {
643 return new WikiError( 'Bad interwiki link' );
644 } else {
645 $import = wfUrlencode( "Special:Export/$page" );
646 $url = str_replace( "$1", $import, $base );
647 return ImportStreamSource::newFromURL( $url );
648 }
649 }
650 }
651
652
653 ?>