*/
class FileCache {
var $repoGroup;
- var $cache = array(), $notFound = array();
+ var $cache = array(), $oldCache = array(), $notFound = array();
protected static $instance;
/**
* Search the cache for a file.
* @param mixed $title Title object or string
+ * @param string or false $time, old version time
* @return File object or false if it is not found
- * @todo Implement searching for old file versions(?)
*/
- function findFile( $title ) {
+ function findFile( $title, $time = false ) {
if( !( $title instanceof Title ) ) {
$title = Title::makeTitleSafe( NS_FILE, $title );
}
}
$dbkey = $title->getDBkey();
+ # Is there a current version cached?
if( array_key_exists( $dbkey, $this->cache ) ) {
- wfDebug( "FileCache HIT for $dbkey\n" );
- return $this->cache[$dbkey];
+ if( !$time || $this->cache[$dbkey]->getTimestamp() === $time ) {
+ wfDebug( "FileCache HIT for $dbkey\n" );
+ return $this->cache[$dbkey];
+ }
}
+ # Is there no current version? Then assume no old versions too.
if( array_key_exists( $dbkey, $this->notFound ) ) {
wfDebug( "FileCache negative HIT for $dbkey\n" );
return false;
}
+ # Is this old version cached?
+ if( $time && array_key_exists( $dbkey, $this->oldCache ) &&
+ array_key_exists( $time, $this->oldCache[$dbkey] ) )
+ {
+ wfDebug( "FileCache HIT for $dbkey on $time\n" );
+ return $this->oldCache[$dbkey][$time];
+ }
// Not in cache, fall back to a direct query
- $file = $this->repoGroup->findFile( $title );
+ $file = $this->repoGroup->findFile( $title, $time );
if( $file ) {
wfDebug( "FileCache MISS for $dbkey\n" );
- $this->cache[$dbkey] = $file;
+ if( !$file->isOld() ) {
+ $this->cache[$dbkey] = $file; // cache the current version
+ }
+ if( !array_key_exists( $dbkey, $this->oldCache ) ) {
+ $this->oldCache[$dbkey] = array();
+ }
+ $this->oldCache[$dbkey][$file->getTimestamp()] = $file; // cache this version
} else {
wfDebug( "FileCache negative MISS for $dbkey\n" );
$this->notFound[$dbkey] = true;