Cleanups to WikiRevision
[lhc/web/wiklou.git] / includes / import / WikiRevision.php
index 90b048a..356a79f 100644 (file)
@@ -51,6 +51,9 @@ class WikiRevision {
        /** @var string */
        public $user_text = "";
 
+       /** @var User */
+       public $userObj = null;
+
        /** @var string */
        public $model = null;
 
@@ -154,6 +157,13 @@ class WikiRevision {
                $this->user_text = $user;
        }
 
+       /**
+        * @param User $user
+        */
+       function setUserObj( $user ) {
+               $this->userObj = $user;
+       }
+
        /**
         * @param string $ip
         */
@@ -296,6 +306,13 @@ class WikiRevision {
                return $this->user_text;
        }
 
+       /**
+        * @return User
+        */
+       function getUserObj() {
+               return $this->userObj;
+       }
+
        /**
         * @return string
         *
@@ -446,15 +463,14 @@ class WikiRevision {
                $dbw = wfGetDB( DB_MASTER );
 
                # Sneak a single revision into place
-               $user = User::newFromName( $this->getUser() );
+               $user = $this->getUserObj() ?: User::newFromName( $this->getUser() );
                if ( $user ) {
                        $userId = intval( $user->getId() );
                        $userText = $user->getName();
-                       $userObj = $user;
                } else {
                        $userId = 0;
                        $userText = $this->getUser();
-                       $userObj = new User;
+                       $user = new User;
                }
 
                // avoid memory leak...?
@@ -472,10 +488,10 @@ class WikiRevision {
                        $created = false;
 
                        $prior = $dbw->selectField( 'revision', '1',
-                               array( 'rev_page' => $pageId,
+                               [ 'rev_page' => $pageId,
                                        'rev_timestamp' => $dbw->timestamp( $this->timestamp ),
                                        'rev_user_text' => $userText,
-                                       'rev_comment' => $this->getComment() ),
+                                       'rev_comment' => $this->getComment() ],
                                __METHOD__
                        );
                        if ( $prior ) {
@@ -498,21 +514,21 @@ class WikiRevision {
                // @todo This assumes that multiple revisions of the same page are imported
                // in order from oldest to newest.
                $prevId = $dbw->selectField( 'revision', 'rev_id',
-                       array(
+                       [
                                'rev_page' => $pageId,
-                               'rev_timestamp <= ' . $dbw->timestamp( $this->timestamp ),
-                       ),
+                               'rev_timestamp <= ' . $dbw->addQuotes( $dbw->timestamp( $this->timestamp ) ),
+                       ],
                        __METHOD__,
-                       array( 'ORDER BY' => array(
+                       [ 'ORDER BY' => [
                                        '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(
+               $revision = new Revision( [
                        'title' => $this->title,
                        'page' => $pageId,
                        'content_model' => $this->getModel(),
@@ -525,7 +541,7 @@ class WikiRevision {
                        'timestamp' => $this->timestamp,
                        'minor_edit' => $this->minor,
                        'parent_id' => $prevId,
-                       ) );
+                       ] );
                $revision->insertOn( $dbw );
                $changed = $page->updateIfNewerOn( $dbw, $revision );
 
@@ -534,8 +550,8 @@ class WikiRevision {
                        // countable/oldcountable stuff is handled in WikiImporter::finishImportPage
                        $page->doEditUpdates(
                                $revision,
-                               $userObj,
-                               array( 'created' => $created, 'oldcountable' => 'no-change' )
+                               $user,
+                               [ 'created' => $created, 'oldcountable' => 'no-change' ]
                        );
                }
 
@@ -544,6 +560,16 @@ class WikiRevision {
 
        function importLogItem() {
                $dbw = wfGetDB( DB_MASTER );
+
+               $user = $this->getUserObj() ?: 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 " .
@@ -553,14 +579,14 @@ class WikiRevision {
                # Check if it exists already
                // @todo FIXME: Use original log ID (better for backups)
                $prior = $dbw->selectField( 'logging', '1',
-                       array( 'log_type' => $this->getType(),
+                       [ 'log_type' => $this->getType(),
                                'log_action' => $this->getAction(),
                                'log_timestamp' => $dbw->timestamp( $this->timestamp ),
                                'log_namespace' => $this->getTitle()->getNamespace(),
                                'log_title' => $this->getTitle()->getDBkey(),
                                'log_comment' => $this->getComment(),
                                # 'log_user_text' => $this->user_text,
-                               'log_params' => $this->params ),
+                               'log_params' => $this->params ],
                        __METHOD__
                );
                // @todo FIXME: This could fail slightly for multiple matches :P
@@ -571,18 +597,18 @@ class WikiRevision {
                        return;
                }
                $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
-               $data = array(
+               $data = [
                        'log_id' => $log_id,
                        '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(),
                        'log_params' => $this->params
-               );
+               ];
                $dbw->insert( 'logging', $data, __METHOD__ );
        }
 
@@ -614,33 +640,36 @@ class WikiRevision {
 
                # Get the file source or download if necessary
                $source = $this->getFileSrc();
-               $flags = $this->isTempSrc() ? File::DELETE_SOURCE : 0;
-               if ( !$source ) {
+               $autoDeleteSource = $this->isTempSrc();
+               if ( !strlen( $source ) ) {
                        $source = $this->downloadSource();
-                       $flags |= File::DELETE_SOURCE;
+                       $autoDeleteSource = true;
                }
-               if ( !$source ) {
+               if ( !strlen( $source ) ) {
                        wfDebug( __METHOD__ . ": Could not fetch remote file.\n" );
                        return false;
                }
+
+               $tmpFile = new TempFSFile( $source );
+               if ( $autoDeleteSource ) {
+                       $tmpFile->autocollect();
+               }
+
                $sha1File = ltrim( sha1_file( $source ), '0' );
                $sha1 = $this->getSha1();
                if ( $sha1 && ( $sha1 !== $sha1File ) ) {
-                       if ( $flags & File::DELETE_SOURCE ) {
-                               # Broken file; delete it if it is a temporary file
-                               unlink( $source );
-                       }
                        wfDebug( __METHOD__ . ": Corrupt file $source.\n" );
                        return false;
                }
 
-               $user = User::newFromName( $this->user_text );
+               $user = $this->getUserObj() ?: User::newFromName( $this->getUser() );
 
                # Do the actual upload
                if ( $archiveName ) {
                        $status = $file->uploadOld( $source, $archiveName,
-                               $this->getTimestamp(), $this->getComment(), $user, $flags );
+                               $this->getTimestamp(), $this->getComment(), $user );
                } else {
+                       $flags = 0;
                        $status = $file->upload( $source, $this->getComment(), $this->getComment(),
                                $flags, false, $this->getTimestamp(), $user );
                }
@@ -671,7 +700,7 @@ class WikiRevision {
 
                // @todo FIXME!
                $src = $this->getSrc();
-               $data = Http::get( $src, array(), __METHOD__ );
+               $data = Http::get( $src, [], __METHOD__ );
                if ( !$data ) {
                        wfDebug( "IMPORT: couldn't fetch source $src\n" );
                        fclose( $f );