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