Merge "Localisation updates from http://translatewiki.net."
[lhc/web/wiklou.git] / includes / Import.php
index bc02669..9ebc34c 100644 (file)
@@ -34,7 +34,7 @@ class WikiImporter {
        private $reader = null;
        private $mLogItemCallback, $mUploadCallback, $mRevisionCallback, $mPageCallback;
        private $mSiteInfoCallback, $mTargetNamespace, $mPageOutCallback;
-       private $mDebug;
+       private $mNoticeCallback, $mDebug;
        private $mImportUploads, $mImageBasePath;
        private $mNoUpdates = false;
 
@@ -75,13 +75,14 @@ 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";
                }
        }
 
@@ -101,6 +102,16 @@ class WikiImporter {
                $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
@@ -290,7 +301,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 ) ) {
@@ -303,7 +315,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 ) ) {
@@ -383,6 +396,7 @@ class WikiImporter {
 
        /**
         * Primary entry point
+        * @return bool
         */
        public function doImport() {
                $this->reader->read();
@@ -558,7 +572,7 @@ class WikiImporter {
        }
 
        /**
-        * @param $pageInfo
+        * @param $pageInfo array
         */
        private function handleRevision( &$pageInfo ) {
                $this->debug( "Enter revision handler" );
@@ -772,7 +786,9 @@ 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 {
                        $title = Title::newFromText( $workTitle );
@@ -780,18 +796,21 @@ class WikiImporter {
 
                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;
                }
 
@@ -812,7 +831,7 @@ class UploadSourceAdapter {
         * @return string
         */
        static function registerSource( $source ) {
-               $id = wfGenerateToken();
+               $id = wfRandomString();
 
                self::$sourceRegistrations[$id] = $source;