* Add target namespace override selector for transwiki imports.
[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 * Constructor
28 */
29 function wfSpecialImport( $page = '' ) {
30 global $wgUser, $wgOut, $wgRequest, $wgTitle, $wgImportSources;
31 global $wgImportTargetNamespace;
32
33 $interwiki = false;
34 $namespace = $wgImportTargetNamespace;
35 $frompage = '';
36 $history = true;
37
38 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'action' ) == 'submit') {
39 $isUpload = false;
40 $namespace = $wgRequest->getIntOrNull( 'namespace' );
41
42 switch( $wgRequest->getVal( "source" ) ) {
43 case "upload":
44 $isUpload = true;
45 if( $wgUser->isAllowed( 'importupload' ) ) {
46 $source = ImportStreamSource::newFromUpload( "xmlimport" );
47 } else {
48 return $wgOut->permissionRequired( 'importupload' );
49 }
50 break;
51 case "interwiki":
52 $interwiki = $wgRequest->getVal( 'interwiki' );
53 $history = $wgRequest->getCheck( 'interwikiHistory' );
54 $frompage = $wgRequest->getText( "frompage" );
55 $source = ImportStreamSource::newFromInterwiki(
56 $interwiki,
57 $frompage,
58 $history );
59 break;
60 default:
61 $source = new WikiErrorMsg( "importunknownsource" );
62 }
63
64 if( WikiError::isError( $source ) ) {
65 $wgOut->addWikiText( wfEscapeWikiText( $source->getMessage() ) );
66 } else {
67 $wgOut->addWikiText( wfMsg( "importstart" ) );
68
69 $importer = new WikiImporter( $source );
70 if( !is_null( $namespace ) ) {
71 $importer->setTargetNamespace( $namespace );
72 }
73 $reporter = new ImportReporter( $importer, $isUpload, $interwiki );
74
75 $reporter->open();
76 $result = $importer->doImport();
77 $reporter->close();
78
79 if( WikiError::isError( $result ) ) {
80 $wgOut->addWikiText( wfMsg( "importfailed",
81 wfEscapeWikiText( $result->getMessage() ) ) );
82 } else {
83 # Success!
84 $wgOut->addWikiText( wfMsg( "importsuccess" ) );
85 }
86 }
87 }
88
89 $action = $wgTitle->escapeLocalUrl( 'action=submit' );
90
91 if( $wgUser->isAllowed( 'importupload' ) ) {
92 $wgOut->addWikiText( wfMsg( "importtext" ) );
93 $wgOut->addHTML( "
94 <fieldset>
95 <legend>" . wfMsgHtml('upload') . "</legend>
96 <form enctype='multipart/form-data' method='post' action=\"$action\">
97 <input type='hidden' name='action' value='submit' />
98 <input type='hidden' name='source' value='upload' />
99 <input type='hidden' name='MAX_FILE_SIZE' value='2000000' />
100 <input type='file' name='xmlimport' value='' size='30' />
101 <input type='submit' value=\"" . wfMsgHtml( "uploadbtn" ) . "\" />
102 </form>
103 </fieldset>
104 " );
105 } else {
106 if( empty( $wgImportSources ) ) {
107 $wgOut->addWikiText( wfMsg( 'importnosources' ) );
108 }
109 }
110
111 if( !empty( $wgImportSources ) ) {
112 $wgOut->addHTML( "
113 <fieldset>
114 <legend>" . wfMsgHtml('importinterwiki') . "</legend>
115 <form method='post' action=\"$action\">" .
116 $wgOut->parse( wfMsg( 'import-interwiki-text' ) ) . "
117 <input type='hidden' name='action' value='submit' />
118 <input type='hidden' name='source' value='interwiki' />
119 <table>
120 <tr>
121 <td>
122 <select name='interwiki'>" );
123 foreach( $wgImportSources as $prefix ) {
124 $iw = htmlspecialchars( $prefix );
125 $selected = ($interwiki === $prefix) ? ' selected="selected"' : '';
126 $wgOut->addHTML( "<option value=\"$iw\"$selected>$iw</option>\n" );
127 }
128 $wgOut->addHTML( "
129 </select>
130 </td>
131 <td>" .
132 wfInput( 'frompage', 50, $frompage ) .
133 "</td>
134 </tr>
135 <tr>
136 <td></td>
137 <td>" .
138 wfCheckLabel( wfMsg( 'import-interwiki-history' ),
139 'interwikiHistory', 'interwikiHistory', $history ) .
140 "</td>
141 </tr>
142 <tr>
143 <td></td>
144 <td>
145 Transfer pages into namespace: " .
146 HTMLnamespaceselector( $namespace, '' ) . "
147 </td>
148 </tr>
149 <tr>
150 <td></td>
151 <td>" .
152 wfSubmitButton( wfMsg( 'import-interwiki-submit' ) ) .
153 "</td>
154 </tr>
155 </table>
156 </form>
157 </fieldset>
158 " );
159 }
160 }
161
162 /**
163 * Reporting callback
164 */
165 class ImportReporter {
166 function __construct( $importer, $upload, $interwiki ) {
167 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
168 $this->mPageCount = 0;
169 $this->mIsUpload = $upload;
170 $this->mInterwiki = $interwiki;
171 }
172
173 function open() {
174 global $wgOut;
175 $wgOut->addHtml( "<ul>\n" );
176 }
177
178 function reportPage( $title, $origTitle, $revisionCount ) {
179 global $wgOut, $wgUser, $wgLang, $wgContLang;
180
181 $skin = $wgUser->getSkin();
182
183 $this->mPageCount++;
184
185 $localCount = $wgLang->formatNum( $revisionCount );
186 $contentCount = $wgContLang->formatNum( $revisionCount );
187
188 $wgOut->addHtml( "<li>" . $skin->makeKnownLinkObj( $title ) .
189 " " .
190 wfMsgHtml( 'import-revision-count', $localCount ) .
191 "</li>\n" );
192
193 $log = new LogPage( 'import' );
194 if( $this->mIsUpload ) {
195 $detail = wfMsgForContent( 'import-logentry-upload-detail',
196 $contentCount );
197 $log->addEntry( 'upload', $title, $detail );
198 } else {
199 $interwiki = '[[:' . $this->mInterwiki . ':' .
200 $origTitle->getPrefixedText() . ']]';
201 $detail = wfMsgForContent( 'import-logentry-interwiki-detail',
202 $contentCount, $interwiki );
203 $log->addEntry( 'interwiki', $title, $detail );
204 }
205
206 $comment = $detail; // quick
207 $dbw = wfGetDB( DB_MASTER );
208 $nullRevision = Revision::newNullRevision(
209 $dbw, $title->getArticleId(), $comment, true );
210 $nullRevId = $nullRevision->insertOn( $dbw );
211 }
212
213 function close() {
214 global $wgOut;
215 if( $this->mPageCount == 0 ) {
216 $wgOut->addHtml( "<li>" . wfMsgHtml( 'importnopages' ) . "</li>\n" );
217 }
218 $wgOut->addHtml( "</ul>\n" );
219 }
220 }
221
222 /**
223 *
224 * @package MediaWiki
225 * @subpackage SpecialPage
226 */
227 class WikiRevision {
228 var $title = null;
229 var $id = 0;
230 var $timestamp = "20010115000000";
231 var $user = 0;
232 var $user_text = "";
233 var $text = "";
234 var $comment = "";
235 var $minor = false;
236
237 function setTitle( $title ) {
238 if( is_object( $title ) ) {
239 $this->title = $title;
240 } elseif( is_null( $title ) ) {
241 throw new MWException( "WikiRevision given a null title in import." );
242 } else {
243 throw new MWException( "WikiRevision given non-object title in import." );
244 }
245 }
246
247 function setID( $id ) {
248 $this->id = $id;
249 }
250
251 function setTimestamp( $ts ) {
252 # 2003-08-05T18:30:02Z
253 $this->timestamp = wfTimestamp( TS_MW, $ts );
254 }
255
256 function setUsername( $user ) {
257 $this->user_text = $user;
258 }
259
260 function setUserIP( $ip ) {
261 $this->user_text = $ip;
262 }
263
264 function setText( $text ) {
265 $this->text = $text;
266 }
267
268 function setComment( $text ) {
269 $this->comment = $text;
270 }
271
272 function setMinor( $minor ) {
273 $this->minor = (bool)$minor;
274 }
275
276 function getTitle() {
277 return $this->title;
278 }
279
280 function getID() {
281 return $this->id;
282 }
283
284 function getTimestamp() {
285 return $this->timestamp;
286 }
287
288 function getUser() {
289 return $this->user_text;
290 }
291
292 function getText() {
293 return $this->text;
294 }
295
296 function getComment() {
297 return $this->comment;
298 }
299
300 function getMinor() {
301 return $this->minor;
302 }
303
304 function importOldRevision() {
305 $fname = "WikiImporter::importOldRevision";
306 $dbw =& wfGetDB( DB_MASTER );
307
308 # Sneak a single revision into place
309 $user = User::newFromName( $this->getUser() );
310 if( $user ) {
311 $userId = intval( $user->getId() );
312 $userText = $user->getName();
313 } else {
314 $userId = 0;
315 $userText = $this->getUser();
316 }
317
318 // avoid memory leak...?
319 $linkCache =& LinkCache::singleton();
320 $linkCache->clear();
321
322 $article = new Article( $this->title );
323 $pageId = $article->getId();
324 if( $pageId == 0 ) {
325 # must create the page...
326 $pageId = $article->insertOn( $dbw );
327 $created = true;
328 } else {
329 $created = false;
330 }
331
332 # FIXME: Check for exact conflicts
333 # FIXME: Use original rev_id optionally
334 # FIXME: blah blah blah
335
336 #if( $numrows > 0 ) {
337 # return wfMsg( "importhistoryconflict" );
338 #}
339
340 # Insert the row
341 $revision = new Revision( array(
342 'page' => $pageId,
343 'text' => $this->getText(),
344 'comment' => $this->getComment(),
345 'user' => $userId,
346 'user_text' => $userText,
347 'timestamp' => $this->timestamp,
348 'minor_edit' => $this->minor,
349 ) );
350 $revId = $revision->insertOn( $dbw );
351 $changed = $article->updateIfNewerOn( $dbw, $revision );
352
353 if( $created ) {
354 wfDebug( __METHOD__ . ": running onArticleCreate\n" );
355 Article::onArticleCreate( $this->title );
356 } else {
357 if( $changed ) {
358 wfDebug( __METHOD__ . ": running onArticleEdit\n" );
359 Article::onArticleEdit( $this->title );
360 }
361 }
362 if( $created || $changed ) {
363 wfDebug( __METHOD__ . ": running edit updates\n" );
364 $article->editUpdates(
365 $this->getText(),
366 $this->getComment(),
367 $this->minor,
368 $this->timestamp,
369 $revId );
370 }
371
372 return true;
373 }
374
375 }
376
377 /**
378 *
379 * @package MediaWiki
380 * @subpackage SpecialPage
381 */
382 class WikiImporter {
383 var $mSource = null;
384 var $mPageCallback = null;
385 var $mPageOutCallback = null;
386 var $mRevisionCallback = null;
387 var $mTargetNamespace = null;
388 var $lastfield;
389
390 function WikiImporter( $source ) {
391 $this->setRevisionCallback( array( &$this, "importRevision" ) );
392 $this->mSource = $source;
393 }
394
395 function throwXmlError( $err ) {
396 $this->debug( "FAILURE: $err" );
397 wfDebug( "WikiImporter XML error: $err\n" );
398 }
399
400 # --------------
401
402 function doImport() {
403 if( empty( $this->mSource ) ) {
404 return new WikiErrorMsg( "importnotext" );
405 }
406
407 $parser = xml_parser_create( "UTF-8" );
408
409 # case folding violates XML standard, turn it off
410 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
411
412 xml_set_object( $parser, $this );
413 xml_set_element_handler( $parser, "in_start", "" );
414
415 $offset = 0; // for context extraction on error reporting
416 do {
417 $chunk = $this->mSource->readChunk();
418 if( !xml_parse( $parser, $chunk, $this->mSource->atEnd() ) ) {
419 wfDebug( "WikiImporter::doImport encountered XML parsing error\n" );
420 return new WikiXmlError( $parser, 'XML import parse failure', $chunk, $offset );
421 }
422 $offset += strlen( $chunk );
423 } while( $chunk !== false && !$this->mSource->atEnd() );
424 xml_parser_free( $parser );
425
426 return true;
427 }
428
429 function debug( $data ) {
430 #wfDebug( "IMPORT: $data\n" );
431 }
432
433 function notice( $data ) {
434 global $wgCommandLineMode;
435 if( $wgCommandLineMode ) {
436 print "$data\n";
437 } else {
438 global $wgOut;
439 $wgOut->addHTML( "<li>$data</li>\n" );
440 }
441 }
442
443 /**
444 * Sets the action to perform as each new page in the stream is reached.
445 * @param callable $callback
446 * @return callable
447 */
448 function setPageCallback( $callback ) {
449 $previous = $this->mPageCallback;
450 $this->mPageCallback = $callback;
451 return $previous;
452 }
453
454 /**
455 * Sets the action to perform as each page in the stream is completed.
456 * Callback accepts the page title (as a Title object), a second object
457 * with the original title form (in case it's been overridden into a
458 * local namespace), and a count of revisions.
459 *
460 * @param callable $callback
461 * @return callable
462 */
463 function setPageOutCallback( $callback ) {
464 $previous = $this->mPageOutCallback;
465 $this->mPageOutCallback = $callback;
466 return $previous;
467 }
468
469 /**
470 * Sets the action to perform as each page revision is reached.
471 * @param callable $callback
472 * @return callable
473 */
474 function setRevisionCallback( $callback ) {
475 $previous = $this->mRevisionCallback;
476 $this->mRevisionCallback = $callback;
477 return $previous;
478 }
479
480 /**
481 * Set a target namespace to override the defaults
482 */
483 function setTargetNamespace( $namespace ) {
484 if( is_null( $namespace ) ) {
485 // Don't override namespaces
486 $this->mTargetNamespace = null;
487 } elseif( $namespace >= 0 ) {
488 // FIXME: Check for validity
489 $this->mTargetNamespace = intval( $namespace );
490 } else {
491 return false;
492 }
493 }
494
495 /**
496 * Default per-revision callback, performs the import.
497 * @param WikiRevision $revision
498 * @private
499 */
500 function importRevision( &$revision ) {
501 $dbw =& wfGetDB( DB_MASTER );
502 $dbw->deadlockLoop( array( &$revision, 'importOldRevision' ) );
503 }
504
505 /**
506 * Alternate per-revision callback, for debugging.
507 * @param WikiRevision $revision
508 * @private
509 */
510 function debugRevisionHandler( &$revision ) {
511 $this->debug( "Got revision:" );
512 if( is_object( $revision->title ) ) {
513 $this->debug( "-- Title: " . $revision->title->getPrefixedText() );
514 } else {
515 $this->debug( "-- Title: <invalid>" );
516 }
517 $this->debug( "-- User: " . $revision->user_text );
518 $this->debug( "-- Timestamp: " . $revision->timestamp );
519 $this->debug( "-- Comment: " . $revision->comment );
520 $this->debug( "-- Text: " . $revision->text );
521 }
522
523 /**
524 * Notify the callback function when a new <page> is reached.
525 * @param Title $title
526 * @private
527 */
528 function pageCallback( $title ) {
529 if( is_callable( $this->mPageCallback ) ) {
530 call_user_func( $this->mPageCallback, $title );
531 }
532 }
533
534 /**
535 * Notify the callback function when a </page> is closed.
536 * @param Title $title
537 * @param Title $origTitle
538 * @param int $revisionCount
539 * @private
540 */
541 function pageOutCallback( $title, $origTitle, $revisionCount ) {
542 if( is_callable( $this->mPageOutCallback ) ) {
543 call_user_func( $this->mPageOutCallback, $title, $origTitle,
544 $revisionCount );
545 }
546 }
547
548
549 # XML parser callbacks from here out -- beware!
550 function donothing( $parser, $x, $y="" ) {
551 #$this->debug( "donothing" );
552 }
553
554 function in_start( $parser, $name, $attribs ) {
555 $this->debug( "in_start $name" );
556 if( $name != "mediawiki" ) {
557 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
558 }
559 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
560 }
561
562 function in_mediawiki( $parser, $name, $attribs ) {
563 $this->debug( "in_mediawiki $name" );
564 if( $name == 'siteinfo' ) {
565 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
566 } elseif( $name == 'page' ) {
567 $this->workRevisionCount = 0;
568 xml_set_element_handler( $parser, "in_page", "out_page" );
569 } else {
570 return $this->throwXMLerror( "Expected <page>, got <$name>" );
571 }
572 }
573 function out_mediawiki( $parser, $name ) {
574 $this->debug( "out_mediawiki $name" );
575 if( $name != "mediawiki" ) {
576 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
577 }
578 xml_set_element_handler( $parser, "donothing", "donothing" );
579 }
580
581
582 function in_siteinfo( $parser, $name, $attribs ) {
583 // no-ops for now
584 $this->debug( "in_siteinfo $name" );
585 switch( $name ) {
586 case "sitename":
587 case "base":
588 case "generator":
589 case "case":
590 case "namespaces":
591 case "namespace":
592 break;
593 default:
594 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
595 }
596 }
597
598 function out_siteinfo( $parser, $name ) {
599 if( $name == "siteinfo" ) {
600 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
601 }
602 }
603
604
605 function in_page( $parser, $name, $attribs ) {
606 $this->debug( "in_page $name" );
607 switch( $name ) {
608 case "id":
609 case "title":
610 case "restrictions":
611 $this->appendfield = $name;
612 $this->appenddata = "";
613 $this->parenttag = "page";
614 xml_set_element_handler( $parser, "in_nothing", "out_append" );
615 xml_set_character_data_handler( $parser, "char_append" );
616 break;
617 case "revision":
618 $this->workRevision = new WikiRevision;
619 $this->workRevision->setTitle( $this->pageTitle );
620 $this->workRevisionCount++;
621 xml_set_element_handler( $parser, "in_revision", "out_revision" );
622 break;
623 default:
624 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
625 }
626 }
627
628 function out_page( $parser, $name ) {
629 $this->debug( "out_page $name" );
630 if( $name != "page" ) {
631 return $this->throwXMLerror( "Expected </page>, got </$name>" );
632 }
633 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
634
635 $this->pageOutCallback( $this->pageTitle, $this->origTitle,
636 $this->workRevisionCount );
637
638 $this->workTitle = null;
639 $this->workRevision = null;
640 $this->workRevisionCount = 0;
641 $this->pageTitle = null;
642 $this->origTitle = null;
643 }
644
645 function in_nothing( $parser, $name, $attribs ) {
646 $this->debug( "in_nothing $name" );
647 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
648 }
649 function char_append( $parser, $data ) {
650 $this->debug( "char_append '$data'" );
651 $this->appenddata .= $data;
652 }
653 function out_append( $parser, $name ) {
654 $this->debug( "out_append $name" );
655 if( $name != $this->appendfield ) {
656 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
657 }
658 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
659 xml_set_character_data_handler( $parser, "donothing" );
660
661 switch( $this->appendfield ) {
662 case "title":
663 $this->workTitle = $this->appenddata;
664 $this->origTitle = Title::newFromText( $this->workTitle );
665 if( !is_null( $this->mTargetNamespace ) && !is_null( $this->origTitle ) ) {
666 $this->pageTitle = Title::makeTitle( $this->mTargetNamespace,
667 $this->origTitle->getDbKey() );
668 } else {
669 $this->pageTitle = Title::newFromText( $this->workTitle );
670 }
671 $this->pageCallback( $this->workTitle );
672 break;
673 case "id":
674 if ( $this->parenttag == 'revision' ) {
675 $this->workRevision->setID( $this->appenddata );
676 }
677 break;
678 case "text":
679 $this->workRevision->setText( $this->appenddata );
680 break;
681 case "username":
682 $this->workRevision->setUsername( $this->appenddata );
683 break;
684 case "ip":
685 $this->workRevision->setUserIP( $this->appenddata );
686 break;
687 case "timestamp":
688 $this->workRevision->setTimestamp( $this->appenddata );
689 break;
690 case "comment":
691 $this->workRevision->setComment( $this->appenddata );
692 break;
693 case "minor":
694 $this->workRevision->setMinor( true );
695 break;
696 default:
697 $this->debug( "Bad append: {$this->appendfield}" );
698 }
699 $this->appendfield = "";
700 $this->appenddata = "";
701 }
702
703 function in_revision( $parser, $name, $attribs ) {
704 $this->debug( "in_revision $name" );
705 switch( $name ) {
706 case "id":
707 case "timestamp":
708 case "comment":
709 case "minor":
710 case "text":
711 $this->parenttag = "revision";
712 $this->appendfield = $name;
713 xml_set_element_handler( $parser, "in_nothing", "out_append" );
714 xml_set_character_data_handler( $parser, "char_append" );
715 break;
716 case "contributor":
717 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
718 break;
719 default:
720 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
721 }
722 }
723
724 function out_revision( $parser, $name ) {
725 $this->debug( "out_revision $name" );
726 if( $name != "revision" ) {
727 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
728 }
729 xml_set_element_handler( $parser, "in_page", "out_page" );
730
731 $out = call_user_func_array( $this->mRevisionCallback,
732 array( &$this->workRevision, &$this ) );
733 if( !empty( $out ) ) {
734 global $wgOut;
735 $wgOut->addHTML( "<li>" . $out . "</li>\n" );
736 }
737 }
738
739 function in_contributor( $parser, $name, $attribs ) {
740 $this->debug( "in_contributor $name" );
741 switch( $name ) {
742 case "username":
743 case "ip":
744 case "id":
745 $this->parenttag = "contributor";
746 $this->appendfield = $name;
747 xml_set_element_handler( $parser, "in_nothing", "out_append" );
748 xml_set_character_data_handler( $parser, "char_append" );
749 break;
750 default:
751 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
752 }
753 }
754
755 function out_contributor( $parser, $name ) {
756 $this->debug( "out_contributor $name" );
757 if( $name != "contributor" ) {
758 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
759 }
760 xml_set_element_handler( $parser, "in_revision", "out_revision" );
761 }
762
763 }
764
765 /** @package MediaWiki */
766 class ImportStringSource {
767 function ImportStringSource( $string ) {
768 $this->mString = $string;
769 $this->mRead = false;
770 }
771
772 function atEnd() {
773 return $this->mRead;
774 }
775
776 function readChunk() {
777 if( $this->atEnd() ) {
778 return false;
779 } else {
780 $this->mRead = true;
781 return $this->mString;
782 }
783 }
784 }
785
786 /** @package MediaWiki */
787 class ImportStreamSource {
788 function ImportStreamSource( $handle ) {
789 $this->mHandle = $handle;
790 }
791
792 function atEnd() {
793 return feof( $this->mHandle );
794 }
795
796 function readChunk() {
797 return fread( $this->mHandle, 32768 );
798 }
799
800 function newFromFile( $filename ) {
801 $file = @fopen( $filename, 'rt' );
802 if( !$file ) {
803 return new WikiErrorMsg( "importcantopen" );
804 }
805 return new ImportStreamSource( $file );
806 }
807
808 function newFromUpload( $fieldname = "xmlimport" ) {
809 $upload =& $_FILES[$fieldname];
810
811 if( !isset( $upload ) || !$upload['name'] ) {
812 return new WikiErrorMsg( 'importnofile' );
813 }
814 if( !empty( $upload['error'] ) ) {
815 return new WikiErrorMsg( 'importuploaderror', $upload['error'] );
816 }
817 $fname = $upload['tmp_name'];
818 if( is_uploaded_file( $fname ) ) {
819 return ImportStreamSource::newFromFile( $fname );
820 } else {
821 return new WikiErrorMsg( 'importnofile' );
822 }
823 }
824
825 function newFromURL( $url ) {
826 wfDebug( __METHOD__ . ": opening $url\n" );
827 # fopen-wrappers are normally turned off for security.
828 ini_set( "allow_url_fopen", true );
829 $ret = ImportStreamSource::newFromFile( $url );
830 ini_set( "allow_url_fopen", false );
831 return $ret;
832 }
833
834 function newFromInterwiki( $interwiki, $page, $history=false ) {
835 $base = Title::getInterwikiLink( $interwiki );
836 $link = Title::newFromText( "$interwiki:Special:Export/$page" );
837 if( empty( $base ) || empty( $link ) ) {
838 return new WikiErrorMsg( 'importbadinterwiki' );
839 } else {
840 $params = $history ? 'history=1' : '';
841 $url = $link->getFullUrl( $params );
842 return ImportStreamSource::newFromURL( $url );
843 }
844 }
845 }
846
847
848 ?>