Merge "prop=duplicatefiles does not show duplicates under same name"
[lhc/web/wiklou.git] / includes / Import.php
index 502b015..11f3795 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * MediaWiki page data importer
+ * MediaWiki page data importer.
  *
  * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
  * http://www.mediawiki.org/
 class WikiImporter {
        private $reader = null;
        private $mLogItemCallback, $mUploadCallback, $mRevisionCallback, $mPageCallback;
-       private $mSiteInfoCallback, $mTargetNamespace, $mPageOutCallback;
-       private $mDebug;
+       private $mSiteInfoCallback, $mTargetNamespace, $mTargetRootPage, $mPageOutCallback;
+       private $mNoticeCallback, $mDebug;
        private $mImportUploads, $mImageBasePath;
        private $mNoUpdates = false;
 
        /**
         * Creates an ImportXMLReader drawing from the source provided
+        * @param $source
         */
        function __construct( $source ) {
                $this->reader = new XMLReader();
@@ -48,8 +49,7 @@ class WikiImporter {
                $id = UploadSourceAdapter::registerSource( $source );
                if (defined( 'LIBXML_PARSEHUGE' ) ) {
                        $this->reader->open( "uploadsource://$id", null, LIBXML_PARSEHUGE );
-               }
-               else {
+               } else {
                        $this->reader->open( "uploadsource://$id" );
                }
 
@@ -75,18 +75,20 @@ class WikiImporter {
                wfDebug( "IMPORT: $data\n" );
        }
 
-       private function notice( $data ) {
-               global $wgCommandLineMode;
-               if( $wgCommandLineMode ) {
-                       print "$data\n";
-               } else {
-                       global $wgOut;
-                       $wgOut->addHTML( "<li>" . htmlspecialchars( $data ) . "</li>\n" );
+       private function notice( $msg /*, $param, ...*/ ) {
+               $params = func_get_args();
+               array_shift( $params );
+
+               if ( is_callable( $this->mNoticeCallback ) ) {
+                       call_user_func( $this->mNoticeCallback, $msg, $params );
+               } else { # No ImportReporter -> CLI
+                       echo wfMessage( $msg, $params )->text() . "\n";
                }
        }
 
        /**
         * Set debug mode...
+        * @param $debug bool
         */
        function setDebug( $debug ) {
                $this->mDebug = $debug;
@@ -94,11 +96,22 @@ class WikiImporter {
 
        /**
         * Set 'no updates' mode. In this mode, the link tables will not be updated by the importer
+        * @param $noupdates bool
         */
        function setNoUpdates( $noupdates ) {
                $this->mNoUpdates = $noupdates;
        }
 
+       /**
+        * Set a callback that displays notice messages
+        *
+        * @param $callback callback
+        * @return callback
+        */
+       public function setNoticeCallback( $callback ) {
+               return wfSetVar( $this->mNoticeCallback, $callback );
+       }
+
        /**
         * Sets the action to perform as each new page in the stream is reached.
         * @param $callback callback
@@ -171,6 +184,8 @@ class WikiImporter {
 
        /**
         * Set a target namespace to override the defaults
+        * @param $namespace
+        * @return bool
         */
        public function setTargetNamespace( $namespace ) {
                if( is_null( $namespace ) ) {
@@ -185,7 +200,40 @@ class WikiImporter {
        }
 
        /**
-        * @parma $dir
+        * Set a target root page under which all pages are imported
+        * @param $rootpage
+        * @return status object
+        */
+       public function setTargetRootPage( $rootpage ) {
+               $status = Status::newGood();
+               if( is_null( $rootpage ) ) {
+                       // No rootpage
+                       $this->mTargetRootPage = null;
+               } elseif( $rootpage !== '' ) {
+                       $rootpage = rtrim( $rootpage, '/' ); //avoid double slashes
+                       $title = Title::newFromText( $rootpage, !is_null( $this->mTargetNamespace ) ? $this->mTargetNamespace : NS_MAIN );
+                       if( !$title || $title->isExternal() ) {
+                               $status->fatal( 'import-rootpage-invalid' );
+                       } else {
+                               if( !MWNamespace::hasSubpages( $title->getNamespace() ) ) {
+                                       global $wgContLang;
+
+                                       $displayNSText = $title->getNamespace() == NS_MAIN
+                                               ? wfMessage( 'blanknamespace' )->text()
+                                               : $wgContLang->getNsText( $title->getNamespace() );
+                                       $status->fatal( 'import-rootpage-nosubpage', $displayNSText );
+                               } else {
+                                       // set namespace to 'all', so the namespace check in processTitle() can passed
+                                       $this->setTargetNamespace( null );
+                                       $this->mTargetRootPage = $title->getPrefixedDBKey();
+                               }
+                       }
+               }
+               return $status;
+       }
+
+       /**
+        * @param $dir
         */
        public function setImageBasePath( $dir ) {
                $this->mImageBasePath = $dir;
@@ -201,6 +249,7 @@ class WikiImporter {
        /**
         * Default per-revision callback, performs the import.
         * @param $revision WikiRevision
+        * @return bool
         */
        public function importRevision( $revision ) {
                $dbw = wfGetDB( DB_MASTER );
@@ -210,6 +259,7 @@ class WikiImporter {
        /**
         * Default per-revision callback, performs the import.
         * @param $rev WikiRevision
+        * @return bool
         */
        public function importLogItem( $rev ) {
                $dbw = wfGetDB( DB_MASTER );
@@ -218,6 +268,8 @@ class WikiImporter {
 
        /**
         * Dummy for now...
+        * @param $revision
+        * @return bool
         */
        public function importUpload( $revision ) {
                $dbw = wfGetDB( DB_MASTER );
@@ -226,6 +278,12 @@ class WikiImporter {
 
        /**
         * Mostly for hook use
+        * @param $title
+        * @param $origTitle
+        * @param $revCount
+        * @param $sRevCount
+        * @param $pageInfo
+        * @return
         */
        public function finishImportPage( $title, $origTitle, $revCount, $sRevCount, $pageInfo ) {
                $args = func_get_args();
@@ -250,7 +308,7 @@ class WikiImporter {
        }
 
        /**
-        * Notify the callback function when a new <page> is reached.
+        * Notify the callback function when a new "<page>" is reached.
         * @param $title Title
         */
        function pageCallback( $title ) {
@@ -260,7 +318,7 @@ class WikiImporter {
        }
 
        /**
-        * Notify the callback function when a </page> is closed.
+        * Notify the callback function when a "</page>" is closed.
         * @param $title Title
         * @param $origTitle Title
         * @param $revCount Integer
@@ -276,7 +334,8 @@ class WikiImporter {
 
        /**
         * Notify the callback function of a revision
-        * @param $revision A WikiRevision object
+        * @param $revision WikiRevision object
+        * @return bool|mixed
         */
        private function revisionCallback( $revision ) {
                if ( isset( $this->mRevisionCallback ) ) {
@@ -289,7 +348,8 @@ class WikiImporter {
 
        /**
         * Notify the callback function of a new log item
-        * @param $revision A WikiRevision object
+        * @param $revision WikiRevision object
+        * @return bool|mixed
         */
        private function logItemCallback( $revision ) {
                if ( isset( $this->mLogItemCallback ) ) {
@@ -369,6 +429,7 @@ class WikiImporter {
 
        /**
         * Primary entry point
+        * @return bool
         */
        public function doImport() {
                $this->reader->read();
@@ -544,7 +605,7 @@ class WikiImporter {
        }
 
        /**
-        * @param $pageInfo
+        * @param $pageInfo array
         */
        private function handleRevision( &$pageInfo ) {
                $this->debug( "Enter revision handler" );
@@ -758,26 +819,34 @@ class WikiImporter {
                $origTitle = Title::newFromText( $workTitle );
 
                if( !is_null( $this->mTargetNamespace ) && !is_null( $origTitle ) ) {
-                       $title = Title::makeTitle( $this->mTargetNamespace,
+                       # makeTitleSafe, because $origTitle can have a interwiki (different setting of interwiki map)
+                       # and than dbKey can begin with a lowercase char
+                       $title = Title::makeTitleSafe( $this->mTargetNamespace,
                                $origTitle->getDBkey() );
                } else {
+                       if( !is_null( $this->mTargetRootPage ) ) {
+                               $workTitle = $this->mTargetRootPage . '/' . $workTitle;
+                       }
                        $title = Title::newFromText( $workTitle );
                }
 
                if( is_null( $title ) ) {
                        # Invalid page title? Ignore the page
-                       $this->notice( "Skipping invalid page title '$workTitle'" );
+                       $this->notice( 'import-error-invalid', $workTitle );
+                       return false;
+               } elseif( $title->isExternal() ) {
+                       $this->notice( 'import-error-interwiki', $title->getPrefixedText() );
                        return false;
-               } elseif( $title->getInterwiki() != '' ) {
-                       $this->notice( "Skipping interwiki page title '$workTitle'" );
+               } elseif( !$title->canExist() ) {
+                       $this->notice( 'import-error-special', $title->getPrefixedText() );
                        return false;
                } elseif( !$title->userCan( 'edit' ) && !$wgCommandLineMode ) {
                        # Do not import if the importing wiki user cannot edit this page
-                       $this->notice( wfMessage( 'import-error-edit', $title->getText() )->text() );
+                       $this->notice( 'import-error-edit', $title->getPrefixedText() );
                        return false;
                } elseif( !$title->exists() && !$title->userCan( 'create' ) && !$wgCommandLineMode ) {
                        # Do not import if the importing wiki user cannot create this page
-                       $this->notice( wfMessage( 'import-error-create', $title->getText() )->text() );
+                       $this->notice( 'import-error-create', $title->getPrefixedText() );
                        return false;
                }
 
@@ -798,7 +867,7 @@ class UploadSourceAdapter {
         * @return string
         */
        static function registerSource( $source ) {
-               $id = wfGenerateToken();
+               $id = wfRandomString();
 
                self::$sourceRegistrations[$id] = $source;
 
@@ -930,6 +999,10 @@ class XMLReader2 extends XMLReader {
  */
 class WikiRevision {
        var $importer = null;
+
+       /**
+        * @var Title
+        */
        var $title = null;
        var $id = 0;
        var $timestamp = "20010115000000";
@@ -945,6 +1018,7 @@ class WikiRevision {
        var $sha1base36 = false;
        var $isTemp = false;
        var $archiveName = '';
+       var $fileIsTemp;
        private $mNoUpdates = false;
 
        /**
@@ -1062,6 +1136,9 @@ class WikiRevision {
                $this->type = $type;
        }
 
+       /**
+        * @param $action
+        */
        function setAction( $action ) {
                $this->action = $action;
        }
@@ -1224,14 +1301,14 @@ class WikiRevision {
                $linkCache = LinkCache::singleton();
                $linkCache->clear();
 
-               $article = new Article( $this->title );
-               $pageId = $article->getId();
-               if( $pageId == 0 ) {
+               $page = WikiPage::factory( $this->title );
+               if( !$page->exists() ) {
                        # must create the page...
-                       $pageId = $article->insertOn( $dbw );
+                       $pageId = $page->insertOn( $dbw );
                        $created = true;
                        $oldcountable = null;
                } else {
+                       $pageId = $page->getId();
                        $created = false;
 
                        $prior = $dbw->selectField( 'revision', '1',
@@ -1247,7 +1324,7 @@ class WikiRevision {
                                        $this->title->getPrefixedText() . "]], timestamp " . $this->timestamp . "\n" );
                                return false;
                        }
-                       $oldcountable = $article->isCountable();
+                       $oldcountable = $page->isCountable();
                }
 
                # @todo FIXME: Use original rev_id optionally (better for backups)
@@ -1262,11 +1339,11 @@ class WikiRevision {
                        'minor_edit' => $this->minor,
                        ) );
                $revision->insertOn( $dbw );
-               $changed = $article->updateIfNewerOn( $dbw, $revision );
+               $changed = $page->updateIfNewerOn( $dbw, $revision );
 
                if ( $changed !== false && !$this->mNoUpdates ) {
                        wfDebug( __METHOD__ . ": running updates\n" );
-                       $article->doEditUpdates( $revision, $userObj, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
+                       $page->doEditUpdates( $revision, $userObj, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
                }
 
                return true;
@@ -1428,10 +1505,16 @@ class ImportStringSource {
                $this->mRead = false;
        }
 
+       /**
+        * @return bool
+        */
        function atEnd() {
                return $this->mRead;
        }
 
+       /**
+        * @return bool|string
+        */
        function readChunk() {
                if( $this->atEnd() ) {
                        return false;
@@ -1457,6 +1540,9 @@ class ImportStreamSource {
                return feof( $this->mHandle );
        }
 
+       /**
+        * @return string
+        */
        function readChunk() {
                return fread( $this->mHandle, 32768 );
        }
@@ -1518,7 +1604,7 @@ class ImportStreamSource {
                # quicker and sorts out user-agent problems which might
                # otherwise prevent importing from large sites, such
                # as the Wikimedia cluster, etc.
-               $data = Http::request( $method, $url );
+               $data = Http::request( $method, $url, array( 'followRedirects' => true ) );
                if( $data !== false ) {
                        $file = tmpfile();
                        fwrite( $file, $data );