Tweak timestamp handling to allow no lower limit
[lhc/web/wiklou.git] / includes / filerepo / FileRepo.php
index bf06aef..5beac73 100644 (file)
@@ -3,6 +3,7 @@
 /**
  * Base class for file repositories
  * Do not instantiate, use a derived class.
+ * @ingroup FileRepo
  */
 abstract class FileRepo {
        const DELETE_SOURCE = 1;
@@ -14,6 +15,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
@@ -29,7 +31,8 @@ abstract class FileRepo {
                // Optional settings
                $this->initialCapital = true; // by default
                foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
-                       'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection' ) as $var )
+                       'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection', 
+                       'descriptionCacheExpiry', 'apiThumbCacheExpiry', 'hashLevels' ) as $var )
                {
                        if ( isset( $info[$var] ) ) {
                                $this->$var = $info[$var];
@@ -56,7 +59,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;
                        }
@@ -82,7 +85,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;
                        }
@@ -98,7 +101,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) ) {
@@ -112,7 +115,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;
@@ -126,16 +129,16 @@ abstract class FileRepo {
        }
        
        /*
-        * Find many files at once
+        * Find many files at once. 
+        * @param array $titles, an array of titles
+        * @todo Think of a good way to optionally pass timestamps to this function.
         */
-       function findFiles( &$titles, $time = false, $flags ) {
+       function findFiles( $titles ) {
                $result = array();
                foreach ( $titles as $index => $title ) {
                        $file = $this->findFile( $title );
-                       if ( $file ) {
+                       if ( $file )
                                $result[$file->getTitle()->getDBkey()] = $file;
-                               unset( $titles[$index] );
-                       }
                }
                return $result;
        }
@@ -235,6 +238,14 @@ abstract class FileRepo {
                        return $path;
                }
        }
+       
+       /**
+        * Get a relative path including trailing slash, e.g. f/fa/
+        * If the repo is not hashed, returns an empty string
+        */
+       function getHashPath( $name ) {
+               return self::getHashPathForLevel( $name, $this->hashLevels );
+       }
 
        /**
         * Get the name of this repository, as specified by $info['name]' to the constructor
@@ -243,25 +254,6 @@ abstract class FileRepo {
                return $this->name;
        }
 
-       /**
-        * Get the file description page base URL, or false if there isn't one.
-        * @private
-        */
-       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;
-       }
-
        /**
         * Get the URL of an image description page. May return false if it is
         * unknown or not applicable. In general this should only be called by the
@@ -272,12 +264,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;
        }
 
        /**
@@ -289,12 +298,12 @@ abstract class FileRepo {
        function getDescriptionRenderUrl( $name ) {
                if ( isset( $this->scriptDirUrl ) ) {
                        return $this->scriptDirUrl . '/index.php?title=' .
-                               wfUrlencode( MWNamespace::getCanonicalName( NS_IMAGE ) . ':' . $name ) .
+                               wfUrlencode( 'Image:' . $name ) .
                                '&action=render';
                } else {
-                       $descBase = $this->getDescBaseUrl();
-                       if ( $descBase ) {
-                               return wfAppendQuery( $descBase . wfUrlencode( $name ), 'action=render' );
+                       $descUrl = $this->getDescriptionUrl( $name );
+                       if ( $descUrl ) {
+                               return wfAppendQuery( $descUrl, 'action=render' );
                        } else {
                                return false;
                        }