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 $mLogItemCallback, $mUploadCallback, $mRevisionCallback, $mPageCallback;
36 private $mSiteInfoCallback, $mTargetNamespace, $mTargetRootPage, $mPageOutCallback;
37 private $mNoticeCallback, $mDebug;
38 private $mImportUploads, $mImageBasePath;
39 private $mNoUpdates = false;
42 * Creates an ImportXMLReader drawing from the source provided
43 * @param ImportStreamSource $source
45 function __construct( ImportStreamSource
$source ) {
46 $this->reader
= new XMLReader();
48 if ( !in_array( 'uploadsource', stream_get_wrappers() ) ) {
49 stream_wrapper_register( 'uploadsource', 'UploadSourceAdapter' );
51 $id = UploadSourceAdapter
::registerSource( $source );
52 if ( defined( 'LIBXML_PARSEHUGE' ) ) {
53 $this->reader
->open( "uploadsource://$id", null, LIBXML_PARSEHUGE
);
55 $this->reader
->open( "uploadsource://$id" );
59 $this->setRevisionCallback( array( $this, "importRevision" ) );
60 $this->setUploadCallback( array( $this, 'importUpload' ) );
61 $this->setLogItemCallback( array( $this, 'importLogItem' ) );
62 $this->setPageOutCallback( array( $this, 'finishImportPage' ) );
66 * @return null|XMLReader
68 public function getReader() {
72 public function throwXmlError( $err ) {
73 $this->debug( "FAILURE: $err" );
74 wfDebug( "WikiImporter XML error: $err\n" );
77 public function debug( $data ) {
78 if ( $this->mDebug
) {
79 wfDebug( "IMPORT: $data\n" );
83 public function warn( $data ) {
84 wfDebug( "IMPORT: $data\n" );
87 public function notice( $msg /*, $param, ...*/ ) {
88 $params = func_get_args();
89 array_shift( $params );
91 if ( is_callable( $this->mNoticeCallback
) ) {
92 call_user_func( $this->mNoticeCallback
, $msg, $params );
93 } else { # No ImportReporter -> CLI
94 echo wfMessage( $msg, $params )->text() . "\n";
102 function setDebug( $debug ) {
103 $this->mDebug
= $debug;
107 * Set 'no updates' mode. In this mode, the link tables will not be updated by the importer
108 * @param bool $noupdates
110 function setNoUpdates( $noupdates ) {
111 $this->mNoUpdates
= $noupdates;
115 * Set a callback that displays notice messages
117 * @param callable $callback
120 public function setNoticeCallback( $callback ) {
121 return wfSetVar( $this->mNoticeCallback
, $callback );
125 * Sets the action to perform as each new page in the stream is reached.
126 * @param callable $callback
129 public function setPageCallback( $callback ) {
130 $previous = $this->mPageCallback
;
131 $this->mPageCallback
= $callback;
136 * Sets the action to perform as each page in the stream is completed.
137 * Callback accepts the page title (as a Title object), a second object
138 * with the original title form (in case it's been overridden into a
139 * local namespace), and a count of revisions.
141 * @param callable $callback
144 public function setPageOutCallback( $callback ) {
145 $previous = $this->mPageOutCallback
;
146 $this->mPageOutCallback
= $callback;
151 * Sets the action to perform as each page revision is reached.
152 * @param callable $callback
155 public function setRevisionCallback( $callback ) {
156 $previous = $this->mRevisionCallback
;
157 $this->mRevisionCallback
= $callback;
162 * Sets the action to perform as each file upload version is reached.
163 * @param callable $callback
166 public function setUploadCallback( $callback ) {
167 $previous = $this->mUploadCallback
;
168 $this->mUploadCallback
= $callback;
173 * Sets the action to perform as each log item reached.
174 * @param callable $callback
177 public function setLogItemCallback( $callback ) {
178 $previous = $this->mLogItemCallback
;
179 $this->mLogItemCallback
= $callback;
184 * Sets the action to perform when site info is encountered
185 * @param callable $callback
188 public function setSiteInfoCallback( $callback ) {
189 $previous = $this->mSiteInfoCallback
;
190 $this->mSiteInfoCallback
= $callback;
195 * Set a target namespace to override the defaults
196 * @param null|int $namespace
199 public function setTargetNamespace( $namespace ) {
200 if ( is_null( $namespace ) ) {
201 // Don't override namespaces
202 $this->mTargetNamespace
= null;
203 } elseif ( $namespace >= 0 ) {
204 // @todo FIXME: Check for validity
205 $this->mTargetNamespace
= intval( $namespace );
212 * Set a target root page under which all pages are imported
213 * @param null|string $rootpage
216 public function setTargetRootPage( $rootpage ) {
217 $status = Status
::newGood();
218 if ( is_null( $rootpage ) ) {
220 $this->mTargetRootPage
= null;
221 } elseif ( $rootpage !== '' ) {
222 $rootpage = rtrim( $rootpage, '/' ); //avoid double slashes
223 $title = Title
::newFromText( $rootpage, !is_null( $this->mTargetNamespace
)
224 ?
$this->mTargetNamespace
228 if ( !$title ||
$title->isExternal() ) {
229 $status->fatal( 'import-rootpage-invalid' );
231 if ( !MWNamespace
::hasSubpages( $title->getNamespace() ) ) {
234 $displayNSText = $title->getNamespace() == NS_MAIN
235 ?
wfMessage( 'blanknamespace' )->text()
236 : $wgContLang->getNsText( $title->getNamespace() );
237 $status->fatal( 'import-rootpage-nosubpage', $displayNSText );
239 // set namespace to 'all', so the namespace check in processTitle() can passed
240 $this->setTargetNamespace( null );
241 $this->mTargetRootPage
= $title->getPrefixedDBkey();
251 public function setImageBasePath( $dir ) {
252 $this->mImageBasePath
= $dir;
256 * @param bool $import
258 public function setImportUploads( $import ) {
259 $this->mImportUploads
= $import;
263 * Default per-revision callback, performs the import.
264 * @param WikiRevision $revision
267 public function importRevision( $revision ) {
268 if ( !$revision->getContentHandler()->canBeUsedOn( $revision->getTitle() ) ) {
269 $this->notice( 'import-error-bad-location',
270 $revision->getTitle()->getPrefixedText(),
272 $revision->getModel(),
273 $revision->getFormat() );
279 $dbw = wfGetDB( DB_MASTER
);
280 return $dbw->deadlockLoop( array( $revision, 'importOldRevision' ) );
281 } catch ( MWContentSerializationException
$ex ) {
282 $this->notice( 'import-error-unserialize',
283 $revision->getTitle()->getPrefixedText(),
285 $revision->getModel(),
286 $revision->getFormat() );
293 * Default per-revision callback, performs the import.
294 * @param WikiRevision $revision
297 public function importLogItem( $revision ) {
298 $dbw = wfGetDB( DB_MASTER
);
299 return $dbw->deadlockLoop( array( $revision, 'importLogItem' ) );
304 * @param WikiRevision $revision
307 public function importUpload( $revision ) {
308 $dbw = wfGetDB( DB_MASTER
);
309 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
313 * Mostly for hook use
314 * @param Title $title
315 * @param string $origTitle
316 * @param int $revCount
317 * @param int $sRevCount
318 * @param array $pageInfo
321 public function finishImportPage( $title, $origTitle, $revCount, $sRevCount, $pageInfo ) {
322 $args = func_get_args();
323 return wfRunHooks( 'AfterImportPage', $args );
327 * Alternate per-revision callback, for debugging.
328 * @param WikiRevision $revision
330 public function debugRevisionHandler( &$revision ) {
331 $this->debug( "Got revision:" );
332 if ( is_object( $revision->title
) ) {
333 $this->debug( "-- Title: " . $revision->title
->getPrefixedText() );
335 $this->debug( "-- Title: <invalid>" );
337 $this->debug( "-- User: " . $revision->user_text
);
338 $this->debug( "-- Timestamp: " . $revision->timestamp
);
339 $this->debug( "-- Comment: " . $revision->comment
);
340 $this->debug( "-- Text: " . $revision->text
);
344 * Notify the callback function when a new "<page>" is reached.
345 * @param Title $title
347 function pageCallback( $title ) {
348 if ( isset( $this->mPageCallback
) ) {
349 call_user_func( $this->mPageCallback
, $title );
354 * Notify the callback function when a "</page>" is closed.
355 * @param Title $title
356 * @param Title $origTitle
357 * @param int $revCount
358 * @param int $sucCount Number of revisions for which callback returned true
359 * @param array $pageInfo Associative array of page information
361 private function pageOutCallback( $title, $origTitle, $revCount, $sucCount, $pageInfo ) {
362 if ( isset( $this->mPageOutCallback
) ) {
363 $args = func_get_args();
364 call_user_func_array( $this->mPageOutCallback
, $args );
369 * Notify the callback function of a revision
370 * @param WikiRevision $revision
373 private function revisionCallback( $revision ) {
374 if ( isset( $this->mRevisionCallback
) ) {
375 return call_user_func_array( $this->mRevisionCallback
,
376 array( $revision, $this ) );
383 * Notify the callback function of a new log item
384 * @param WikiRevision $revision
387 private function logItemCallback( $revision ) {
388 if ( isset( $this->mLogItemCallback
) ) {
389 return call_user_func_array( $this->mLogItemCallback
,
390 array( $revision, $this ) );
397 * Retrieves the contents of the named attribute of the current element.
398 * @param string $attr The name of the attribute
399 * @return string The value of the attribute or an empty string if it is not set in the current element.
401 public function nodeAttribute( $attr ) {
402 return $this->reader
->getAttribute( $attr );
406 * Shouldn't something like this be built-in to XMLReader?
407 * Fetches text contents of the current element, assuming
408 * no sub-elements or such scary things.
412 public function nodeContents() {
413 if ( $this->reader
->isEmptyElement
) {
417 while ( $this->reader
->read() ) {
418 switch ( $this->reader
->nodeType
) {
419 case XmlReader
::TEXT
:
420 case XmlReader
::SIGNIFICANT_WHITESPACE
:
421 $buffer .= $this->reader
->value
;
423 case XmlReader
::END_ELEMENT
:
428 $this->reader
->close();
433 * Primary entry point
434 * @throws MWException
437 public function doImport() {
438 // Calls to reader->read need to be wrapped in calls to
439 // libxml_disable_entity_loader() to avoid local file
440 // inclusion attacks (bug 46932).
441 $oldDisable = libxml_disable_entity_loader( true );
442 $this->reader
->read();
444 if ( $this->reader
->name
!= 'mediawiki' ) {
445 libxml_disable_entity_loader( $oldDisable );
446 throw new MWException( "Expected <mediawiki> tag, got " .
447 $this->reader
->name
);
449 $this->debug( "<mediawiki> tag is correct." );
451 $this->debug( "Starting primary dump processing loop." );
453 $keepReading = $this->reader
->read();
455 while ( $keepReading ) {
456 $tag = $this->reader
->name
;
457 $type = $this->reader
->nodeType
;
459 if ( !wfRunHooks( 'ImportHandleToplevelXMLTag', array( $this ) ) ) {
461 } elseif ( $tag == 'mediawiki' && $type == XmlReader
::END_ELEMENT
) {
463 } elseif ( $tag == 'siteinfo' ) {
464 $this->handleSiteInfo();
465 } elseif ( $tag == 'page' ) {
467 } elseif ( $tag == 'logitem' ) {
468 $this->handleLogItem();
469 } elseif ( $tag != '#text' ) {
470 $this->warn( "Unhandled top-level XML tag $tag" );
476 $keepReading = $this->reader
->next();
478 $this->debug( "Skip" );
480 $keepReading = $this->reader
->read();
484 libxml_disable_entity_loader( $oldDisable );
490 * @throws MWException
492 private function handleSiteInfo() {
493 // Site info is useful, but not actually used for dump imports.
494 // Includes a quick short-circuit to save performance.
495 if ( !$this->mSiteInfoCallback
) {
496 $this->reader
->next();
499 throw new MWException( "SiteInfo tag is not yet handled, do not set mSiteInfoCallback" );
502 private function handleLogItem() {
503 $this->debug( "Enter log item handler." );
506 // Fields that can just be stuffed in the pageInfo object
507 $normalFields = array( 'id', 'comment', 'type', 'action', 'timestamp',
508 'logtitle', 'params' );
510 while ( $this->reader
->read() ) {
511 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
512 $this->reader
->name
== 'logitem' ) {
516 $tag = $this->reader
->name
;
518 if ( !wfRunHooks( 'ImportHandleLogItemXMLTag', array(
522 } elseif ( in_array( $tag, $normalFields ) ) {
523 $logInfo[$tag] = $this->nodeContents();
524 } elseif ( $tag == 'contributor' ) {
525 $logInfo['contributor'] = $this->handleContributor();
526 } elseif ( $tag != '#text' ) {
527 $this->warn( "Unhandled log-item XML tag $tag" );
531 $this->processLogItem( $logInfo );
535 * @param array $logInfo
538 private function processLogItem( $logInfo ) {
539 $revision = new WikiRevision
;
541 $revision->setID( $logInfo['id'] );
542 $revision->setType( $logInfo['type'] );
543 $revision->setAction( $logInfo['action'] );
544 $revision->setTimestamp( $logInfo['timestamp'] );
545 $revision->setParams( $logInfo['params'] );
546 $revision->setTitle( Title
::newFromText( $logInfo['logtitle'] ) );
547 $revision->setNoUpdates( $this->mNoUpdates
);
549 if ( isset( $logInfo['comment'] ) ) {
550 $revision->setComment( $logInfo['comment'] );
553 if ( isset( $logInfo['contributor']['ip'] ) ) {
554 $revision->setUserIP( $logInfo['contributor']['ip'] );
556 if ( isset( $logInfo['contributor']['username'] ) ) {
557 $revision->setUserName( $logInfo['contributor']['username'] );
560 return $this->logItemCallback( $revision );
563 private function handlePage() {
565 $this->debug( "Enter page handler." );
566 $pageInfo = array( 'revisionCount' => 0, 'successfulRevisionCount' => 0 );
568 // Fields that can just be stuffed in the pageInfo object
569 $normalFields = array( 'title', 'id', 'redirect', 'restrictions' );
574 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
575 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
576 $this->reader
->name
== 'page' ) {
580 $tag = $this->reader
->name
;
583 // The title is invalid, bail out of this page
585 } elseif ( !wfRunHooks( 'ImportHandlePageXMLTag', array( $this,
588 } elseif ( in_array( $tag, $normalFields ) ) {
592 // <title>Page</title>
593 // <redirect title="NewTitle"/>
595 // Because the redirect tag is built differently, we need special handling for that case.
596 if ( $tag == 'redirect' ) {
597 $pageInfo[$tag] = $this->nodeAttribute( 'title' );
599 $pageInfo[$tag] = $this->nodeContents();
600 if ( $tag == 'title' ) {
601 $title = $this->processTitle( $pageInfo['title'] );
608 $this->pageCallback( $title );
609 list( $pageInfo['_title'], $origTitle ) = $title;
612 } elseif ( $tag == 'revision' ) {
613 $this->handleRevision( $pageInfo );
614 } elseif ( $tag == 'upload' ) {
615 $this->handleUpload( $pageInfo );
616 } elseif ( $tag != '#text' ) {
617 $this->warn( "Unhandled page XML tag $tag" );
622 $this->pageOutCallback( $pageInfo['_title'], $origTitle,
623 $pageInfo['revisionCount'],
624 $pageInfo['successfulRevisionCount'],
629 * @param array $pageInfo
631 private function handleRevision( &$pageInfo ) {
632 $this->debug( "Enter revision handler" );
633 $revisionInfo = array();
635 $normalFields = array( 'id', 'timestamp', 'comment', 'minor', 'model', 'format', 'text' );
639 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
640 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
641 $this->reader
->name
== 'revision' ) {
645 $tag = $this->reader
->name
;
647 if ( !wfRunHooks( 'ImportHandleRevisionXMLTag', array(
648 $this, $pageInfo, $revisionInfo
651 } elseif ( in_array( $tag, $normalFields ) ) {
652 $revisionInfo[$tag] = $this->nodeContents();
653 } elseif ( $tag == 'contributor' ) {
654 $revisionInfo['contributor'] = $this->handleContributor();
655 } elseif ( $tag != '#text' ) {
656 $this->warn( "Unhandled revision XML tag $tag" );
661 $pageInfo['revisionCount']++
;
662 if ( $this->processRevision( $pageInfo, $revisionInfo ) ) {
663 $pageInfo['successfulRevisionCount']++
;
668 * @param array $pageInfo
669 * @param array $revisionInfo
672 private function processRevision( $pageInfo, $revisionInfo ) {
673 $revision = new WikiRevision
;
675 if ( isset( $revisionInfo['id'] ) ) {
676 $revision->setID( $revisionInfo['id'] );
678 if ( isset( $revisionInfo['model'] ) ) {
679 $revision->setModel( $revisionInfo['model'] );
681 if ( isset( $revisionInfo['format'] ) ) {
682 $revision->setFormat( $revisionInfo['format'] );
684 $revision->setTitle( $pageInfo['_title'] );
686 if ( isset( $revisionInfo['text'] ) ) {
687 $handler = $revision->getContentHandler();
688 $text = $handler->importTransform(
689 $revisionInfo['text'],
690 $revision->getFormat() );
692 $revision->setText( $text );
694 if ( isset( $revisionInfo['timestamp'] ) ) {
695 $revision->setTimestamp( $revisionInfo['timestamp'] );
697 $revision->setTimestamp( wfTimestampNow() );
700 if ( isset( $revisionInfo['comment'] ) ) {
701 $revision->setComment( $revisionInfo['comment'] );
704 if ( isset( $revisionInfo['minor'] ) ) {
705 $revision->setMinor( true );
707 if ( isset( $revisionInfo['contributor']['ip'] ) ) {
708 $revision->setUserIP( $revisionInfo['contributor']['ip'] );
710 if ( isset( $revisionInfo['contributor']['username'] ) ) {
711 $revision->setUserName( $revisionInfo['contributor']['username'] );
713 $revision->setNoUpdates( $this->mNoUpdates
);
715 return $this->revisionCallback( $revision );
719 * @param array $pageInfo
722 private function handleUpload( &$pageInfo ) {
723 $this->debug( "Enter upload handler" );
724 $uploadInfo = array();
726 $normalFields = array( 'timestamp', 'comment', 'filename', 'text',
727 'src', 'size', 'sha1base36', 'archivename', 'rel' );
731 while ( $skip ?
$this->reader
->next() : $this->reader
->read() ) {
732 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
733 $this->reader
->name
== 'upload' ) {
737 $tag = $this->reader
->name
;
739 if ( !wfRunHooks( 'ImportHandleUploadXMLTag', array(
743 } elseif ( in_array( $tag, $normalFields ) ) {
744 $uploadInfo[$tag] = $this->nodeContents();
745 } elseif ( $tag == 'contributor' ) {
746 $uploadInfo['contributor'] = $this->handleContributor();
747 } elseif ( $tag == 'contents' ) {
748 $contents = $this->nodeContents();
749 $encoding = $this->reader
->getAttribute( 'encoding' );
750 if ( $encoding === 'base64' ) {
751 $uploadInfo['fileSrc'] = $this->dumpTemp( base64_decode( $contents ) );
752 $uploadInfo['isTempSrc'] = true;
754 } elseif ( $tag != '#text' ) {
755 $this->warn( "Unhandled upload XML tag $tag" );
760 if ( $this->mImageBasePath
&& isset( $uploadInfo['rel'] ) ) {
761 $path = "{$this->mImageBasePath}/{$uploadInfo['rel']}";
762 if ( file_exists( $path ) ) {
763 $uploadInfo['fileSrc'] = $path;
764 $uploadInfo['isTempSrc'] = false;
768 if ( $this->mImportUploads
) {
769 return $this->processUpload( $pageInfo, $uploadInfo );
774 * @param string $contents
777 private function dumpTemp( $contents ) {
778 $filename = tempnam( wfTempDir(), 'importupload' );
779 file_put_contents( $filename, $contents );
784 * @param array $pageInfo
785 * @param array $uploadInfo
788 private function processUpload( $pageInfo, $uploadInfo ) {
789 $revision = new WikiRevision
;
790 $text = isset( $uploadInfo['text'] ) ?
$uploadInfo['text'] : '';
792 $revision->setTitle( $pageInfo['_title'] );
793 $revision->setID( $pageInfo['id'] );
794 $revision->setTimestamp( $uploadInfo['timestamp'] );
795 $revision->setText( $text );
796 $revision->setFilename( $uploadInfo['filename'] );
797 if ( isset( $uploadInfo['archivename'] ) ) {
798 $revision->setArchiveName( $uploadInfo['archivename'] );
800 $revision->setSrc( $uploadInfo['src'] );
801 if ( isset( $uploadInfo['fileSrc'] ) ) {
802 $revision->setFileSrc( $uploadInfo['fileSrc'],
803 !empty( $uploadInfo['isTempSrc'] ) );
805 if ( isset( $uploadInfo['sha1base36'] ) ) {
806 $revision->setSha1Base36( $uploadInfo['sha1base36'] );
808 $revision->setSize( intval( $uploadInfo['size'] ) );
809 $revision->setComment( $uploadInfo['comment'] );
811 if ( isset( $uploadInfo['contributor']['ip'] ) ) {
812 $revision->setUserIP( $uploadInfo['contributor']['ip'] );
814 if ( isset( $uploadInfo['contributor']['username'] ) ) {
815 $revision->setUserName( $uploadInfo['contributor']['username'] );
817 $revision->setNoUpdates( $this->mNoUpdates
);
819 return call_user_func( $this->mUploadCallback
, $revision );
825 private function handleContributor() {
826 $fields = array( 'id', 'ip', 'username' );
829 while ( $this->reader
->read() ) {
830 if ( $this->reader
->nodeType
== XmlReader
::END_ELEMENT
&&
831 $this->reader
->name
== 'contributor' ) {
835 $tag = $this->reader
->name
;
837 if ( in_array( $tag, $fields ) ) {
838 $info[$tag] = $this->nodeContents();
846 * @param string $text
849 private function processTitle( $text ) {
850 global $wgCommandLineMode;
853 $origTitle = Title
::newFromText( $workTitle );
855 if ( !is_null( $this->mTargetNamespace
) && !is_null( $origTitle ) ) {
856 # makeTitleSafe, because $origTitle can have a interwiki (different setting of interwiki map)
857 # and than dbKey can begin with a lowercase char
858 $title = Title
::makeTitleSafe( $this->mTargetNamespace
,
859 $origTitle->getDBkey() );
861 if ( !is_null( $this->mTargetRootPage
) ) {
862 $workTitle = $this->mTargetRootPage
. '/' . $workTitle;
864 $title = Title
::newFromText( $workTitle );
867 if ( is_null( $title ) ) {
868 # Invalid page title? Ignore the page
869 $this->notice( 'import-error-invalid', $workTitle );
871 } elseif ( $title->isExternal() ) {
872 $this->notice( 'import-error-interwiki', $title->getPrefixedText() );
874 } elseif ( !$title->canExist() ) {
875 $this->notice( 'import-error-special', $title->getPrefixedText() );
877 } elseif ( !$title->userCan( 'edit' ) && !$wgCommandLineMode ) {
878 # Do not import if the importing wiki user cannot edit this page
879 $this->notice( 'import-error-edit', $title->getPrefixedText() );
881 } elseif ( !$title->exists() && !$title->userCan( 'create' ) && !$wgCommandLineMode ) {
882 # Do not import if the importing wiki user cannot create this page
883 $this->notice( 'import-error-create', $title->getPrefixedText() );
887 return array( $title, $origTitle );
891 /** This is a horrible hack used to keep source compatibility */
892 class UploadSourceAdapter
{
894 private static $sourceRegistrations = array();
906 * @param ImportStreamSource $source
909 static function registerSource( ImportStreamSource
$source ) {
910 $id = wfRandomString();
912 self
::$sourceRegistrations[$id] = $source;
918 * @param string $path
919 * @param string $mode
920 * @param array $options
921 * @param string $opened_path
924 function stream_open( $path, $mode, $options, &$opened_path ) {
925 $url = parse_url( $path );
928 if ( !isset( self
::$sourceRegistrations[$id] ) ) {
932 $this->mSource
= self
::$sourceRegistrations[$id];
941 function stream_read( $count ) {
945 while ( !$leave && !$this->mSource
->atEnd() &&
946 strlen( $this->mBuffer
) < $count ) {
947 $read = $this->mSource
->readChunk();
949 if ( !strlen( $read ) ) {
953 $this->mBuffer
.= $read;
956 if ( strlen( $this->mBuffer
) ) {
957 $return = substr( $this->mBuffer
, 0, $count );
958 $this->mBuffer
= substr( $this->mBuffer
, $count );
961 $this->mPosition +
= strlen( $return );
967 * @param string $data
970 function stream_write( $data ) {
977 function stream_tell() {
978 return $this->mPosition
;
984 function stream_eof() {
985 return $this->mSource
->atEnd();
991 function url_stat() {
994 $result['dev'] = $result[0] = 0;
995 $result['ino'] = $result[1] = 0;
996 $result['mode'] = $result[2] = 0;
997 $result['nlink'] = $result[3] = 0;
998 $result['uid'] = $result[4] = 0;
999 $result['gid'] = $result[5] = 0;
1000 $result['rdev'] = $result[6] = 0;
1001 $result['size'] = $result[7] = 0;
1002 $result['atime'] = $result[8] = 0;
1003 $result['mtime'] = $result[9] = 0;
1004 $result['ctime'] = $result[10] = 0;
1005 $result['blksize'] = $result[11] = 0;
1006 $result['blocks'] = $result[12] = 0;
1013 * @todo document (e.g. one-sentence class description).
1014 * @ingroup SpecialPage
1016 class WikiRevision
{
1017 /** @todo Unused? */
1018 private $importer = null;
1021 public $title = null;
1027 public $timestamp = "20010115000000";
1031 * @todo Can't find any uses. Public, because that's suspicious. Get clarity. */
1035 public $user_text = "";
1038 protected $model = null;
1041 protected $format = null;
1050 protected $content = null;
1052 /** @var ContentHandler */
1053 protected $contentHandler = null;
1056 public $comment = "";
1059 protected $minor = false;
1062 protected $type = "";
1065 protected $action = "";
1068 protected $params = "";
1071 protected $fileSrc = '';
1073 /** @var bool|string */
1074 protected $sha1base36 = false;
1080 private $isTemp = false;
1083 protected $archiveName = '';
1085 protected $filename;
1090 /** @todo Unused? */
1091 private $fileIsTemp;
1094 private $mNoUpdates = false;
1097 * @param Title $title
1098 * @throws MWException
1100 function setTitle( $title ) {
1101 if ( is_object( $title ) ) {
1102 $this->title
= $title;
1103 } elseif ( is_null( $title ) ) {
1104 throw new MWException( "WikiRevision given a null title in import. "
1105 . "You may need to adjust \$wgLegalTitleChars." );
1107 throw new MWException( "WikiRevision given non-object title in import." );
1114 function setID( $id ) {
1121 function setTimestamp( $ts ) {
1122 # 2003-08-05T18:30:02Z
1123 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
1127 * @param string $user
1129 function setUsername( $user ) {
1130 $this->user_text
= $user;
1136 function setUserIP( $ip ) {
1137 $this->user_text
= $ip;
1141 * @param string $model
1143 function setModel( $model ) {
1144 $this->model
= $model;
1148 * @param string $format
1150 function setFormat( $format ) {
1151 $this->format
= $format;
1155 * @param string $text
1157 function setText( $text ) {
1158 $this->text
= $text;
1162 * @param string $text
1164 function setComment( $text ) {
1165 $this->comment
= $text;
1169 * @param bool $minor
1171 function setMinor( $minor ) {
1172 $this->minor
= (bool)$minor;
1178 function setSrc( $src ) {
1183 * @param string $src
1184 * @param bool $isTemp
1186 function setFileSrc( $src, $isTemp ) {
1187 $this->fileSrc
= $src;
1188 $this->fileIsTemp
= $isTemp;
1192 * @param string $sha1base36
1194 function setSha1Base36( $sha1base36 ) {
1195 $this->sha1base36
= $sha1base36;
1199 * @param string $filename
1201 function setFilename( $filename ) {
1202 $this->filename
= $filename;
1206 * @param string $archiveName
1208 function setArchiveName( $archiveName ) {
1209 $this->archiveName
= $archiveName;
1215 function setSize( $size ) {
1216 $this->size
= intval( $size );
1220 * @param string $type
1222 function setType( $type ) {
1223 $this->type
= $type;
1227 * @param string $action
1229 function setAction( $action ) {
1230 $this->action
= $action;
1234 * @param array $params
1236 function setParams( $params ) {
1237 $this->params
= $params;
1241 * @param bool $noupdates
1243 public function setNoUpdates( $noupdates ) {
1244 $this->mNoUpdates
= $noupdates;
1250 function getTitle() {
1251 return $this->title
;
1264 function getTimestamp() {
1265 return $this->timestamp
;
1271 function getUser() {
1272 return $this->user_text
;
1278 * @deprecated Since 1.21, use getContent() instead.
1280 function getText() {
1281 ContentHandler
::deprecated( __METHOD__
, '1.21' );
1287 * @return ContentHandler
1289 function getContentHandler() {
1290 if ( is_null( $this->contentHandler
) ) {
1291 $this->contentHandler
= ContentHandler
::getForModelID( $this->getModel() );
1294 return $this->contentHandler
;
1300 function getContent() {
1301 if ( is_null( $this->content
) ) {
1302 $handler = $this->getContentHandler();
1303 $this->content
= $handler->unserializeContent( $this->text
, $this->getFormat() );
1306 return $this->content
;
1312 function getModel() {
1313 if ( is_null( $this->model
) ) {
1314 $this->model
= $this->getTitle()->getContentModel();
1317 return $this->model
;
1323 function getFormat() {
1324 if ( is_null( $this->format
) ) {
1325 $this->format
= $this->getContentHandler()->getDefaultFormat();
1328 return $this->format
;
1334 function getComment() {
1335 return $this->comment
;
1341 function getMinor() {
1342 return $this->minor
;
1353 * @return bool|string
1355 function getSha1() {
1356 if ( $this->sha1base36
) {
1357 return wfBaseConvert( $this->sha1base36
, 36, 16 );
1365 function getFileSrc() {
1366 return $this->fileSrc
;
1372 function isTempSrc() {
1373 return $this->isTemp
;
1379 function getFilename() {
1380 return $this->filename
;
1386 function getArchiveName() {
1387 return $this->archiveName
;
1393 function getSize() {
1400 function getType() {
1407 function getAction() {
1408 return $this->action
;
1414 function getParams() {
1415 return $this->params
;
1421 function importOldRevision() {
1422 $dbw = wfGetDB( DB_MASTER
);
1424 # Sneak a single revision into place
1425 $user = User
::newFromName( $this->getUser() );
1427 $userId = intval( $user->getId() );
1428 $userText = $user->getName();
1432 $userText = $this->getUser();
1433 $userObj = new User
;
1436 // avoid memory leak...?
1437 $linkCache = LinkCache
::singleton();
1438 $linkCache->clear();
1440 $page = WikiPage
::factory( $this->title
);
1441 $page->loadPageData( 'fromdbmaster' );
1442 if ( !$page->exists() ) {
1443 # must create the page...
1444 $pageId = $page->insertOn( $dbw );
1446 $oldcountable = null;
1448 $pageId = $page->getId();
1451 $prior = $dbw->selectField( 'revision', '1',
1452 array( 'rev_page' => $pageId,
1453 'rev_timestamp' => $dbw->timestamp( $this->timestamp
),
1454 'rev_user_text' => $userText,
1455 'rev_comment' => $this->getComment() ),
1459 // @todo FIXME: This could fail slightly for multiple matches :P
1460 wfDebug( __METHOD__
. ": skipping existing revision for [[" .
1461 $this->title
->getPrefixedText() . "]], timestamp " . $this->timestamp
. "\n" );
1464 $oldcountable = $page->isCountable();
1467 # @todo FIXME: Use original rev_id optionally (better for backups)
1469 $revision = new Revision( array(
1470 'title' => $this->title
,
1472 'content_model' => $this->getModel(),
1473 'content_format' => $this->getFormat(),
1474 //XXX: just set 'content' => $this->getContent()?
1475 'text' => $this->getContent()->serialize( $this->getFormat() ),
1476 'comment' => $this->getComment(),
1478 'user_text' => $userText,
1479 'timestamp' => $this->timestamp
,
1480 'minor_edit' => $this->minor
,
1482 $revision->insertOn( $dbw );
1483 $changed = $page->updateIfNewerOn( $dbw, $revision );
1485 if ( $changed !== false && !$this->mNoUpdates
) {
1486 wfDebug( __METHOD__
. ": running updates\n" );
1487 $page->doEditUpdates(
1490 array( 'created' => $created, 'oldcountable' => $oldcountable )
1497 function importLogItem() {
1498 $dbw = wfGetDB( DB_MASTER
);
1499 # @todo FIXME: This will not record autoblocks
1500 if ( !$this->getTitle() ) {
1501 wfDebug( __METHOD__
. ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
1502 $this->timestamp
. "\n" );
1505 # Check if it exists already
1506 // @todo FIXME: Use original log ID (better for backups)
1507 $prior = $dbw->selectField( 'logging', '1',
1508 array( 'log_type' => $this->getType(),
1509 'log_action' => $this->getAction(),
1510 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
1511 'log_namespace' => $this->getTitle()->getNamespace(),
1512 'log_title' => $this->getTitle()->getDBkey(),
1513 'log_comment' => $this->getComment(),
1514 #'log_user_text' => $this->user_text,
1515 'log_params' => $this->params
),
1518 // @todo FIXME: This could fail slightly for multiple matches :P
1521 . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
1522 . $this->timestamp
. "\n" );
1525 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
1527 'log_id' => $log_id,
1528 'log_type' => $this->type
,
1529 'log_action' => $this->action
,
1530 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
1531 'log_user' => User
::idFromName( $this->user_text
),
1532 #'log_user_text' => $this->user_text,
1533 'log_namespace' => $this->getTitle()->getNamespace(),
1534 'log_title' => $this->getTitle()->getDBkey(),
1535 'log_comment' => $this->getComment(),
1536 'log_params' => $this->params
1538 $dbw->insert( 'logging', $data, __METHOD__
);
1544 function importUpload() {
1546 $archiveName = $this->getArchiveName();
1547 if ( $archiveName ) {
1548 wfDebug( __METHOD__
. "Importing archived file as $archiveName\n" );
1549 $file = OldLocalFile
::newFromArchiveName( $this->getTitle(),
1550 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
1552 $file = wfLocalFile( $this->getTitle() );
1553 wfDebug( __METHOD__
. 'Importing new file as ' . $file->getName() . "\n" );
1554 if ( $file->exists() && $file->getTimestamp() > $this->getTimestamp() ) {
1555 $archiveName = $file->getTimestamp() . '!' . $file->getName();
1556 $file = OldLocalFile
::newFromArchiveName( $this->getTitle(),
1557 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
1558 wfDebug( __METHOD__
. "File already exists; importing as $archiveName\n" );
1562 wfDebug( __METHOD__
. ': Bad file for ' . $this->getTitle() . "\n" );
1566 # Get the file source or download if necessary
1567 $source = $this->getFileSrc();
1568 $flags = $this->isTempSrc() ? File
::DELETE_SOURCE
: 0;
1570 $source = $this->downloadSource();
1571 $flags |
= File
::DELETE_SOURCE
;
1574 wfDebug( __METHOD__
. ": Could not fetch remote file.\n" );
1577 $sha1 = $this->getSha1();
1578 if ( $sha1 && ( $sha1 !== sha1_file( $source ) ) ) {
1579 if ( $flags & File
::DELETE_SOURCE
) {
1580 # Broken file; delete it if it is a temporary file
1583 wfDebug( __METHOD__
. ": Corrupt file $source.\n" );
1587 $user = User
::newFromName( $this->user_text
);
1589 # Do the actual upload
1590 if ( $archiveName ) {
1591 $status = $file->uploadOld( $source, $archiveName,
1592 $this->getTimestamp(), $this->getComment(), $user, $flags );
1594 $status = $file->upload( $source, $this->getComment(), $this->getComment(),
1595 $flags, false, $this->getTimestamp(), $user );
1598 if ( $status->isGood() ) {
1599 wfDebug( __METHOD__
. ": Successful\n" );
1602 wfDebug( __METHOD__
. ': failed: ' . $status->getXml() . "\n" );
1608 * @return bool|string
1610 function downloadSource() {
1611 global $wgEnableUploads;
1612 if ( !$wgEnableUploads ) {
1616 $tempo = tempnam( wfTempDir(), 'download' );
1617 $f = fopen( $tempo, 'wb' );
1619 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
1624 $src = $this->getSrc();
1625 $data = Http
::get( $src );
1627 wfDebug( "IMPORT: couldn't fetch source $src\n" );
1633 fwrite( $f, $data );
1642 * @todo document (e.g. one-sentence class description).
1643 * @ingroup SpecialPage
1645 class ImportStringSource
{
1646 function __construct( $string ) {
1647 $this->mString
= $string;
1648 $this->mRead
= false;
1655 return $this->mRead
;
1659 * @return bool|string
1661 function readChunk() {
1662 if ( $this->atEnd() ) {
1665 $this->mRead
= true;
1666 return $this->mString
;
1671 * @todo document (e.g. one-sentence class description).
1672 * @ingroup SpecialPage
1674 class ImportStreamSource
{
1675 function __construct( $handle ) {
1676 $this->mHandle
= $handle;
1683 return feof( $this->mHandle
);
1689 function readChunk() {
1690 return fread( $this->mHandle
, 32768 );
1694 * @param string $filename
1697 static function newFromFile( $filename ) {
1698 wfSuppressWarnings();
1699 $file = fopen( $filename, 'rt' );
1700 wfRestoreWarnings();
1702 return Status
::newFatal( "importcantopen" );
1704 return Status
::newGood( new ImportStreamSource( $file ) );
1708 * @param string $fieldname
1711 static function newFromUpload( $fieldname = "xmlimport" ) {
1712 $upload =& $_FILES[$fieldname];
1714 if ( $upload === null ||
!$upload['name'] ) {
1715 return Status
::newFatal( 'importnofile' );
1717 if ( !empty( $upload['error'] ) ) {
1718 switch ( $upload['error'] ) {
1720 # The uploaded file exceeds the upload_max_filesize directive in php.ini.
1721 return Status
::newFatal( 'importuploaderrorsize' );
1723 # The uploaded file exceeds the MAX_FILE_SIZE directive that
1724 # was specified in the HTML form.
1725 return Status
::newFatal( 'importuploaderrorsize' );
1727 # The uploaded file was only partially uploaded
1728 return Status
::newFatal( 'importuploaderrorpartial' );
1730 # Missing a temporary folder.
1731 return Status
::newFatal( 'importuploaderrortemp' );
1732 # case else: # Currently impossible
1736 $fname = $upload['tmp_name'];
1737 if ( is_uploaded_file( $fname ) ) {
1738 return ImportStreamSource
::newFromFile( $fname );
1740 return Status
::newFatal( 'importnofile' );
1745 * @param string $url
1746 * @param string $method
1749 static function newFromURL( $url, $method = 'GET' ) {
1750 wfDebug( __METHOD__
. ": opening $url\n" );
1751 # Use the standard HTTP fetch function; it times out
1752 # quicker and sorts out user-agent problems which might
1753 # otherwise prevent importing from large sites, such
1754 # as the Wikimedia cluster, etc.
1755 $data = Http
::request( $method, $url, array( 'followRedirects' => true ) );
1756 if ( $data !== false ) {
1758 fwrite( $file, $data );
1761 return Status
::newGood( new ImportStreamSource( $file ) );
1763 return Status
::newFatal( 'importcantopen' );
1768 * @param string $interwiki
1769 * @param string $page
1770 * @param bool $history
1771 * @param bool $templates
1772 * @param int $pageLinkDepth
1775 public static function newFromInterwiki( $interwiki, $page, $history = false,
1776 $templates = false, $pageLinkDepth = 0
1778 if ( $page == '' ) {
1779 return Status
::newFatal( 'import-noarticle' );
1781 $link = Title
::newFromText( "$interwiki:Special:Export/$page" );
1782 if ( is_null( $link ) ||
!$link->isExternal() ) {
1783 return Status
::newFatal( 'importbadinterwiki' );
1787 $params['history'] = 1;
1790 $params['templates'] = 1;
1792 if ( $pageLinkDepth ) {
1793 $params['pagelink-depth'] = $pageLinkDepth;
1795 $url = $link->getFullURL( $params );
1796 # For interwikis, use POST to avoid redirects.
1797 return ImportStreamSource
::newFromURL( $url, "POST" );