Some HipHop fixes:
[lhc/web/wiklou.git] / includes / filerepo / FileRepo.php
index 4176d3a..6b87f63 100644 (file)
@@ -1,22 +1,29 @@
 <?php
+/**
+ * Base code for file repositories.
+ *
+ * @file
+ * @ingroup FileRepo
+ */
 
 /**
- * Base class for file repositories
+ * Base class for file repositories.
  * Do not instantiate, use a derived class.
+ *
  * @ingroup FileRepo
  */
 abstract class FileRepo {
        const FILES_ONLY = 1;
        const DELETE_SOURCE = 1;
-       const FIND_PRIVATE = 1;
-       const FIND_IGNORE_REDIRECT = 2;
        const OVERWRITE = 2;
        const OVERWRITE_SAME = 4;
+       const SKIP_VALIDATION = 8;
 
        var $thumbScriptUrl, $transformVia404;
-       var $descBaseUrl, $scriptDirUrl, $articleUrl, $fetchDescription, $initialCapital;
+       var $descBaseUrl, $scriptDirUrl, $scriptExtension, $articleUrl;
+       var $fetchDescription, $initialCapital;
        var $pathDisclosureProtection = 'paranoid';
-       var $descriptionCacheExpiry, $apiThumbCacheExpiry, $hashLevels;
+       var $descriptionCacheExpiry, $hashLevels, $url, $thumbUrl;
 
        /**
         * Factory functions for creating new files
@@ -30,10 +37,11 @@ abstract class FileRepo {
                $this->name = $info['name'];
 
                // Optional settings
-               $this->initialCapital = true; // by default
+               $this->initialCapital = MWNamespace::isCapitalized( NS_FILE );
                foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
-                       'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection', 
-                       'descriptionCacheExpiry', 'apiThumbCacheExpiry', 'hashLevels' ) as $var )
+                       'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection',
+                       'descriptionCacheExpiry', 'hashLevels', 'url', 'thumbUrl', 'scriptExtension' ) 
+                       as $var )
                {
                        if ( isset( $info[$var] ) ) {
                                $this->$var = $info[$var];
@@ -51,12 +59,15 @@ abstract class FileRepo {
 
        /**
         * Create a new File object from the local repository
-        * @param mixed $title Title object or string
-        * @param mixed $time Time at which the image was uploaded.
-        *                    If this is specified, the returned object will be an
-        *                    instance 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.
+        *
+        * @param $title Mixed: Title object or string
+        * @param $time Mixed: Time at which the image was uploaded.
+        *              If this is specified, the returned object will be an
+        *              instance 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 newFile( $title, $time = false ) {
                if ( !($title instanceof Title) ) {
@@ -81,10 +92,20 @@ abstract class FileRepo {
         * Returns false if the file does not exist. Repositories not supporting
         * version control should return false if the time is specified.
         *
-        * @param mixed $title Title object or string
-        * @param mixed $time 14-character timestamp, or false for the current version
+        * @param $title Mixed: Title object or string
+        * @param $options 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.
+        *
+        *     ignoreRedirect: If true, do not follow file redirects
+        *
+        *     private:        If true, return restricted (deleted) files if the current
+        *                     user is allowed to view them. Otherwise, such files will not
+        *                     be found.
         */
-       function findFile( $title, $time = false, $flags = 0 ) {
+       function findFile( $title, $options = array() ) {
+               $time = isset( $options['time'] ) ? $options['time'] : false;
                if ( !($title instanceof Title) ) {
                        $title = Title::makeTitleSafe( NS_FILE, $title );
                        if ( !is_object( $title ) ) {
@@ -105,18 +126,18 @@ abstract class FileRepo {
                        if ( $img && $img->exists() ) {
                                if ( !$img->isDeleted(File::DELETED_FILE) ) {
                                        return $img;
-                               } else if ( ($flags & FileRepo::FIND_PRIVATE) && $img->userCan(File::DELETED_FILE) ) {
+                               } else if ( !empty( $options['private'] )  && $img->userCan(File::DELETED_FILE) ) {
                                        return $img;
                                }
                        }
                }
-                               
+
                # Now try redirects
-               if ( $flags & FileRepo::FIND_IGNORE_REDIRECT ) {
+               if ( !empty( $options['ignoreRedirect'] ) ) {
                        return false;
                }
-               $redir = $this->checkRedirect( $title );                
-               if( $redir && $redir->getNamespace() == NS_FILE) {
+               $redir = $this->checkRedirect( $title );
+               if( $redir && $title->getNamespace() == NS_FILE) {
                        $img = $this->newFile( $redir );
                        if( !$img ) {
                                return false;
@@ -128,52 +149,70 @@ abstract class FileRepo {
                }
                return false;
        }
-       
-       /*
-        * Find many files at once. 
-        * @param array $titles, an array of titles
-        * @todo Think of a good way to optionally pass timestamps to this function.
+
+       /**
+        * 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:
+        *
+        *     $findItem = array( 'title' => $title, 'private' => true );
+        *     $findBatch = array( $findItem );
+        *     $repo->findFiles( $findBatch );
         */
-       function findFiles( $titles ) {
+       function findFiles( $items ) {
                $result = array();
-               foreach ( $titles as $index => $title ) {
-                       $file = $this->findFile( $title );
-                       if ( $file )
+               foreach ( $items as $item ) {
+                       if ( is_array( $item ) ) {
+                               $title = $item['title'];
+                               $options = $item;
+                               unset( $options['title'] );
+                       } else {
+                               $title = $item;
+                               $options = array();
+                       }
+                       $file = $this->findFile( $title, $options );
+                       if ( $file ) {
                                $result[$file->getTitle()->getDBkey()] = $file;
+                       }
                }
                return $result;
        }
-       
+
        /**
         * Create a new File object from the local repository
-        * @param mixed $sha1 SHA-1 key
-        * @param mixed $time Time at which the image was uploaded.
-        *                    If this is specified, the returned object will be an
-        *                    instance 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.
+        * @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 );
+                       if ( $this->fileFactoryKey ) {
+                               return call_user_func( $this->fileFactoryKey, $sha1, $this );
+                       }
                }
+               return false;
        }
-       
+
        /**
         * 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
         * version control should return false if the time is specified.
         *
-        * @param string $sha1 string
-        * @param mixed $time 14-character timestamp, or false for the current version
+        * @param $sha1 String base 36 SHA-1 hash
+        * @param $options Option array, same as findFile().
         */
-       function findFileFromKey( $sha1, $time = false, $flags = 0 ) {
+       function findFileFromKey( $sha1, $options = array() ) {
+               $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 ) {
@@ -185,10 +224,10 @@ abstract class FileRepo {
                # Now try an old version of the file
                if ( $time !== false ) {
                        $img = $this->newFileFromKey( $sha1, $time );
-                       if ( $img->exists() ) {
+                       if ( $img && $img->exists() ) {
                                if ( !$img->isDeleted(File::DELETED_FILE) ) {
                                        return $img;
-                               } else if ( ($flags & FileRepo::FIND_PRIVATE) && $img->userCan(File::DELETED_FILE) ) {
+                               } else if ( !empty( $options['private'] ) && $img->userCan(File::DELETED_FILE) ) {
                                        return $img;
                                }
                        }
@@ -203,6 +242,15 @@ abstract class FileRepo {
                return $this->thumbScriptUrl;
        }
 
+       /**
+        * Get the URL corresponding to one of the four basic zones
+        * @param $zone String: one of: public, deleted, temp, thumb
+        * @return String or false
+        */
+       function getZoneUrl( $zone ) {
+               return false;
+       }
+
        /**
         * Returns true if the repository can transform files via a 404 handler
         */
@@ -212,10 +260,10 @@ abstract class FileRepo {
 
        /**
         * Get the name of an image from its title object
+        * @param $title Title
         */
        function getNameFromTitle( $title ) {
-               global $wgCapitalLinks;
-               if ( $this->initialCapital != $wgCapitalLinks ) {
+               if ( $this->initialCapital != MWNamespace::isCapitalized( NS_FILE ) ) {
                        global $wgContLang;
                        $name = $title->getUserCaseDBKey();
                        if ( $this->initialCapital ) {
@@ -239,7 +287,7 @@ abstract class FileRepo {
                        return $path;
                }
        }
-       
+
        /**
         * Get a relative path including trailing slash, e.g. f/fa/
         * If the repo is not hashed, returns an empty string
@@ -254,6 +302,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 ); 
+       } 
 
        /**
         * Get the URL of an image description page. May return false if it is
@@ -284,8 +344,7 @@ abstract class FileRepo {
                        # We use "Image:" as the canonical namespace for
                        # compatibility across all MediaWiki versions,
                        # and just sort of hope index.php is right. ;)
-                       return $this->scriptDirUrl .
-                               "/index.php?title=Image:$encName";
+                       return $this->makeUrl( "title=Image:$encName" );
                }
                return false;
        }
@@ -295,8 +354,8 @@ abstract class FileRepo {
         * MediaWiki this means action=render. This should only be called by the
         * repository's file class, since it may return invalid results. User code
         * should use File::getDescriptionText().
-        * @param string $name Name of image to fetch
-        * @param string $lang Language to fetch it in, if any.
+        * @param $name String: name of image to fetch
+        * @param $lang String: language to fetch it in, if any.
         */
        function getDescriptionRenderUrl( $name, $lang = null ) {
                $query = 'action=render';
@@ -304,9 +363,10 @@ abstract class FileRepo {
                        $query .= '&uselang=' . $lang;
                }
                if ( isset( $this->scriptDirUrl ) ) {
-                       return $this->scriptDirUrl . '/index.php?title=' .
+                       return $this->makeUrl( 
+                               'title=' .
                                wfUrlencode( 'Image:' . $name ) .
-                               "&$query";
+                               "&$query" );
                } else {
                        $descUrl = $this->getDescriptionUrl( $name );
                        if ( $descUrl ) {
@@ -316,14 +376,25 @@ abstract class FileRepo {
                        }
                }
        }
+       
+       /**
+        * Get the URL of the stylesheet to apply to description pages
+        * @return string
+        */
+       function getDescriptionStylesheetUrl() {
+               if ( $this->scriptDirUrl ) {
+                       return $this->makeUrl( 'title=MediaWiki:Filepage.css&' .
+                               wfArrayToCGI( Skin::getDynamicStylesheetQuery() ) );
+               }
+       }
 
        /**
         * Store a file to a given destination.
         *
-        * @param string $srcPath Source path or virtual URL
-        * @param string $dstZone Destination zone
-        * @param string $dstRel Destination relative path
-        * @param integer $flags Bitwise combination of the following flags:
+        * @param $srcPath String: source path or virtual URL
+        * @param $dstZone String: destination zone
+        * @param $dstRel String: destination relative path
+        * @param $flags Integer: bitwise combination of the following flags:
         *     self::DELETE_SOURCE     Delete the source file after upload
         *     self::OVERWRITE         Overwrite an existing destination file instead of failing
         *     self::OVERWRITE_SAME    Overwrite the file if the destination exists and has the
@@ -341,8 +412,8 @@ abstract class FileRepo {
        /**
         * Store a batch of files
         *
-        * @param array $triplets (src,zone,dest) triplets as per store()
-        * @param integer $flags Flags as per store
+        * @param $triplets Array: (src,zone,dest) triplets as per store()
+        * @param $flags Integer: flags as per store
         */
        abstract function storeBatch( $triplets, $flags = 0 );
 
@@ -350,16 +421,35 @@ abstract class FileRepo {
         * Pick a random name in the temp zone and store a file to it.
         * Returns a FileRepoStatus object with the URL in the value.
         *
-        * @param string $originalName The base name of the file as specified
+        * @param $originalName String: the base name of the file as specified
         *     by the user. The file extension will be maintained.
-        * @param string $srcPath The current location of the file.
+        * @param $srcPath String: the current location of the file.
         */
        abstract function storeTemp( $originalName, $srcPath );
 
+
+       /**
+        * 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
+        *        that the source file should be deleted if possible
+        * @return mixed Status or false
+        */
+       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 string $virtualUrl The virtual URL returned by storeTemp
-        * @return boolean True on success, false on failure
+        * @param $virtualUrl String: the virtual URL returned by storeTemp
+        * @return Boolean: true on success, false on failure
         * STUB
         */
        function freeTemp( $virtualUrl ) {
@@ -373,11 +463,11 @@ abstract class FileRepo {
         * Returns a FileRepoStatus object. On success, the value contains "new" or
         * "archived", to indicate whether the file was new with that name.
         *
-        * @param string $srcPath The source path or URL
-        * @param string $dstRel The destination relative path
-        * @param string $archiveRel The relative path where the existing file is to
+        * @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
         *        be archived, if there is one. Relative to the public zone root.
-        * @param integer $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
+        * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
         *        that the source file should be deleted if possible
         */
        function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 ) {
@@ -395,12 +485,27 @@ abstract class FileRepo {
 
        /**
         * Publish a batch of files
-        * @param array $triplets (source,dest,archive) triplets as per publish()
-        * @param integer $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
+        * @param $triplets Array: (source,dest,archive) triplets as per publish()
+        * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
         *        that the source files should be deleted if possible
         */
        abstract function publishBatch( $triplets, $flags = 0 );
 
+       function fileExists( $file, $flags = 0 ) {
+               $result = $this->fileExistsBatch( array( $file ), $flags );
+               return $result[0];
+       }
+
+       /**
+        * Checks existence of an array of files.
+        *
+        * @param $files Array: URLs (or paths) of files to check
+        * @param $flags Integer: bitwise combination of the following flags:
+        *     self::FILES_ONLY     Mark file as existing only if it is a file (not directory)
+        * @return Either array of files and existence flags, or false
+        */
+       abstract function fileExistsBatch( $files, $flags = 0 );
+
        /**
         * Move a group of files to the deletion archive.
         *
@@ -411,7 +516,7 @@ abstract class FileRepo {
         * assumes a naming scheme in the deleted zone based on content hash, as
         * opposed to the public zone which is assumed to be unique.
         *
-        * @param array $sourceDestPairs Array of source/destination pairs. Each element
+        * @param $sourceDestPairs Array of source/destination pairs. Each element
         *        is a two-element array containing the source file path relative to the
         *        public root in the first element, and the archive file path relative
         *        to the deleted zone root in the second element.
@@ -423,10 +528,10 @@ abstract class FileRepo {
         * Move a file to the deletion archive.
         * If no valid deletion archive exists, this may either delete the file
         * or throw an exception, depending on the preference of the repository
-        * @param mixed $srcRel Relative path for the file to be deleted
-        * @param mixed $archiveRel Relative path for the archive location.
+        * @param $srcRel Mixed: relative path for the file to be deleted
+        * @param $archiveRel Mixed: relative path for the archive location.
         *        Relative to a private archive directory.
-        * @return WikiError object (wikitext-formatted), or true for success
+        * @return FileRepoStatus object
         */
        function delete( $srcRel, $archiveRel ) {
                return $this->deleteBatch( array( array( $srcRel, $archiveRel ) ) );
@@ -501,7 +606,7 @@ 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 );
        }
 
        /**
@@ -522,7 +627,8 @@ abstract class FileRepo {
         * title object. If not, return false.
         * STUB
         *
-        * @param Title $title Title of image
+        * @param $title Title of image
+        * @return Bool
         */
        function checkRedirect( $title ) {
                return false;
@@ -530,45 +636,74 @@ abstract class FileRepo {
 
        /**
         * Invalidates image redirect cache related to that image
+        * Doesn't do anything for repositories that don't support image redirects.
         *
-        * @param Title $title Title of image
-        */     
-       function invalidateImageRedirect( $title ) {
-               global $wgMemc;
-               $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
-               $wgMemc->delete( $memcKey );
-       }
-       
+        * STUB
+        * @param $title Title of image
+        */
+       function invalidateImageRedirect( $title ) {}
+
+       /**
+        * Get an array or iterator of file objects for files that have a given
+        * SHA-1 content hash.
+        *
+        * STUB
+        */
        function findBySha1( $hash ) {
                return array();
        }
-       
+
        /**
-        * Get the human-readable name of the repo. 
+        * Get the human-readable name of the repo.
         * @return string
         */
        public function getDisplayName() {
                // We don't name our own repo, return nothing
-               if ( $this->name == 'local' ) {
+               if ( $this->isLocal() ) {
                        return null;
                }
-               $repoName = wfMsg( 'shared-repo-name-' . $this->name );
-               if ( !wfEmptyMsg( 'shared-repo-name-' . $this->name, $repoName ) ) {
-                       return $repoName;
-               }
-               return wfMsg( 'shared-repo' ); 
-       }
-       
-       function getSlaveDB() {
-               return wfGetDB( DB_SLAVE );
+               // 'shared-repo-name-wikimediacommons' is used when $wgUseInstantCommons = true
+               return wfMessageFallback( 'shared-repo-name-' . $this->name, 'shared-repo' )->text();
        }
 
-       function getMasterDB() {
-               return wfGetDB( DB_MASTER );
+       /**
+        * Returns true if this the local file repository.
+        *
+        * @return bool
+        */
+       function isLocal() {
+               return $this->getName() == 'local';
        }
 
-       function getMemcKey( $key ) {
-               return wfWikiID( $this->getSlaveDB() ) . ":{$key}";
+
+       /**
+        * Get a key on the primary cache for this repository.
+        * Returns false if the repository's cache is not accessible at this site.
+        * The parameters are the parts of the key, as for wfMemcKey().
+        *
+        * STUB
+        */
+       function getSharedCacheKey( /*...*/ ) {
+               return false;
        }
 
+       /**
+        * Get a key for this repo in the local cache domain. These cache keys are
+        * not shared with remote instances of the repo.
+        * The parameters are the parts of the key, as for wfMemcKey().
+        */
+       function getLocalCacheKey( /*...*/ ) {
+               $args = func_get_args();
+               array_unshift( $args, 'filerepo', $this->getName() );
+               return call_user_func_array( 'wfMemcKey', $args );
+       }
+       
+       /**
+        * Get an UploadStash associated with this repo.
+        *
+        * @return UploadStash
+        */
+       function getUploadStash() {
+               return new UploadStash( $this );
+       }
 }