* close connection in class destructor (unlike MySql, Oracle does not commit on close)
[lhc/web/wiklou.git] / includes / filerepo / RepoGroup.php
index 82f7939..2d0b1ac 100644 (file)
  * @ingroup FileRepo
  */
 class RepoGroup {
-       var $localRepo, $foreignRepos, $reposInitialised = false;
+
+       /**
+        * @var LocalRepo
+        */
+       var $localRepo;
+
+       var $foreignRepos, $reposInitialised = false;
        var $localInfo, $foreignInfo;
        var $cache;
 
@@ -26,6 +32,7 @@ class RepoGroup {
        /**
         * Get a RepoGroup instance. At present only one instance of RepoGroup is
         * needed in a MediaWiki invocation, this may change in the future.
+        * @return RepoGroup
         */
        static function singleton() {
                if ( self::$instance ) {
@@ -100,10 +107,15 @@ class RepoGroup {
                        }
                }
 
+               if ( $title->getNamespace() != NS_MEDIA && $title->getNamespace() != NS_FILE ) {
+                       throw new MWException( __METHOD__ . ' received an Title object with incorrect namespace' );
+               }
+
                # Check the cache
                if ( empty( $options['ignoreRedirect'] )
                        && empty( $options['private'] )
-                       && empty( $options['bypassCache'] ) )
+                       && empty( $options['bypassCache'] )
+                       && $title->getNamespace() == NS_FILE )
                {
                        $useCache = true;
                        $time = isset( $options['time'] ) ? $options['time'] : '';
@@ -199,14 +211,44 @@ 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 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 ) );
+               }
                return $result;
        }
 
@@ -302,7 +344,7 @@ class RepoGroup {
         */
        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 );