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