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