Merge "Add option to expose original sha1 in thumb url"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 30 Sep 2015 01:43:52 +0000 (01:43 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 30 Sep 2015 01:43:52 +0000 (01:43 +0000)
1  2 
includes/filerepo/FileRepo.php
includes/filerepo/LocalRepo.php
includes/filerepo/file/File.php

@@@ -52,6 -52,9 +52,9 @@@ class FileRepo 
        /** @var bool */
        protected $hasSha1Storage = false;
  
+       /** @var bool */
+       protected $supportsSha1URLs = false;
        /** @var FileBackend */
        protected $backend;
  
                                $this->zones[$zone]['urlsByExt'] = array();
                        }
                }
+               $this->supportsSha1URLs = !empty( $info['supportsSha1URLs'] );
        }
  
        /**
                }
                if ( !is_null( $this->articleUrl ) ) {
                        # "http://example.com/wiki/$1"
 -                      #
                        # We use "Image:" as the canonical namespace for
                        # compatibility across all MediaWiki versions.
                        return str_replace( '$1',
                }
                if ( !is_null( $this->scriptDirUrl ) ) {
                        # "http://example.com/w"
 -                      #
                        # We use "Image:" as the canonical namespace for
                        # compatibility across all MediaWiki versions,
                        # and just sort of hope index.php is right. ;)
        public function hasSha1Storage() {
                return $this->hasSha1Storage;
        }
+       /**
+        * Returns whether or not repo supports having originals SHA-1s in the thumb URLs
+        * @return boolean
+        */
+       public function supportsSha1URLs() {
+               return $this->supportsSha1URLs;
+       }
  }
  
  /**
@@@ -29,9 -29,6 +29,6 @@@
   * @ingroup FileRepo
   */
  class LocalRepo extends FileRepo {
-       /** @var bool */
-       protected $hasSha1Storage = false;
        /** @var array */
        protected $fileFactory = array( 'LocalFile', 'newFromTitle' );
  
                } else {
                        $expiry = 86400; // has invalidation, 1 day
                }
 +
                $cachedValue = $cache->get( $memcKey );
                if ( $cachedValue === ' ' || $cachedValue === '' ) {
                        // Does not exist
                        return Title::newFromText( $cachedValue, NS_FILE );
                } // else $cachedValue is false or null: cache miss
  
 +              $opts = array( 'since' => $this->getSlaveDB()->trxTimestamp() );
 +
                $id = $this->getArticleID( $title );
                if ( !$id ) {
 -                      $cache->set( $memcKey, " ", $expiry );
 +                      $cache->set( $memcKey, " ", $expiry, $opts );
  
                        return false;
                }
  
                if ( $row && $row->rd_namespace == NS_FILE ) {
                        $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
 -                      $cache->set( $memcKey, $targetTitle->getDBkey(), $expiry );
 +                      $cache->set( $memcKey, $targetTitle->getDBkey(), $expiry, $opts );
  
                        return $targetTitle;
                } else {
 -                      $cache->set( $memcKey, '', $expiry );
 +                      $cache->set( $memcKey, '', $expiry, $opts );
  
                        return false;
                }
                $dbr = $this->getSlaveDB();
                $id = $dbr->selectField(
                        'page', // Table
 -                      'page_id', //Field
 -                      array( //Conditions
 +                      'page_id', // Field
 +                      array( // Conditions
                                'page_namespace' => $title->getNamespace(),
                                'page_title' => $title->getDBkey(),
                        ),
 -                      __METHOD__ //Function name
 +                      __METHOD__ // Function name
                );
  
                return $id;
         */
        function findBySha1s( array $hashes ) {
                if ( !count( $hashes ) ) {
 -                      return array(); //empty parameter
 +                      return array(); // empty parameter
                }
  
                $dbr = $this->getSlaveDB();
         * @return void
         */
        function invalidateImageRedirect( Title $title ) {
 -              $cache = ObjectCache::getMainWANInstance();
 -
 -              $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
 -              if ( $memcKey ) {
 -                      // Set a temporary value for the cache key, to ensure
 -                      // that this value stays purged long enough so that
 -                      // it isn't refreshed with a stale value due to a
 -                      // lagged slave.
 -                      $cache->delete( $memcKey, 12 );
 +              $key = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
 +              if ( $key ) {
 +                      $this->getMasterDB()->onTransactionPreCommitOrIdle( function() use ( $key ) {
 +                              ObjectCache::getMainWANInstance()->delete( $key );
 +                      } );
                }
        }
  
@@@ -383,7 -383,7 +383,7 @@@ abstract class File implements IDBAcces
                                wfDebug( __METHOD__ . ': supposed to render ' . $this->getName() .
                                        ' (' . $this->getMimeType() . "), but can't!\n" );
  
 -                              return $this->getURL(); #hm... return NULL?
 +                              return $this->getURL(); # hm... return NULL?
                        }
                } else {
                        return $this->getURL();
  
                $type = $this->getMediaType();
                $mime = $this->getMimeType();
 -              #wfDebug( "LocalFile::isSafeFile: type= $type, mime= $mime\n" );
 +              # wfDebug( "LocalFile::isSafeFile: type= $type, mime= $mime\n" );
  
                if ( !$type || $type === MEDIATYPE_UNKNOWN ) {
 -                      return false; #unknown type, not trusted
 +                      return false; # unknown type, not trusted
                }
                if ( in_array( $type, $wgTrustedMediaFormats ) ) {
                        return true;
                }
  
                if ( $mime === "unknown/unknown" ) {
 -                      return false; #unknown type, not trusted
 +                      return false; # unknown type, not trusted
                }
                if ( in_array( $mime, $wgTrustedMediaFormats ) ) {
                        return true;
         * @return bool
         */
        function isTrustedFile() {
 -              #this could be implemented to check a flag in the database,
 -              #look for signatures, etc
 +              # this could be implemented to check a flag in the database,
 +              # look for signatures, etc
                return false;
        }
  
                $extension = $this->getExtension();
                list( $thumbExt, ) = $this->getHandler()->getThumbType(
                        $extension, $this->getMimeType(), $params );
-               $thumbName = $this->getHandler()->makeParamString( $params ) . '-' . $name;
-               if ( $thumbExt != $extension ) {
-                       $thumbName .= ".$thumbExt";
+               $thumbName = $this->getHandler()->makeParamString( $params );
+               if ( $this->repo->supportsSha1URLs() ) {
+                       $thumbName .= '-' . $this->getSha1() . '.' . $thumbExt;
+               } else {
+                       $thumbName .= '-' . $name;
+                       if ( $thumbExt != $extension ) {
+                               $thumbName .= ".$thumbExt";
+                       }
                }
  
                return $thumbName;