[FileBackend] Make sure multiwrite sub-backends use the proxy backend wiki ID.
[lhc/web/wiklou.git] / includes / filerepo / RepoGroup.php
index 65de63b..6b31b7e 100644 (file)
@@ -1,27 +1,46 @@
 <?php
 /**
- * Prioritized list of file repositories
+ * Prioritized list of file repositories.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
  *
  * @file
  * @ingroup FileRepo
  */
 
-/**
- * @defgroup FileRepo FileRepo
- */
-
 /**
  * Prioritized list of file repositories
  *
  * @ingroup FileRepo
  */
 class RepoGroup {
-       var $localRepo, $foreignRepos, $reposInitialised = false;
+       /**
+        * @var LocalRepo
+        */
+       var $localRepo;
+
+       var $foreignRepos, $reposInitialised = false;
        var $localInfo, $foreignInfo;
        var $cache;
 
+       /**
+        * @var RepoGroup
+        */
        protected static $instance;
-       const MAX_CACHE_SIZE = 1000;
+       const MAX_CACHE_SIZE = 500;
 
        /**
         * Get a RepoGroup instance. At present only one instance of RepoGroup is
@@ -47,6 +66,11 @@ class RepoGroup {
 
        /**
         * Set the singleton instance to a given object
+        * Used by extensions which hook into the Repo chain.
+        * It's not enough to just create a superclass ... you have
+        * to get people to call into it even though all they know is RepoGroup::singleton()
+        *
+        * @param $instance RepoGroup
         */
        static function setSingleton( $instance ) {
                self::$instance = $instance;
@@ -55,7 +79,7 @@ class RepoGroup {
        /**
         * Construct a group of file repositories.
         *
-        * @param $localInfo Associative array for local repo's info
+        * @param $localInfo array Associative array for local repo's info
         * @param $foreignInfo Array of repository info arrays.
         *     Each info array is an associative array with the 'class' member
         *     giving the class name. The entire array is passed to the repository
@@ -71,8 +95,8 @@ class RepoGroup {
         * Search repositories for an image.
         * You can also use wfFindFile() to do this.
         *
-        * @param $title Mixed: Title object or string
-        * @param $options Associative array of options:
+        * @param $title Title|string Title object or string
+        * @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.
@@ -94,67 +118,57 @@ class RepoGroup {
                if ( !$this->reposInitialised ) {
                        $this->initialiseRepos();
                }
-               if ( !($title instanceof Title) ) {
-                       $title = Title::makeTitleSafe( NS_FILE, $title );
-                       if ( !is_object( $title ) ) {
-                               return false;
-                       }
-               }
-
-               if ( $title->getNamespace() != NS_MEDIA && $title->getNamespace() != NS_FILE ) {
-                       throw new MWException( __METHOD__ . ' received an Title object with incorrect namespace' );
+               $title = File::normalizeTitle( $title );
+               if ( !$title ) {
+                       return false;
                }
 
                # Check the cache
                if ( empty( $options['ignoreRedirect'] )
                        && empty( $options['private'] )
-                       && empty( $options['bypassCache'] )
-                       && $title->getNamespace() == NS_FILE )
+                       && empty( $options['bypassCache'] ) )
                {
-                       $useCache = true;
                        $time = isset( $options['time'] ) ? $options['time'] : '';
                        $dbkey = $title->getDBkey();
                        if ( isset( $this->cache[$dbkey][$time] ) ) {
                                wfDebug( __METHOD__.": got File:$dbkey from process cache\n" );
                                # Move it to the end of the list so that we can delete the LRU entry later
-                               $tmp = $this->cache[$dbkey];
-                               unset( $this->cache[$dbkey] );
-                               $this->cache[$dbkey] = $tmp;
+                               $this->pingCache( $dbkey );
                                # Return the entry
                                return $this->cache[$dbkey][$time];
-                       } else {
-                               # Add a negative cache entry, may be overridden
-                               $this->trimCache();
-                               $this->cache[$dbkey][$time] = false;
-                               $cacheEntry =& $this->cache[$dbkey][$time];
                        }
+                       $useCache = true;
                } else {
                        $useCache = false;
                }
 
                # Check the local repo
                $image = $this->localRepo->findFile( $title, $options );
-               if ( $image ) {
-                       if ( $useCache ) {
-                               $cacheEntry = $image;
-                       }
-                       return $image;
-               }
 
                # Check the foreign repos
-               foreach ( $this->foreignRepos as $repo ) {
-                       $image = $repo->findFile( $title, $options );
-                       if ( $image ) {
-                               if ( $useCache ) {
-                                       $cacheEntry = $image;
+               if ( !$image ) {
+                       foreach ( $this->foreignRepos as $repo ) {
+                               $image = $repo->findFile( $title, $options );
+                               if ( $image ) {
+                                       break;
                                }
-                               return $image;
                        }
                }
-               # Not found, do not override negative cache
-               return false;
+
+               $image = $image ? $image : false; // type sanity
+               # Cache file existence or non-existence
+               if ( $useCache && ( !$image || $image->isCacheable() ) ) {
+                       $this->trimCache();
+                       $this->cache[$dbkey][$time] = $image;
+               }
+
+               return $image;
        }
 
+       /**
+        * @param $inputItems array
+        * @return array
+        */
        function findFiles( $inputItems ) {
                if ( !$this->reposInitialised ) {
                        $this->initialiseRepos();
@@ -165,10 +179,10 @@ class RepoGroup {
                        if ( !is_array( $item ) ) {
                                $item = array( 'title' => $item );
                        }
-                       if ( !( $item['title'] instanceof Title ) )
-                               $item['title'] = Title::makeTitleSafe( NS_FILE, $item['title'] );
-                       if ( $item['title'] )
+                       $item['title'] = File::normalizeTitle( $item['title'] );
+                       if ( $item['title'] ) {
                                $items[$item['title']->getDBkey()] = $item;
+                       }
                }
 
                $images = $this->localRepo->findFiles( $items );
@@ -186,8 +200,10 @@ class RepoGroup {
 
        /**
         * Interface for FileRepo::checkRedirect()
+        * @param $title Title
+        * @return bool
         */
-       function checkRedirect( $title ) {
+       function checkRedirect( Title $title ) {
                if ( !$this->reposInitialised ) {
                        $this->initialiseRepos();
                }
@@ -205,19 +221,52 @@ class RepoGroup {
                return false;
        }
 
+       /**
+        * Find an instance of the file with this key, created at the specified time
+        * Returns false if the file does not exist.
+        *
+        * @param $hash String base 36 SHA-1 hash
+        * @param $options array Option array, same as findFile()
+        * @return File object or false if it is not found
+        */
+       function findFileFromKey( $hash, $options = array() ) {
+               if ( !$this->reposInitialised ) {
+                       $this->initialiseRepos();
+               }
+
+               $file = $this->localRepo->findFileFromKey( $hash, $options );
+               if ( !$file ) {
+                       foreach ( $this->foreignRepos as $repo ) {
+                               $file = $repo->findFileFromKey( $hash, $options );
+                               if ( $file ) break;
+                       }
+               }
+               return $file;
+       }
+
+       /**
+        * Find all instances of files with this key
+        *
+        * @param $hash String base 36 SHA-1 hash
+        * @return Array of File objects
+        */
        function findBySha1( $hash ) {
                if ( !$this->reposInitialised ) {
                        $this->initialiseRepos();
                }
 
                $result = $this->localRepo->findBySha1( $hash );
-               foreach ( $this->foreignRepos as $repo )
+               foreach ( $this->foreignRepos as $repo ) {
                        $result = array_merge( $result, $repo->findBySha1( $hash ) );
+               }
+               usort( $result, 'File::compare' );
                return $result;
        }
 
        /**
         * Get the repo instance with a given key.
+        * @param $index string|int
+        * @return bool|LocalRepo
         */
        function getRepo( $index ) {
                if ( !$this->reposInitialised ) {
@@ -231,16 +280,20 @@ class RepoGroup {
                        return false;
                }
        }
+
        /**
         * Get the repo instance by its name
+        * @param $name string
+        * @return bool
         */
        function getRepoByName( $name ) {
                if ( !$this->reposInitialised ) {
                        $this->initialiseRepos();
                }
                foreach ( $this->foreignRepos as $repo ) {
-                       if ( $repo->name == $name)
+                       if ( $repo->name == $name ) {
                                return $repo;
+                       }
                }
                return false;
        }
@@ -248,6 +301,8 @@ class RepoGroup {
        /**
         * Get the local repository, i.e. the one corresponding to the local image
         * table. Files are typically uploaded to the local repository.
+        *
+        * @return LocalRepo
         */
        function getLocalRepo() {
                return $this->getRepo( 'local' );
@@ -259,6 +314,7 @@ class RepoGroup {
         *
         * @param $callback Callback: the function to call
         * @param $params Array: optional additional parameters to pass to the function
+        * @return bool
         */
        function forEachForeignRepo( $callback, $params = array() ) {
                foreach( $this->foreignRepos as $repo ) {
@@ -304,11 +360,13 @@ class RepoGroup {
 
        /**
         * Split a virtual URL into repo, zone and rel parts
-        * @return an array containing repo, zone and rel
+        * @param $url string
+        * @throws MWException
+        * @return array containing repo, zone and rel
         */
        function splitVirtualUrl( $url ) {
                if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
-                       throw new MWException( __METHOD__.': unknown protoocl' );
+                       throw new MWException( __METHOD__.': unknown protocol' );
                }
 
                $bits = explode( '/', substr( $url, 9 ), 3 );
@@ -318,6 +376,10 @@ class RepoGroup {
                return $bits;
        }
 
+       /**
+        * @param $fileName string
+        * @return array
+        */
        function getFileProps( $fileName ) {
                if ( FileRepo::isVirtualUrl( $fileName ) ) {
                        list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
@@ -327,14 +389,25 @@ class RepoGroup {
                        $repo = $this->getRepo( $repoName );
                        return $repo->getFileProps( $fileName );
                } else {
-                       return File::getPropsFromPath( $fileName );
+                       return FSFile::getPropsFromPath( $fileName );
+               }
+       }
+
+       /**
+        * Move a cache entry to the top (such as when accessed)
+        */
+       protected function pingCache( $key ) {
+               if ( isset( $this->cache[$key] ) ) {
+                       $tmp = $this->cache[$key];
+                       unset( $this->cache[$key] );
+                       $this->cache[$key] = $tmp;
                }
        }
 
        /**
         * Limit cache memory
         */
-       function trimCache() {
+       protected function trimCache() {
                while ( count( $this->cache ) >= self::MAX_CACHE_SIZE ) {
                        reset( $this->cache );
                        $key = key( $this->cache );
@@ -342,4 +415,19 @@ class RepoGroup {
                        unset( $this->cache[$key] );
                }
        }
+
+       /**
+        * Clear RepoGroup process cache used for finding a file
+        * @param $title Title|null Title of the file or null to clear all files
+        */
+       public function clearCache( Title $title = null ) {
+               if ( $title == null ) {
+                       $this->cache = array();
+               } else {
+                       $dbKey = $title->getDBkey();
+                       if ( isset( $this->cache[$dbKey] ) ) {
+                               unset( $this->cache[$dbKey] );
+                       }
+               }
+       }
 }