Merge "Expand the protocol for proto-relative links when printing"
[lhc/web/wiklou.git] / includes / filerepo / RepoGroup.php
index c6e93b6..103e78e 100644 (file)
@@ -42,7 +42,7 @@ class RepoGroup {
        /** @var array */
        protected $foreignInfo;
 
-       /** @var array  */
+       /** @var ProcessCacheLRU */
        protected $cache;
 
        /** @var RepoGroup */
@@ -90,7 +90,7 @@ class RepoGroup {
         * Construct a group of file repositories.
         *
         * @param array $localInfo Associative array for local repo's info
-        * @param array $foreignInfo of repository info arrays.
+        * @param array $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
         *   constructor as the first parameter.
@@ -98,14 +98,14 @@ class RepoGroup {
        function __construct( $localInfo, $foreignInfo ) {
                $this->localInfo = $localInfo;
                $this->foreignInfo = $foreignInfo;
-               $this->cache = array();
+               $this->cache = new ProcessCacheLRU( self::MAX_CACHE_SIZE );
        }
 
        /**
         * Search repositories for an image.
         * You can also use wfFindFile() to do this.
         *
-        * @param $title Title|string Title object or string
+        * @param Title|string $title Title object or string
         * @param array $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
@@ -137,13 +137,8 @@ class RepoGroup {
                ) {
                        $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
-                               $this->pingCache( $dbkey );
-
-                               # Return the entry
-                               return $this->cache[$dbkey][$time];
+                       if ( $this->cache->has( $dbkey, $time, 60 ) ) {
+                               return $this->cache->get( $dbkey, $time );
                        }
                        $useCache = true;
                } else {
@@ -166,8 +161,7 @@ class RepoGroup {
                $image = $image ? $image : false; // type sanity
                # Cache file existence or non-existence
                if ( $useCache && ( !$image || $image->isCacheable() ) ) {
-                       $this->trimCache();
-                       $this->cache[$dbkey][$time] = $image;
+                       $this->cache->set( $dbkey, $time, $image );
                }
 
                return $image;
@@ -191,7 +185,7 @@ class RepoGroup {
         * @return array Map of (file name => File objects) for matches
         *
         * @param array $inputItems
-        * @param integer $flags
+        * @param int $flags
         * @return array
         */
        function findFiles( array $inputItems, $flags = 0 ) {
@@ -226,7 +220,7 @@ class RepoGroup {
 
        /**
         * Interface for FileRepo::checkRedirect()
-        * @param $title Title
+        * @param Title $title
         * @return bool
         */
        function checkRedirect( Title $title ) {
@@ -253,9 +247,9 @@ class RepoGroup {
         * Find an instance of the file with this key, created at the specified time
         * Returns false if the file does not exist.
         *
-        * @param string $hash base 36 SHA-1 hash
+        * @param string $hash Base 36 SHA-1 hash
         * @param array $options Option array, same as findFile()
-        * @return File object or false if it is not found
+        * @return File|bool File object or false if it is not found
         */
        function findFileFromKey( $hash, $options = array() ) {
                if ( !$this->reposInitialised ) {
@@ -279,7 +273,7 @@ class RepoGroup {
         * Find all instances of files with this key
         *
         * @param string $hash base 36 SHA-1 hash
-        * @return Array of File objects
+        * @return array Array of File objects
         */
        function findBySha1( $hash ) {
                if ( !$this->reposInitialised ) {
@@ -299,7 +293,7 @@ class RepoGroup {
         * Find all instances of files with this keys
         *
         * @param array $hashes base 36 SHA-1 hashes
-        * @return array of array of File objects
+        * @return array Array of array of File objects
         */
        function findBySha1s( array $hashes ) {
                if ( !$this->reposInitialised ) {
@@ -453,41 +447,15 @@ class RepoGroup {
                }
        }
 
-       /**
-        * 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
-        */
-       protected function trimCache() {
-               while ( count( $this->cache ) >= self::MAX_CACHE_SIZE ) {
-                       reset( $this->cache );
-                       $key = key( $this->cache );
-                       wfDebug( __METHOD__ . ": evicting $key\n" );
-                       unset( $this->cache[$key] );
-               }
-       }
-
        /**
         * Clear RepoGroup process cache used for finding a file
         * @param Title|null $title Title of the file or null to clear all files
         */
        public function clearCache( Title $title = null ) {
                if ( $title == null ) {
-                       $this->cache = array();
+                       $this->cache->clear();
                } else {
-                       $dbKey = $title->getDBkey();
-                       if ( isset( $this->cache[$dbKey] ) ) {
-                               unset( $this->cache[$dbKey] );
-                       }
+                       $this->cache->clear( $title->getDBkey() );
                }
        }
 }