Added some lock call profiling
[lhc/web/wiklou.git] / includes / filerepo / RepoGroup.php
index 2d0b1ac..8096336 100644 (file)
@@ -6,10 +6,6 @@
  * @ingroup FileRepo
  */
 
-/**
- * @defgroup FileRepo FileRepo
- */
-
 /**
  * Prioritized list of file repositories
  *
@@ -26,6 +22,9 @@ class RepoGroup {
        var $localInfo, $foreignInfo;
        var $cache;
 
+       /**
+        * @var RepoGroup
+        */
        protected static $instance;
        const MAX_CACHE_SIZE = 1000;
 
@@ -53,6 +52,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;
@@ -61,7 +65,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
@@ -77,8 +81,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.
@@ -100,22 +104,15 @@ 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'] : '';
@@ -171,10 +168,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 );
@@ -192,8 +189,9 @@ class RepoGroup {
 
        /**
         * Interface for FileRepo::checkRedirect()
+        * @return bool
         */
-       function checkRedirect( $title ) {
+       function checkRedirect( Title $title ) {
                if ( !$this->reposInitialised ) {
                        $this->initialiseRepos();
                }
@@ -216,7 +214,7 @@ class RepoGroup {
         * Returns false if the file does not exist.
         *
         * @param $hash String base 36 SHA-1 hash
-        * @param $options Option array, same as findFile()
+        * @param $options array Option array, same as findFile()
         * @return File object or false if it is not found
         */
        function findFileFromKey( $hash, $options = array() ) {
@@ -254,6 +252,7 @@ class RepoGroup {
 
        /**
         * Get the repo instance with a given key.
+        * @return bool|LocalRepo
         */
        function getRepo( $index ) {
                if ( !$this->reposInitialised ) {
@@ -269,6 +268,7 @@ class RepoGroup {
        }
        /**
         * Get the repo instance by its name
+        * @return bool
         */
        function getRepoByName( $name ) {
                if ( !$this->reposInitialised ) {
@@ -284,6 +284,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' );
@@ -295,6 +297,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 ) {
@@ -340,7 +343,8 @@ class RepoGroup {
 
        /**
         * Split a virtual URL into repo, zone and rel parts
-        * @return an array containing repo, zone and rel
+        * @param $url string
+        * @return array containing repo, zone and rel
         */
        function splitVirtualUrl( $url ) {
                if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
@@ -363,14 +367,14 @@ class RepoGroup {
                        $repo = $this->getRepo( $repoName );
                        return $repo->getFileProps( $fileName );
                } else {
-                       return File::getPropsFromPath( $fileName );
+                       return FSFile::getPropsFromPath( $fileName );
                }
        }
 
        /**
         * Limit cache memory
         */
-       function trimCache() {
+       protected function trimCache() {
                while ( count( $this->cache ) >= self::MAX_CACHE_SIZE ) {
                        reset( $this->cache );
                        $key = key( $this->cache );
@@ -378,4 +382,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] );
+                       }
+               }
+       }
 }