2007-05-30 21:02:32 +00:00
|
|
|
<?php
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
/**
|
|
|
|
|
* @defgroup FileRepo FileRepo
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup FileRepo
|
|
|
|
|
*/
|
2007-05-30 21:02:32 +00:00
|
|
|
|
2007-05-31 01:43:41 +00:00
|
|
|
/**
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup FileRepo
|
2007-05-31 01:43:41 +00:00
|
|
|
* Prioritized list of file repositories
|
|
|
|
|
*/
|
2007-05-30 21:02:32 +00:00
|
|
|
class RepoGroup {
|
|
|
|
|
var $localRepo, $foreignRepos, $reposInitialised = false;
|
|
|
|
|
var $localInfo, $foreignInfo;
|
2009-08-15 09:59:59 +00:00
|
|
|
var $cache;
|
2007-05-30 21:02:32 +00:00
|
|
|
|
|
|
|
|
protected static $instance;
|
2009-08-15 09:59:59 +00:00
|
|
|
const MAX_CACHE_SIZE = 1000;
|
2007-05-30 21:02:32 +00:00
|
|
|
|
2007-05-31 01:43:41 +00:00
|
|
|
/**
|
|
|
|
|
* Get a RepoGroup instance. At present only one instance of RepoGroup is
|
|
|
|
|
* needed in a MediaWiki invocation, this may change in the future.
|
|
|
|
|
*/
|
2007-06-03 09:05:29 +00:00
|
|
|
static function singleton() {
|
2007-05-30 21:02:32 +00:00
|
|
|
if ( self::$instance ) {
|
|
|
|
|
return self::$instance;
|
|
|
|
|
}
|
|
|
|
|
global $wgLocalFileRepo, $wgForeignFileRepos;
|
|
|
|
|
self::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
|
|
|
|
|
return self::$instance;
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-03 09:05:29 +00:00
|
|
|
/**
|
|
|
|
|
* Destroy the singleton instance, so that a new one will be created next
|
|
|
|
|
* time singleton() is called.
|
|
|
|
|
*/
|
|
|
|
|
static function destroySingleton() {
|
|
|
|
|
self::$instance = null;
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-09 00:21:16 +00:00
|
|
|
/**
|
|
|
|
|
* Set the singleton instance to a given object
|
|
|
|
|
*/
|
|
|
|
|
static function setSingleton( $instance ) {
|
|
|
|
|
self::$instance = $instance;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-30 21:02:32 +00:00
|
|
|
/**
|
2008-04-14 07:45:50 +00:00
|
|
|
* Construct a group of file repositories.
|
|
|
|
|
* @param array $data Array of repository info arrays.
|
|
|
|
|
* Each info array is an associative array with the 'class' member
|
2007-05-30 21:02:32 +00:00
|
|
|
* giving the class name. The entire array is passed to the repository
|
|
|
|
|
* constructor as the first parameter.
|
|
|
|
|
*/
|
|
|
|
|
function __construct( $localInfo, $foreignInfo ) {
|
|
|
|
|
$this->localInfo = $localInfo;
|
|
|
|
|
$this->foreignInfo = $foreignInfo;
|
2009-08-15 09:59:59 +00:00
|
|
|
$this->cache = array();
|
2007-05-30 21:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Search repositories for an image.
|
|
|
|
|
* You can also use wfGetFile() to do this.
|
|
|
|
|
* @param mixed $title Title object or string
|
2009-08-15 09:59:59 +00:00
|
|
|
* @param $options 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.
|
|
|
|
|
*
|
|
|
|
|
* ignoreRedirect: If true, do not follow file redirects
|
|
|
|
|
*
|
|
|
|
|
* private: If true, return restricted (deleted) files if the current
|
|
|
|
|
* user is allowed to view them. Otherwise, such files will not
|
|
|
|
|
* be found.
|
|
|
|
|
*
|
|
|
|
|
* bypassCache: If true, do not use the process-local cache of File objects
|
2007-05-30 21:02:32 +00:00
|
|
|
* @return File object or false if it is not found
|
|
|
|
|
*/
|
2009-08-15 09:59:59 +00:00
|
|
|
function findFile( $title, $options = array() ) {
|
|
|
|
|
if ( !is_array( $options ) ) {
|
|
|
|
|
// MW 1.15 compat
|
|
|
|
|
$options = array( 'time' => $options );
|
|
|
|
|
}
|
2007-05-30 21:02:32 +00:00
|
|
|
if ( !$this->reposInitialised ) {
|
|
|
|
|
$this->initialiseRepos();
|
|
|
|
|
}
|
2009-08-15 09:59:59 +00:00
|
|
|
if ( !($title instanceof Title) ) {
|
|
|
|
|
$title = Title::makeTitleSafe( NS_FILE, $title );
|
|
|
|
|
if ( !is_object( $title ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Check the cache
|
|
|
|
|
if ( empty( $options['ignoreRedirect'] )
|
|
|
|
|
&& empty( $options['private'] )
|
|
|
|
|
&& empty( $options['bypassCache'] ) )
|
|
|
|
|
{
|
|
|
|
|
$useCache = true;
|
|
|
|
|
$time = isset( $options['time'] ) ? $options['time'] : '';
|
|
|
|
|
$dbkey = $title->getDBkey();
|
|
|
|
|
if ( isset( $this->cache[$dbkey][$time] ) ) {
|
|
|
|
|
wfDebug( __METHOD__.": got File:$dbkey from process cache\n" );
|
|
|
|
|
# Move it to the end of the list so that we can delete the LRU entry later
|
|
|
|
|
$tmp = $this->cache[$dbkey];
|
|
|
|
|
unset( $this->cache[$dbkey] );
|
|
|
|
|
$this->cache[$dbkey] = $tmp;
|
|
|
|
|
# Return the entry
|
|
|
|
|
return $this->cache[$dbkey][$time];
|
|
|
|
|
} else {
|
|
|
|
|
# Add a negative cache entry, may be overridden
|
|
|
|
|
$this->trimCache();
|
|
|
|
|
$this->cache[$dbkey][$time] = false;
|
|
|
|
|
$cacheEntry =& $this->cache[$dbkey][$time];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$useCache = false;
|
|
|
|
|
}
|
2007-05-30 21:02:32 +00:00
|
|
|
|
2009-08-15 09:59:59 +00:00
|
|
|
# Check the local repo
|
|
|
|
|
$image = $this->localRepo->findFile( $title, $options );
|
2007-05-30 21:02:32 +00:00
|
|
|
if ( $image ) {
|
2009-08-15 09:59:59 +00:00
|
|
|
if ( $useCache ) {
|
|
|
|
|
$cacheEntry = $image;
|
|
|
|
|
}
|
2007-05-30 21:02:32 +00:00
|
|
|
return $image;
|
|
|
|
|
}
|
2009-08-15 09:59:59 +00:00
|
|
|
|
|
|
|
|
# Check the foreign repos
|
2007-05-30 21:02:32 +00:00
|
|
|
foreach ( $this->foreignRepos as $repo ) {
|
2009-08-15 09:59:59 +00:00
|
|
|
$image = $repo->findFile( $title, $options );
|
2007-05-30 21:02:32 +00:00
|
|
|
if ( $image ) {
|
2009-08-15 09:59:59 +00:00
|
|
|
if ( $useCache ) {
|
|
|
|
|
$cacheEntry = $image;
|
|
|
|
|
}
|
2007-05-30 21:02:32 +00:00
|
|
|
return $image;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-08-15 09:59:59 +00:00
|
|
|
# Not found, do not override negative cache
|
2007-05-30 21:02:32 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2009-08-15 09:59:59 +00:00
|
|
|
|
|
|
|
|
function findFiles( $inputItems ) {
|
2008-05-20 17:05:57 +00:00
|
|
|
if ( !$this->reposInitialised ) {
|
|
|
|
|
$this->initialiseRepos();
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-15 09:59:59 +00:00
|
|
|
$items = array();
|
|
|
|
|
foreach ( $inputItems as $item ) {
|
|
|
|
|
if ( !is_array( $item ) ) {
|
|
|
|
|
$item = array( 'title' => $item );
|
|
|
|
|
}
|
|
|
|
|
if ( !( $item['title'] instanceof Title ) )
|
|
|
|
|
$item['title'] = Title::makeTitleSafe( NS_FILE, $item['title'] );
|
|
|
|
|
if ( $item['title'] )
|
|
|
|
|
$items[$item['title']->getDBkey()] = $item;
|
2008-05-30 13:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
2009-08-15 09:59:59 +00:00
|
|
|
$images = $this->localRepo->findFiles( $items );
|
2008-05-20 17:05:57 +00:00
|
|
|
|
|
|
|
|
foreach ( $this->foreignRepos as $repo ) {
|
2009-08-15 09:59:59 +00:00
|
|
|
// Remove found files from $items
|
|
|
|
|
foreach ( $images as $name => $image ) {
|
|
|
|
|
unset( $items[$name] );
|
|
|
|
|
}
|
2008-05-30 13:16:08 +00:00
|
|
|
|
2009-08-15 09:59:59 +00:00
|
|
|
$images = array_merge( $images, $repo->findFiles( $items ) );
|
2008-05-20 17:05:57 +00:00
|
|
|
}
|
|
|
|
|
return $images;
|
|
|
|
|
}
|
2007-05-30 21:02:32 +00:00
|
|
|
|
2008-01-16 18:27:43 +00:00
|
|
|
/**
|
|
|
|
|
* Interface for FileRepo::checkRedirect()
|
|
|
|
|
*/
|
|
|
|
|
function checkRedirect( $title ) {
|
|
|
|
|
if ( !$this->reposInitialised ) {
|
|
|
|
|
$this->initialiseRepos();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$redir = $this->localRepo->checkRedirect( $title );
|
|
|
|
|
if( $redir ) {
|
|
|
|
|
return $redir;
|
|
|
|
|
}
|
|
|
|
|
foreach ( $this->foreignRepos as $repo ) {
|
|
|
|
|
$redir = $repo->checkRedirect( $title );
|
|
|
|
|
if ( $redir ) {
|
|
|
|
|
return $redir;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2008-05-14 15:11:48 +00:00
|
|
|
|
|
|
|
|
function findBySha1( $hash ) {
|
|
|
|
|
if ( !$this->reposInitialised ) {
|
|
|
|
|
$this->initialiseRepos();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result = $this->localRepo->findBySha1( $hash );
|
|
|
|
|
foreach ( $this->foreignRepos as $repo )
|
|
|
|
|
$result = array_merge( $result, $repo->findBySha1( $hash ) );
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
2008-01-16 18:27:43 +00:00
|
|
|
|
2007-05-30 21:02:32 +00:00
|
|
|
/**
|
|
|
|
|
* Get the repo instance with a given key.
|
|
|
|
|
*/
|
|
|
|
|
function getRepo( $index ) {
|
|
|
|
|
if ( !$this->reposInitialised ) {
|
|
|
|
|
$this->initialiseRepos();
|
|
|
|
|
}
|
2007-11-19 09:43:47 +00:00
|
|
|
if ( $index === 'local' ) {
|
2007-05-30 21:02:32 +00:00
|
|
|
return $this->localRepo;
|
|
|
|
|
} elseif ( isset( $this->foreignRepos[$index] ) ) {
|
|
|
|
|
return $this->foreignRepos[$index];
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-02-09 13:40:16 +00:00
|
|
|
/**
|
|
|
|
|
* Get the repo instance by its name
|
|
|
|
|
*/
|
|
|
|
|
function getRepoByName( $name ) {
|
|
|
|
|
if ( !$this->reposInitialised ) {
|
|
|
|
|
$this->initialiseRepos();
|
|
|
|
|
}
|
|
|
|
|
foreach ( $this->foreignRepos as $key => $repo ) {
|
|
|
|
|
if ( $repo->name == $name)
|
|
|
|
|
return $repo;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2007-05-30 21:02:32 +00:00
|
|
|
|
2007-05-31 01:43:41 +00:00
|
|
|
/**
|
|
|
|
|
* Get the local repository, i.e. the one corresponding to the local image
|
|
|
|
|
* table. Files are typically uploaded to the local repository.
|
|
|
|
|
*/
|
2007-05-30 21:02:32 +00:00
|
|
|
function getLocalRepo() {
|
|
|
|
|
return $this->getRepo( 'local' );
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-03 13:50:37 +00:00
|
|
|
/**
|
2008-09-16 06:13:31 +00:00
|
|
|
* Call a function for each foreign repo, with the repo object as the
|
|
|
|
|
* first parameter.
|
|
|
|
|
*
|
|
|
|
|
* @param $callback callback The function to call
|
|
|
|
|
* @param $params array Optional additional parameters to pass to the function
|
2008-09-03 13:50:37 +00:00
|
|
|
*/
|
2008-05-01 22:16:23 +00:00
|
|
|
function forEachForeignRepo( $callback, $params = array() ) {
|
|
|
|
|
foreach( $this->foreignRepos as $repo ) {
|
|
|
|
|
$args = array_merge( array( $repo ), $params );
|
|
|
|
|
if( call_user_func_array( $callback, $args ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-03 13:50:37 +00:00
|
|
|
/**
|
2008-09-16 06:13:31 +00:00
|
|
|
* Does the installation have any foreign repos set up?
|
2008-09-03 13:50:37 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2008-05-01 22:16:23 +00:00
|
|
|
function hasForeignRepos() {
|
2008-09-16 06:13:31 +00:00
|
|
|
return (bool)$this->foreignRepos;
|
2008-05-01 22:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
2007-05-30 21:02:32 +00:00
|
|
|
/**
|
|
|
|
|
* Initialise the $repos array
|
|
|
|
|
*/
|
|
|
|
|
function initialiseRepos() {
|
|
|
|
|
if ( $this->reposInitialised ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$this->reposInitialised = true;
|
|
|
|
|
|
|
|
|
|
$this->localRepo = $this->newRepo( $this->localInfo );
|
|
|
|
|
$this->foreignRepos = array();
|
|
|
|
|
foreach ( $this->foreignInfo as $key => $info ) {
|
|
|
|
|
$this->foreignRepos[$key] = $this->newRepo( $info );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-31 01:43:41 +00:00
|
|
|
/**
|
|
|
|
|
* Create a repo class based on an info structure
|
|
|
|
|
*/
|
|
|
|
|
protected function newRepo( $info ) {
|
2007-05-30 21:02:32 +00:00
|
|
|
$class = $info['class'];
|
|
|
|
|
return new $class( $info );
|
|
|
|
|
}
|
2007-06-16 02:55:25 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Split a virtual URL into repo, zone and rel parts
|
|
|
|
|
* @return an array containing repo, zone and rel
|
|
|
|
|
*/
|
|
|
|
|
function splitVirtualUrl( $url ) {
|
|
|
|
|
if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
|
|
|
|
|
throw new MWException( __METHOD__.': unknown protoocl' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bits = explode( '/', substr( $url, 9 ), 3 );
|
|
|
|
|
if ( count( $bits ) != 3 ) {
|
|
|
|
|
throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
|
|
|
|
|
}
|
|
|
|
|
return $bits;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getFileProps( $fileName ) {
|
|
|
|
|
if ( FileRepo::isVirtualUrl( $fileName ) ) {
|
2007-08-21 03:57:54 +00:00
|
|
|
list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
|
2007-06-16 02:55:25 +00:00
|
|
|
if ( $repoName === '' ) {
|
|
|
|
|
$repoName = 'local';
|
|
|
|
|
}
|
|
|
|
|
$repo = $this->getRepo( $repoName );
|
|
|
|
|
return $repo->getFileProps( $fileName );
|
|
|
|
|
} else {
|
|
|
|
|
return File::getPropsFromPath( $fileName );
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-08-15 09:59:59 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Limit cache memory
|
|
|
|
|
*/
|
|
|
|
|
function trimCache() {
|
|
|
|
|
while ( count( $this->cache ) >= self::MAX_CACHE_SIZE ) {
|
|
|
|
|
reset( $this->cache );
|
|
|
|
|
$key = key( $this->cache );
|
|
|
|
|
wfDebug( __METHOD__.": evicting $key\n" );
|
|
|
|
|
unset( $this->cache[$key] );
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-05-30 21:02:32 +00:00
|
|
|
}
|