[FileRepo] Allow different file URLs for media based on file extension.
authorAaron Schulz <aschulz@wikimedia.org>
Sat, 13 Oct 2012 00:24:31 +0000 (17:24 -0700)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 8 Nov 2012 06:19:39 +0000 (06:19 +0000)
Change-Id: Ib647377312c93c8ed046f7b9510d0e656788cdc3

includes/DefaultSettings.php
includes/filerepo/FileRepo.php
includes/filerepo/file/File.php
thumb.php

index 16cae7d..e57b9d7 100644 (file)
@@ -361,7 +361,9 @@ $wgImgAuthPublicTest = true;
  *                          container  : backend container name the zone is in
  *                          directory  : root path within container for the zone
  *                          url        : base URL to the root of the zone
- *                          handlerUrl : base script handled URL to the root of the zone
+ *                          urlsByExt  : map of file extension types to base URLs
+ *                                       (useful for using a different cache for videos)
+ *                          handlerUrl : base script-handled URL to the root of the zone
  *                                       (see FileRepo::getZoneHandlerUrl() function)
  *                      Zones default to using "<repo name>-<zone name>" as the container name
  *                      and default to using the container root as the zone's root directory.
index 6cd93cc..651ee27 100644 (file)
@@ -127,6 +127,9 @@ class FileRepo {
                        if ( !isset( $this->zones[$zone]['directory'] ) ) {
                                $this->zones[$zone]['directory'] = '';
                        }
+                       if ( !isset( $this->zones[$zone]['urlsByExt'] ) ) {
+                               $this->zones[$zone]['urlsByExt'] = array();
+                       }
                }
        }
 
@@ -196,14 +199,17 @@ class FileRepo {
        /**
         * Get the URL corresponding to one of the four basic zones
         *
-        * @param $zone String: one of: public, deleted, temp, thumb
+        * @param $zone String One of: public, deleted, temp, thumb
+        * @param $ext String|null Optional file extension
         * @return String or false
         */
-       public function getZoneUrl( $zone ) {
-               if ( isset( $this->zones[$zone]['url'] )
-                       && in_array( $zone, array( 'public', 'temp', 'thumb' ) ) )
-               {
-                       return $this->zones[$zone]['url']; // custom URL
+       public function getZoneUrl( $zone, $ext = null ) {
+               if ( in_array( $zone, array( 'public', 'temp', 'thumb' ) ) ) { // standard public zones
+                       if ( $ext !== null && isset( $this->zones[$zone]['urlsByExt'][$ext] ) ) {
+                               return $this->zones[$zone]['urlsByExt'][$ext]; // custom URL for extension/zone
+                       } elseif ( isset( $this->zones[$zone]['url'] ) ) {
+                               return $this->zones[$zone]['url']; // custom URL for zone
+                       }
                }
                switch ( $zone ) {
                        case 'public':
index 557609d..9a080ae 100644 (file)
@@ -316,7 +316,8 @@ abstract class File {
        public function getUrl() {
                if ( !isset( $this->url ) ) {
                        $this->assertRepoDefined();
-                       $this->url = $this->repo->getZoneUrl( 'public' ) . '/' . $this->getUrlRel();
+                       $ext = $this->getExtension();
+                       $this->url = $this->repo->getZoneUrl( 'public', $ext ) . '/' . $this->getUrlRel();
                }
                return $this->url;
        }
@@ -1253,7 +1254,8 @@ abstract class File {
         */
        function getArchiveUrl( $suffix = false ) {
                $this->assertRepoDefined();
-               $path = $this->repo->getZoneUrl( 'public' ) . '/archive/' . $this->getHashPath();
+               $ext = $this->getExtension();
+               $path = $this->repo->getZoneUrl( 'public', $ext ) . '/archive/' . $this->getHashPath();
                if ( $suffix === false ) {
                        $path = substr( $path, 0, -1 );
                } else {
@@ -1272,7 +1274,8 @@ abstract class File {
         */
        function getArchiveThumbUrl( $archiveName, $suffix = false ) {
                $this->assertRepoDefined();
-               $path = $this->repo->getZoneUrl( 'thumb' ) . '/archive/' .
+               $ext = $this->getExtension();
+               $path = $this->repo->getZoneUrl( 'thumb', $ext ) . '/archive/' .
                        $this->getHashPath() . rawurlencode( $archiveName ) . "/";
                if ( $suffix === false ) {
                        $path = substr( $path, 0, -1 );
@@ -1291,7 +1294,8 @@ abstract class File {
         */
        function getThumbUrl( $suffix = false ) {
                $this->assertRepoDefined();
-               $path = $this->repo->getZoneUrl( 'thumb' ) . '/' . $this->getUrlRel();
+               $ext = $this->getExtension();
+               $path = $this->repo->getZoneUrl( 'thumb', $ext ) . '/' . $this->getUrlRel();
                if ( $suffix !== false ) {
                        $path .= '/' . rawurlencode( $suffix );
                }
index b0d72f3..aa23335 100644 (file)
--- a/thumb.php
+++ b/thumb.php
@@ -305,10 +305,11 @@ function wfStreamThumb( array $params ) {
 function wfExtractThumbParams( $uriPath ) {
        $repo = RepoGroup::singleton()->getLocalRepo();
 
+       $ext = FileBackend::extensionFromPath( $uriPath );
        // Zone URL might be relative ("/images") or protocol-relative ("//lang.site/image")
        $zoneUriPath = $repo->getZoneHandlerUrl( 'thumb' )
                ? $repo->getZoneHandlerUrl( 'thumb' ) // custom URL
-               : $repo->getZoneUrl( 'thumb' ); // default to main URL
+               : $repo->getZoneUrl( 'thumb', $ext ); // default to main URL
        $bits = wfParseUrl( wfExpandUrl( $zoneUriPath, PROTO_INTERNAL ) );
        if ( $bits && isset( $bits['path'] ) ) {
                $zoneUriPath = $bits['path'];