Handle missing titles and usernames when importing log items
[lhc/web/wiklou.git] / includes / Import.php
index 60d4a1f..ee1cab5 100644 (file)
@@ -515,6 +515,7 @@ class WikiImporter {
                while ( $this->reader->read() ) {
                        switch ( $this->reader->nodeType ) {
                        case XMLReader::TEXT:
+                       case XMLReader::CDATA:
                        case XMLReader::SIGNIFICANT_WHITESPACE:
                                $buffer .= $this->reader->value;
                                break;
@@ -659,14 +660,26 @@ class WikiImporter {
         * @return bool|mixed
         */
        private function processLogItem( $logInfo ) {
+
                $revision = new WikiRevision( $this->config );
 
-               $revision->setID( $logInfo['id'] );
+               if ( isset( $logInfo['id'] ) ) {
+                       $revision->setID( $logInfo['id'] );
+               }
                $revision->setType( $logInfo['type'] );
                $revision->setAction( $logInfo['action'] );
-               $revision->setTimestamp( $logInfo['timestamp'] );
-               $revision->setParams( $logInfo['params'] );
-               $revision->setTitle( Title::newFromText( $logInfo['logtitle'] ) );
+               if ( isset( $logInfo['timestamp'] ) ) {
+                       $revision->setTimestamp( $logInfo['timestamp'] );
+               }
+               if ( isset( $logInfo['params'] ) ) {
+                       $revision->setParams( $logInfo['params'] );
+               }
+               if ( isset( $logInfo['logtitle'] ) ) {
+                       // @todo Using Title for non-local titles is a recipe for disaster.
+                       // We should use ForeignTitle here instead.
+                       $revision->setTitle( Title::newFromText( $logInfo['logtitle'] ) );
+               }
+
                $revision->setNoUpdates( $this->mNoUpdates );
 
                if ( isset( $logInfo['comment'] ) ) {
@@ -676,7 +689,10 @@ class WikiImporter {
                if ( isset( $logInfo['contributor']['ip'] ) ) {
                        $revision->setUserIP( $logInfo['contributor']['ip'] );
                }
-               if ( isset( $logInfo['contributor']['username'] ) ) {
+
+               if ( !isset( $logInfo['contributor']['username'] ) ) {
+                       $revision->setUsername( 'Unknown user' );
+               } else {
                        $revision->setUserName( $logInfo['contributor']['username'] );
                }
 
@@ -1498,7 +1514,7 @@ class WikiRevision {
         */
        function getSha1() {
                if ( $this->sha1base36 ) {
-                       return wfBaseConvert( $this->sha1base36, 36, 16 );
+                       return Wikimedia\base_convert( $this->sha1base36, 36, 16 );
                }
                return false;
        }
@@ -1606,6 +1622,20 @@ class WikiRevision {
                        }
                }
 
+               // Select previous version to make size diffs correct
+               $prevId = $dbw->selectField( 'revision', 'rev_id',
+                       array(
+                               'rev_page' => $pageId,
+                               'rev_timestamp <= ' . $dbw->timestamp( $this->timestamp ),
+                       ),
+                       __METHOD__,
+                       array( 'ORDER BY' => array(
+                                       'rev_timestamp DESC',
+                                       'rev_id DESC', // timestamp is not unique per page
+                               )
+                       )
+               );
+
                # @todo FIXME: Use original rev_id optionally (better for backups)
                # Insert the row
                $revision = new Revision( array(
@@ -1620,6 +1650,7 @@ class WikiRevision {
                        'user_text' => $userText,
                        'timestamp' => $this->timestamp,
                        'minor_edit' => $this->minor,
+                       'parent_id' => $prevId,
                        ) );
                $revision->insertOn( $dbw );
                $changed = $page->updateIfNewerOn( $dbw, $revision );
@@ -1639,6 +1670,16 @@ class WikiRevision {
 
        function importLogItem() {
                $dbw = wfGetDB( DB_MASTER );
+
+               $user = User::newFromName( $this->getUser() );
+               if ( $user ) {
+                       $userId = intval( $user->getId() );
+                       $userText = $user->getName();
+               } else {
+                       $userId = 0;
+                       $userText = $this->getUser();
+               }
+
                # @todo FIXME: This will not record autoblocks
                if ( !$this->getTitle() ) {
                        wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
@@ -1671,8 +1712,8 @@ class WikiRevision {
                        'log_type' => $this->type,
                        'log_action' => $this->action,
                        'log_timestamp' => $dbw->timestamp( $this->timestamp ),
-                       'log_user' => User::idFromName( $this->user_text ),
-                       # 'log_user_text' => $this->user_text,
+                       'log_user' =>  $userId,
+                       'log_user_text' => $userText,
                        'log_namespace' => $this->getTitle()->getNamespace(),
                        'log_title' => $this->getTitle()->getDBkey(),
                        'log_comment' => $this->getComment(),
@@ -1948,23 +1989,38 @@ class ImportStreamSource implements ImportSource {
                if ( $page == '' ) {
                        return Status::newFatal( 'import-noarticle' );
                }
-               $link = Title::newFromText( "$interwiki:Special:Export/$page" );
-               if ( is_null( $link ) || !$link->isExternal() ) {
+
+               # Look up the first interwiki prefix, and let the foreign site handle
+               # subsequent interwiki prefixes
+               $firstIwPrefix = strtok( $interwiki, ':' );
+               $firstIw = Interwiki::fetch( $firstIwPrefix );
+               if ( !$firstIw ) {
                        return Status::newFatal( 'importbadinterwiki' );
-               } else {
-                       $params = array();
-                       if ( $history ) {
-                               $params['history'] = 1;
-                       }
-                       if ( $templates ) {
-                               $params['templates'] = 1;
-                       }
-                       if ( $pageLinkDepth ) {
-                               $params['pagelink-depth'] = $pageLinkDepth;
-                       }
-                       $url = $link->getFullURL( $params );
-                       # For interwikis, use POST to avoid redirects.
-                       return ImportStreamSource::newFromURL( $url, "POST" );
                }
+
+               $additionalIwPrefixes = strtok( '' );
+               if ( $additionalIwPrefixes ) {
+                       $additionalIwPrefixes .= ':';
+               }
+               # Have to do a DB-key replacement ourselves; otherwise spaces get
+               # URL-encoded to +, which is wrong in this case. Similar to logic in
+               # Title::getLocalURL
+               $link = $firstIw->getURL( strtr( "${additionalIwPrefixes}Special:Export/$page",
+                       ' ', '_' ) );
+
+               $params = array();
+               if ( $history ) {
+                       $params['history'] = 1;
+               }
+               if ( $templates ) {
+                       $params['templates'] = 1;
+               }
+               if ( $pageLinkDepth ) {
+                       $params['pagelink-depth'] = $pageLinkDepth;
+               }
+
+               $url = wfAppendQuery( $link, $params );
+               # For interwikis, use POST to avoid redirects.
+               return ImportStreamSource::newFromURL( $url, "POST" );
        }
 }