* (bug 20131) PHP Notice: Undfined index: page_latest in includes/ChangesList.php...
[lhc/web/wiklou.git] / includes / filerepo / FileRepo.php
index b6318d6..79fd87e 100644 (file)
@@ -6,6 +6,7 @@
  * @ingroup FileRepo
  */
 abstract class FileRepo {
+       const FILES_ONLY = 1;
        const DELETE_SOURCE = 1;
        const FIND_PRIVATE = 1;
        const FIND_IGNORE_REDIRECT = 2;
@@ -15,6 +16,7 @@ abstract class FileRepo {
        var $thumbScriptUrl, $transformVia404;
        var $descBaseUrl, $scriptDirUrl, $articleUrl, $fetchDescription, $initialCapital;
        var $pathDisclosureProtection = 'paranoid';
+       var $descriptionCacheExpiry, $apiThumbCacheExpiry, $hashLevels;
 
        /**
         * Factory functions for creating new files
@@ -31,7 +33,7 @@ abstract class FileRepo {
                $this->initialCapital = true; // by default
                foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
                        'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection', 
-                       'useLocalCache', 'localCacheExpiry' ) as $var )
+                       'descriptionCacheExpiry', 'apiThumbCacheExpiry', 'hashLevels' ) as $var )
                {
                        if ( isset( $info[$var] ) ) {
                                $this->$var = $info[$var];
@@ -58,7 +60,7 @@ abstract class FileRepo {
         */
        function newFile( $title, $time = false ) {
                if ( !($title instanceof Title) ) {
-                       $title = Title::makeTitleSafe( NS_IMAGE, $title );
+                       $title = Title::makeTitleSafe( NS_FILE, $title );
                        if ( !is_object( $title ) ) {
                                return null;
                        }
@@ -84,7 +86,7 @@ abstract class FileRepo {
         */
        function findFile( $title, $time = false, $flags = 0 ) {
                if ( !($title instanceof Title) ) {
-                       $title = Title::makeTitleSafe( NS_IMAGE, $title );
+                       $title = Title::makeTitleSafe( NS_FILE, $title );
                        if ( !is_object( $title ) ) {
                                return false;
                        }
@@ -100,7 +102,7 @@ abstract class FileRepo {
                # Now try an old version of the file
                if ( $time !== false ) {
                        $img = $this->newFile( $title, $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) ) {
@@ -114,7 +116,7 @@ abstract class FileRepo {
                        return false;
                }
                $redir = $this->checkRedirect( $title );                
-               if( $redir && $redir->getNamespace() == NS_IMAGE) {
+               if( $redir && $redir->getNamespace() == NS_FILE) {
                        $img = $this->newFile( $redir );
                        if( !$img ) {
                                return false;
@@ -130,12 +132,12 @@ abstract class FileRepo {
        /*
         * Find many files at once. 
         * @param array $titles, an array of titles
-        * @param int $flags
+        * @todo Think of a good way to optionally pass timestamps to this function.
         */
-       function findFiles( $titles, $flags ) {
+       function findFiles( $titles ) {
                $result = array();
                foreach ( $titles as $index => $title ) {
-                       $file = $this->findFile( $title, $flags );
+                       $file = $this->findFile( $title );
                        if ( $file )
                                $result[$file->getTitle()->getDBkey()] = $file;
                }
@@ -237,31 +239,20 @@ abstract class FileRepo {
                        return $path;
                }
        }
-
+       
        /**
-        * Get the name of this repository, as specified by $info['name]' to the constructor
+        * Get a relative path including trailing slash, e.g. f/fa/
+        * If the repo is not hashed, returns an empty string
         */
-       function getName() {
-               return $this->name;
+       function getHashPath( $name ) {
+               return self::getHashPathForLevel( $name, $this->hashLevels );
        }
 
        /**
-        * Get the file description page base URL, or false if there isn't one.
-        * @private
+        * Get the name of this repository, as specified by $info['name]' to the constructor
         */
-       function getDescBaseUrl() {
-               if ( is_null( $this->descBaseUrl ) ) {
-                       if ( !is_null( $this->articleUrl ) ) {
-                               $this->descBaseUrl = str_replace( '$1',
-                                       wfUrlencode( MWNamespace::getCanonicalName( NS_IMAGE ) ) . ':', $this->articleUrl );
-                       } elseif ( !is_null( $this->scriptDirUrl ) ) {
-                               $this->descBaseUrl = $this->scriptDirUrl . '/index.php?title=' .
-                                       wfUrlencode( MWNamespace::getCanonicalName( NS_IMAGE ) ) . ':';
-                       } else {
-                               $this->descBaseUrl = false;
-                       }
-               }
-               return $this->descBaseUrl;
+       function getName() {
+               return $this->name;
        }
 
        /**
@@ -274,12 +265,29 @@ abstract class FileRepo {
         * constructor, whereas local repositories use the local Title functions.
         */
        function getDescriptionUrl( $name ) {
-               $base = $this->getDescBaseUrl();
-               if ( $base ) {
-                       return $base . wfUrlencode( $name );
-               } else {
-                       return false;
+               $encName = wfUrlencode( $name );
+               if ( !is_null( $this->descBaseUrl ) ) {
+                       # "http://example.com/wiki/Image:"
+                       return $this->descBaseUrl . $encName;
+               }
+               if ( !is_null( $this->articleUrl ) ) {
+                       # "http://example.com/wiki/$1"
+                       #
+                       # We use "Image:" as the canonical namespace for
+                       # compatibility across all MediaWiki versions.
+                       return str_replace( '$1',
+                               "Image:$encName", $this->articleUrl );
                }
+               if ( !is_null( $this->scriptDirUrl ) ) {
+                       # "http://example.com/w"
+                       #
+                       # 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 false;
        }
 
        /**
@@ -287,16 +295,22 @@ 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.
         */
-       function getDescriptionRenderUrl( $name ) {
+       function getDescriptionRenderUrl( $name, $lang = null ) {
+               $query = 'action=render';
+               if ( !is_null( $lang ) ) {
+                       $query .= '&uselang=' . $lang;
+               }
                if ( isset( $this->scriptDirUrl ) ) {
                        return $this->scriptDirUrl . '/index.php?title=' .
-                               wfUrlencode( MWNamespace::getCanonicalName( NS_IMAGE ) . ':' . $name ) .
-                               '&action=render';
+                               wfUrlencode( 'Image:' . $name ) .
+                               "&$query";
                } else {
-                       $descBase = $this->getDescBaseUrl();
-                       if ( $descBase ) {
-                               return wfAppendQuery( $descBase . wfUrlencode( $name ), 'action=render' );
+                       $descUrl = $this->getDescriptionUrl( $name );
+                       if ( $descUrl ) {
+                               return wfAppendQuery( $descUrl, $query );
                        } else {
                                return false;
                        }
@@ -387,6 +401,21 @@ abstract class FileRepo {
         */
        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 array $files URLs (or paths) of files to check
+        * @param integer $flags 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.
         *
@@ -504,7 +533,8 @@ abstract class FileRepo {
        function cleanupDeletedBatch( $storageKeys ) {}
 
        /**
-        * Checks if there is a redirect named as $title
+        * Checks if there is a redirect named as $title. If there is, return the
+        * title object. If not, return false.
         * STUB
         *
         * @param Title $title Title of image
@@ -515,14 +545,58 @@ abstract class FileRepo {
 
        /**
         * Invalidates image redirect cache related to that image
+        * Doesn't do anything for repositories that don't support image redirects.
+        * 
         * STUB
-        *
         * @param Title $title Title of image
-        */
-       function invalidateImageRedirect( $title ) {
-       }
+        */     
+       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. 
+        * @return string
+        */
+       public function getDisplayName() {
+               // We don't name our own repo, return nothing
+               if ( $this->name == 'local' ) {
+                       return null;
+               }
+               $repoName = wfMsg( 'shared-repo-name-' . $this->name );
+               if ( !wfEmptyMsg( 'shared-repo-name-' . $this->name, $repoName ) ) {
+                       return $repoName;
+               }
+               return wfMsg( 'shared-repo' ); 
+       }
+       
+       /**
+        * 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 );
+       }
 }