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
26 use MediaWiki\MediaWikiServices
;
29 * XML file reader for the page data importer.
31 * implements Special:Import
32 * @ingroup SpecialPage
35 private $reader = null;
36 private $foreignNamespaces = null;
37 private $mLogItemCallback, $mUploadCallback, $mRevisionCallback, $mPageCallback;
38 private $mSiteInfoCallback, $mPageOutCallback;
39 private $mNoticeCallback, $mDebug;
40 private $mImportUploads, $mImageBasePath;
41 private $mNoUpdates = false;
42 private $pageOffset = 0;
45 /** @var ImportTitleFactory */
46 private $importTitleFactory;
48 private $countableCache = [];
50 private $disableStatisticsUpdate = false;
53 * Creates an ImportXMLReader drawing from the source provided
54 * @param ImportSource $source
55 * @param Config $config
58 function __construct( ImportSource
$source, Config
$config ) {
59 if ( !class_exists( 'XMLReader' ) ) {
60 throw new Exception( 'Import requires PHP to have been compiled with libxml support' );
63 $this->reader
= new XMLReader();
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( [ $this, 'beforeImportPage' ] );
89 $this->setRevisionCallback( [ $this, "importRevision" ] );
90 $this->setUploadCallback( [ $this, 'importUpload' ] );
91 $this->setLogItemCallback( [ $this, 'importLogItem' ] );
92 $this->setPageOutCallback( [ $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 * Sets 'pageOffset' value. So it will skip the first n-1 pages
148 * and start from the nth page. It's 1-based indexing.
149 * @param int $nthPage
152 function setPageOffset( $nthPage ) {
153 $this->pageOffset
= $nthPage;
157 * Set a callback that displays notice messages
159 * @param callable $callback
162 public function setNoticeCallback( $callback ) {
163 return wfSetVar( $this->mNoticeCallback
, $callback );
167 * Sets the action to perform as each new page in the stream is reached.
168 * @param callable $callback
171 public function setPageCallback( $callback ) {
172 $previous = $this->mPageCallback
;
173 $this->mPageCallback
= $callback;
178 * Sets the action to perform as each page in the stream is completed.
179 * Callback accepts the page title (as a Title object), a second object
180 * with the original title form (in case it's been overridden into a
181 * local namespace), and a count of revisions.
183 * @param callable $callback
186 public function setPageOutCallback( $callback ) {
187 $previous = $this->mPageOutCallback
;
188 $this->mPageOutCallback
= $callback;
193 * Sets the action to perform as each page revision is reached.
194 * @param callable $callback
197 public function setRevisionCallback( $callback ) {
198 $previous = $this->mRevisionCallback
;
199 $this->mRevisionCallback
= $callback;
204 * Sets the action to perform as each file upload version is reached.
205 * @param callable $callback
208 public function setUploadCallback( $callback ) {
209 $previous = $this->mUploadCallback
;
210 $this->mUploadCallback
= $callback;
215 * Sets the action to perform as each log item reached.
216 * @param callable $callback
219 public function setLogItemCallback( $callback ) {
220 $previous = $this->mLogItemCallback
;
221 $this->mLogItemCallback
= $callback;
226 * Sets the action to perform when site info is encountered
227 * @param callable $callback
230 public function setSiteInfoCallback( $callback ) {
231 $previous = $this->mSiteInfoCallback
;
232 $this->mSiteInfoCallback
= $callback;
237 * Sets the factory object to use to convert ForeignTitle objects into local
239 * @param ImportTitleFactory $factory
241 public function setImportTitleFactory( $factory ) {
242 $this->importTitleFactory
= $factory;
246 * Set a target namespace to override the defaults
247 * @param null|int $namespace
250 public function setTargetNamespace( $namespace ) {
251 if ( is_null( $namespace ) ) {
252 // Don't override namespaces
253 $this->setImportTitleFactory( new NaiveImportTitleFactory() );
257 MWNamespace
::exists( intval( $namespace ) )
259 $namespace = intval( $namespace );
260 $this->setImportTitleFactory( new NamespaceImportTitleFactory( $namespace ) );
268 * Set a target root page under which all pages are imported
269 * @param null|string $rootpage
272 public function setTargetRootPage( $rootpage ) {
273 $status = Status
::newGood();
274 if ( is_null( $rootpage ) ) {
276 $this->setImportTitleFactory( new NaiveImportTitleFactory() );
277 } elseif ( $rootpage !== '' ) {
278 $rootpage = rtrim( $rootpage, '/' ); // avoid double slashes
279 $title = Title
::newFromText( $rootpage );
281 if ( !$title ||
$title->isExternal() ) {
282 $status->fatal( 'import-rootpage-invalid' );
284 if ( !MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
287 $displayNSText = $title->getNamespace() == NS_MAIN
288 ?
wfMessage( 'blanknamespace' )->text()
289 : $wgContLang->getNsText( $title->getNamespace() );
290 $status->fatal( 'import-rootpage-nosubpage', $displayNSText );
292 // set namespace to 'all', so the namespace check in processTitle() can pass
293 $this->setTargetNamespace( null );
294 $this->setImportTitleFactory( new SubpageImportTitleFactory( $title ) );
304 public function setImageBasePath( $dir ) {
305 $this->mImageBasePath
= $dir;
309 * @param bool $import
311 public function setImportUploads( $import ) {
312 $this->mImportUploads
= $import;
316 * Statistics update can cause a lot of time
319 public function disableStatisticsUpdate() {
320 $this->disableStatisticsUpdate
= true;
324 * Default per-page callback. Sets up some things related to site statistics
325 * @param array $titleAndForeignTitle Two-element array, with Title object at
326 * index 0 and ForeignTitle object at index 1
329 public function beforeImportPage( $titleAndForeignTitle ) {
330 $title = $titleAndForeignTitle[0];
331 $page = WikiPage
::factory( $title );
332 $this->countableCache
['title_' . $title->getPrefixedText()] = $page->isCountable();
337 * Default per-revision callback, performs the import.
338 * @param WikiRevision $revision
341 public function importRevision( $revision ) {
342 if ( !$revision->getContentHandler()->canBeUsedOn( $revision->getTitle() ) ) {
343 $this->notice( 'import-error-bad-location',
344 $revision->getTitle()->getPrefixedText(),
346 $revision->getModel(),
347 $revision->getFormat() );
353 return $revision->importOldRevision();
354 } catch ( MWContentSerializationException
$ex ) {
355 $this->notice( 'import-error-unserialize',
356 $revision->getTitle()->getPrefixedText(),
358 $revision->getModel(),
359 $revision->getFormat() );
366 * Default per-revision callback, performs the import.
367 * @param WikiRevision $revision
370 public function importLogItem( $revision ) {
371 return $revision->importLogItem();
376 * @param WikiRevision $revision
379 public function importUpload( $revision ) {
380 return $revision->importUpload();
384 * Mostly for hook use
385 * @param Title $title
386 * @param ForeignTitle $foreignTitle
387 * @param int $revCount
388 * @param int $sRevCount
389 * @param array $pageInfo
392 public function finishImportPage( $title, $foreignTitle, $revCount,
393 $sRevCount, $pageInfo
395 // Update article count statistics (T42009)
396 // The normal counting logic in WikiPage->doEditUpdates() is designed for
397 // one-revision-at-a-time editing, not bulk imports. In this situation it
398 // suffers from issues of replica DB lag. We let WikiPage handle the total page
399 // and revision count, and we implement our own custom logic for the
400 // article (content page) count.
401 if ( !$this->disableStatisticsUpdate
) {
402 $page = WikiPage
::factory( $title );
403 $page->loadPageData( 'fromdbmaster' );
404 $content = $page->getContent();
405 if ( $content === null ) {
406 wfDebug( __METHOD__
. ': Skipping article count adjustment for ' . $title .
407 ' because WikiPage::getContent() returned null' );
409 $editInfo = $page->prepareContentForEdit( $content );
410 $countKey = 'title_' . $title->getPrefixedText();
411 $countable = $page->isCountable( $editInfo );
412 if ( array_key_exists( $countKey, $this->countableCache
) &&
413 $countable != $this->countableCache
[$countKey] ) {
414 DeferredUpdates
::addUpdate( SiteStatsUpdate
::factory( [
415 'articles' => ( (int)$countable - (int)$this->countableCache
[$countKey] )
421 $args = func_get_args();
422 return Hooks
::run( 'AfterImportPage', $args );
426 * Alternate per-revision callback, for debugging.
427 * @param WikiRevision &$revision
429 public function debugRevisionHandler( &$revision ) {
430 $this->debug( "Got revision:" );
431 if ( is_object( $revision->title
) ) {
432 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
434 $this->debug( "-- Title: <invalid>" );
436 $this->debug( "-- User: " . $revision->user_text
);
437 $this->debug( "-- Timestamp: " . $revision->timestamp
);
438 $this->debug( "-- Comment: " . $revision->comment
);
439 $this->debug( "-- Text: " . $revision->text
);
443 * Notify the callback function of site info
444 * @param array $siteInfo
447 private function siteInfoCallback( $siteInfo ) {
448 if ( isset( $this->mSiteInfoCallback
) ) {
449 return call_user_func_array( $this->mSiteInfoCallback
,
450 [ $siteInfo, $this ] );
457 * Notify the callback function when a new "<page>" is reached.
458 * @param Title $title
460 function pageCallback( $title ) {
461 if ( isset( $this->mPageCallback
) ) {
462 call_user_func( $this->mPageCallback
, $title );
467 * Notify the callback function when a "</page>" is closed.
468 * @param Title $title
469 * @param ForeignTitle $foreignTitle
470 * @param int $revCount
471 * @param int $sucCount Number of revisions for which callback returned true
472 * @param array $pageInfo Associative array of page information
474 private function pageOutCallback( $title, $foreignTitle, $revCount,
475 $sucCount, $pageInfo ) {
476 if ( isset( $this->mPageOutCallback
) ) {
477 $args = func_get_args();
478 call_user_func_array( $this->mPageOutCallback
, $args );
483 * Notify the callback function of a revision
484 * @param WikiRevision $revision
487 private function revisionCallback( $revision ) {
488 if ( isset( $this->mRevisionCallback
) ) {
489 return call_user_func_array( $this->mRevisionCallback
,
490 [ $revision, $this ] );
497 * Notify the callback function of a new log item
498 * @param WikiRevision $revision
501 private function logItemCallback( $revision ) {
502 if ( isset( $this->mLogItemCallback
) ) {
503 return call_user_func_array( $this->mLogItemCallback
,
504 [ $revision, $this ] );
511 * Retrieves the contents of the named attribute of the current element.
512 * @param string $attr The name of the attribute
513 * @return string The value of the attribute or an empty string if it is not set in the current
516 public function nodeAttribute( $attr ) {
517 return $this->reader
->getAttribute( $attr );
521 * Shouldn't something like this be built-in to XMLReader?
522 * Fetches text contents of the current element, assuming
523 * no sub-elements or such scary things.
527 public function nodeContents() {
528 if ( $this->reader
->isEmptyElement
) {
532 while ( $this->reader
->read() ) {
533 switch ( $this->reader
->nodeType
) {
534 case XMLReader
::TEXT
:
535 case XMLReader
::CDATA
:
536 case XMLReader
::SIGNIFICANT_WHITESPACE
:
537 $buffer .= $this->reader
->value
;
539 case XMLReader
::END_ELEMENT
:
544 $this->reader
->close();
549 * Primary entry point
550 * @throws MWException
553 public function doImport() {
554 // Calls to reader->read need to be wrapped in calls to
555 // libxml_disable_entity_loader() to avoid local file
556 // inclusion attacks (T48932).
557 $oldDisable = libxml_disable_entity_loader( true );
558 $this->reader
->read();
560 if ( $this->reader
->localName
!= 'mediawiki' ) {
561 libxml_disable_entity_loader( $oldDisable );
562 throw new MWException( "Expected <mediawiki> tag, got " .
563 $this->reader
->localName
);
565 $this->debug( "<mediawiki> tag is correct." );
567 $this->debug( "Starting primary dump processing loop." );
569 $keepReading = $this->reader
->read();
574 while ( $keepReading ) {
575 $tag = $this->reader
->localName
;
576 if ( $this->pageOffset
) {
577 if ( $tag === 'page' ) {
580 if ( $pageCount < $this->pageOffset
) {
581 $keepReading = $this->reader
->next();
585 $type = $this->reader
->nodeType
;
587 if ( !Hooks
::run( 'ImportHandleToplevelXMLTag', [ $this ] ) ) {
589 } elseif ( $tag == 'mediawiki' && $type == XMLReader
::END_ELEMENT
) {
591 } elseif ( $tag == 'siteinfo' ) {
592 $this->handleSiteInfo();
593 } elseif ( $tag == 'page' ) {
595 } elseif ( $tag == 'logitem' ) {
596 $this->handleLogItem();
597 } elseif ( $tag != '#text' ) {
598 $this->warn( "Unhandled top-level XML tag $tag" );
604 $keepReading = $this->reader
->next();
606 $this->debug( "Skip" );
608 $keepReading = $this->reader
->read();
611 } catch ( Exception
$ex ) {
616 libxml_disable_entity_loader( $oldDisable );
617 $this->reader
->close();
626 private function handleSiteInfo() {
627 $this->debug( "Enter site info handler." );
630 // Fields that can just be stuffed in the siteInfo object
631 $normalFields = [ 'sitename', 'base', 'generator', 'case' ];
633 while ( $this->reader
->read() ) {
634 if ( $this->reader
->nodeType
== XMLReader
::END_ELEMENT
&&
635 $this->reader
->localName
== 'siteinfo' ) {
639 $tag = $this->reader
->localName
;
641 if ( $tag == 'namespace' ) {
642 $this->foreignNamespaces
[$this->nodeAttribute( 'key' )] =
643 $this->nodeContents();
644 } elseif ( in_array( $tag, $normalFields ) ) {
645 $siteInfo[$tag] = $this->nodeContents();
649 $siteInfo['_namespaces'] = $this->foreignNamespaces
;
650 $this->siteInfoCallback( $siteInfo );
653 private function handleLogItem() {
654 $this->debug( "Enter log item handler." );
657 // Fields that can just be stuffed in the pageInfo object
658 $normalFields = [ 'id', 'comment', 'type', 'action', 'timestamp',
659 'logtitle', 'params' ];
661 while ( $this->reader
->read() ) {
662 if ( $this->reader
->nodeType
== XMLReader
::END_ELEMENT
&&
663 $this->reader
->localName
== 'logitem' ) {
667 $tag = $this->reader
->localName
;
669 if ( !Hooks
::run( 'ImportHandleLogItemXMLTag', [
673 } elseif ( in_array( $tag, $normalFields ) ) {
674 $logInfo[$tag] = $this->nodeContents();
675 } elseif ( $tag == 'contributor' ) {
676 $logInfo['contributor'] = $this->handleContributor();
677 } elseif ( $tag != '#text' ) {
678 $this->warn( "Unhandled log-item XML tag $tag" );
682 $this->processLogItem( $logInfo );
686 * @param array $logInfo
689 private function processLogItem( $logInfo ) {
690 $revision = new WikiRevision( $this->config
);
692 if ( isset( $logInfo['id'] ) ) {
693 $revision->setID( $logInfo['id'] );
695 $revision->setType( $logInfo['type'] );
696 $revision->setAction( $logInfo['action'] );
697 if ( isset( $logInfo['timestamp'] ) ) {
698 $revision->setTimestamp( $logInfo['timestamp'] );
700 if ( isset( $logInfo['params'] ) ) {
701 $revision->setParams( $logInfo['params'] );
703 if ( isset( $logInfo['logtitle'] ) ) {
704 // @todo Using Title for non-local titles is a recipe for disaster.
705 // We should use ForeignTitle here instead.
706 $revision->setTitle( Title
::newFromText( $logInfo['logtitle'] ) );
709 $revision->setNoUpdates( $this->mNoUpdates
);
711 if ( isset( $logInfo['comment'] ) ) {
712 $revision->setComment( $logInfo['comment'] );
715 if ( isset( $logInfo['contributor']['ip'] ) ) {
716 $revision->setUserIP( $logInfo['contributor']['ip'] );
719 if ( !isset( $logInfo['contributor']['username'] ) ) {
720 $revision->setUsername( 'Unknown user' );
722 $revision->setUsername( $logInfo['contributor']['username'] );
725 return $this->logItemCallback( $revision );
728 private function handlePage() {
730 $this->debug( "Enter page handler." );
731 $pageInfo = [ 'revisionCount' => 0, 'successfulRevisionCount' => 0 ];
733 // Fields that can just be stuffed in the pageInfo object
734 $normalFields = [ 'title', 'ns', 'id', 'redirect', 'restrictions' ];
739 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
740 if ( $this->reader
->nodeType
== XMLReader
::END_ELEMENT
&&
741 $this->reader
->localName
== 'page' ) {
747 $tag = $this->reader
->localName
;
750 // The title is invalid, bail out of this page
752 } elseif ( !Hooks
::run( 'ImportHandlePageXMLTag', [ $this,
755 } elseif ( in_array( $tag, $normalFields ) ) {
759 // <title>Page</title>
760 // <redirect title="NewTitle"/>
762 // Because the redirect tag is built differently, we need special handling for that case.
763 if ( $tag == 'redirect' ) {
764 $pageInfo[$tag] = $this->nodeAttribute( 'title' );
766 $pageInfo[$tag] = $this->nodeContents();
768 } elseif ( $tag == 'revision' ||
$tag == 'upload' ) {
769 if ( !isset( $title ) ) {
770 $title = $this->processTitle( $pageInfo['title'],
771 isset( $pageInfo['ns'] ) ?
$pageInfo['ns'] : null );
773 // $title is either an array of two titles or false.
774 if ( is_array( $title ) ) {
775 $this->pageCallback( $title );
776 list( $pageInfo['_title'], $foreignTitle ) = $title;
784 if ( $tag == 'revision' ) {
785 $this->handleRevision( $pageInfo );
787 $this->handleUpload( $pageInfo );
790 } elseif ( $tag != '#text' ) {
791 $this->warn( "Unhandled page XML tag $tag" );
796 // @note $pageInfo is only set if a valid $title is processed above with
797 // no error. If we have a valid $title, then pageCallback is called
798 // above, $pageInfo['title'] is set and we do pageOutCallback here.
799 // If $pageInfo['_title'] is not set, then $foreignTitle is also not
800 // set since they both come from $title above.
801 if ( array_key_exists( '_title', $pageInfo ) ) {
802 $this->pageOutCallback( $pageInfo['_title'], $foreignTitle,
803 $pageInfo['revisionCount'],
804 $pageInfo['successfulRevisionCount'],
810 * @param array $pageInfo
812 private function handleRevision( &$pageInfo ) {
813 $this->debug( "Enter revision handler" );
816 $normalFields = [ 'id', 'timestamp', 'comment', 'minor', 'model', 'format', 'text' ];
820 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
821 if ( $this->reader
->nodeType
== XMLReader
::END_ELEMENT
&&
822 $this->reader
->localName
== 'revision' ) {
826 $tag = $this->reader
->localName
;
828 if ( !Hooks
::run( 'ImportHandleRevisionXMLTag', [
829 $this, $pageInfo, $revisionInfo
832 } elseif ( in_array( $tag, $normalFields ) ) {
833 $revisionInfo[$tag] = $this->nodeContents();
834 } elseif ( $tag == 'contributor' ) {
835 $revisionInfo['contributor'] = $this->handleContributor();
836 } elseif ( $tag != '#text' ) {
837 $this->warn( "Unhandled revision XML tag $tag" );
842 $pageInfo['revisionCount']++
;
843 if ( $this->processRevision( $pageInfo, $revisionInfo ) ) {
844 $pageInfo['successfulRevisionCount']++
;
849 * @param array $pageInfo
850 * @param array $revisionInfo
853 private function processRevision( $pageInfo, $revisionInfo ) {
854 global $wgMaxArticleSize;
856 // Make sure revisions won't violate $wgMaxArticleSize, which could lead to
857 // database errors and instability. Testing for revisions with only listed
858 // content models, as other content models might use serialization formats
859 // which aren't checked against $wgMaxArticleSize.
860 if ( ( !isset( $revisionInfo['model'] ) ||
861 in_array( $revisionInfo['model'], [
869 strlen( $revisionInfo['text'] ) > $wgMaxArticleSize * 1024
871 throw new MWException( 'The text of ' .
872 ( isset( $revisionInfo['id'] ) ?
873 "the revision with ID $revisionInfo[id]" :
875 ) . " exceeds the maximum allowable size ($wgMaxArticleSize KB)" );
878 $revision = new WikiRevision( $this->config
);
880 if ( isset( $revisionInfo['id'] ) ) {
881 $revision->setID( $revisionInfo['id'] );
883 if ( isset( $revisionInfo['model'] ) ) {
884 $revision->setModel( $revisionInfo['model'] );
886 if ( isset( $revisionInfo['format'] ) ) {
887 $revision->setFormat( $revisionInfo['format'] );
889 $revision->setTitle( $pageInfo['_title'] );
891 if ( isset( $revisionInfo['text'] ) ) {
892 $handler = $revision->getContentHandler();
893 $text = $handler->importTransform(
894 $revisionInfo['text'],
895 $revision->getFormat() );
897 $revision->setText( $text );
899 if ( isset( $revisionInfo['timestamp'] ) ) {
900 $revision->setTimestamp( $revisionInfo['timestamp'] );
902 $revision->setTimestamp( wfTimestampNow() );
905 if ( isset( $revisionInfo['comment'] ) ) {
906 $revision->setComment( $revisionInfo['comment'] );
909 if ( isset( $revisionInfo['minor'] ) ) {
910 $revision->setMinor( true );
912 if ( isset( $revisionInfo['contributor']['ip'] ) ) {
913 $revision->setUserIP( $revisionInfo['contributor']['ip'] );
914 } elseif ( isset( $revisionInfo['contributor']['username'] ) ) {
915 $revision->setUsername( $revisionInfo['contributor']['username'] );
917 $revision->setUsername( 'Unknown user' );
919 $revision->setNoUpdates( $this->mNoUpdates
);
921 return $this->revisionCallback( $revision );
925 * @param array $pageInfo
928 private function handleUpload( &$pageInfo ) {
929 $this->debug( "Enter upload handler" );
932 $normalFields = [ 'timestamp', 'comment', 'filename', 'text',
933 'src', 'size', 'sha1base36', 'archivename', 'rel' ];
937 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
938 if ( $this->reader
->nodeType
== XMLReader
::END_ELEMENT
&&
939 $this->reader
->localName
== 'upload' ) {
943 $tag = $this->reader
->localName
;
945 if ( !Hooks
::run( 'ImportHandleUploadXMLTag', [
949 } elseif ( in_array( $tag, $normalFields ) ) {
950 $uploadInfo[$tag] = $this->nodeContents();
951 } elseif ( $tag == 'contributor' ) {
952 $uploadInfo['contributor'] = $this->handleContributor();
953 } elseif ( $tag == 'contents' ) {
954 $contents = $this->nodeContents();
955 $encoding = $this->reader
->getAttribute( 'encoding' );
956 if ( $encoding === 'base64' ) {
957 $uploadInfo['fileSrc'] = $this->dumpTemp( base64_decode( $contents ) );
958 $uploadInfo['isTempSrc'] = true;
960 } elseif ( $tag != '#text' ) {
961 $this->warn( "Unhandled upload XML tag $tag" );
966 if ( $this->mImageBasePath
&& isset( $uploadInfo['rel'] ) ) {
967 $path = "{$this->mImageBasePath}/{$uploadInfo['rel']}";
968 if ( file_exists( $path ) ) {
969 $uploadInfo['fileSrc'] = $path;
970 $uploadInfo['isTempSrc'] = false;
974 if ( $this->mImportUploads
) {
975 return $this->processUpload( $pageInfo, $uploadInfo );
980 * @param string $contents
983 private function dumpTemp( $contents ) {
984 $filename = tempnam( wfTempDir(), 'importupload' );
985 file_put_contents( $filename, $contents );
990 * @param array $pageInfo
991 * @param array $uploadInfo
994 private function processUpload( $pageInfo, $uploadInfo ) {
995 $revision = new WikiRevision( $this->config
);
996 $text = isset( $uploadInfo['text'] ) ?
$uploadInfo['text'] : '';
998 $revision->setTitle( $pageInfo['_title'] );
999 $revision->setID( $pageInfo['id'] );
1000 $revision->setTimestamp( $uploadInfo['timestamp'] );
1001 $revision->setText( $text );
1002 $revision->setFilename( $uploadInfo['filename'] );
1003 if ( isset( $uploadInfo['archivename'] ) ) {
1004 $revision->setArchiveName( $uploadInfo['archivename'] );
1006 $revision->setSrc( $uploadInfo['src'] );
1007 if ( isset( $uploadInfo['fileSrc'] ) ) {
1008 $revision->setFileSrc( $uploadInfo['fileSrc'],
1009 !empty( $uploadInfo['isTempSrc'] ) );
1011 if ( isset( $uploadInfo['sha1base36'] ) ) {
1012 $revision->setSha1Base36( $uploadInfo['sha1base36'] );
1014 $revision->setSize( intval( $uploadInfo['size'] ) );
1015 $revision->setComment( $uploadInfo['comment'] );
1017 if ( isset( $uploadInfo['contributor']['ip'] ) ) {
1018 $revision->setUserIP( $uploadInfo['contributor']['ip'] );
1020 if ( isset( $uploadInfo['contributor']['username'] ) ) {
1021 $revision->setUsername( $uploadInfo['contributor']['username'] );
1023 $revision->setNoUpdates( $this->mNoUpdates
);
1025 return call_user_func( $this->mUploadCallback
, $revision );
1031 private function handleContributor() {
1032 $fields = [ 'id', 'ip', 'username' ];
1035 if ( $this->reader
->isEmptyElement
) {
1038 while ( $this->reader
->read() ) {
1039 if ( $this->reader
->nodeType
== XMLReader
::END_ELEMENT
&&
1040 $this->reader
->localName
== 'contributor' ) {
1044 $tag = $this->reader
->localName
;
1046 if ( in_array( $tag, $fields ) ) {
1047 $info[$tag] = $this->nodeContents();
1055 * @param string $text
1056 * @param string|null $ns
1057 * @return array|bool
1059 private function processTitle( $text, $ns = null ) {
1060 if ( is_null( $this->foreignNamespaces
) ) {
1061 $foreignTitleFactory = new NaiveForeignTitleFactory();
1063 $foreignTitleFactory = new NamespaceAwareForeignTitleFactory(
1064 $this->foreignNamespaces
);
1067 $foreignTitle = $foreignTitleFactory->createForeignTitle( $text,
1070 $title = $this->importTitleFactory
->createTitleFromForeignTitle(
1073 $commandLineMode = $this->config
->get( 'CommandLineMode' );
1074 if ( is_null( $title ) ) {
1075 # Invalid page title? Ignore the page
1076 $this->notice( 'import-error-invalid', $foreignTitle->getFullText() );
1078 } elseif ( $title->isExternal() ) {
1079 $this->notice( 'import-error-interwiki', $title->getPrefixedText() );
1081 } elseif ( !$title->canExist() ) {
1082 $this->notice( 'import-error-special', $title->getPrefixedText() );
1084 } elseif ( !$title->userCan( 'edit' ) && !$commandLineMode ) {
1085 # Do not import if the importing wiki user cannot edit this page
1086 $this->notice( 'import-error-edit', $title->getPrefixedText() );
1088 } elseif ( !$title->exists() && !$title->userCan( 'create' ) && !$commandLineMode ) {
1089 # Do not import if the importing wiki user cannot create this page
1090 $this->notice( 'import-error-create', $title->getPrefixedText() );
1094 return [ $title, $foreignTitle ];