3 * MediaWiki page data importer.
5 * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
24 * @ingroup SpecialPage
28 * XML file reader for the page data importer
30 * implements Special:Import
31 * @ingroup SpecialPage
34 private $reader = null;
35 private $foreignNamespaces = null;
36 private $mLogItemCallback, $mUploadCallback, $mRevisionCallback, $mPageCallback;
37 private $mSiteInfoCallback, $mPageOutCallback;
38 private $mNoticeCallback, $mDebug;
39 private $mImportUploads, $mImageBasePath;
40 private $mNoUpdates = false;
43 /** @var ImportTitleFactory */
44 private $importTitleFactory;
46 private $countableCache = array();
49 * Creates an ImportXMLReader drawing from the source provided
50 * @param ImportSource $source
51 * @param Config $config
54 function __construct( ImportSource
$source, Config
$config = null ) {
55 if ( !class_exists( 'XMLReader' ) ) {
56 throw new Exception( 'Import requires PHP to have been compiled with libxml support' );
59 $this->reader
= new XMLReader();
61 wfDeprecated( __METHOD__
. ' without a Config instance', '1.25' );
62 $config = ConfigFactory
::getDefaultInstance()->makeConfig( 'main' );
64 $this->config
= $config;
66 if ( !in_array( 'uploadsource', stream_get_wrappers() ) ) {
67 stream_wrapper_register( 'uploadsource', 'UploadSourceAdapter' );
69 $id = UploadSourceAdapter
::registerSource( $source );
71 // Enable the entity loader, as it is needed for loading external URLs via
72 // XMLReader::open (T86036)
73 $oldDisable = libxml_disable_entity_loader( false );
74 if ( defined( 'LIBXML_PARSEHUGE' ) ) {
75 $status = $this->reader
->open( "uploadsource://$id", null, LIBXML_PARSEHUGE
);
77 $status = $this->reader
->open( "uploadsource://$id" );
80 $error = libxml_get_last_error();
81 libxml_disable_entity_loader( $oldDisable );
82 throw new MWException( 'Encountered an internal error while initializing WikiImporter object: ' .
85 libxml_disable_entity_loader( $oldDisable );
88 $this->setPageCallback( array( $this, 'beforeImportPage' ) );
89 $this->setRevisionCallback( array( $this, "importRevision" ) );
90 $this->setUploadCallback( array( $this, 'importUpload' ) );
91 $this->setLogItemCallback( array( $this, 'importLogItem' ) );
92 $this->setPageOutCallback( array( $this, 'finishImportPage' ) );
94 $this->importTitleFactory
= new NaiveImportTitleFactory();
98 * @return null|XMLReader
100 public function getReader() {
101 return $this->reader
;
104 public function throwXmlError( $err ) {
105 $this->debug( "FAILURE: $err" );
106 wfDebug( "WikiImporter XML error: $err\n" );
109 public function debug( $data ) {
110 if ( $this->mDebug
) {
111 wfDebug( "IMPORT: $data\n" );
115 public function warn( $data ) {
116 wfDebug( "IMPORT: $data\n" );
119 public function notice( $msg /*, $param, ...*/ ) {
120 $params = func_get_args();
121 array_shift( $params );
123 if ( is_callable( $this->mNoticeCallback
) ) {
124 call_user_func( $this->mNoticeCallback
, $msg, $params );
125 } else { # No ImportReporter -> CLI
126 echo wfMessage( $msg, $params )->text() . "\n";
134 function setDebug( $debug ) {
135 $this->mDebug
= $debug;
139 * Set 'no updates' mode. In this mode, the link tables will not be updated by the importer
140 * @param bool $noupdates
142 function setNoUpdates( $noupdates ) {
143 $this->mNoUpdates
= $noupdates;
147 * Set a callback that displays notice messages
149 * @param callable $callback
152 public function setNoticeCallback( $callback ) {
153 return wfSetVar( $this->mNoticeCallback
, $callback );
157 * Sets the action to perform as each new page in the stream is reached.
158 * @param callable $callback
161 public function setPageCallback( $callback ) {
162 $previous = $this->mPageCallback
;
163 $this->mPageCallback
= $callback;
168 * Sets the action to perform as each page in the stream is completed.
169 * Callback accepts the page title (as a Title object), a second object
170 * with the original title form (in case it's been overridden into a
171 * local namespace), and a count of revisions.
173 * @param callable $callback
176 public function setPageOutCallback( $callback ) {
177 $previous = $this->mPageOutCallback
;
178 $this->mPageOutCallback
= $callback;
183 * Sets the action to perform as each page revision is reached.
184 * @param callable $callback
187 public function setRevisionCallback( $callback ) {
188 $previous = $this->mRevisionCallback
;
189 $this->mRevisionCallback
= $callback;
194 * Sets the action to perform as each file upload version is reached.
195 * @param callable $callback
198 public function setUploadCallback( $callback ) {
199 $previous = $this->mUploadCallback
;
200 $this->mUploadCallback
= $callback;
205 * Sets the action to perform as each log item reached.
206 * @param callable $callback
209 public function setLogItemCallback( $callback ) {
210 $previous = $this->mLogItemCallback
;
211 $this->mLogItemCallback
= $callback;
216 * Sets the action to perform when site info is encountered
217 * @param callable $callback
220 public function setSiteInfoCallback( $callback ) {
221 $previous = $this->mSiteInfoCallback
;
222 $this->mSiteInfoCallback
= $callback;
227 * Sets the factory object to use to convert ForeignTitle objects into local
229 * @param ImportTitleFactory $factory
231 public function setImportTitleFactory( $factory ) {
232 $this->importTitleFactory
= $factory;
236 * Set a target namespace to override the defaults
237 * @param null|int $namespace
240 public function setTargetNamespace( $namespace ) {
241 if ( is_null( $namespace ) ) {
242 // Don't override namespaces
243 $this->setImportTitleFactory( new NaiveImportTitleFactory() );
247 MWNamespace
::exists( intval( $namespace ) )
249 $namespace = intval( $namespace );
250 $this->setImportTitleFactory( new NamespaceImportTitleFactory( $namespace ) );
258 * Set a target root page under which all pages are imported
259 * @param null|string $rootpage
262 public function setTargetRootPage( $rootpage ) {
263 $status = Status
::newGood();
264 if ( is_null( $rootpage ) ) {
266 $this->setImportTitleFactory( new NaiveImportTitleFactory() );
267 } elseif ( $rootpage !== '' ) {
268 $rootpage = rtrim( $rootpage, '/' ); // avoid double slashes
269 $title = Title
::newFromText( $rootpage );
271 if ( !$title ||
$title->isExternal() ) {
272 $status->fatal( 'import-rootpage-invalid' );
274 if ( !MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
277 $displayNSText = $title->getNamespace() == NS_MAIN
278 ?
wfMessage( 'blanknamespace' )->text()
279 : $wgContLang->getNsText( $title->getNamespace() );
280 $status->fatal( 'import-rootpage-nosubpage', $displayNSText );
282 // set namespace to 'all', so the namespace check in processTitle() can pass
283 $this->setTargetNamespace( null );
284 $this->setImportTitleFactory( new SubpageImportTitleFactory( $title ) );
294 public function setImageBasePath( $dir ) {
295 $this->mImageBasePath
= $dir;
299 * @param bool $import
301 public function setImportUploads( $import ) {
302 $this->mImportUploads
= $import;
306 * Default per-page callback. Sets up some things related to site statistics
307 * @param array $titleAndForeignTitle Two-element array, with Title object at
308 * index 0 and ForeignTitle object at index 1
311 public function beforeImportPage( $titleAndForeignTitle ) {
312 $title = $titleAndForeignTitle[0];
313 $page = WikiPage
::factory( $title );
314 $this->countableCache
['title_' . $title->getPrefixedText()] = $page->isCountable();
319 * Default per-revision callback, performs the import.
320 * @param WikiRevision $revision
323 public function importRevision( $revision ) {
324 if ( !$revision->getContentHandler()->canBeUsedOn( $revision->getTitle() ) ) {
325 $this->notice( 'import-error-bad-location',
326 $revision->getTitle()->getPrefixedText(),
328 $revision->getModel(),
329 $revision->getFormat() );
335 $dbw = wfGetDB( DB_MASTER
);
336 return $dbw->deadlockLoop( array( $revision, 'importOldRevision' ) );
337 } catch ( MWContentSerializationException
$ex ) {
338 $this->notice( 'import-error-unserialize',
339 $revision->getTitle()->getPrefixedText(),
341 $revision->getModel(),
342 $revision->getFormat() );
349 * Default per-revision callback, performs the import.
350 * @param WikiRevision $revision
353 public function importLogItem( $revision ) {
354 $dbw = wfGetDB( DB_MASTER
);
355 return $dbw->deadlockLoop( array( $revision, 'importLogItem' ) );
360 * @param WikiRevision $revision
363 public function importUpload( $revision ) {
364 $dbw = wfGetDB( DB_MASTER
);
365 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
369 * Mostly for hook use
370 * @param Title $title
371 * @param ForeignTitle $foreignTitle
372 * @param int $revCount
373 * @param int $sRevCount
374 * @param array $pageInfo
377 public function finishImportPage( $title, $foreignTitle, $revCount,
378 $sRevCount, $pageInfo ) {
380 // Update article count statistics (T42009)
381 // The normal counting logic in WikiPage->doEditUpdates() is designed for
382 // one-revision-at-a-time editing, not bulk imports. In this situation it
383 // suffers from issues of slave lag. We let WikiPage handle the total page
384 // and revision count, and we implement our own custom logic for the
385 // article (content page) count.
386 $page = WikiPage
::factory( $title );
387 $page->loadPageData( 'fromdbmaster' );
388 $content = $page->getContent();
389 if ( $content === null ) {
390 wfDebug( __METHOD__
. ': Skipping article count adjustment for ' . $title .
391 ' because WikiPage::getContent() returned null' );
393 $editInfo = $page->prepareContentForEdit( $content );
394 $countKey = 'title_' . $title->getPrefixedText();
395 $countable = $page->isCountable( $editInfo );
396 if ( array_key_exists( $countKey, $this->countableCache
) &&
397 $countable != $this->countableCache
[$countKey] ) {
398 DeferredUpdates
::addUpdate( SiteStatsUpdate
::factory( array(
399 'articles' => ( (int)$countable - (int)$this->countableCache
[$countKey] )
404 $args = func_get_args();
405 return Hooks
::run( 'AfterImportPage', $args );
409 * Alternate per-revision callback, for debugging.
410 * @param WikiRevision $revision
412 public function debugRevisionHandler( &$revision ) {
413 $this->debug( "Got revision:" );
414 if ( is_object( $revision->title
) ) {
415 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
417 $this->debug( "-- Title: <invalid>" );
419 $this->debug( "-- User: " . $revision->user_text
);
420 $this->debug( "-- Timestamp: " . $revision->timestamp
);
421 $this->debug( "-- Comment: " . $revision->comment
);
422 $this->debug( "-- Text: " . $revision->text
);
426 * Notify the callback function of site info
427 * @param array $siteInfo
430 private function siteInfoCallback( $siteInfo ) {
431 if ( isset( $this->mSiteInfoCallback
) ) {
432 return call_user_func_array( $this->mSiteInfoCallback
,
433 array( $siteInfo, $this ) );
440 * Notify the callback function when a new "<page>" is reached.
441 * @param Title $title
443 function pageCallback( $title ) {
444 if ( isset( $this->mPageCallback
) ) {
445 call_user_func( $this->mPageCallback
, $title );
450 * Notify the callback function when a "</page>" is closed.
451 * @param Title $title
452 * @param ForeignTitle $foreignTitle
453 * @param int $revCount
454 * @param int $sucCount Number of revisions for which callback returned true
455 * @param array $pageInfo Associative array of page information
457 private function pageOutCallback( $title, $foreignTitle, $revCount,
458 $sucCount, $pageInfo ) {
459 if ( isset( $this->mPageOutCallback
) ) {
460 $args = func_get_args();
461 call_user_func_array( $this->mPageOutCallback
, $args );
466 * Notify the callback function of a revision
467 * @param WikiRevision $revision
470 private function revisionCallback( $revision ) {
471 if ( isset( $this->mRevisionCallback
) ) {
472 return call_user_func_array( $this->mRevisionCallback
,
473 array( $revision, $this ) );
480 * Notify the callback function of a new log item
481 * @param WikiRevision $revision
484 private function logItemCallback( $revision ) {
485 if ( isset( $this->mLogItemCallback
) ) {
486 return call_user_func_array( $this->mLogItemCallback
,
487 array( $revision, $this ) );
494 * Retrieves the contents of the named attribute of the current element.
495 * @param string $attr The name of the attribute
496 * @return string The value of the attribute or an empty string if it is not set in the current
499 public function nodeAttribute( $attr ) {
500 return $this->reader
->getAttribute( $attr );
504 * Shouldn't something like this be built-in to XMLReader?
505 * Fetches text contents of the current element, assuming
506 * no sub-elements or such scary things.
510 public function nodeContents() {
511 if ( $this->reader
->isEmptyElement
) {
515 while ( $this->reader
->read() ) {
516 switch ( $this->reader
->nodeType
) {
517 case XMLReader
::TEXT
:
518 case XMLReader
::CDATA
:
519 case XMLReader
::SIGNIFICANT_WHITESPACE
:
520 $buffer .= $this->reader
->value
;
522 case XMLReader
::END_ELEMENT
:
527 $this->reader
->close();
532 * Primary entry point
533 * @throws MWException
536 public function doImport() {
537 // Calls to reader->read need to be wrapped in calls to
538 // libxml_disable_entity_loader() to avoid local file
539 // inclusion attacks (bug 46932).
540 $oldDisable = libxml_disable_entity_loader( true );
541 $this->reader
->read();
543 if ( $this->reader
->localName
!= 'mediawiki' ) {
544 libxml_disable_entity_loader( $oldDisable );
545 throw new MWException( "Expected <mediawiki> tag, got " .
546 $this->reader
->localName
);
548 $this->debug( "<mediawiki> tag is correct." );
550 $this->debug( "Starting primary dump processing loop." );
552 $keepReading = $this->reader
->read();
556 while ( $keepReading ) {
557 $tag = $this->reader
->localName
;
558 $type = $this->reader
->nodeType
;
560 if ( !Hooks
::run( 'ImportHandleToplevelXMLTag', array( $this ) ) ) {
562 } elseif ( $tag == 'mediawiki' && $type == XMLReader
::END_ELEMENT
) {
564 } elseif ( $tag == 'siteinfo' ) {
565 $this->handleSiteInfo();
566 } elseif ( $tag == 'page' ) {
568 } elseif ( $tag == 'logitem' ) {
569 $this->handleLogItem();
570 } elseif ( $tag != '#text' ) {
571 $this->warn( "Unhandled top-level XML tag $tag" );
577 $keepReading = $this->reader
->next();
579 $this->debug( "Skip" );
581 $keepReading = $this->reader
->read();
584 } catch ( Exception
$ex ) {
589 libxml_disable_entity_loader( $oldDisable );
590 $this->reader
->close();
599 private function handleSiteInfo() {
600 $this->debug( "Enter site info handler." );
603 // Fields that can just be stuffed in the siteInfo object
604 $normalFields = array( 'sitename', 'base', 'generator', 'case' );
606 while ( $this->reader
->read() ) {
607 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
608 $this->reader
->localName
== 'siteinfo' ) {
612 $tag = $this->reader
->localName
;
614 if ( $tag == 'namespace' ) {
615 $this->foreignNamespaces
[$this->nodeAttribute( 'key' )] =
616 $this->nodeContents();
617 } elseif ( in_array( $tag, $normalFields ) ) {
618 $siteInfo[$tag] = $this->nodeContents();
622 $siteInfo['_namespaces'] = $this->foreignNamespaces
;
623 $this->siteInfoCallback( $siteInfo );
626 private function handleLogItem() {
627 $this->debug( "Enter log item handler." );
630 // Fields that can just be stuffed in the pageInfo object
631 $normalFields = array( 'id', 'comment', 'type', 'action', 'timestamp',
632 'logtitle', 'params' );
634 while ( $this->reader
->read() ) {
635 if ( $this->reader
->nodeType
== XMLReader
::END_ELEMENT
&&
636 $this->reader
->localName
== 'logitem' ) {
640 $tag = $this->reader
->localName
;
642 if ( !Hooks
::run( 'ImportHandleLogItemXMLTag', array(
646 } elseif ( in_array( $tag, $normalFields ) ) {
647 $logInfo[$tag] = $this->nodeContents();
648 } elseif ( $tag == 'contributor' ) {
649 $logInfo['contributor'] = $this->handleContributor();
650 } elseif ( $tag != '#text' ) {
651 $this->warn( "Unhandled log-item XML tag $tag" );
655 $this->processLogItem( $logInfo );
659 * @param array $logInfo
662 private function processLogItem( $logInfo ) {
664 $revision = new WikiRevision( $this->config
);
666 if ( isset( $logInfo['id'] ) ) {
667 $revision->setID( $logInfo['id'] );
669 $revision->setType( $logInfo['type'] );
670 $revision->setAction( $logInfo['action'] );
671 if ( isset( $logInfo['timestamp'] ) ) {
672 $revision->setTimestamp( $logInfo['timestamp'] );
674 if ( isset( $logInfo['params'] ) ) {
675 $revision->setParams( $logInfo['params'] );
677 if ( isset( $logInfo['logtitle'] ) ) {
678 // @todo Using Title for non-local titles is a recipe for disaster.
679 // We should use ForeignTitle here instead.
680 $revision->setTitle( Title
::newFromText( $logInfo['logtitle'] ) );
683 $revision->setNoUpdates( $this->mNoUpdates
);
685 if ( isset( $logInfo['comment'] ) ) {
686 $revision->setComment( $logInfo['comment'] );
689 if ( isset( $logInfo['contributor']['ip'] ) ) {
690 $revision->setUserIP( $logInfo['contributor']['ip'] );
693 if ( !isset( $logInfo['contributor']['username'] ) ) {
694 $revision->setUsername( 'Unknown user' );
696 $revision->setUserName( $logInfo['contributor']['username'] );
699 return $this->logItemCallback( $revision );
702 private function handlePage() {
704 $this->debug( "Enter page handler." );
705 $pageInfo = array( 'revisionCount' => 0, 'successfulRevisionCount' => 0 );
707 // Fields that can just be stuffed in the pageInfo object
708 $normalFields = array( 'title', 'ns', 'id', 'redirect', 'restrictions' );
713 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
714 if ( $this->reader
->nodeType
== XMLReader
::END_ELEMENT
&&
715 $this->reader
->localName
== 'page' ) {
721 $tag = $this->reader
->localName
;
724 // The title is invalid, bail out of this page
726 } elseif ( !Hooks
::run( 'ImportHandlePageXMLTag', array( $this,
729 } elseif ( in_array( $tag, $normalFields ) ) {
733 // <title>Page</title>
734 // <redirect title="NewTitle"/>
736 // Because the redirect tag is built differently, we need special handling for that case.
737 if ( $tag == 'redirect' ) {
738 $pageInfo[$tag] = $this->nodeAttribute( 'title' );
740 $pageInfo[$tag] = $this->nodeContents();
742 } elseif ( $tag == 'revision' ||
$tag == 'upload' ) {
743 if ( !isset( $title ) ) {
744 $title = $this->processTitle( $pageInfo['title'],
745 isset( $pageInfo['ns'] ) ?
$pageInfo['ns'] : null );
747 // $title is either an array of two titles or false.
748 if ( is_array( $title ) ) {
749 $this->pageCallback( $title );
750 list( $pageInfo['_title'], $foreignTitle ) = $title;
758 if ( $tag == 'revision' ) {
759 $this->handleRevision( $pageInfo );
761 $this->handleUpload( $pageInfo );
764 } elseif ( $tag != '#text' ) {
765 $this->warn( "Unhandled page XML tag $tag" );
770 // @note $pageInfo is only set if a valid $title is processed above with
771 // no error. If we have a valid $title, then pageCallback is called
772 // above, $pageInfo['title'] is set and we do pageOutCallback here.
773 // If $pageInfo['_title'] is not set, then $foreignTitle is also not
774 // set since they both come from $title above.
775 if ( array_key_exists( '_title', $pageInfo ) ) {
776 $this->pageOutCallback( $pageInfo['_title'], $foreignTitle,
777 $pageInfo['revisionCount'],
778 $pageInfo['successfulRevisionCount'],
784 * @param array $pageInfo
786 private function handleRevision( &$pageInfo ) {
787 $this->debug( "Enter revision handler" );
788 $revisionInfo = array();
790 $normalFields = array( 'id', 'timestamp', 'comment', 'minor', 'model', 'format', 'text' );
794 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
795 if ( $this->reader
->nodeType
== XMLReader
::END_ELEMENT
&&
796 $this->reader
->localName
== 'revision' ) {
800 $tag = $this->reader
->localName
;
802 if ( !Hooks
::run( 'ImportHandleRevisionXMLTag', array(
803 $this, $pageInfo, $revisionInfo
806 } elseif ( in_array( $tag, $normalFields ) ) {
807 $revisionInfo[$tag] = $this->nodeContents();
808 } elseif ( $tag == 'contributor' ) {
809 $revisionInfo['contributor'] = $this->handleContributor();
810 } elseif ( $tag != '#text' ) {
811 $this->warn( "Unhandled revision XML tag $tag" );
816 $pageInfo['revisionCount']++
;
817 if ( $this->processRevision( $pageInfo, $revisionInfo ) ) {
818 $pageInfo['successfulRevisionCount']++
;
823 * @param array $pageInfo
824 * @param array $revisionInfo
827 private function processRevision( $pageInfo, $revisionInfo ) {
828 global $wgMaxArticleSize;
830 // Make sure revisions won't violate $wgMaxArticleSize, which could lead to
831 // database errors and instability. Testing for revisions with only listed
832 // content models, as other content models might use serialization formats
833 // which aren't checked against $wgMaxArticleSize.
834 if ( ( !isset( $revisionInfo['model'] ) ||
835 in_array( $revisionInfo['model'], array(
843 (int)( strlen( $revisionInfo['text'] ) / 1024 ) > $wgMaxArticleSize
845 throw new MWException( 'The text of ' .
846 ( isset( $revisionInfo['id'] ) ?
847 "the revision with ID $revisionInfo[id]" :
849 ) . " exceeds the maximum allowable size ($wgMaxArticleSize KB)" );
852 $revision = new WikiRevision( $this->config
);
854 if ( isset( $revisionInfo['id'] ) ) {
855 $revision->setID( $revisionInfo['id'] );
857 if ( isset( $revisionInfo['model'] ) ) {
858 $revision->setModel( $revisionInfo['model'] );
860 if ( isset( $revisionInfo['format'] ) ) {
861 $revision->setFormat( $revisionInfo['format'] );
863 $revision->setTitle( $pageInfo['_title'] );
865 if ( isset( $revisionInfo['text'] ) ) {
866 $handler = $revision->getContentHandler();
867 $text = $handler->importTransform(
868 $revisionInfo['text'],
869 $revision->getFormat() );
871 $revision->setText( $text );
873 if ( isset( $revisionInfo['timestamp'] ) ) {
874 $revision->setTimestamp( $revisionInfo['timestamp'] );
876 $revision->setTimestamp( wfTimestampNow() );
879 if ( isset( $revisionInfo['comment'] ) ) {
880 $revision->setComment( $revisionInfo['comment'] );
883 if ( isset( $revisionInfo['minor'] ) ) {
884 $revision->setMinor( true );
886 if ( isset( $revisionInfo['contributor']['ip'] ) ) {
887 $revision->setUserIP( $revisionInfo['contributor']['ip'] );
888 } elseif ( isset( $revisionInfo['contributor']['username'] ) ) {
889 $revision->setUserName( $revisionInfo['contributor']['username'] );
891 $revision->setUserName( 'Unknown user' );
893 $revision->setNoUpdates( $this->mNoUpdates
);
895 return $this->revisionCallback( $revision );
899 * @param array $pageInfo
902 private function handleUpload( &$pageInfo ) {
903 $this->debug( "Enter upload handler" );
904 $uploadInfo = array();
906 $normalFields = array( 'timestamp', 'comment', 'filename', 'text',
907 'src', 'size', 'sha1base36', 'archivename', 'rel' );
911 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
912 if ( $this->reader
->nodeType
== XMLReader
::END_ELEMENT
&&
913 $this->reader
->localName
== 'upload' ) {
917 $tag = $this->reader
->localName
;
919 if ( !Hooks
::run( 'ImportHandleUploadXMLTag', array(
923 } elseif ( in_array( $tag, $normalFields ) ) {
924 $uploadInfo[$tag] = $this->nodeContents();
925 } elseif ( $tag == 'contributor' ) {
926 $uploadInfo['contributor'] = $this->handleContributor();
927 } elseif ( $tag == 'contents' ) {
928 $contents = $this->nodeContents();
929 $encoding = $this->reader
->getAttribute( 'encoding' );
930 if ( $encoding === 'base64' ) {
931 $uploadInfo['fileSrc'] = $this->dumpTemp( base64_decode( $contents ) );
932 $uploadInfo['isTempSrc'] = true;
934 } elseif ( $tag != '#text' ) {
935 $this->warn( "Unhandled upload XML tag $tag" );
940 if ( $this->mImageBasePath
&& isset( $uploadInfo['rel'] ) ) {
941 $path = "{$this->mImageBasePath}/{$uploadInfo['rel']}";
942 if ( file_exists( $path ) ) {
943 $uploadInfo['fileSrc'] = $path;
944 $uploadInfo['isTempSrc'] = false;
948 if ( $this->mImportUploads
) {
949 return $this->processUpload( $pageInfo, $uploadInfo );
954 * @param string $contents
957 private function dumpTemp( $contents ) {
958 $filename = tempnam( wfTempDir(), 'importupload' );
959 file_put_contents( $filename, $contents );
964 * @param array $pageInfo
965 * @param array $uploadInfo
968 private function processUpload( $pageInfo, $uploadInfo ) {
969 $revision = new WikiRevision( $this->config
);
970 $text = isset( $uploadInfo['text'] ) ?
$uploadInfo['text'] : '';
972 $revision->setTitle( $pageInfo['_title'] );
973 $revision->setID( $pageInfo['id'] );
974 $revision->setTimestamp( $uploadInfo['timestamp'] );
975 $revision->setText( $text );
976 $revision->setFilename( $uploadInfo['filename'] );
977 if ( isset( $uploadInfo['archivename'] ) ) {
978 $revision->setArchiveName( $uploadInfo['archivename'] );
980 $revision->setSrc( $uploadInfo['src'] );
981 if ( isset( $uploadInfo['fileSrc'] ) ) {
982 $revision->setFileSrc( $uploadInfo['fileSrc'],
983 !empty( $uploadInfo['isTempSrc'] ) );
985 if ( isset( $uploadInfo['sha1base36'] ) ) {
986 $revision->setSha1Base36( $uploadInfo['sha1base36'] );
988 $revision->setSize( intval( $uploadInfo['size'] ) );
989 $revision->setComment( $uploadInfo['comment'] );
991 if ( isset( $uploadInfo['contributor']['ip'] ) ) {
992 $revision->setUserIP( $uploadInfo['contributor']['ip'] );
994 if ( isset( $uploadInfo['contributor']['username'] ) ) {
995 $revision->setUserName( $uploadInfo['contributor']['username'] );
997 $revision->setNoUpdates( $this->mNoUpdates
);
999 return call_user_func( $this->mUploadCallback
, $revision );
1005 private function handleContributor() {
1006 $fields = array( 'id', 'ip', 'username' );
1009 if ( $this->reader
->isEmptyElement
) {
1012 while ( $this->reader
->read() ) {
1013 if ( $this->reader
->nodeType
== XMLReader
::END_ELEMENT
&&
1014 $this->reader
->localName
== 'contributor' ) {
1018 $tag = $this->reader
->localName
;
1020 if ( in_array( $tag, $fields ) ) {
1021 $info[$tag] = $this->nodeContents();
1029 * @param string $text
1030 * @param string|null $ns
1031 * @return array|bool
1033 private function processTitle( $text, $ns = null ) {
1034 if ( is_null( $this->foreignNamespaces
) ) {
1035 $foreignTitleFactory = new NaiveForeignTitleFactory();
1037 $foreignTitleFactory = new NamespaceAwareForeignTitleFactory(
1038 $this->foreignNamespaces
);
1041 $foreignTitle = $foreignTitleFactory->createForeignTitle( $text,
1044 $title = $this->importTitleFactory
->createTitleFromForeignTitle(
1047 $commandLineMode = $this->config
->get( 'CommandLineMode' );
1048 if ( is_null( $title ) ) {
1049 # Invalid page title? Ignore the page
1050 $this->notice( 'import-error-invalid', $foreignTitle->getFullText() );
1052 } elseif ( $title->isExternal() ) {
1053 $this->notice( 'import-error-interwiki', $title->getPrefixedText() );
1055 } elseif ( !$title->canExist() ) {
1056 $this->notice( 'import-error-special', $title->getPrefixedText() );
1058 } elseif ( !$title->userCan( 'edit' ) && !$commandLineMode ) {
1059 # Do not import if the importing wiki user cannot edit this page
1060 $this->notice( 'import-error-edit', $title->getPrefixedText() );
1062 } elseif ( !$title->exists() && !$title->userCan( 'create' ) && !$commandLineMode ) {
1063 # Do not import if the importing wiki user cannot create this page
1064 $this->notice( 'import-error-create', $title->getPrefixedText() );
1068 return array( $title, $foreignTitle );
1072 /** This is a horrible hack used to keep source compatibility */
1073 class UploadSourceAdapter
{
1075 public static $sourceRegistrations = array();
1087 * @param ImportSource $source
1090 static function registerSource( ImportSource
$source ) {
1091 $id = wfRandomString();
1093 self
::$sourceRegistrations[$id] = $source;
1099 * @param string $path
1100 * @param string $mode
1101 * @param array $options
1102 * @param string $opened_path
1105 function stream_open( $path, $mode, $options, &$opened_path ) {
1106 $url = parse_url( $path );
1109 if ( !isset( self
::$sourceRegistrations[$id] ) ) {
1113 $this->mSource
= self
::$sourceRegistrations[$id];
1122 function stream_read( $count ) {
1126 while ( !$leave && !$this->mSource
->atEnd() &&
1127 strlen( $this->mBuffer
) < $count ) {
1128 $read = $this->mSource
->readChunk();
1130 if ( !strlen( $read ) ) {
1134 $this->mBuffer
.= $read;
1137 if ( strlen( $this->mBuffer
) ) {
1138 $return = substr( $this->mBuffer
, 0, $count );
1139 $this->mBuffer
= substr( $this->mBuffer
, $count );
1142 $this->mPosition +
= strlen( $return );
1148 * @param string $data
1151 function stream_write( $data ) {
1158 function stream_tell() {
1159 return $this->mPosition
;
1165 function stream_eof() {
1166 return $this->mSource
->atEnd();
1172 function url_stat() {
1175 $result['dev'] = $result[0] = 0;
1176 $result['ino'] = $result[1] = 0;
1177 $result['mode'] = $result[2] = 0;
1178 $result['nlink'] = $result[3] = 0;
1179 $result['uid'] = $result[4] = 0;
1180 $result['gid'] = $result[5] = 0;
1181 $result['rdev'] = $result[6] = 0;
1182 $result['size'] = $result[7] = 0;
1183 $result['atime'] = $result[8] = 0;
1184 $result['mtime'] = $result[9] = 0;
1185 $result['ctime'] = $result[10] = 0;
1186 $result['blksize'] = $result[11] = 0;
1187 $result['blocks'] = $result[12] = 0;
1194 * @todo document (e.g. one-sentence class description).
1195 * @ingroup SpecialPage
1197 class WikiRevision
{
1198 /** @todo Unused? */
1199 public $importer = null;
1202 public $title = null;
1208 public $timestamp = "20010115000000";
1212 * @todo Can't find any uses. Public, because that's suspicious. Get clarity. */
1216 public $user_text = "";
1219 public $model = null;
1222 public $format = null;
1231 public $content = null;
1233 /** @var ContentHandler */
1234 protected $contentHandler = null;
1237 public $comment = "";
1240 public $minor = false;
1246 public $action = "";
1249 public $params = "";
1252 public $fileSrc = '';
1254 /** @var bool|string */
1255 public $sha1base36 = false;
1261 public $isTemp = false;
1264 public $archiveName = '';
1266 protected $filename;
1271 /** @todo Unused? */
1275 private $mNoUpdates = false;
1277 /** @var Config $config */
1280 public function __construct( Config
$config ) {
1281 $this->config
= $config;
1285 * @param Title $title
1286 * @throws MWException
1288 function setTitle( $title ) {
1289 if ( is_object( $title ) ) {
1290 $this->title
= $title;
1291 } elseif ( is_null( $title ) ) {
1292 throw new MWException( "WikiRevision given a null title in import. "
1293 . "You may need to adjust \$wgLegalTitleChars." );
1295 throw new MWException( "WikiRevision given non-object title in import." );
1302 function setID( $id ) {
1309 function setTimestamp( $ts ) {
1310 # 2003-08-05T18:30:02Z
1311 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
1315 * @param string $user
1317 function setUsername( $user ) {
1318 $this->user_text
= $user;
1324 function setUserIP( $ip ) {
1325 $this->user_text
= $ip;
1329 * @param string $model
1331 function setModel( $model ) {
1332 $this->model
= $model;
1336 * @param string $format
1338 function setFormat( $format ) {
1339 $this->format
= $format;
1343 * @param string $text
1345 function setText( $text ) {
1346 $this->text
= $text;
1350 * @param string $text
1352 function setComment( $text ) {
1353 $this->comment
= $text;
1357 * @param bool $minor
1359 function setMinor( $minor ) {
1360 $this->minor
= (bool)$minor;
1366 function setSrc( $src ) {
1371 * @param string $src
1372 * @param bool $isTemp
1374 function setFileSrc( $src, $isTemp ) {
1375 $this->fileSrc
= $src;
1376 $this->fileIsTemp
= $isTemp;
1380 * @param string $sha1base36
1382 function setSha1Base36( $sha1base36 ) {
1383 $this->sha1base36
= $sha1base36;
1387 * @param string $filename
1389 function setFilename( $filename ) {
1390 $this->filename
= $filename;
1394 * @param string $archiveName
1396 function setArchiveName( $archiveName ) {
1397 $this->archiveName
= $archiveName;
1403 function setSize( $size ) {
1404 $this->size
= intval( $size );
1408 * @param string $type
1410 function setType( $type ) {
1411 $this->type
= $type;
1415 * @param string $action
1417 function setAction( $action ) {
1418 $this->action
= $action;
1422 * @param array $params
1424 function setParams( $params ) {
1425 $this->params
= $params;
1429 * @param bool $noupdates
1431 public function setNoUpdates( $noupdates ) {
1432 $this->mNoUpdates
= $noupdates;
1438 function getTitle() {
1439 return $this->title
;
1452 function getTimestamp() {
1453 return $this->timestamp
;
1459 function getUser() {
1460 return $this->user_text
;
1466 * @deprecated Since 1.21, use getContent() instead.
1468 function getText() {
1469 ContentHandler
::deprecated( __METHOD__
, '1.21' );
1475 * @return ContentHandler
1477 function getContentHandler() {
1478 if ( is_null( $this->contentHandler
) ) {
1479 $this->contentHandler
= ContentHandler
::getForModelID( $this->getModel() );
1482 return $this->contentHandler
;
1488 function getContent() {
1489 if ( is_null( $this->content
) ) {
1490 $handler = $this->getContentHandler();
1491 $this->content
= $handler->unserializeContent( $this->text
, $this->getFormat() );
1494 return $this->content
;
1500 function getModel() {
1501 if ( is_null( $this->model
) ) {
1502 $this->model
= $this->getTitle()->getContentModel();
1505 return $this->model
;
1511 function getFormat() {
1512 if ( is_null( $this->format
) ) {
1513 $this->format
= $this->getContentHandler()->getDefaultFormat();
1516 return $this->format
;
1522 function getComment() {
1523 return $this->comment
;
1529 function getMinor() {
1530 return $this->minor
;
1541 * @return bool|string
1543 function getSha1() {
1544 if ( $this->sha1base36
) {
1545 return Wikimedia\base_convert
( $this->sha1base36
, 36, 16 );
1553 function getFileSrc() {
1554 return $this->fileSrc
;
1560 function isTempSrc() {
1561 return $this->isTemp
;
1567 function getFilename() {
1568 return $this->filename
;
1574 function getArchiveName() {
1575 return $this->archiveName
;
1581 function getSize() {
1588 function getType() {
1595 function getAction() {
1596 return $this->action
;
1602 function getParams() {
1603 return $this->params
;
1609 function importOldRevision() {
1610 $dbw = wfGetDB( DB_MASTER
);
1612 # Sneak a single revision into place
1613 $user = User
::newFromName( $this->getUser() );
1615 $userId = intval( $user->getId() );
1616 $userText = $user->getName();
1620 $userText = $this->getUser();
1621 $userObj = new User
;
1624 // avoid memory leak...?
1625 Title
::clearCaches();
1627 $page = WikiPage
::factory( $this->title
);
1628 $page->loadPageData( 'fromdbmaster' );
1629 if ( !$page->exists() ) {
1630 # must create the page...
1631 $pageId = $page->insertOn( $dbw );
1633 $oldcountable = null;
1635 $pageId = $page->getId();
1638 $prior = $dbw->selectField( 'revision', '1',
1639 array( 'rev_page' => $pageId,
1640 'rev_timestamp' => $dbw->timestamp( $this->timestamp
),
1641 'rev_user_text' => $userText,
1642 'rev_comment' => $this->getComment() ),
1646 // @todo FIXME: This could fail slightly for multiple matches :P
1647 wfDebug( __METHOD__
. ": skipping existing revision for [[" .
1648 $this->title
->getPrefixedText() . "]], timestamp " . $this->timestamp
. "\n" );
1653 // Select previous version to make size diffs correct
1654 $prevId = $dbw->selectField( 'revision', 'rev_id',
1656 'rev_page' => $pageId,
1657 'rev_timestamp <= ' . $dbw->timestamp( $this->timestamp
),
1660 array( 'ORDER BY' => array(
1661 'rev_timestamp DESC',
1662 'rev_id DESC', // timestamp is not unique per page
1667 # @todo FIXME: Use original rev_id optionally (better for backups)
1669 $revision = new Revision( array(
1670 'title' => $this->title
,
1672 'content_model' => $this->getModel(),
1673 'content_format' => $this->getFormat(),
1674 // XXX: just set 'content' => $this->getContent()?
1675 'text' => $this->getContent()->serialize( $this->getFormat() ),
1676 'comment' => $this->getComment(),
1678 'user_text' => $userText,
1679 'timestamp' => $this->timestamp
,
1680 'minor_edit' => $this->minor
,
1681 'parent_id' => $prevId,
1683 $revision->insertOn( $dbw );
1684 $changed = $page->updateIfNewerOn( $dbw, $revision );
1686 if ( $changed !== false && !$this->mNoUpdates
) {
1687 wfDebug( __METHOD__
. ": running updates\n" );
1688 // countable/oldcountable stuff is handled in WikiImporter::finishImportPage
1689 $page->doEditUpdates(
1692 array( 'created' => $created, 'oldcountable' => 'no-change' )
1699 function importLogItem() {
1700 $dbw = wfGetDB( DB_MASTER
);
1702 $user = User
::newFromName( $this->getUser() );
1704 $userId = intval( $user->getId() );
1705 $userText = $user->getName();
1708 $userText = $this->getUser();
1711 # @todo FIXME: This will not record autoblocks
1712 if ( !$this->getTitle() ) {
1713 wfDebug( __METHOD__
. ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
1714 $this->timestamp
. "\n" );
1717 # Check if it exists already
1718 // @todo FIXME: Use original log ID (better for backups)
1719 $prior = $dbw->selectField( 'logging', '1',
1720 array( 'log_type' => $this->getType(),
1721 'log_action' => $this->getAction(),
1722 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
1723 'log_namespace' => $this->getTitle()->getNamespace(),
1724 'log_title' => $this->getTitle()->getDBkey(),
1725 'log_comment' => $this->getComment(),
1726 # 'log_user_text' => $this->user_text,
1727 'log_params' => $this->params
),
1730 // @todo FIXME: This could fail slightly for multiple matches :P
1733 . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
1734 . $this->timestamp
. "\n" );
1737 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
1739 'log_id' => $log_id,
1740 'log_type' => $this->type
,
1741 'log_action' => $this->action
,
1742 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
1743 'log_user' => $userId,
1744 'log_user_text' => $userText,
1745 'log_namespace' => $this->getTitle()->getNamespace(),
1746 'log_title' => $this->getTitle()->getDBkey(),
1747 'log_comment' => $this->getComment(),
1748 'log_params' => $this->params
1750 $dbw->insert( 'logging', $data, __METHOD__
);
1756 function importUpload() {
1758 $archiveName = $this->getArchiveName();
1759 if ( $archiveName ) {
1760 wfDebug( __METHOD__
. "Importing archived file as $archiveName\n" );
1761 $file = OldLocalFile
::newFromArchiveName( $this->getTitle(),
1762 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
1764 $file = wfLocalFile( $this->getTitle() );
1765 $file->load( File
::READ_LATEST
);
1766 wfDebug( __METHOD__
. 'Importing new file as ' . $file->getName() . "\n" );
1767 if ( $file->exists() && $file->getTimestamp() > $this->getTimestamp() ) {
1768 $archiveName = $file->getTimestamp() . '!' . $file->getName();
1769 $file = OldLocalFile
::newFromArchiveName( $this->getTitle(),
1770 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
1771 wfDebug( __METHOD__
. "File already exists; importing as $archiveName\n" );
1775 wfDebug( __METHOD__
. ': Bad file for ' . $this->getTitle() . "\n" );
1779 # Get the file source or download if necessary
1780 $source = $this->getFileSrc();
1781 $flags = $this->isTempSrc() ? File
::DELETE_SOURCE
: 0;
1783 $source = $this->downloadSource();
1784 $flags |
= File
::DELETE_SOURCE
;
1787 wfDebug( __METHOD__
. ": Could not fetch remote file.\n" );
1790 $sha1 = $this->getSha1();
1791 if ( $sha1 && ( $sha1 !== sha1_file( $source ) ) ) {
1792 if ( $flags & File
::DELETE_SOURCE
) {
1793 # Broken file; delete it if it is a temporary file
1796 wfDebug( __METHOD__
. ": Corrupt file $source.\n" );
1800 $user = User
::newFromName( $this->user_text
);
1802 # Do the actual upload
1803 if ( $archiveName ) {
1804 $status = $file->uploadOld( $source, $archiveName,
1805 $this->getTimestamp(), $this->getComment(), $user, $flags );
1807 $status = $file->upload( $source, $this->getComment(), $this->getComment(),
1808 $flags, false, $this->getTimestamp(), $user );
1811 if ( $status->isGood() ) {
1812 wfDebug( __METHOD__
. ": Successful\n" );
1815 wfDebug( __METHOD__
. ': failed: ' . $status->getHTML() . "\n" );
1821 * @return bool|string
1823 function downloadSource() {
1824 if ( !$this->config
->get( 'EnableUploads' ) ) {
1828 $tempo = tempnam( wfTempDir(), 'download' );
1829 $f = fopen( $tempo, 'wb' );
1831 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
1836 $src = $this->getSrc();
1837 $data = Http
::get( $src, array(), __METHOD__
);
1839 wfDebug( "IMPORT: couldn't fetch source $src\n" );
1845 fwrite( $f, $data );
1854 * Source interface for XML import.
1856 interface ImportSource
{
1859 * Indicates whether the end of the input has been reached.
1860 * Will return true after a finite number of calls to readChunk.
1862 * @return bool true if there is no more input, false otherwise.
1867 * Return a chunk of the input, as a (possibly empty) string.
1868 * When the end of input is reached, readChunk() returns false.
1869 * If atEnd() returns false, readChunk() will return a string.
1870 * If atEnd() returns true, readChunk() will return false.
1872 * @return bool|string
1874 function readChunk();
1878 * Used for importing XML dumps where the content of the dump is in a string.
1879 * This class is ineffecient, and should only be used for small dumps.
1880 * For larger dumps, ImportStreamSource should be used instead.
1882 * @ingroup SpecialPage
1884 class ImportStringSource
implements ImportSource
{
1885 function __construct( $string ) {
1886 $this->mString
= $string;
1887 $this->mRead
= false;
1894 return $this->mRead
;
1898 * @return bool|string
1900 function readChunk() {
1901 if ( $this->atEnd() ) {
1904 $this->mRead
= true;
1905 return $this->mString
;
1910 * Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
1911 * @ingroup SpecialPage
1913 class ImportStreamSource
implements ImportSource
{
1914 function __construct( $handle ) {
1915 $this->mHandle
= $handle;
1922 return feof( $this->mHandle
);
1928 function readChunk() {
1929 return fread( $this->mHandle
, 32768 );
1933 * @param string $filename
1936 static function newFromFile( $filename ) {
1937 MediaWiki\
suppressWarnings();
1938 $file = fopen( $filename, 'rt' );
1939 MediaWiki\restoreWarnings
();
1941 return Status
::newFatal( "importcantopen" );
1943 return Status
::newGood( new ImportStreamSource( $file ) );
1947 * @param string $fieldname
1950 static function newFromUpload( $fieldname = "xmlimport" ) {
1951 $upload =& $_FILES[$fieldname];
1953 if ( $upload === null ||
!$upload['name'] ) {
1954 return Status
::newFatal( 'importnofile' );
1956 if ( !empty( $upload['error'] ) ) {
1957 switch ( $upload['error'] ) {
1959 # The uploaded file exceeds the upload_max_filesize directive in php.ini.
1960 return Status
::newFatal( 'importuploaderrorsize' );
1962 # The uploaded file exceeds the MAX_FILE_SIZE directive that
1963 # was specified in the HTML form.
1964 return Status
::newFatal( 'importuploaderrorsize' );
1966 # The uploaded file was only partially uploaded
1967 return Status
::newFatal( 'importuploaderrorpartial' );
1969 # Missing a temporary folder.
1970 return Status
::newFatal( 'importuploaderrortemp' );
1971 # case else: # Currently impossible
1975 $fname = $upload['tmp_name'];
1976 if ( is_uploaded_file( $fname ) ) {
1977 return ImportStreamSource
::newFromFile( $fname );
1979 return Status
::newFatal( 'importnofile' );
1984 * @param string $url
1985 * @param string $method
1988 static function newFromURL( $url, $method = 'GET' ) {
1989 wfDebug( __METHOD__
. ": opening $url\n" );
1990 # Use the standard HTTP fetch function; it times out
1991 # quicker and sorts out user-agent problems which might
1992 # otherwise prevent importing from large sites, such
1993 # as the Wikimedia cluster, etc.
1994 $data = Http
::request( $method, $url, array( 'followRedirects' => true ), __METHOD__
);
1995 if ( $data !== false ) {
1997 fwrite( $file, $data );
2000 return Status
::newGood( new ImportStreamSource( $file ) );
2002 return Status
::newFatal( 'importcantopen' );
2007 * @param string $interwiki
2008 * @param string $page
2009 * @param bool $history
2010 * @param bool $templates
2011 * @param int $pageLinkDepth
2014 public static function newFromInterwiki( $interwiki, $page, $history = false,
2015 $templates = false, $pageLinkDepth = 0
2017 if ( $page == '' ) {
2018 return Status
::newFatal( 'import-noarticle' );
2021 # Look up the first interwiki prefix, and let the foreign site handle
2022 # subsequent interwiki prefixes
2023 $firstIwPrefix = strtok( $interwiki, ':' );
2024 $firstIw = Interwiki
::fetch( $firstIwPrefix );
2026 return Status
::newFatal( 'importbadinterwiki' );
2029 $additionalIwPrefixes = strtok( '' );
2030 if ( $additionalIwPrefixes ) {
2031 $additionalIwPrefixes .= ':';
2033 # Have to do a DB-key replacement ourselves; otherwise spaces get
2034 # URL-encoded to +, which is wrong in this case. Similar to logic in
2035 # Title::getLocalURL
2036 $link = $firstIw->getURL( strtr( "${additionalIwPrefixes}Special:Export/$page",
2041 $params['history'] = 1;
2044 $params['templates'] = 1;
2046 if ( $pageLinkDepth ) {
2047 $params['pagelink-depth'] = $pageLinkDepth;
2050 $url = wfAppendQuery( $link, $params );
2051 # For interwikis, use POST to avoid redirects.
2052 return ImportStreamSource
::newFromURL( $url, "POST" );