Merge "copy paste comment should probably have end instead of start"
[lhc/web/wiklou.git] / includes / filerepo / file / LocalFile.php
index 325c673..3be66d3 100644 (file)
@@ -604,7 +604,7 @@ class LocalFile extends File {
         * Return the width of the image
         *
         * @param $page int
-        * @return bool|int Returns false on error
+        * @return int
         */
        public function getWidth( $page = 1 ) {
                $this->load();
@@ -614,7 +614,9 @@ class LocalFile extends File {
                        if ( $dim ) {
                                return $dim['width'];
                        } else {
-                               return false;
+                               // For non-paged media, the false goes through an
+                               // intval, turning failure into 0, so do same here.
+                               return 0;
                        }
                } else {
                        return $this->width;
@@ -625,7 +627,7 @@ class LocalFile extends File {
         * Return the height of the image
         *
         * @param $page int
-        * @return bool|int Returns false on error
+        * @return int
         */
        public function getHeight( $page = 1 ) {
                $this->load();
@@ -635,7 +637,9 @@ class LocalFile extends File {
                        if ( $dim ) {
                                return $dim['height'];
                        } else {
-                               return false;
+                               // For non-paged media, the false goes through an
+                               // intval, turning failure into 0, so do same here.
+                               return 0;
                        }
                } else {
                        return $this->height;
@@ -1275,20 +1279,45 @@ class LocalFile extends File {
                $wikiPage->setFile( $this );
 
                # Add the log entry
-               $log = new LogPage( 'upload' );
                $action = $reupload ? 'overwrite' : 'upload';
-               $logId = $log->addEntry( $action, $descTitle, $comment, array(), $user );
 
-               wfProfileIn( __METHOD__ . '-edit' );
+               $logEntry = new ManualLogEntry( 'upload', $action );
+               $logEntry->setPerformer( $user );
+               $logEntry->setComment( $comment );
+               $logEntry->setTarget( $descTitle );
+
+               // Allow people using the api to associate log entries with the upload.
+               // Log has a timestamp, but sometimes different from upload timestamp.
+               $logEntry->setParameters(
+                       array(
+                               'img_sha1' => $this->sha1,
+                               'img_timestamp' => $timestamp,
+                       )
+               );
+               // Note we keep $logId around since during new image
+               // creation, page doesn't exist yet, so log_page = 0
+               // but we want it to point to the page we're making,
+               // so we later modify the log entry.
+               // For a similar reason, we avoid making an RC entry
+               // now and wait until the page exists.
+               $logId = $logEntry->insert();
+
                $exists = $descTitle->exists();
+               if ( $exists ) {
+                       // Page exists, do RC entry now (otherwise we wait for later).
+                       $logEntry->publish( $logId );
+               }
+               wfProfileIn( __METHOD__ . '-edit' );
 
                if ( $exists ) {
                        # Create a null revision
                        $latest = $descTitle->getLatestRevID();
+                       $editSummary = LogFormatter::newFromEntry( $logEntry )->getPlainActionText();
+
                        $nullRevision = Revision::newNullRevision(
                                $dbw,
                                $descTitle->getArticleID(),
-                               $log->getRcComment(),
+                               $editSummary,
                                false
                        );
                        if ( !is_null( $nullRevision ) ) {
@@ -1316,16 +1345,20 @@ class LocalFile extends File {
                        $content = ContentHandler::makeContent( $pageText, $descTitle );
                        $status = $wikiPage->doEditContent( $content, $comment, EDIT_NEW | EDIT_SUPPRESS_RC, false, $user );
 
-                       if ( isset( $status->value['revision'] ) ) { // XXX; doEdit() uses a transaction
-                               $dbw->begin( __METHOD__ );
+                       $dbw->begin( __METHOD__ ); // XXX; doEdit() uses a transaction
+                       // Now that the page exists, make an RC entry.
+                       $logEntry->publish( $logId );
+                       if ( isset( $status->value['revision'] ) ) {
                                $dbw->update( 'logging',
                                        array( 'log_page' => $status->value['revision']->getPage() ),
                                        array( 'log_id' => $logId ),
                                        __METHOD__
                                );
-                               $dbw->commit( __METHOD__ ); // commit before anything bad can happen
                        }
+                       $dbw->commit( __METHOD__ ); // commit before anything bad can happen
                }
+
+
                wfProfileOut( __METHOD__ . '-edit' );
 
                # Save to cache and purge the squid
@@ -1352,11 +1385,17 @@ class LocalFile extends File {
                # Invalidate cache for all pages using this file
                $update = new HTMLCacheUpdate( $this->getTitle(), 'imagelinks' );
                $update->doUpdate();
+               if ( !$reupload ) {
+                       LinksUpdate::queueRecursiveJobsForTable( $this->getTitle(), 'imagelinks' );
+               }
 
                # Invalidate cache for all pages that redirects on this page
                $redirs = $this->getTitle()->getRedirectsHere();
 
                foreach ( $redirs as $redir ) {
+                       if ( !$reupload && $redir->getNamespace() === NS_FILE ) {
+                               LinksUpdate::queueRecursiveJobsForTable( $redir, 'imagelinks' );
+                       }
                        $update = new HTMLCacheUpdate( $redir, 'imagelinks' );
                        $update->doUpdate();
                }
@@ -1666,8 +1705,10 @@ class LocalFile extends File {
         * @return bool Whether to cache in RepoGroup (this avoids OOMs)
         */
        function isCacheable() {
-               $this->load(); // if loaded from cache, metadata will be null if it didn't fit
-               return $this->metadata !== null && strlen( $this->metadata ) <= self::CACHE_FIELD_MAX_LEN;
+               $this->load();
+               // If extra data (metadata) was not loaded then it must have been large
+               return $this->extraDataLoaded
+                       && strlen( serialize( $this->metadata ) ) <= self::CACHE_FIELD_MAX_LEN;
        }
 
        /**