FU r101117: removed cURL thumb handler code and made thumb_handler.php a thin wrapper...
[lhc/web/wiklou.git] / includes / filerepo / FileRepo.php
index 7314e11..58b64e4 100644 (file)
@@ -40,7 +40,7 @@ abstract class FileRepo {
                $this->initialCapital = MWNamespace::isCapitalized( NS_FILE );
                foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
                        'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection',
-                       'descriptionCacheExpiry', 'hashLevels', 'url', 'thumbUrl', 'scriptExtension' ) 
+                       'descriptionCacheExpiry', 'hashLevels', 'url', 'thumbUrl', 'scriptExtension' )
                        as $var )
                {
                        if ( isset( $info[$var] ) ) {
@@ -52,6 +52,10 @@ abstract class FileRepo {
 
        /**
         * Determine if a string is an mwrepo:// URL
+        *
+        * @param $url string
+        *
+        * @return bool
         */
        static function isVirtualUrl( $url ) {
                return substr( $url, 0, 9 ) == 'mwrepo://';
@@ -67,14 +71,12 @@ abstract class FileRepo {
         *              current file. Repositories not supporting version control
         *              should return false if this parameter is set.
         *
-        * @return File
+        * @return File|null A File, or null if passed an invalid Title
         */
        function newFile( $title, $time = false ) {
-               if ( !($title instanceof Title) ) {
-                       $title = Title::makeTitleSafe( NS_FILE, $title );
-                       if ( !is_object( $title ) ) {
-                               return null;
-                       }
+               $title = File::normalizeTitle( $title );
+               if ( !$title ) {
+                       return null;
                }
                if ( $time ) {
                        if ( $this->oldFileFactory ) {
@@ -93,7 +95,7 @@ abstract class FileRepo {
         * version control should return false if the time is specified.
         *
         * @param $title Mixed: Title object or string
-        * @param $options Associative array of options:
+        * @param $options array Associative array of options:
         *     time:           requested time for an archived image, or false for the
         *                     current version. An image object will be returned which was
         *                     created at the specified time.
@@ -103,21 +105,15 @@ abstract class FileRepo {
         *     private:        If true, return restricted (deleted) files if the current
         *                     user is allowed to view them. Otherwise, such files will not
         *                     be found.
+        *
+        * @return File|false
         */
        function findFile( $title, $options = array() ) {
-               if ( !is_array( $options ) ) {
-                       // MW 1.15 compat
-                       $time = $options;
-                       $options = array();
-               } else {
-                       $time = isset( $options['time'] ) ? $options['time'] : false;
-               }
-               if ( !($title instanceof Title) ) {
-                       $title = Title::makeTitleSafe( NS_FILE, $title );
-                       if ( !is_object( $title ) ) {
-                               return false;
-                       }
+               $title = File::normalizeTitle( $title );
+               if ( !$title ) {
+                       return false;
                }
+               $time = isset( $options['time'] ) ? $options['time'] : false;
                # First try the current version of the file to see if it precedes the timestamp
                $img = $this->newFile( $title );
                if ( !$img ) {
@@ -130,9 +126,9 @@ abstract class FileRepo {
                if ( $time !== false ) {
                        $img = $this->newFile( $title, $time );
                        if ( $img && $img->exists() ) {
-                               if ( !$img->isDeleted(File::DELETED_FILE) ) {
-                                       return $img;
-                               } else if ( !empty( $options['private'] )  && $img->userCan(File::DELETED_FILE) ) {
+                               if ( !$img->isDeleted( File::DELETED_FILE ) ) {
+                                       return $img; // always OK
+                               } elseif ( !empty( $options['private'] ) && $img->userCan( File::DELETED_FILE ) ) {
                                        return $img;
                                }
                        }
@@ -156,7 +152,7 @@ abstract class FileRepo {
                return false;
        }
 
-       /*
+       /**
         * Find many files at once.
         * @param $items An array of titles, or an array of findFile() options with
         *    the "title" option giving the title. Example:
@@ -164,6 +160,8 @@ abstract class FileRepo {
         *     $findItem = array( 'title' => $title, 'private' => true );
         *     $findBatch = array( $findItem );
         *     $repo->findFiles( $findBatch );
+        *
+        * @return array
         */
        function findFiles( $items ) {
                $result = array();
@@ -184,29 +182,6 @@ abstract class FileRepo {
                return $result;
        }
 
-       /**
-        * Create a new File object from the local repository
-        * @param $sha1 Mixed: base 36 SHA-1 hash
-        * @param $time Mixed: time at which the image was uploaded.
-        *              If this is specified, the returned object will be an
-        *              of the repository's old file class instead of a current
-        *              file. Repositories not supporting version control should
-        *              return false if this parameter is set.
-        *
-        * @return File
-        */
-       function newFileFromKey( $sha1, $time = false ) {
-               if ( $time ) {
-                       if ( $this->oldFileFactoryKey ) {
-                               return call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
-                       } else {
-                               return false;
-                       }
-               } else {
-                       return call_user_func( $this->fileFactoryKey, $sha1, $this );
-               }
-       }
-
        /**
         * Find an instance of the file with this key, created at the specified time
         * Returns false if the file does not exist. Repositories not supporting
@@ -216,29 +191,24 @@ abstract class FileRepo {
         * @param $options Option array, same as findFile().
         */
        function findFileFromKey( $sha1, $options = array() ) {
-               if ( !is_array( $options ) ) {
-                       # MW 1.15 compat
-                       $time = $options;
-                       $options = array();
-               } else {
-                       $time = isset( $options['time'] ) ? $options['time'] : false;
-               }
+               $time = isset( $options['time'] ) ? $options['time'] : false;
 
-               # First try the current version of the file to see if it precedes the timestamp
-               $img = $this->newFileFromKey( $sha1 );
-               if ( !$img ) {
-                       return false;
+               # First try to find a matching current version of a file...
+               if ( $this->fileFactoryKey ) {
+                       $img = call_user_func( $this->fileFactoryKey, $sha1, $this, $time );
+               } else {
+                       return false; // find-by-sha1 not supported
                }
-               if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
+               if ( $img && $img->exists() ) {
                        return $img;
                }
-               # Now try an old version of the file
-               if ( $time !== false ) {
-                       $img = $this->newFileFromKey( $sha1, $time );
-                       if ( $img->exists() ) {
-                               if ( !$img->isDeleted(File::DELETED_FILE) ) {
-                                       return $img;
-                               } else if ( !empty( $options['private'] ) && $img->userCan(File::DELETED_FILE) ) {
+               # Now try to find a matching old version of a file...
+               if ( $time !== false && $this->oldFileFactoryKey ) { // find-by-sha1 supported?
+                       $img = call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
+                       if ( $img && $img->exists() ) {
+                               if ( !$img->isDeleted( File::DELETED_FILE ) ) {
+                                       return $img; // always OK
+                               } elseif ( !empty( $options['private'] ) && $img->userCan( File::DELETED_FILE ) ) {
                                        return $img;
                                }
                        }
@@ -264,6 +234,8 @@ abstract class FileRepo {
 
        /**
         * Returns true if the repository can transform files via a 404 handler
+        *
+        * @return bool
         */
        function canTransformVia404() {
                return $this->transformVia404;
@@ -273,7 +245,7 @@ abstract class FileRepo {
         * Get the name of an image from its title object
         * @param $title Title
         */
-       function getNameFromTitle( $title ) {
+       function getNameFromTitle( Title $title ) {
                if ( $this->initialCapital != MWNamespace::isCapitalized( NS_FILE ) ) {
                        global $wgContLang;
                        $name = $title->getUserCaseDBKey();
@@ -286,6 +258,11 @@ abstract class FileRepo {
                return $name;
        }
 
+       /**
+        * @param $name
+        * @param $levels
+        * @return string
+        */
        static function getHashPathForLevel( $name, $levels ) {
                if ( $levels == 0 ) {
                        return '';
@@ -299,9 +276,22 @@ abstract class FileRepo {
                }
        }
 
+       /**
+        * Get the number of hash directory levels
+        *
+        * @return integer
+        */
+       function getHashLevels() {
+               return $this->hashLevels;
+       }
+
        /**
         * Get a relative path including trailing slash, e.g. f/fa/
         * If the repo is not hashed, returns an empty string
+        *
+        * @param $name string
+        *
+        * @return string
         */
        function getHashPath( $name ) {
                return self::getHashPathForLevel( $name, $this->hashLevels );
@@ -313,18 +303,18 @@ abstract class FileRepo {
        function getName() {
                return $this->name;
        }
-       
+
        /**
         * Make an url to this repo
-        * 
+        *
         * @param $query mixed Query string to append
         * @param $entry string Entry point; defaults to index
         * @return string
         */
        function makeUrl( $query = '', $entry = 'index' ) {
                $ext = isset( $this->scriptExtension ) ? $this->scriptExtension : '.php';
-               return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}{$ext}", $query ); 
-       } 
+               return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}{$ext}", $query );
+       }
 
        /**
         * Get the URL of an image description page. May return false if it is
@@ -374,7 +364,7 @@ abstract class FileRepo {
                        $query .= '&uselang=' . $lang;
                }
                if ( isset( $this->scriptDirUrl ) ) {
-                       return $this->makeUrl( 
+                       return $this->makeUrl(
                                'title=' .
                                wfUrlencode( 'Image:' . $name ) .
                                "&$query" );
@@ -387,7 +377,7 @@ abstract class FileRepo {
                        }
                }
        }
-       
+
        /**
         * Get the URL of the stylesheet to apply to description pages
         * @return string
@@ -440,7 +430,16 @@ abstract class FileRepo {
 
 
        /**
-        * Append the contents of the source path to the given file.
+        * Concatenate and array of file sources. 
+        * @param $fileList Array of file sources
+        * @param $targetPath String target destination for file.
+        * @throws MWException
+        */
+       abstract function concatenate( $fileList, $targetPath, $flags = 0 );
+       
+       /**
+        * Append the contents of the source path to the given file, OR queue
+        * the appending operation in anticipation of a later appendFinish() call.
         * @param $srcPath String: location of the source file
         * @param $toAppendPath String: path to append to.
         * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
@@ -449,6 +448,13 @@ abstract class FileRepo {
         */
        abstract function append( $srcPath, $toAppendPath, $flags = 0 );
 
+       /**
+        * Finish the append operation.
+        * @param $toAppendPath String: path to append to.
+        * @return mixed Status or false
+        */
+       abstract function appendFinish( $toAppendPath );
+
        /**
         * Remove a temporary file or mark it for garbage collection
         * @param $virtualUrl String: the virtual URL returned by storeTemp
@@ -468,7 +474,7 @@ abstract class FileRepo {
         *
         * @param $srcPath String: the source path or URL
         * @param $dstRel String: the destination relative path
-        * @param $archiveRel String: rhe relative path where the existing file is to
+        * @param $archiveRel String: the relative path where the existing file is to
         *        be archived, if there is one. Relative to the public zone root.
         * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
         *        that the source file should be deleted if possible
@@ -494,6 +500,11 @@ abstract class FileRepo {
         */
        abstract function publishBatch( $triplets, $flags = 0 );
 
+       /**
+        * @param $file
+        * @param int $flags
+        * @return bool
+        */
        function fileExists( $file, $flags = 0 ) {
                $result = $this->fileExistsBatch( array( $file ), $flags );
                return $result[0];
@@ -544,6 +555,8 @@ abstract class FileRepo {
         * Get properties of a file with a given virtual URL
         * The virtual URL must refer to this repo
         * Properties should ultimately be obtained via File::getPropsFromPath()
+        *
+        * @param $virtualUrl string
         */
        abstract function getFileProps( $virtualUrl );
 
@@ -558,6 +571,10 @@ abstract class FileRepo {
 
        /**
         * Determine if a relative path is valid, i.e. not blank or involving directory traveral
+        *
+        * @param $filename string
+        *
+        * @return bool
         */
        function validateFilename( $filename ) {
                if ( strval( $filename ) == '' ) {
@@ -570,11 +587,11 @@ abstract class FileRepo {
                 * Use the same traversal protection as Title::secureAndSplit()
                 */
                if ( strpos( $filename, '.' ) !== false &&
-                    ( $filename === '.' || $filename === '..' ||
-                      strpos( $filename, './' ) === 0  ||
-                      strpos( $filename, '../' ) === 0 ||
-                      strpos( $filename, '/./' ) !== false ||
-                      strpos( $filename, '/../' ) !== false ) )
+                       ( $filename === '.' || $filename === '..' ||
+                               strpos( $filename, './' ) === 0  ||
+                               strpos( $filename, '../' ) === 0 ||
+                               strpos( $filename, '/./' ) !== false ||
+                               strpos( $filename, '/../' ) !== false ) )
                {
                        return false;
                } else {
@@ -586,6 +603,11 @@ abstract class FileRepo {
         * Path disclosure protection functions
         */
        function paranoidClean( $param ) { return '[hidden]'; }
+
+       /**
+        * @param $param
+        * @return
+        */
        function passThrough( $param ) { return $param; }
 
        /**
@@ -609,11 +631,13 @@ abstract class FileRepo {
        function newFatal( $message /*, parameters...*/ ) {
                $params = func_get_args();
                array_unshift( $params, $this );
-               return call_user_func_array( array( 'FileRepoStatus', 'newFatal' ), $params );
+               return MWInit::callStaticMethod( 'FileRepoStatus', 'newFatal', $params );
        }
 
        /**
         * Create a new good result
+        *
+        * @return FileRepoStatus
         */
        function newGood( $value = null ) {
                return FileRepoStatus::newGood( $this, $value );
@@ -633,7 +657,7 @@ abstract class FileRepo {
         * @param $title Title of image
         * @return Bool
         */
-       function checkRedirect( $title ) {
+       function checkRedirect( Title $title ) {
                return false;
        }
 
@@ -644,7 +668,7 @@ abstract class FileRepo {
         * STUB
         * @param $title Title of image
         */
-       function invalidateImageRedirect( $title ) {}
+       function invalidateImageRedirect( Title $title ) {}
 
        /**
         * Get an array or iterator of file objects for files that have a given
@@ -678,7 +702,6 @@ abstract class FileRepo {
                return $this->getName() == 'local';
        }
 
-
        /**
         * Get a key on the primary cache for this repository.
         * Returns false if the repository's cache is not accessible at this site.
@@ -700,7 +723,7 @@ abstract class FileRepo {
                array_unshift( $args, 'filerepo', $this->getName() );
                return call_user_func_array( 'wfMemcKey', $args );
        }
-       
+
        /**
         * Get an UploadStash associated with this repo.
         *