3 * MediaWiki page data importer
4 * Copyright (C) 2003,2005 Brion Vibber <brion@pobox.com>
5 * http://www.mediawiki.org/
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.
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.
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
23 * @ingroup SpecialPage
28 * @ingroup SpecialPage
33 var $timestamp = "20010115000000";
43 function setTitle( $title ) {
44 if( is_object( $title ) ) {
45 $this->title
= $title;
46 } elseif( is_null( $title ) ) {
47 throw new MWException( "WikiRevision given a null title in import. You may need to adjust \$wgLegalTitleChars." );
49 throw new MWException( "WikiRevision given non-object title in import." );
53 function setID( $id ) {
57 function setTimestamp( $ts ) {
58 # 2003-08-05T18:30:02Z
59 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
62 function setUsername( $user ) {
63 $this->user_text
= $user;
66 function setUserIP( $ip ) {
67 $this->user_text
= $ip;
70 function setText( $text ) {
74 function setComment( $text ) {
75 $this->comment
= $text;
78 function setMinor( $minor ) {
79 $this->minor
= (bool)$minor;
82 function setSrc( $src ) {
86 function setFilename( $filename ) {
87 $this->filename
= $filename;
90 function setSize( $size ) {
91 $this->size
= intval( $size );
94 function setType( $type ) {
98 function setAction( $action ) {
99 $this->action
= $action;
102 function setParams( $params ) {
103 $this->params
= $params;
106 function getTitle() {
114 function getTimestamp() {
115 return $this->timestamp
;
119 return $this->user_text
;
126 function getComment() {
127 return $this->comment
;
130 function getMinor() {
138 function getFilename() {
139 return $this->filename
;
150 function getAction() {
151 return $this->action
;
154 function getParams() {
155 return $this->params
;
158 function importOldRevision() {
159 $dbw = wfGetDB( DB_MASTER
);
161 # Sneak a single revision into place
162 $user = User
::newFromName( $this->getUser() );
164 $userId = intval( $user->getId() );
165 $userText = $user->getName();
168 $userText = $this->getUser();
171 // avoid memory leak...?
172 $linkCache = LinkCache
::singleton();
175 $article = new Article( $this->title
);
176 $pageId = $article->getId();
178 # must create the page...
179 $pageId = $article->insertOn( $dbw );
184 $prior = $dbw->selectField( 'revision', '1',
185 array( 'rev_page' => $pageId,
186 'rev_timestamp' => $dbw->timestamp( $this->timestamp
),
187 'rev_user_text' => $userText,
188 'rev_comment' => $this->getComment() ),
192 // FIXME: this could fail slightly for multiple matches :P
193 wfDebug( __METHOD__
. ": skipping existing revision for [[" .
194 $this->title
->getPrefixedText() . "]], timestamp " . $this->timestamp
. "\n" );
199 # FIXME: Use original rev_id optionally (better for backups)
201 $revision = new Revision( array(
203 'text' => $this->getText(),
204 'comment' => $this->getComment(),
206 'user_text' => $userText,
207 'timestamp' => $this->timestamp
,
208 'minor_edit' => $this->minor
,
210 $revId = $revision->insertOn( $dbw );
211 $changed = $article->updateIfNewerOn( $dbw, $revision );
214 wfDebug( __METHOD__
. ": running onArticleCreate\n" );
215 Article
::onArticleCreate( $this->title
);
217 wfDebug( __METHOD__
. ": running create updates\n" );
218 $article->createUpdates( $revision );
220 } elseif( $changed ) {
221 wfDebug( __METHOD__
. ": running onArticleEdit\n" );
222 Article
::onArticleEdit( $this->title
, false ); // leave templatelinks for editUpdates()
224 wfDebug( __METHOD__
. ": running edit updates\n" );
225 $article->editUpdates(
236 function importLogItem() {
237 $dbw = wfGetDB( DB_MASTER
);
238 # FIXME: this will not record autoblocks
239 if( !$this->getTitle() ) {
240 wfDebug( __METHOD__
. ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
241 $this->timestamp
. "\n" );
244 # Check if it exists already
245 // FIXME: use original log ID (better for backups)
246 $prior = $dbw->selectField( 'logging', '1',
247 array( 'log_type' => $this->getType(),
248 'log_action' => $this->getAction(),
249 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
250 'log_namespace' => $this->getTitle()->getNamespace(),
251 'log_title' => $this->getTitle()->getDBkey(),
252 'log_comment' => $this->getComment(),
253 #'log_user_text' => $this->user_text,
254 'log_params' => $this->params
),
257 // FIXME: this could fail slightly for multiple matches :P
259 wfDebug( __METHOD__
. ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp " .
260 $this->timestamp
. "\n" );
263 $log_id = $dbw->nextSequenceValue( 'log_log_id_seq' );
266 'log_type' => $this->type
,
267 'log_action' => $this->action
,
268 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
269 'log_user' => User
::idFromName( $this->user_text
),
270 #'log_user_text' => $this->user_text,
271 'log_namespace' => $this->getTitle()->getNamespace(),
272 'log_title' => $this->getTitle()->getDBkey(),
273 'log_comment' => $this->getComment(),
274 'log_params' => $this->params
276 $dbw->insert( 'logging', $data, __METHOD__
);
279 function importUpload() {
280 wfDebug( __METHOD__
. ": STUB\n" );
283 // from file revert...
284 $source = $this->file->getArchiveVirtualUrl( $this->oldimage );
285 $comment = $wgRequest->getText( 'wpComment' );
286 // TODO: Preserve file properties from database instead of reloading from file
287 $status = $this->file->upload( $source, $comment, $comment );
288 if( $status->isGood() ) {
292 // from file upload...
293 $this->mLocalFile = wfLocalFile( $nt );
294 $this->mDestName = $this->mLocalFile->getName();
296 $status = $this->mLocalFile->upload( $this->mTempPath, $this->mComment, $pageText,
297 File::DELETE_SOURCE, $this->mFileProps );
298 if ( !$status->isGood() ) {
299 $resultDetails = array( 'internal' => $status->getWikiText() );
302 // @fixme upload() uses $wgUser, which is wrong here
303 // it may also create a page without our desire, also wrong potentially.
304 // and, it will record a *current* upload, but we might want an archive version here
306 $file = wfLocalFile( $this->getTitle() );
309 wfDebug( "IMPORT: Bad file. :(\n" );
313 $source = $this->downloadSource();
315 wfDebug( "IMPORT: Could not fetch remote file. :(\n" );
319 $status = $file->upload( $source,
321 $this->getComment(), // Initial page, if none present...
324 $this->getTimestamp() );
326 if( $status->isGood() ) {
328 wfDebug( "IMPORT: is ok?\n" );
332 wfDebug( "IMPORT: is bad? " . $status->getXml() . "\n" );
337 function downloadSource() {
338 global $wgEnableUploads;
339 if( !$wgEnableUploads ) {
343 $tempo = tempnam( wfTempDir(), 'download' );
344 $f = fopen( $tempo, 'wb' );
346 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
351 $src = $this->getSrc();
352 $data = Http
::get( $src );
354 wfDebug( "IMPORT: couldn't fetch source $src\n" );
369 * implements Special:Import
370 * @ingroup SpecialPage
375 var $mPageCallback = null;
376 var $mPageOutCallback = null;
377 var $mRevisionCallback = null;
378 var $mLogItemCallback = null;
379 var $mUploadCallback = null;
380 var $mTargetNamespace = null;
382 var $tagStack = array();
384 function __construct( $source ) {
385 $this->setRevisionCallback( array( $this, "importRevision" ) );
386 $this->setUploadCallback( array( $this, "importUpload" ) );
387 $this->setLogItemCallback( array( $this, "importLogItem" ) );
388 $this->mSource
= $source;
391 function throwXmlError( $err ) {
392 $this->debug( "FAILURE: $err" );
393 wfDebug( "WikiImporter XML error: $err\n" );
398 function doImport() {
399 if( empty( $this->mSource
) ) {
400 return new WikiErrorMsg( "importnotext" );
403 $parser = xml_parser_create( "UTF-8" );
405 # case folding violates XML standard, turn it off
406 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
408 xml_set_object( $parser, $this );
409 xml_set_element_handler( $parser, "in_start", "" );
411 $offset = 0; // for context extraction on error reporting
413 $chunk = $this->mSource
->readChunk();
414 if( !xml_parse( $parser, $chunk, $this->mSource
->atEnd() ) ) {
415 wfDebug( "WikiImporter::doImport encountered XML parsing error\n" );
416 return new WikiXmlError( $parser, wfMsgHtml( 'import-parse-failure' ), $chunk, $offset );
418 $offset +
= strlen( $chunk );
419 } while( $chunk !== false && !$this->mSource
->atEnd() );
420 xml_parser_free( $parser );
425 function debug( $data ) {
426 if( $this->mDebug
) {
427 wfDebug( "IMPORT: $data\n" );
431 function notice( $data ) {
432 global $wgCommandLineMode;
433 if( $wgCommandLineMode ) {
437 $wgOut->addHTML( "<li>" . htmlspecialchars( $data ) . "</li>\n" );
444 function setDebug( $debug ) {
445 $this->mDebug
= $debug;
449 * Sets the action to perform as each new page in the stream is reached.
450 * @param $callback callback
453 function setPageCallback( $callback ) {
454 $previous = $this->mPageCallback
;
455 $this->mPageCallback
= $callback;
460 * Sets the action to perform as each page in the stream is completed.
461 * Callback accepts the page title (as a Title object), a second object
462 * with the original title form (in case it's been overridden into a
463 * local namespace), and a count of revisions.
465 * @param $callback callback
468 function setPageOutCallback( $callback ) {
469 $previous = $this->mPageOutCallback
;
470 $this->mPageOutCallback
= $callback;
475 * Sets the action to perform as each page revision is reached.
476 * @param $callback callback
479 function setRevisionCallback( $callback ) {
480 $previous = $this->mRevisionCallback
;
481 $this->mRevisionCallback
= $callback;
486 * Sets the action to perform as each file upload version is reached.
487 * @param $callback callback
490 function setUploadCallback( $callback ) {
491 $previous = $this->mUploadCallback
;
492 $this->mUploadCallback
= $callback;
497 * Sets the action to perform as each log item reached.
498 * @param $callback callback
501 function setLogItemCallback( $callback ) {
502 $previous = $this->mLogItemCallback
;
503 $this->mLogItemCallback
= $callback;
508 * Set a target namespace to override the defaults
510 function setTargetNamespace( $namespace ) {
511 if( is_null( $namespace ) ) {
512 // Don't override namespaces
513 $this->mTargetNamespace
= null;
514 } elseif( $namespace >= 0 ) {
515 // FIXME: Check for validity
516 $this->mTargetNamespace
= intval( $namespace );
523 * Default per-revision callback, performs the import.
524 * @param $revision WikiRevision
527 function importRevision( $revision ) {
528 $dbw = wfGetDB( DB_MASTER
);
529 return $dbw->deadlockLoop( array( $revision, 'importOldRevision' ) );
533 * Default per-revision callback, performs the import.
534 * @param $revision WikiRevision
537 function importLogItem( $rev ) {
538 $dbw = wfGetDB( DB_MASTER
);
539 return $dbw->deadlockLoop( array( $rev, 'importLogItem' ) );
545 function importUpload( $revision ) {
546 //$dbw = wfGetDB( DB_MASTER );
547 //return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
552 * Alternate per-revision callback, for debugging.
553 * @param $revision WikiRevision
556 function debugRevisionHandler( &$revision ) {
557 $this->debug( "Got revision:" );
558 if( is_object( $revision->title
) ) {
559 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
561 $this->debug( "-- Title: <invalid>" );
563 $this->debug( "-- User: " . $revision->user_text
);
564 $this->debug( "-- Timestamp: " . $revision->timestamp
);
565 $this->debug( "-- Comment: " . $revision->comment
);
566 $this->debug( "-- Text: " . $revision->text
);
570 * Notify the callback function when a new <page> is reached.
571 * @param $title Title
574 function pageCallback( $title ) {
575 if( is_callable( $this->mPageCallback
) ) {
576 call_user_func( $this->mPageCallback
, $title );
581 * Notify the callback function when a </page> is closed.
582 * @param $title Title
583 * @param $origTitle Title
584 * @param $revisionCount int
585 * @param $successCount Int: number of revisions for which callback returned true
588 function pageOutCallback( $title, $origTitle, $revisionCount, $successCount ) {
589 if( is_callable( $this->mPageOutCallback
) ) {
590 call_user_func( $this->mPageOutCallback
, $title, $origTitle,
591 $revisionCount, $successCount );
595 # XML parser callbacks from here out -- beware!
596 function donothing( $parser, $x, $y="" ) {
597 #$this->debug( "donothing" );
600 function in_start( $parser, $name, $attribs ) {
601 $this->debug( "in_start $name" );
602 if( $name != "mediawiki" ) {
603 return $this->throwXMLerror( "Expected <mediawiki>, got <$name>" );
605 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
608 function in_mediawiki( $parser, $name, $attribs ) {
609 $this->debug( "in_mediawiki $name" );
610 if( $name == 'siteinfo' ) {
611 xml_set_element_handler( $parser, "in_siteinfo", "out_siteinfo" );
612 } elseif( $name == 'page' ) {
613 $this->push( $name );
614 $this->workRevisionCount
= 0;
615 $this->workSuccessCount
= 0;
616 $this->uploadCount
= 0;
617 $this->uploadSuccessCount
= 0;
618 xml_set_element_handler( $parser, "in_page", "out_page" );
619 } elseif( $name == 'logitem' ) {
620 $this->push( $name );
621 $this->workRevision
= new WikiRevision
;
622 xml_set_element_handler( $parser, "in_logitem", "out_logitem" );
624 return $this->throwXMLerror( "Expected <page>, got <$name>" );
627 function out_mediawiki( $parser, $name ) {
628 $this->debug( "out_mediawiki $name" );
629 if( $name != "mediawiki" ) {
630 return $this->throwXMLerror( "Expected </mediawiki>, got </$name>" );
632 xml_set_element_handler( $parser, "donothing", "donothing" );
636 function in_siteinfo( $parser, $name, $attribs ) {
638 $this->debug( "in_siteinfo $name" );
648 return $this->throwXMLerror( "Element <$name> not allowed in <siteinfo>." );
652 function out_siteinfo( $parser, $name ) {
653 if( $name == "siteinfo" ) {
654 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
659 function in_page( $parser, $name, $attribs ) {
660 $this->debug( "in_page $name" );
665 $this->appendfield
= $name;
666 $this->appenddata
= "";
667 xml_set_element_handler( $parser, "in_nothing", "out_append" );
668 xml_set_character_data_handler( $parser, "char_append" );
671 $this->push( "revision" );
672 if( is_object( $this->pageTitle
) ) {
673 $this->workRevision
= new WikiRevision
;
674 $this->workRevision
->setTitle( $this->pageTitle
);
675 $this->workRevisionCount++
;
677 // Skipping items due to invalid page title
678 $this->workRevision
= null;
680 xml_set_element_handler( $parser, "in_revision", "out_revision" );
683 $this->push( "upload" );
684 if( is_object( $this->pageTitle
) ) {
685 $this->workRevision
= new WikiRevision
;
686 $this->workRevision
->setTitle( $this->pageTitle
);
687 $this->uploadCount++
;
689 // Skipping items due to invalid page title
690 $this->workRevision
= null;
692 xml_set_element_handler( $parser, "in_upload", "out_upload" );
695 return $this->throwXMLerror( "Element <$name> not allowed in a <page>." );
699 function out_page( $parser, $name ) {
700 $this->debug( "out_page $name" );
702 if( $name != "page" ) {
703 return $this->throwXMLerror( "Expected </page>, got </$name>" );
705 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
707 $this->pageOutCallback( $this->pageTitle
, $this->origTitle
,
708 $this->workRevisionCount
, $this->workSuccessCount
);
710 $this->workTitle
= null;
711 $this->workRevision
= null;
712 $this->workRevisionCount
= 0;
713 $this->workSuccessCount
= 0;
714 $this->pageTitle
= null;
715 $this->origTitle
= null;
718 function in_nothing( $parser, $name, $attribs ) {
719 $this->debug( "in_nothing $name" );
720 return $this->throwXMLerror( "No child elements allowed here; got <$name>" );
723 function char_append( $parser, $data ) {
724 $this->debug( "char_append '$data'" );
725 $this->appenddata
.= $data;
728 function out_append( $parser, $name ) {
729 $this->debug( "out_append $name" );
730 if( $name != $this->appendfield
) {
731 return $this->throwXMLerror( "Expected </{$this->appendfield}>, got </$name>" );
734 switch( $this->appendfield
) {
736 $this->workTitle
= $this->appenddata
;
737 $this->origTitle
= Title
::newFromText( $this->workTitle
);
738 if( !is_null( $this->mTargetNamespace
) && !is_null( $this->origTitle
) ) {
739 $this->pageTitle
= Title
::makeTitle( $this->mTargetNamespace
,
740 $this->origTitle
->getDBkey() );
742 $this->pageTitle
= Title
::newFromText( $this->workTitle
);
744 if( is_null( $this->pageTitle
) ) {
745 // Invalid page title? Ignore the page
746 $this->notice( "Skipping invalid page title '$this->workTitle'" );
748 $this->pageCallback( $this->workTitle
);
752 if ( $this->parentTag() == 'revision' ||
$this->parentTag() == 'logitem' ) {
753 if( $this->workRevision
)
754 $this->workRevision
->setID( $this->appenddata
);
758 if( $this->workRevision
)
759 $this->workRevision
->setText( $this->appenddata
);
762 if( $this->workRevision
)
763 $this->workRevision
->setUsername( $this->appenddata
);
766 if( $this->workRevision
)
767 $this->workRevision
->setUserIP( $this->appenddata
);
770 if( $this->workRevision
)
771 $this->workRevision
->setTimestamp( $this->appenddata
);
774 if( $this->workRevision
)
775 $this->workRevision
->setComment( $this->appenddata
);
778 if( $this->workRevision
)
779 $this->workRevision
->setType( $this->appenddata
);
782 if( $this->workRevision
)
783 $this->workRevision
->setAction( $this->appenddata
);
786 if( $this->workRevision
)
787 $this->workRevision
->setTitle( Title
::newFromText( $this->appenddata
) );
790 if( $this->workRevision
)
791 $this->workRevision
->setParams( $this->appenddata
);
794 if( $this->workRevision
)
795 $this->workRevision
->setMinor( true );
798 if( $this->workRevision
)
799 $this->workRevision
->setFilename( $this->appenddata
);
802 if( $this->workRevision
)
803 $this->workRevision
->setSrc( $this->appenddata
);
806 if( $this->workRevision
)
807 $this->workRevision
->setSize( intval( $this->appenddata
) );
810 $this->debug( "Bad append: {$this->appendfield}" );
812 $this->appendfield
= "";
813 $this->appenddata
= "";
815 $parent = $this->parentTag();
816 xml_set_element_handler( $parser, "in_$parent", "out_$parent" );
817 xml_set_character_data_handler( $parser, "donothing" );
820 function in_revision( $parser, $name, $attribs ) {
821 $this->debug( "in_revision $name" );
828 $this->appendfield
= $name;
829 xml_set_element_handler( $parser, "in_nothing", "out_append" );
830 xml_set_character_data_handler( $parser, "char_append" );
833 $this->push( "contributor" );
834 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
837 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
841 function out_revision( $parser, $name ) {
842 $this->debug( "out_revision $name" );
844 if( $name != "revision" ) {
845 return $this->throwXMLerror( "Expected </revision>, got </$name>" );
847 xml_set_element_handler( $parser, "in_page", "out_page" );
849 if( $this->workRevision
) {
850 $ok = call_user_func_array( $this->mRevisionCallback
,
851 array( $this->workRevision
, $this ) );
853 $this->workSuccessCount++
;
858 function in_logitem( $parser, $name, $attribs ) {
859 $this->debug( "in_logitem $name" );
868 $this->appendfield
= $name;
869 xml_set_element_handler( $parser, "in_nothing", "out_append" );
870 xml_set_character_data_handler( $parser, "char_append" );
873 $this->push( "contributor" );
874 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
877 return $this->throwXMLerror( "Element <$name> not allowed in a <revision>." );
881 function out_logitem( $parser, $name ) {
882 $this->debug( "out_logitem $name" );
884 if( $name != "logitem" ) {
885 return $this->throwXMLerror( "Expected </logitem>, got </$name>" );
887 xml_set_element_handler( $parser, "in_mediawiki", "out_mediawiki" );
889 if( $this->workRevision
) {
890 $ok = call_user_func_array( $this->mLogItemCallback
,
891 array( $this->workRevision
, $this ) );
893 $this->workSuccessCount++
;
898 function in_upload( $parser, $name, $attribs ) {
899 $this->debug( "in_upload $name" );
907 $this->appendfield
= $name;
908 xml_set_element_handler( $parser, "in_nothing", "out_append" );
909 xml_set_character_data_handler( $parser, "char_append" );
912 $this->push( "contributor" );
913 xml_set_element_handler( $parser, "in_contributor", "out_contributor" );
916 return $this->throwXMLerror( "Element <$name> not allowed in an <upload>." );
920 function out_upload( $parser, $name ) {
921 $this->debug( "out_revision $name" );
923 if( $name != "upload" ) {
924 return $this->throwXMLerror( "Expected </upload>, got </$name>" );
926 xml_set_element_handler( $parser, "in_page", "out_page" );
928 if( $this->workRevision
) {
929 $ok = call_user_func_array( $this->mUploadCallback
,
930 array( $this->workRevision
, $this ) );
932 $this->workUploadSuccessCount++
;
937 function in_contributor( $parser, $name, $attribs ) {
938 $this->debug( "in_contributor $name" );
943 $this->appendfield
= $name;
944 xml_set_element_handler( $parser, "in_nothing", "out_append" );
945 xml_set_character_data_handler( $parser, "char_append" );
948 $this->throwXMLerror( "Invalid tag <$name> in <contributor>" );
952 function out_contributor( $parser, $name ) {
953 $this->debug( "out_contributor $name" );
955 if( $name != "contributor" ) {
956 return $this->throwXMLerror( "Expected </contributor>, got </$name>" );
958 $parent = $this->parentTag();
959 xml_set_element_handler( $parser, "in_$parent", "out_$parent" );
962 private function push( $name ) {
963 array_push( $this->tagStack
, $name );
964 $this->debug( "PUSH $name" );
967 private function pop() {
968 $name = array_pop( $this->tagStack
);
969 $this->debug( "POP $name" );
973 private function parentTag() {
974 $name = $this->tagStack
[count( $this->tagStack
) - 1];
975 $this->debug( "PARENT $name" );
982 * @todo document (e.g. one-sentence class description).
983 * @ingroup SpecialPage
985 class ImportStringSource
{
986 function __construct( $string ) {
987 $this->mString
= $string;
988 $this->mRead
= false;
995 function readChunk() {
996 if( $this->atEnd() ) {
1000 return $this->mString
;
1006 * @todo document (e.g. one-sentence class description).
1007 * @ingroup SpecialPage
1009 class ImportStreamSource
{
1010 function __construct( $handle ) {
1011 $this->mHandle
= $handle;
1015 return feof( $this->mHandle
);
1018 function readChunk() {
1019 return fread( $this->mHandle
, 32768 );
1022 static function newFromFile( $filename ) {
1023 $file = @fopen
( $filename, 'rt' );
1025 return new WikiErrorMsg( "importcantopen" );
1027 return new ImportStreamSource( $file );
1030 static function newFromUpload( $fieldname = "xmlimport" ) {
1031 $upload =& $_FILES[$fieldname];
1033 if( !isset( $upload ) ||
!$upload['name'] ) {
1034 return new WikiErrorMsg( 'importnofile' );
1036 if( !empty( $upload['error'] ) ) {
1037 switch($upload['error']){
1038 case 1: # The uploaded file exceeds the upload_max_filesize directive in php.ini.
1039 return new WikiErrorMsg( 'importuploaderrorsize' );
1040 case 2: # The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
1041 return new WikiErrorMsg( 'importuploaderrorsize' );
1042 case 3: # The uploaded file was only partially uploaded
1043 return new WikiErrorMsg( 'importuploaderrorpartial' );
1044 case 6: #Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.
1045 return new WikiErrorMsg( 'importuploaderrortemp' );
1046 # case else: # Currently impossible
1050 $fname = $upload['tmp_name'];
1051 if( is_uploaded_file( $fname ) ) {
1052 return ImportStreamSource
::newFromFile( $fname );
1054 return new WikiErrorMsg( 'importnofile' );
1058 static function newFromURL( $url, $method = 'GET' ) {
1059 wfDebug( __METHOD__
. ": opening $url\n" );
1060 # Use the standard HTTP fetch function; it times out
1061 # quicker and sorts out user-agent problems which might
1062 # otherwise prevent importing from large sites, such
1063 # as the Wikimedia cluster, etc.
1064 $data = Http
::request( $method, $url );
1065 if( $data !== false ) {
1067 fwrite( $file, $data );
1070 return new ImportStreamSource( $file );
1072 return new WikiErrorMsg( 'importcantopen' );
1076 public static function newFromInterwiki( $interwiki, $page, $history=false ) {
1078 return new WikiErrorMsg( 'import-noarticle' );
1080 $link = Title
::newFromText( "$interwiki:Special:Export/$page" );
1081 if( is_null( $link ) ||
$link->getInterwiki() == '' ) {
1082 return new WikiErrorMsg( 'importbadinterwiki' );
1084 $params = $history ?
'history=1' : '';
1085 $url = $link->getFullUrl( $params );
1086 # For interwikis, use POST to avoid redirects.
1087 return ImportStreamSource
::newFromURL( $url, "POST" );