FU r102073:
[lhc/web/wiklou.git] / includes / filerepo / File.php
index 63719ee..4dffbfe 100644 (file)
@@ -30,7 +30,14 @@ abstract class File {
        const DELETED_COMMENT = 2;
        const DELETED_USER = 4;
        const DELETED_RESTRICTED = 8;
-       const RENDER_NOW = 1;
+
+       /** Force rendering in the current process */
+       const RENDER_NOW   = 1;
+       /**
+        * Force rendering even if thumbnail already exist and using RENDER_NOW
+        * I.e. you have to pass both flags: File::RENDER_NOW | File::RENDER_FORCE 
+        */
+       const RENDER_FORCE = 2;
 
        const DELETE_SOURCE = 1;
 
@@ -54,12 +61,12 @@ abstract class File {
         */
 
        /**
-        * @var LocalRepo
+        * @var FileRepo|false
         */
        var $repo;
 
        /**
-        * @var Title
+        * @var Title|false
         */
        var $title;
 
@@ -78,16 +85,55 @@ abstract class File {
        protected $canRender, $isSafeFile;
 
        /**
-        * Call this constructor from child classes
+        * @var string Required Repository class type
+        */
+       protected $repoClass = 'FileRepo';
+
+       /**
+        * Call this constructor from child classes.
+        * 
+        * Both $title and $repo are optional, though some functions
+        * may return false or throw exceptions if they are not set.
+        * Most subclasses will want to call assertRepoDefined() here.
         *
-        * @param $title
-        * @param $repo
+        * @param $title Title|string|false
+        * @param $repo FileRepo|false
         */
        function __construct( $title, $repo ) {
+               if ( $title !== false ) { // subclasses may not use MW titles
+                       $title = self::normalizeTitle( $title, 'exception' );
+               }
                $this->title = $title;
                $this->repo = $repo;
        }
 
+       /**
+        * Given a string or Title object return either a
+        * valid Title object with namespace NS_FILE or null
+        * @param $title Title|string
+        * @param $exception string|false Use 'exception' to throw an error on bad titles
+        * @return Title|null
+        */
+       static function normalizeTitle( $title, $exception = false ) {
+               $ret = $title;
+               if ( $ret instanceof Title ) {
+                       # Normalize NS_MEDIA -> NS_FILE
+                       if ( $ret->getNamespace() == NS_MEDIA ) {
+                               $ret = Title::makeTitleSafe( NS_FILE, $ret->getDBkey() );
+                       # Sanity check the title namespace
+                       } elseif ( $ret->getNamespace() !== NS_FILE ) {
+                               $ret = null;
+                       }
+               } else {
+                       # Convert strings to Title objects
+                       $ret = Title::makeTitleSafe( NS_FILE, (string)$ret );
+               }
+               if ( !$ret && $exception !== false ) {
+                       throw new MWException( "`$title` is not a valid file title." );
+               }
+               return $ret;
+       }
+
        function __get( $name ) {
                $function = array( $this, 'get' . ucfirst( $name ) );
                if ( !is_callable( $function ) ) {
@@ -168,6 +214,7 @@ abstract class File {
         */
        public function getName() {
                if ( !isset( $this->name ) ) {
+                       $this->assertRepoDefined();
                        $this->name = $this->repo->getNameFromTitle( $this->title );
                }
                return $this->name;
@@ -189,7 +236,7 @@ abstract class File {
 
        /**
         * Return the associated title object
-        * @return Title
+        * @return Title|false
         */
        public function getTitle() { return $this->title; }
 
@@ -212,6 +259,7 @@ abstract class File {
         */
        public function getUrl() {
                if ( !isset( $this->url ) ) {
+                       $this->assertRepoDefined();
                        $this->url = $this->repo->getZoneUrl( 'public' ) . '/' . $this->getUrlRel();
                }
                return $this->url;
@@ -266,7 +314,8 @@ abstract class File {
        */
        public function getPath() {
                if ( !isset( $this->path ) ) {
-                       $this->path = $this->repo->getZonePath('public') . '/' . $this->getRel();
+                       $this->assertRepoDefined();
+                       $this->path = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
                }
                return $this->path;
        }
@@ -672,18 +721,21 @@ abstract class File {
 
                $thumbPath = $this->getThumbPath( $thumbName );
                if ( $this->repo && $this->repo->canTransformVia404() && !($flags & self::RENDER_NOW ) ) {
+                       wfDebug( __METHOD__ . " transformation deferred." );
                        return $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
                }
 
                wfDebug( __METHOD__.": Doing stat for $thumbPath\n" );
                $this->migrateThumbFile( $thumbName );
-               if ( file_exists( $thumbPath )) {
+               if ( file_exists( $thumbPath ) && !($flags & self::RENDER_FORCE) ) { 
                        $thumbTime = filemtime( $thumbPath );
                        if ( $thumbTime !== FALSE &&
                             gmdate( 'YmdHis', $thumbTime ) >= $wgThumbnailEpoch ) { 
 
                                return $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
                        }
+               } elseif( $flags & self::RENDER_FORCE ) {
+                       wfDebug( __METHOD__ . " forcing rendering per flag File::RENDER_FORCE\n" ); 
                }
                $thumb = $this->handler->doTransform( $this, $thumbPath, $thumbUrl, $params );
 
@@ -888,6 +940,7 @@ abstract class File {
         */
        function getHashPath() {
                if ( !isset( $this->hashPath ) ) {
+                       $this->assertRepoDefined();
                        $this->hashPath = $this->repo->getHashPath( $this->getName() );
                }
                return $this->hashPath;
@@ -955,6 +1008,7 @@ abstract class File {
         * @return string
         */
        function getArchivePath( $suffix = false ) {
+               $this->assertRepoDefined();
                return $this->repo->getZonePath( 'public' ) . '/' . $this->getArchiveRel( $suffix );
        }
 
@@ -967,7 +1021,9 @@ abstract class File {
         * @return string
         */
        function getArchiveThumbPath( $archiveName, $suffix = false ) {
-               return $this->repo->getZonePath( 'thumb' ) . '/' . $this->getArchiveThumbRel( $archiveName, $suffix );
+               $this->assertRepoDefined();
+               return $this->repo->getZonePath( 'thumb' ) . '/' .
+                       $this->getArchiveThumbRel( $archiveName, $suffix );
        }
 
        /**
@@ -978,6 +1034,7 @@ abstract class File {
         * @return string
         */
        function getThumbPath( $suffix = false ) {
+               $this->assertRepoDefined();
                $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getRel();
                if ( $suffix !== false ) {
                        $path .= '/' . $suffix;
@@ -993,7 +1050,8 @@ abstract class File {
         * @return string
         */
        function getArchiveUrl( $suffix = false ) {
-               $path = $this->repo->getZoneUrl('public') . '/archive/' . $this->getHashPath();
+               $this->assertRepoDefined();
+               $path = $this->repo->getZoneUrl( 'public' ) . '/archive/' . $this->getHashPath();
                if ( $suffix === false ) {
                        $path = substr( $path, 0, -1 );
                } else {
@@ -1011,7 +1069,9 @@ abstract class File {
         * @return string
         */
        function getArchiveThumbUrl( $archiveName, $suffix = false ) {
-               $path = $this->repo->getZoneUrl('thumb') . '/archive/' . $this->getHashPath() . rawurlencode( $archiveName ) . "/";
+               $this->assertRepoDefined();
+               $path = $this->repo->getZoneUrl( 'thumb' ) . '/archive/' .
+                       $this->getHashPath() . rawurlencode( $archiveName ) . "/";
                if ( $suffix === false ) {
                        $path = substr( $path, 0, -1 );
                } else {
@@ -1028,6 +1088,7 @@ abstract class File {
         * @return path
         */
        function getThumbUrl( $suffix = false ) {
+               $this->assertRepoDefined();
                $path = $this->repo->getZoneUrl('thumb') . '/' . $this->getUrlRel();
                if ( $suffix !== false ) {
                        $path .= '/' . rawurlencode( $suffix );
@@ -1043,6 +1104,7 @@ abstract class File {
         * @return string
         */
        function getArchiveVirtualUrl( $suffix = false ) {
+               $this->assertRepoDefined();
                $path = $this->repo->getVirtualUrl() . '/public/archive/' . $this->getHashPath();
                if ( $suffix === false ) {
                        $path = substr( $path, 0, -1 );
@@ -1060,6 +1122,7 @@ abstract class File {
         * @return string
         */
        function getThumbVirtualUrl( $suffix = false ) {
+               $this->assertRepoDefined();
                $path = $this->repo->getVirtualUrl() . '/thumb/' . $this->getUrlRel();
                if ( $suffix !== false ) {
                        $path .= '/' . rawurlencode( $suffix );
@@ -1075,6 +1138,7 @@ abstract class File {
         * @return string
         */
        function getVirtualUrl( $suffix = false ) {
+               $this->assertRepoDefined();
                $path = $this->repo->getVirtualUrl() . '/public/' . $this->getUrlRel();
                if ( $suffix !== false ) {
                        $path .= '/' . rawurlencode( $suffix );
@@ -1086,6 +1150,7 @@ abstract class File {
         * @return bool
         */
        function isHashed() {
+               $this->assertRepoDefined();
                return $this->repo->isHashed();
        }
 
@@ -1165,7 +1230,7 @@ abstract class File {
        /**
         * Returns the repository
         *
-        * @return FileRepo
+        * @return FileRepo|false
         */
        function getRepo() {
                return $this->repo;
@@ -1344,7 +1409,7 @@ abstract class File {
         */
        function getDescriptionText() {
                global $wgMemc, $wgLang;
-               if ( !$this->repo->fetchDescription ) {
+               if ( !$this->repo || !$this->repo->fetchDescription ) {
                        return false;
                }
                $renderUrl = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgLang->getCode() );
@@ -1595,6 +1660,26 @@ abstract class File {
        function isMissing() {
                return false;
        }
+
+       /**
+        * Assert that $this->repo is set to a valid FileRepo instance
+        * @throws MWException
+        */
+       protected function assertRepoDefined() {
+               if ( !( $this->repo instanceof $this->repoClass ) ) {
+                       throw new MWException( "A {$this->repoClass} object is not set for this File.\n" );
+               }
+       }
+
+       /**
+        * Assert that $this->title is set to a Title
+        * @throws MWException
+        */
+       protected function assertTitleDefined() {
+               if ( !( $this->title instanceof Title ) ) {
+                       throw new MWException( "A Title object is not set for this File.\n" );
+               }
+       }
 }
 /**
  * Aliases for backwards compatibility with 1.6