wiki.techinc.nl/includes/filerepo/RepoGroup.php

468 lines
12 KiB
PHP
Raw Normal View History

<?php
/**
* Prioritized list of file repositories.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup FileRepo
*/
/**
* Prioritized list of file repositories
*
* @ingroup FileRepo
*/
class RepoGroup {
/** @var LocalRepo */
protected $localRepo;
2011-02-18 23:56:08 +00:00
/** @var FileRepo[] */
protected $foreignRepos;
/** @var bool */
protected $reposInitialised = false;
/** @var array */
protected $localInfo;
/** @var array */
protected $foreignInfo;
/** @var ProcessCacheLRU */
protected $cache;
/** @var RepoGroup */
protected static $instance;
/** Maximum number of cache items */
const MAX_CACHE_SIZE = 500;
/**
* 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 ) {
return self::$instance;
}
global $wgLocalFileRepo, $wgForeignFileRepos;
self::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
return self::$instance;
}
/**
* Destroy the singleton instance, so that a new one will be created next
* time singleton() is called.
*/
static function destroySingleton() {
self::$instance = null;
}
/**
* Set the singleton instance to a given object
* Used by extensions which hook into the Repo chain.
* It's not enough to just create a superclass ... you have
* to get people to call into it even though all they know is RepoGroup::singleton()
2011-05-28 18:59:42 +00:00
*
* @param RepoGroup $instance
*/
static function setSingleton( $instance ) {
self::$instance = $instance;
}
/**
* Construct a group of file repositories.
2010-03-26 21:55:13 +00:00
*
* @param array $localInfo Associative array for local repo's info
* @param array $foreignInfo Array of repository info arrays.
* Each info array is an associative array with the 'class' member
* 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;
$this->cache = new ProcessCacheLRU( self::MAX_CACHE_SIZE );
}
/**
* Search repositories for an image.
* You can also use wfFindFile() to do this.
2010-03-26 21:55:13 +00:00
*
* @param Title|string $title Title object or string
* @param array $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
* @return File|bool False if title is not found
*/
function findFile( $title, $options = array() ) {
if ( !is_array( $options ) ) {
// MW 1.15 compat
$options = array( 'time' => $options );
}
if ( !$this->reposInitialised ) {
$this->initialiseRepos();
}
$title = File::normalizeTitle( $title );
if ( !$title ) {
return false;
}
# Check the cache
2010-01-22 03:14:52 +00:00
if ( empty( $options['ignoreRedirect'] )
&& empty( $options['private'] )
&& empty( $options['bypassCache'] )
) {
$time = isset( $options['time'] ) ? $options['time'] : '';
$dbkey = $title->getDBkey();
if ( $this->cache->has( $dbkey, $time, 60 ) ) {
return $this->cache->get( $dbkey, $time );
}
$useCache = true;
} else {
$useCache = false;
}
# Check the local repo
$image = $this->localRepo->findFile( $title, $options );
# Check the foreign repos
if ( !$image ) {
foreach ( $this->foreignRepos as $repo ) {
$image = $repo->findFile( $title, $options );
if ( $image ) {
break;
}
}
}
$image = $image ? $image : false; // type sanity
# Cache file existence or non-existence
if ( $useCache && ( !$image || $image->isCacheable() ) ) {
$this->cache->set( $dbkey, $time, $image );
}
return $image;
}
/**
* Search repositories for many files at once.
*
* @param array $items An array of titles, or an array of findFile() options with
* the "title" option giving the title. Example:
*
* $findItem = array( 'title' => $title, 'private' => true );
* $findBatch = array( $findItem );
* $repo->findFiles( $findBatch );
*
* No title should appear in $items twice, as the result use titles as keys
* @param int $flags Supports:
* - FileRepo::NAME_AND_TIME_ONLY : return a (search title => (title,timestamp)) map.
* The search title uses the input titles; the other is the final post-redirect title.
* All titles are returned as string DB keys and the inner array is associative.
* @return array Map of (file name => File objects) for matches
*
* @param array $inputItems
* @param int $flags
* @return array
*/
function findFiles( array $inputItems, $flags = 0 ) {
if ( !$this->reposInitialised ) {
$this->initialiseRepos();
}
$items = array();
foreach ( $inputItems as $item ) {
if ( !is_array( $item ) ) {
$item = array( 'title' => $item );
}
$item['title'] = File::normalizeTitle( $item['title'] );
if ( $item['title'] ) {
$items[$item['title']->getDBkey()] = $item;
}
}
$images = $this->localRepo->findFiles( $items, $flags );
foreach ( $this->foreignRepos as $repo ) {
// Remove found files from $items
foreach ( $images as $name => $image ) {
unset( $items[$name] );
}
2010-01-22 03:14:52 +00:00
$images = array_merge( $images, $repo->findFiles( $items, $flags ) );
}
return $images;
}
/**
* Interface for FileRepo::checkRedirect()
* @param Title $title
* @return bool|Title
*/
function checkRedirect( Title $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;
}
2010-01-22 03:14:52 +00:00
2011-03-28 21:40:50 +00:00
/**
* Find an instance of the file with this key, created at the specified time
* Returns false if the file does not exist.
*
* @param string $hash Base 36 SHA-1 hash
* @param array $options Option array, same as findFile()
* @return File|bool File object or false if it is not found
2011-03-28 21:40:50 +00:00
*/
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;
}
2011-03-28 21:40:50 +00:00
}
}
2011-03-28 21:40:50 +00:00
return $file;
}
/**
* Find all instances of files with this key
*
* @param string $hash Base 36 SHA-1 hash
* @return File[]
*/
function findBySha1( $hash ) {
if ( !$this->reposInitialised ) {
$this->initialiseRepos();
}
2010-01-22 03:14:52 +00:00
$result = $this->localRepo->findBySha1( $hash );
foreach ( $this->foreignRepos as $repo ) {
$result = array_merge( $result, $repo->findBySha1( $hash ) );
}
usort( $result, 'File::compare' );
2010-01-22 03:14:52 +00:00
return $result;
}
/**
* Find all instances of files with this keys
*
* @param array $hashes Base 36 SHA-1 hashes
* @return array Array of array of File objects
*/
function findBySha1s( array $hashes ) {
if ( !$this->reposInitialised ) {
$this->initialiseRepos();
}
$result = $this->localRepo->findBySha1s( $hashes );
foreach ( $this->foreignRepos as $repo ) {
$result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) );
}
//sort the merged (and presorted) sublist of each hash
foreach ( $result as $hash => $files ) {
usort( $result[$hash], 'File::compare' );
}
return $result;
}
/**
* Get the repo instance with a given key.
* @param string|int $index
2012-02-09 21:33:27 +00:00
* @return bool|LocalRepo
*/
function getRepo( $index ) {
if ( !$this->reposInitialised ) {
$this->initialiseRepos();
}
if ( $index === 'local' ) {
return $this->localRepo;
} elseif ( isset( $this->foreignRepos[$index] ) ) {
return $this->foreignRepos[$index];
} else {
return false;
}
}
/**
* Get the repo instance by its name
* @param string $name
2012-02-09 21:33:27 +00:00
* @return bool
*/
function getRepoByName( $name ) {
if ( !$this->reposInitialised ) {
$this->initialiseRepos();
}
2010-10-14 20:53:04 +00:00
foreach ( $this->foreignRepos as $repo ) {
if ( $repo->name == $name ) {
return $repo;
}
}
return false;
}
/**
* Get the local repository, i.e. the one corresponding to the local image
* table. Files are typically uploaded to the local repository.
*
* @return LocalRepo
*/
function getLocalRepo() {
return $this->getRepo( 'local' );
}
2008-09-03 13:50:37 +00:00
/**
2010-01-22 03:14:52 +00:00
* Call a function for each foreign repo, with the repo object as the
* first parameter.
*
* @param callable $callback The function to call
* @param array $params Optional additional parameters to pass to the function
2012-02-09 21:33:27 +00:00
* @return bool
2008-09-03 13:50:37 +00:00
*/
function forEachForeignRepo( $callback, $params = array() ) {
if ( !$this->reposInitialised ) {
$this->initialiseRepos();
}
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
/**
* Does the installation have any foreign repos set up?
* @return bool
2008-09-03 13:50:37 +00:00
*/
function hasForeignRepos() {
if ( !$this->reposInitialised ) {
$this->initialiseRepos();
}
return (bool)$this->foreignRepos;
}
/**
* 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 );
}
}
/**
* Create a repo class based on an info structure
*/
protected function newRepo( $info ) {
$class = $info['class'];
return new $class( $info );
}
/**
* Split a virtual URL into repo, zone and rel parts
* @param string $url
* @throws MWException
* @return array Containing repo, zone and rel
*/
function splitVirtualUrl( $url ) {
if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
throw new MWException( __METHOD__ . ': unknown protocol' );
}
$bits = explode( '/', substr( $url, 9 ), 3 );
if ( count( $bits ) != 3 ) {
throw new MWException( __METHOD__ . ": invalid mwrepo URL: $url" );
}
return $bits;
}
/**
* @param string $fileName
* @return array
*/
function getFileProps( $fileName ) {
if ( FileRepo::isVirtualUrl( $fileName ) ) {
Static code analysis housekeeping time... things that could be double-checked are marked with "[Note: some-comment]" : if-if-else without curly braces [api/ApiQuerySiteinfo.php] --> adding Unused global declaration: $wgGroupPermissions --> removing Unused global declaration: $wgEmailConfirmToEdit (line 301) --> removing Variable $id appears only once (line 1021) --> removing Variable $m was used before it was defined (line 805) --> defining. Variable $retval was used before it was defined (line 2346) --> renaming to $result Variable $rcid appears only once (line 244 of RecentChange.php) --> using this instead of $change [Note: was left over from r24607 refactoring, revert if wrong please] Unused global declaration: $wgCommandLineMode (line 11) --> removing Variable $k appears only once (line 132 of ImagePage.php) --> removing. Variable $info appears only once (line 311 of ImagePage.php) --> removing. Unused global declaration: $wgTitle (line 569 of ImagePage.php) -> removing. Variable $handlerParams was used before it was defined (line 616 of Linker.php) --> resolved by Raymond in r24966 Variable $match was used before it was defined (line 1031 of Linker.php) --> defining. Unused global declaration: $wgEnotifWatchlist (line 253 of UserMailer.php) --> removing Unused global declaration: $wgShowUpdatedMarker (line 253 of UserMailer.php) --> removing Variable $img appears only once (line 446 of SpecialUpload.php) --> added definition, defined as null, flagged with @todo [Note: should $img be defined in this context, or is it intended to be null? And should the return value after the hook be checked in some way?] Unused global declaration: $wgEnableAPI (line 739 of SpecialUpload.php) --> removing. Unused global declaration: $wgNamespaceProtection (line 1030 of OutputPage.php) --> removing. Unused global declaration: $wgContLang (line 18 of SpecialWatchlist.php) --> removing. Unused global declaration: $wgRawHtml (line 269 of SpecialMovepage.php) --> removing. The value of variable $page was never used (line 331 of SpecialUndelete.php) --> removing line, as $page gets redefined a few lines down. Variable $synIndex appears only once (line 521 of MagicWord.php) --> commenting out. Variable $case appears only once (line 539 of MagicWord.php) --> removing from foreach index key usage. Variable $wgUser appears only once (line 1039 of Title.php) --> adding line to declare as a global, would be null otherwise. Variable $m was used before it was defined (line 285 of Title.php) --> defining. Variable $id appears only once (line 1150 of Title.php) --> removing from foreach index key usage. Variable $subpage appears only once (line 1297 of Title.php) --> commenting out. Variable $restrictions appears only once (line 1399 of Title.php) --> commenting out. Variable $mime appears only once (line 210 of filerepo/OldLocalFile.php) --> removing. Variable $deprefixedName appears only once (line 213 of filerepo/LocalFile.php) --> removing. Variable $m appears only once (line 541 of filerepo/LocalFile.php) --> removing. Variable $where appears only once (line 1245 of filerepo/LocalFile.php) --> removing. Variable $info appears only once (line 1427 of filerepo/LocalFile.php) --> removing. Variable $rel appears only once (line 138 of filerepo/RepoGroup.php) --> commenting out. Variable $zone appears only once (line 138 of filerepo/RepoGroup.php) --> commenting out. Variable $nbytes appears only once (line 208 of media/Generic.php) --> added a return line that uses $nbytes. [Note: I'm assuming that this was the intent] Variable $offset appears only once (line 201 of SpecialListusers.php) --> removing. Variable $limit appears only once (line 201 of SpecialListusers.php) --> removing. Variable $groupTarget appears only once (line 203 of SpecialListusers.php) --> removing. Unused global declaration: $wgLang (line 74 of SpecialWantedpages.php) --> removing. Variable $block appears only once (line 244 of SpecialProtectedpages.php) --> removing. Variable $offset appears only once (line 281 of SpecialProtectedpages.php) --> removing. Variable $limit appears only once (line 281 of SpecialProtectedpages.php) --> removing. Unused global declaration: $wgLang (line 30 of FileDeleteForm.php) --> removing. Unused global declaration: $wgServer (line 30 of FileDeleteForm.php) --> removing.
2007-08-21 03:57:54 +00:00
list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
if ( $repoName === '' ) {
$repoName = 'local';
}
$repo = $this->getRepo( $repoName );
return $repo->getFileProps( $fileName );
} else {
return FSFile::getPropsFromPath( $fileName );
}
}
/**
* Clear RepoGroup process cache used for finding a file
* @param Title|null $title Title of the file or null to clear all files
*/
public function clearCache( Title $title = null ) {
if ( $title == null ) {
$this->cache->clear();
} else {
$this->cache->clear( $title->getDBkey() );
}
}
}