9c04111adc1bb1cfea905af151d42ee959112869
[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 " . wfMsgHtml( 'import-interwiki-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, $successCount ) {
179 global $wgOut, $wgLang, $wgContLang;
180
181 $this->mPageCount++;
182
183 $localCount = $wgLang->formatNum( $successCount );
184 $contentCount = $wgContLang->formatNum( $successCount );
185
186 $wgOut->addHtml( "<li>" . Linker::makeKnownLinkObj( $title ) .
187 " " .
188 wfMsgExt( 'import-revision-count', array( 'parsemag', 'escape' ), $localCount ) .
189 "</li>\n" );
190
191 if( $successCount > 0 ) {
192 $log = new LogPage( 'import' );
193 if( $this->mIsUpload ) {
194 $detail = wfMsgForContent( 'import-logentry-upload-detail',
195 $contentCount );
196 $log->addEntry( 'upload', $title, $detail );
197 } else {
198 $interwiki = '[[:' . $this->mInterwiki . ':' .
199 $origTitle->getPrefixedText() . ']]';
200 $detail = wfMsgForContent( 'import-logentry-interwiki-detail',
201 $contentCount, $interwiki );
202 $log->addEntry( 'interwiki', $title, $detail );
203 }
204
205 $comment = $detail; // quick
206 $dbw = wfGetDB( DB_MASTER );
207 $nullRevision = Revision::newNullRevision(
208 $dbw, $title->getArticleId(), $comment, true );
209 $nullRevId = $nullRevision->insertOn( $dbw );
210 }
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. You may need to adjust \$wgLegalTitleChars." );
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 $prior = Revision::loadFromTimestamp( $dbw, $this->title, $this->timestamp );
332 if( !is_null( $prior ) ) {
333 // FIXME: this could fail slightly for multiple matches :P
334 wfDebug( __METHOD__ . ": skipping existing revision for [[" .
335 $this->title->getPrefixedText() . "]], timestamp " .
336 $this->timestamp . "\n" );
337 return false;
338 }
339 }
340
341 # FIXME: Use original rev_id optionally
342 # FIXME: blah blah blah
343
344 #if( $numrows > 0 ) {
345 # return wfMsg( "importhistoryconflict" );
346 #}
347
348 # Insert the row
349 $revision = new Revision( array(
350 'page' => $pageId,
351 'text' => $this->getText(),
352 'comment' => $this->getComment(),
353 'user' => $userId,
354 'user_text' => $userText,
355 'timestamp' => $this->timestamp,
356 'minor_edit' => $this->minor,
357 ) );
358 $revId = $revision->insertOn( $dbw );
359 $changed = $article->updateIfNewerOn( $dbw, $revision );
360
361 if( $created ) {
362 wfDebug( __METHOD__ . ": running onArticleCreate\n" );
363 Article::onArticleCreate( $this->title );
364
365 wfDebug( __METHOD__ . ": running create updates\n" );
366 $article->createUpdates( $revision );
367
368 } elseif( $changed ) {
369 wfDebug( __METHOD__ . ": running onArticleEdit\n" );
370 Article::onArticleEdit( $this->title );
371
372 wfDebug( __METHOD__ . ": running edit updates\n" );
373 $article->editUpdates(
374 $this->getText(),
375 $this->getComment(),
376 $this->minor,
377 $this->timestamp,
378 $revId );
379 }
380
381 return true;
382 }
383
384 }
385
386 /**
387 *
388 * @package MediaWiki
389 * @subpackage SpecialPage
390 */
391 class WikiImporter {
392 var $mSource = null;
393 var $mPageCallback = null;
394 var $mPageOutCallback = null;
395 var $mRevisionCallback = null;
396 var $mTargetNamespace = null;
397 var $lastfield;
398
399 function WikiImporter( $source ) {
400 $this->setRevisionCallback( array( &$this, "importRevision" ) );
401 $this->mSource = $source;
402 }
403
404 function throwXmlError( $err ) {
405 $this->debug( "FAILURE: $err" );
406 wfDebug( "WikiImporter XML error: $err\n" );
407 }
408
409 # --------------
410
411 function doImport() {
412 if( empty( $this->mSource ) ) {
413 return new WikiErrorMsg( "importnotext" );
414 }
415
416 $parser = xml_parser_create( "UTF-8" );
417
418 # case folding violates XML standard, turn it off
419 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
420
421 xml_set_object( $parser, $this );
422 xml_set_element_handler( $parser, "in_start", "" );
423
424 $offset = 0; // for context extraction on error reporting
425 do {
426 $chunk = $this->mSource->readChunk();
427 if( !xml_parse( $parser, $chunk, $this->mSource->atEnd() ) ) {
428 wfDebug( "WikiImporter::doImport encountered XML parsing error\n" );
429 return new WikiXmlError( $parser, 'XML import parse failure', $chunk, $offset );
430 }
431 $offset += strlen( $chunk );
432 } while( $chunk !== false && !$this->mSource->atEnd() );
433 xml_parser_free( $parser );
434
435 return true;
436 }
437
438 function debug( $data ) {
439 #wfDebug( "IMPORT: $data\n" );
440 }
441
442 function notice( $data ) {
443 global $wgCommandLineMode;
444 if( $wgCommandLineMode ) {
445 print "$data\n";
446 } else {
447 global $wgOut;
448 $wgOut->addHTML( "<li>$data</li>\n" );
449 }
450 }
451
452 /**
453 * Sets the action to perform as each new page in the stream is reached.
454 * @param callable $callback
455 * @return callable
456 */
457 function setPageCallback( $callback ) {
458 $previous = $this->mPageCallback;
459 $this->mPageCallback = $callback;
460 return $previous;
461 }
462
463 /**
464 * Sets the action to perform as each page in the stream is completed.
465 * Callback accepts the page title (as a Title object), a second object
466 * with the original title form (in case it's been overridden into a
467 * local namespace), and a count of revisions.
468 *
469 * @param callable $callback
470 * @return callable
471 */
472 function setPageOutCallback( $callback ) {
473 $previous = $this->mPageOutCallback;
474 $this->mPageOutCallback = $callback;
475 return $previous;
476 }
477
478 /**
479 * Sets the action to perform as each page revision is reached.
480 * @param callable $callback
481 * @return callable
482 */
483 function setRevisionCallback( $callback ) {
484 $previous = $this->mRevisionCallback;
485 $this->mRevisionCallback = $callback;
486 return $previous;
487 }
488
489 /**
490 * Set a target namespace to override the defaults
491 */
492 function setTargetNamespace( $namespace ) {
493 if( is_null( $namespace ) ) {
494 // Don't override namespaces
495 $this->mTargetNamespace = null;
496 } elseif( $namespace >= 0 ) {
497 // FIXME: Check for validity
498 $this->mTargetNamespace = intval( $namespace );
499 } else {
500 return false;
501 }
502 }
503
504 /**
505 * Default per-revision callback, performs the import.
506 * @param WikiRevision $revision
507 * @private
508 */
509 function importRevision( &$revision ) {
510 $dbw =& wfGetDB( DB_MASTER );
511 return $dbw->deadlockLoop( array( &$revision, 'importOldRevision' ) );
512 }
513
514 /**
515 * Alternate per-revision callback, for debugging.
516 * @param WikiRevision $revision
517 * @private
518 */
519 function debugRevisionHandler( &$revision ) {
520 $this->debug( "Got revision:" );
521 if( is_object( $revision->title ) ) {
522 $this->debug( "-- Title: " . $revision->title->getPrefixedText() );
523 } else {
524 $this->debug( "-- Title: <invalid>" );
525 }
526 $this->debug( "-- User: " . $revision->user_text );
527 $this->debug( "-- Timestamp: " . $revision->timestamp );
528 $this->debug( "-- Comment: " . $revision->comment );
529 $this->debug( "-- Text: " . $revision->text );
530 }
531
532 /**
533 * Notify the callback function when a new <page> is reached.
534 * @param Title $title
535 * @private
536 */
537 function pageCallback( $title ) {
538 if( is_callable( $this->mPageCallback ) ) {
539 call_user_func( $this->mPageCallback, $title );
540 }
541 }
542
543 /**
544 * Notify the callback function when a </page> is closed.
545 * @param Title $title
546 * @param Title $origTitle
547 * @param int $revisionCount
548 * @param int $successCount number of revisions for which callback returned true
549 * @private
550 */
551 function pageOutCallback( $title, $origTitle, $revisionCount, $successCount ) {
552 if( is_callable( $this->mPageOutCallback ) ) {
553 call_user_func( $this->mPageOutCallback, $title, $origTitle,
554 $revisionCount, $successCount );
555 }
556 }
557
558
559 # XML parser callbacks from here out -- beware!
560 function donothing( $parser, $x, $y="" ) {
561 #$this->debug( "donothing" );
562 }
563
564 function in_start( $parser, $name, $attribs ) {
565 $this->debug( "in_start $name" );
566 if( $name != "mediawiki" ) {
567 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
568 }
569 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
570 }
571
572 function in_mediawiki( $parser, $name, $attribs ) {
573 $this->debug( "in_mediawiki $name" );
574 if( $name == 'siteinfo' ) {
575 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
576 } elseif( $name == 'page' ) {
577 $this->workRevisionCount = 0;
578 $this->workSuccessCount = 0;
579 xml_set_element_handler( $parser, "in_page", "out_page" );
580 } else {
581 return $this->throwXMLerror( "Expected <page>, got <$name>" );
582 }
583 }
584 function out_mediawiki( $parser, $name ) {
585 $this->debug( "out_mediawiki $name" );
586 if( $name != "mediawiki" ) {
587 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
588 }
589 xml_set_element_handler( $parser, "donothing", "donothing" );
590 }
591
592
593 function in_siteinfo( $parser, $name, $attribs ) {
594 // no-ops for now
595 $this->debug( "in_siteinfo $name" );
596 switch( $name ) {
597 case "sitename":
598 case "base":
599 case "generator":
600 case "case":
601 case "namespaces":
602 case "namespace":
603 break;
604 default:
605 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
606 }
607 }
608
609 function out_siteinfo( $parser, $name ) {
610 if( $name == "siteinfo" ) {
611 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
612 }
613 }
614
615
616 function in_page( $parser, $name, $attribs ) {
617 $this->debug( "in_page $name" );
618 switch( $name ) {
619 case "id":
620 case "title":
621 case "restrictions":
622 $this->appendfield = $name;
623 $this->appenddata = "";
624 $this->parenttag = "page";
625 xml_set_element_handler( $parser, "in_nothing", "out_append" );
626 xml_set_character_data_handler( $parser, "char_append" );
627 break;
628 case "revision":
629 $this->workRevision = new WikiRevision;
630 $this->workRevision->setTitle( $this->pageTitle );
631 $this->workRevisionCount++;
632 xml_set_element_handler( $parser, "in_revision", "out_revision" );
633 break;
634 default:
635 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
636 }
637 }
638
639 function out_page( $parser, $name ) {
640 $this->debug( "out_page $name" );
641 if( $name != "page" ) {
642 return $this->throwXMLerror( "Expected </page>, got </$name>" );
643 }
644 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
645
646 $this->pageOutCallback( $this->pageTitle, $this->origTitle,
647 $this->workRevisionCount, $this->workSuccessCount );
648
649 $this->workTitle = null;
650 $this->workRevision = null;
651 $this->workRevisionCount = 0;
652 $this->workSuccessCount = 0;
653 $this->pageTitle = null;
654 $this->origTitle = null;
655 }
656
657 function in_nothing( $parser, $name, $attribs ) {
658 $this->debug( "in_nothing $name" );
659 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
660 }
661 function char_append( $parser, $data ) {
662 $this->debug( "char_append '$data'" );
663 $this->appenddata .= $data;
664 }
665 function out_append( $parser, $name ) {
666 $this->debug( "out_append $name" );
667 if( $name != $this->appendfield ) {
668 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
669 }
670 xml_set_element_handler( $parser, "in_$this->parenttag", "out_$this->parenttag" );
671 xml_set_character_data_handler( $parser, "donothing" );
672
673 switch( $this->appendfield ) {
674 case "title":
675 $this->workTitle = $this->appenddata;
676 $this->origTitle = Title::newFromText( $this->workTitle );
677 if( !is_null( $this->mTargetNamespace ) && !is_null( $this->origTitle ) ) {
678 $this->pageTitle = Title::makeTitle( $this->mTargetNamespace,
679 $this->origTitle->getDbKey() );
680 } else {
681 $this->pageTitle = Title::newFromText( $this->workTitle );
682 }
683 $this->pageCallback( $this->workTitle );
684 break;
685 case "id":
686 if ( $this->parenttag == 'revision' ) {
687 $this->workRevision->setID( $this->appenddata );
688 }
689 break;
690 case "text":
691 $this->workRevision->setText( $this->appenddata );
692 break;
693 case "username":
694 $this->workRevision->setUsername( $this->appenddata );
695 break;
696 case "ip":
697 $this->workRevision->setUserIP( $this->appenddata );
698 break;
699 case "timestamp":
700 $this->workRevision->setTimestamp( $this->appenddata );
701 break;
702 case "comment":
703 $this->workRevision->setComment( $this->appenddata );
704 break;
705 case "minor":
706 $this->workRevision->setMinor( true );
707 break;
708 default:
709 $this->debug( "Bad append: {$this->appendfield}" );
710 }
711 $this->appendfield = "";
712 $this->appenddata = "";
713 }
714
715 function in_revision( $parser, $name, $attribs ) {
716 $this->debug( "in_revision $name" );
717 switch( $name ) {
718 case "id":
719 case "timestamp":
720 case "comment":
721 case "minor":
722 case "text":
723 $this->parenttag = "revision";
724 $this->appendfield = $name;
725 xml_set_element_handler( $parser, "in_nothing", "out_append" );
726 xml_set_character_data_handler( $parser, "char_append" );
727 break;
728 case "contributor":
729 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
730 break;
731 default:
732 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
733 }
734 }
735
736 function out_revision( $parser, $name ) {
737 $this->debug( "out_revision $name" );
738 if( $name != "revision" ) {
739 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
740 }
741 xml_set_element_handler( $parser, "in_page", "out_page" );
742
743 $ok = call_user_func_array( $this->mRevisionCallback,
744 array( &$this->workRevision, &$this ) );
745 if( $ok ) {
746 $this->workSuccessCount++;
747 }
748 }
749
750 function in_contributor( $parser, $name, $attribs ) {
751 $this->debug( "in_contributor $name" );
752 switch( $name ) {
753 case "username":
754 case "ip":
755 case "id":
756 $this->parenttag = "contributor";
757 $this->appendfield = $name;
758 xml_set_element_handler( $parser, "in_nothing", "out_append" );
759 xml_set_character_data_handler( $parser, "char_append" );
760 break;
761 default:
762 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
763 }
764 }
765
766 function out_contributor( $parser, $name ) {
767 $this->debug( "out_contributor $name" );
768 if( $name != "contributor" ) {
769 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
770 }
771 xml_set_element_handler( $parser, "in_revision", "out_revision" );
772 }
773
774 }
775
776 /** @package MediaWiki */
777 class ImportStringSource {
778 function ImportStringSource( $string ) {
779 $this->mString = $string;
780 $this->mRead = false;
781 }
782
783 function atEnd() {
784 return $this->mRead;
785 }
786
787 function readChunk() {
788 if( $this->atEnd() ) {
789 return false;
790 } else {
791 $this->mRead = true;
792 return $this->mString;
793 }
794 }
795 }
796
797 /** @package MediaWiki */
798 class ImportStreamSource {
799 function ImportStreamSource( $handle ) {
800 $this->mHandle = $handle;
801 }
802
803 function atEnd() {
804 return feof( $this->mHandle );
805 }
806
807 function readChunk() {
808 return fread( $this->mHandle, 32768 );
809 }
810
811 function newFromFile( $filename ) {
812 $file = @fopen( $filename, 'rt' );
813 if( !$file ) {
814 return new WikiErrorMsg( "importcantopen" );
815 }
816 return new ImportStreamSource( $file );
817 }
818
819 function newFromUpload( $fieldname = "xmlimport" ) {
820 $upload =& $_FILES[$fieldname];
821
822 if( !isset( $upload ) || !$upload['name'] ) {
823 return new WikiErrorMsg( 'importnofile' );
824 }
825 if( !empty( $upload['error'] ) ) {
826 return new WikiErrorMsg( 'importuploaderror', $upload['error'] );
827 }
828 $fname = $upload['tmp_name'];
829 if( is_uploaded_file( $fname ) ) {
830 return ImportStreamSource::newFromFile( $fname );
831 } else {
832 return new WikiErrorMsg( 'importnofile' );
833 }
834 }
835
836 function newFromURL( $url ) {
837 wfDebug( __METHOD__ . ": opening $url\n" );
838 # fopen-wrappers are normally turned off for security.
839 ini_set( "allow_url_fopen", true );
840 $ret = ImportStreamSource::newFromFile( $url );
841 ini_set( "allow_url_fopen", false );
842 return $ret;
843 }
844
845 function newFromInterwiki( $interwiki, $page, $history=false ) {
846 $link = Title::newFromText( "$interwiki:Special:Export/$page" );
847 if( is_null( $link ) || $link->getInterwiki() == '' ) {
848 return new WikiErrorMsg( 'importbadinterwiki' );
849 } else {
850 $params = $history ? 'history=1' : '';
851 $url = $link->getFullUrl( $params );
852 return ImportStreamSource::newFromURL( $url );
853 }
854 }
855 }
856
857
858 ?>