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
|
|
|
/**
|
2012-05-07 07:11:33 +00:00
|
|
|
* 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
|
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
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2007-05-30 21:02:32 +00:00
|
|
|
|
2021-04-09 15:18:14 +00:00
|
|
|
use MediaWiki\Linker\LinkTarget;
|
|
|
|
|
use MediaWiki\Page\PageIdentity;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2024-09-27 18:21:33 +00:00
|
|
|
use Wikimedia\Mime\MimeAnalyzer;
|
2024-09-27 18:13:02 +00:00
|
|
|
use Wikimedia\ObjectCache\WANObjectCache;
|
2018-10-21 12:07:44 +00:00
|
|
|
|
2010-09-04 18:13:18 +00:00
|
|
|
/**
|
2022-04-07 01:20:40 +00:00
|
|
|
* Prioritized list of file repositories.
|
2010-09-04 18:13:18 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup FileRepo
|
2007-05-31 01:43:41 +00:00
|
|
|
*/
|
2007-05-30 21:02:32 +00:00
|
|
|
class RepoGroup {
|
2013-11-24 09:44:10 +00:00
|
|
|
/** @var LocalRepo */
|
|
|
|
|
protected $localRepo;
|
2011-02-18 23:56:08 +00:00
|
|
|
|
2013-12-05 09:13:28 +00:00
|
|
|
/** @var FileRepo[] */
|
2013-11-24 09:44:10 +00:00
|
|
|
protected $foreignRepos;
|
2007-05-30 21:02:32 +00:00
|
|
|
|
2019-05-01 12:54:54 +00:00
|
|
|
/** @var WANObjectCache */
|
|
|
|
|
protected $wanCache;
|
|
|
|
|
|
2013-11-24 09:44:10 +00:00
|
|
|
/** @var bool */
|
|
|
|
|
protected $reposInitialised = false;
|
|
|
|
|
|
2013-12-04 16:18:05 +00:00
|
|
|
/** @var array */
|
2013-11-24 09:44:10 +00:00
|
|
|
protected $localInfo;
|
2013-12-04 16:18:05 +00:00
|
|
|
|
|
|
|
|
/** @var array */
|
2013-11-24 09:44:10 +00:00
|
|
|
protected $foreignInfo;
|
|
|
|
|
|
2019-06-05 18:32:05 +00:00
|
|
|
/** @var MapCacheLRU */
|
2013-11-24 09:44:10 +00:00
|
|
|
protected $cache;
|
|
|
|
|
|
|
|
|
|
/** Maximum number of cache items */
|
2020-05-15 22:16:46 +00:00
|
|
|
private const MAX_CACHE_SIZE = 500;
|
2007-05-30 21:02:32 +00:00
|
|
|
|
2021-08-13 11:46:31 +00:00
|
|
|
/** @var MimeAnalyzer */
|
|
|
|
|
private $mimeAnalyzer;
|
|
|
|
|
|
2007-05-30 21:02:32 +00:00
|
|
|
/**
|
2019-05-01 12:54:54 +00:00
|
|
|
* Construct a group of file repositories. Do not call this -- use
|
|
|
|
|
* MediaWikiServices::getRepoGroup.
|
2010-03-26 21:55:13 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $localInfo Associative array for local repo's info
|
2014-04-19 15:19:17 +00:00
|
|
|
* @param array $foreignInfo Array of repository info arrays.
|
2013-12-04 16:18:05 +00:00
|
|
|
* 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.
|
2019-05-01 12:54:54 +00:00
|
|
|
* @param WANObjectCache $wanCache
|
2021-08-13 11:46:31 +00:00
|
|
|
* @param MimeAnalyzer $mimeAnalyzer
|
2007-05-30 21:02:32 +00:00
|
|
|
*/
|
2021-08-13 11:46:31 +00:00
|
|
|
public function __construct(
|
|
|
|
|
$localInfo,
|
|
|
|
|
$foreignInfo,
|
|
|
|
|
WANObjectCache $wanCache,
|
|
|
|
|
MimeAnalyzer $mimeAnalyzer
|
|
|
|
|
) {
|
2007-05-30 21:02:32 +00:00
|
|
|
$this->localInfo = $localInfo;
|
|
|
|
|
$this->foreignInfo = $foreignInfo;
|
2018-07-11 12:53:23 +00:00
|
|
|
$this->cache = new MapCacheLRU( self::MAX_CACHE_SIZE );
|
2019-05-01 12:54:54 +00:00
|
|
|
$this->wanCache = $wanCache;
|
2021-08-13 11:46:31 +00:00
|
|
|
$this->mimeAnalyzer = $mimeAnalyzer;
|
2007-05-30 21:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Search repositories for an image.
|
2010-03-26 21:55:13 +00:00
|
|
|
*
|
2021-04-09 15:18:14 +00:00
|
|
|
* @param PageIdentity|LinkTarget|string $title The file to find
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $options Associative array of options:
|
2013-12-04 16:18:05 +00:00
|
|
|
* 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
|
2021-05-26 19:43:17 +00:00
|
|
|
* private: If Authority object, return restricted (deleted) files if the
|
|
|
|
|
* performer is allowed to view them. Otherwise, such files will not
|
|
|
|
|
* be found. Authority is only accepted since 1.37, User was required
|
|
|
|
|
* before.
|
2015-03-05 20:13:28 +00:00
|
|
|
* latest: If true, load from the latest available data into File objects
|
2021-05-26 19:43:17 +00:00
|
|
|
* @phpcs:ignore Generic.Files.LineLength
|
|
|
|
|
* @phan-param array{time?:mixed,ignoreRedirect?:bool,private?:bool|MediaWiki\Permissions\Authority,latest?:bool} $options
|
2021-09-03 23:09:25 +00:00
|
|
|
* @return File|false False if title is not found
|
2007-05-30 21:02:32 +00:00
|
|
|
*/
|
2020-05-18 01:39:53 +00:00
|
|
|
public function findFile( $title, $options = [] ) {
|
2009-08-15 09:59:59 +00:00
|
|
|
if ( !is_array( $options ) ) {
|
|
|
|
|
// MW 1.15 compat
|
2016-02-17 09:09:32 +00:00
|
|
|
$options = [ 'time' => $options ];
|
2009-08-15 09:59:59 +00:00
|
|
|
}
|
2015-03-05 20:13:28 +00:00
|
|
|
if ( isset( $options['bypassCache'] ) ) {
|
|
|
|
|
$options['latest'] = $options['bypassCache']; // b/c
|
|
|
|
|
}
|
2022-07-08 23:30:07 +00:00
|
|
|
if ( isset( $options['time'] ) && $options['time'] !== false ) {
|
|
|
|
|
$options['time'] = wfTimestamp( TS_MW, $options['time'] );
|
|
|
|
|
} else {
|
|
|
|
|
$options['time'] = false;
|
|
|
|
|
}
|
2015-03-05 20:13:28 +00:00
|
|
|
|
2007-05-30 21:02:32 +00:00
|
|
|
if ( !$this->reposInitialised ) {
|
|
|
|
|
$this->initialiseRepos();
|
|
|
|
|
}
|
2018-07-19 10:02:57 +00:00
|
|
|
|
2011-11-04 23:49:03 +00:00
|
|
|
$title = File::normalizeTitle( $title );
|
|
|
|
|
if ( !$title ) {
|
|
|
|
|
return false;
|
2010-11-10 09:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
2009-08-15 09:59:59 +00:00
|
|
|
# Check the cache
|
2016-09-19 02:12:05 +00:00
|
|
|
$dbkey = $title->getDBkey();
|
2018-07-19 10:02:57 +00:00
|
|
|
$timeKey = is_string( $options['time'] ) ? $options['time'] : '';
|
2010-01-22 03:14:52 +00:00
|
|
|
if ( empty( $options['ignoreRedirect'] )
|
|
|
|
|
&& empty( $options['private'] )
|
2018-01-03 02:19:58 +00:00
|
|
|
&& empty( $options['latest'] )
|
2013-11-23 20:00:11 +00:00
|
|
|
) {
|
2018-07-19 10:02:57 +00:00
|
|
|
if ( $this->cache->hasField( $dbkey, $timeKey, 60 ) ) {
|
|
|
|
|
return $this->cache->getField( $dbkey, $timeKey );
|
2009-08-15 09:59:59 +00:00
|
|
|
}
|
2012-04-06 17:38:38 +00:00
|
|
|
$useCache = true;
|
2009-08-15 09:59:59 +00:00
|
|
|
} 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 );
|
|
|
|
|
|
|
|
|
|
# Check the foreign repos
|
2012-04-06 17:38:38 +00:00
|
|
|
if ( !$image ) {
|
|
|
|
|
foreach ( $this->foreignRepos as $repo ) {
|
|
|
|
|
$image = $repo->findFile( $title, $options );
|
|
|
|
|
if ( $image ) {
|
|
|
|
|
break;
|
2009-08-15 09:59:59 +00:00
|
|
|
}
|
2007-05-30 21:02:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
2012-04-06 17:38:38 +00:00
|
|
|
|
2021-11-19 23:19:42 +00:00
|
|
|
$image = $image instanceof File ? $image : false; // type check
|
2012-04-06 17:38:38 +00:00
|
|
|
# Cache file existence or non-existence
|
|
|
|
|
if ( $useCache && ( !$image || $image->isCacheable() ) ) {
|
2018-07-19 10:02:57 +00:00
|
|
|
$this->cache->setField( $dbkey, $timeKey, $image );
|
2012-04-06 17:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $image;
|
2007-05-30 21:02:32 +00:00
|
|
|
}
|
2009-08-15 09:59:59 +00:00
|
|
|
|
2012-05-18 02:58:15 +00:00
|
|
|
/**
|
2013-12-09 20:24:32 +00:00
|
|
|
* Search repositories for many files at once.
|
|
|
|
|
*
|
2014-08-16 18:49:41 +00:00
|
|
|
* @param array $inputItems An array of titles, or an array of findFile() options with
|
2013-12-09 20:24:32 +00:00
|
|
|
* the "title" option giving the title. Example:
|
|
|
|
|
*
|
2016-08-13 01:10:40 +00:00
|
|
|
* $findItem = [ 'title' => $title, 'private' => true ];
|
|
|
|
|
* $findBatch = [ $findItem ];
|
2013-12-09 20:24:32 +00:00
|
|
|
* $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.
|
2021-09-03 23:09:25 +00:00
|
|
|
* @return array Map of (file name => File objects) for matches or (search title => (title,timestamp))
|
2012-05-18 02:58:15 +00:00
|
|
|
*/
|
2020-05-18 01:39:53 +00:00
|
|
|
public function findFiles( array $inputItems, $flags = 0 ) {
|
2008-05-20 17:05:57 +00:00
|
|
|
if ( !$this->reposInitialised ) {
|
|
|
|
|
$this->initialiseRepos();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$items = [];
|
2009-08-15 09:59:59 +00:00
|
|
|
foreach ( $inputItems as $item ) {
|
|
|
|
|
if ( !is_array( $item ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$item = [ 'title' => $item ];
|
2009-08-15 09:59:59 +00:00
|
|
|
}
|
2011-11-11 22:14:21 +00:00
|
|
|
$item['title'] = File::normalizeTitle( $item['title'] );
|
|
|
|
|
if ( $item['title'] ) {
|
2009-08-15 09:59:59 +00:00
|
|
|
$items[$item['title']->getDBkey()] = $item;
|
2011-11-11 22:14:21 +00:00
|
|
|
}
|
2008-05-30 13:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
2013-12-09 20:24:32 +00:00
|
|
|
$images = $this->localRepo->findFiles( $items, $flags );
|
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] );
|
|
|
|
|
}
|
2010-01-22 03:14:52 +00:00
|
|
|
|
2013-12-09 20:24:32 +00:00
|
|
|
$images = array_merge( $images, $repo->findFiles( $items, $flags ) );
|
2008-05-20 17:05:57 +00:00
|
|
|
}
|
2013-11-23 20:00:11 +00:00
|
|
|
|
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()
|
2021-04-09 15:18:14 +00:00
|
|
|
* @param PageIdentity|LinkTarget|string $title
|
2021-09-03 23:09:25 +00:00
|
|
|
* @return Title|false
|
2008-01-16 18:27:43 +00:00
|
|
|
*/
|
2021-04-09 15:18:14 +00:00
|
|
|
public function checkRedirect( $title ) {
|
2008-01-16 18:27:43 +00:00
|
|
|
if ( !$this->reposInitialised ) {
|
|
|
|
|
$this->initialiseRepos();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 15:18:14 +00:00
|
|
|
$title = File::normalizeTitle( $title );
|
|
|
|
|
|
2008-01-16 18:27:43 +00:00
|
|
|
$redir = $this->localRepo->checkRedirect( $title );
|
2013-04-20 17:18:13 +00:00
|
|
|
if ( $redir ) {
|
2008-01-16 18:27:43 +00:00
|
|
|
return $redir;
|
|
|
|
|
}
|
2013-12-05 09:13:28 +00:00
|
|
|
|
2008-01-16 18:27:43 +00:00
|
|
|
foreach ( $this->foreignRepos as $repo ) {
|
|
|
|
|
$redir = $repo->checkRedirect( $title );
|
|
|
|
|
if ( $redir ) {
|
|
|
|
|
return $redir;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-23 20:00:11 +00:00
|
|
|
|
2008-01-16 18:27:43 +00:00
|
|
|
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.
|
|
|
|
|
*
|
2014-04-19 15:19:17 +00:00
|
|
|
* @param string $hash Base 36 SHA-1 hash
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $options Option array, same as findFile()
|
2021-09-03 23:09:25 +00:00
|
|
|
* @return File|false File object or false if it is not found
|
2011-03-28 21:40:50 +00:00
|
|
|
*/
|
2020-05-18 01:39:53 +00:00
|
|
|
public function findFileFromKey( $hash, $options = [] ) {
|
2011-03-28 21:40:50 +00:00
|
|
|
if ( !$this->reposInitialised ) {
|
|
|
|
|
$this->initialiseRepos();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$file = $this->localRepo->findFileFromKey( $hash, $options );
|
|
|
|
|
if ( !$file ) {
|
|
|
|
|
foreach ( $this->foreignRepos as $repo ) {
|
|
|
|
|
$file = $repo->findFileFromKey( $hash, $options );
|
2013-04-20 17:18:13 +00:00
|
|
|
if ( $file ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-03-28 21:40:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-23 20:00:11 +00:00
|
|
|
|
2011-03-28 21:40:50 +00:00
|
|
|
return $file;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-20 20:34:03 +00:00
|
|
|
/**
|
|
|
|
|
* Find all instances of files with this key
|
|
|
|
|
*
|
2014-07-24 17:43:03 +00:00
|
|
|
* @param string $hash Base 36 SHA-1 hash
|
2014-07-08 21:40:06 +00:00
|
|
|
* @return File[]
|
2011-03-20 20:34:03 +00:00
|
|
|
*/
|
2020-05-18 01:39:53 +00:00
|
|
|
public function findBySha1( $hash ) {
|
2008-05-14 15:11:48 +00:00
|
|
|
if ( !$this->reposInitialised ) {
|
|
|
|
|
$this->initialiseRepos();
|
|
|
|
|
}
|
2010-01-22 03:14:52 +00:00
|
|
|
|
2008-05-14 15:11:48 +00:00
|
|
|
$result = $this->localRepo->findBySha1( $hash );
|
2011-03-20 20:34:03 +00:00
|
|
|
foreach ( $this->foreignRepos as $repo ) {
|
2008-05-14 15:11:48 +00:00
|
|
|
$result = array_merge( $result, $repo->findBySha1( $hash ) );
|
2011-03-20 20:34:03 +00:00
|
|
|
}
|
2020-12-31 11:37:36 +00:00
|
|
|
usort( $result, [ File::class, 'compare' ] );
|
2013-11-23 20:00:11 +00:00
|
|
|
|
2010-01-22 03:14:52 +00:00
|
|
|
return $result;
|
2008-05-14 15:11:48 +00:00
|
|
|
}
|
2008-01-16 18:27:43 +00:00
|
|
|
|
2012-07-23 16:45:38 +00:00
|
|
|
/**
|
|
|
|
|
* Find all instances of files with this keys
|
|
|
|
|
*
|
2021-09-03 23:09:25 +00:00
|
|
|
* @param string[] $hashes Base 36 SHA-1 hashes
|
2020-10-28 10:01:33 +00:00
|
|
|
* @return File[][]
|
2012-07-23 16:45:38 +00:00
|
|
|
*/
|
2020-05-18 01:39:53 +00:00
|
|
|
public function findBySha1s( array $hashes ) {
|
2012-07-23 16:45:38 +00:00
|
|
|
if ( !$this->reposInitialised ) {
|
|
|
|
|
$this->initialiseRepos();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result = $this->localRepo->findBySha1s( $hashes );
|
|
|
|
|
foreach ( $this->foreignRepos as $repo ) {
|
|
|
|
|
$result = array_merge_recursive( $result, $repo->findBySha1s( $hashes ) );
|
|
|
|
|
}
|
2015-09-11 13:44:59 +00:00
|
|
|
// sort the merged (and presorted) sublist of each hash
|
2013-04-20 17:18:13 +00:00
|
|
|
foreach ( $result as $hash => $files ) {
|
2020-12-31 11:37:36 +00:00
|
|
|
usort( $result[$hash], [ File::class, 'compare' ] );
|
2012-07-23 16:45:38 +00:00
|
|
|
}
|
2013-11-23 20:00:11 +00:00
|
|
|
|
2012-07-23 16:45:38 +00:00
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-30 21:02:32 +00:00
|
|
|
/**
|
|
|
|
|
* Get the repo instance with a given key.
|
2013-12-04 16:18:05 +00:00
|
|
|
* @param string|int $index
|
2021-09-03 23:09:25 +00:00
|
|
|
* @return FileRepo|false
|
2007-05-30 21:02:32 +00:00
|
|
|
*/
|
2020-05-18 01:39:53 +00:00
|
|
|
public function getRepo( $index ) {
|
2007-05-30 21:02:32 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2018-06-12 20:44:33 +00:00
|
|
|
return $this->foreignRepos[$index] ?? false;
|
2007-05-30 21:02:32 +00:00
|
|
|
}
|
2012-05-18 02:58:15 +00:00
|
|
|
|
2008-02-09 13:40:16 +00:00
|
|
|
/**
|
|
|
|
|
* Get the repo instance by its name
|
2013-12-04 16:18:05 +00:00
|
|
|
* @param string $name
|
2021-09-03 23:09:25 +00:00
|
|
|
* @return FileRepo|false
|
2008-02-09 13:40:16 +00:00
|
|
|
*/
|
2020-05-18 01:39:53 +00:00
|
|
|
public function getRepoByName( $name ) {
|
2008-02-09 13:40:16 +00:00
|
|
|
if ( !$this->reposInitialised ) {
|
|
|
|
|
$this->initialiseRepos();
|
|
|
|
|
}
|
2010-10-14 20:53:04 +00:00
|
|
|
foreach ( $this->foreignRepos as $repo ) {
|
2012-05-18 02:58:15 +00:00
|
|
|
if ( $repo->name == $name ) {
|
2008-02-09 13:40:16 +00:00
|
|
|
return $repo;
|
2012-05-18 02:58:15 +00:00
|
|
|
}
|
2008-02-09 13:40:16 +00:00
|
|
|
}
|
2013-11-23 20:00:11 +00:00
|
|
|
|
2008-02-09 13:40:16 +00:00
|
|
|
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.
|
2011-07-01 02:25:19 +00:00
|
|
|
*
|
|
|
|
|
* @return LocalRepo
|
2007-05-31 01:43:41 +00:00
|
|
|
*/
|
2020-05-18 01:39:53 +00:00
|
|
|
public function getLocalRepo() {
|
2021-08-28 08:40:44 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchReturnSuperType
|
2021-09-03 23:09:25 +00:00
|
|
|
return $this->getRepo( 'local' );
|
2007-05-30 21:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
2008-09-16 06:13:31 +00:00
|
|
|
* first parameter.
|
|
|
|
|
*
|
2013-12-04 16:18:05 +00:00
|
|
|
* @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
|
|
|
*/
|
2020-05-18 01:39:53 +00:00
|
|
|
public function forEachForeignRepo( $callback, $params = [] ) {
|
2014-07-03 06:12:27 +00:00
|
|
|
if ( !$this->reposInitialised ) {
|
|
|
|
|
$this->initialiseRepos();
|
|
|
|
|
}
|
2013-04-20 17:18:13 +00:00
|
|
|
foreach ( $this->foreignRepos as $repo ) {
|
2018-06-09 23:26:32 +00:00
|
|
|
if ( $callback( $repo, ...$params ) ) {
|
2008-05-01 22:16:23 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-23 20:00:11 +00:00
|
|
|
|
2008-05-01 22:16:23 +00:00
|
|
|
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?
|
2013-12-04 16:18:05 +00:00
|
|
|
* @return bool
|
2008-09-03 13:50:37 +00:00
|
|
|
*/
|
2020-05-18 01:39:53 +00:00
|
|
|
public function hasForeignRepos() {
|
2014-07-03 06:12:27 +00:00
|
|
|
if ( !$this->reposInitialised ) {
|
|
|
|
|
$this->initialiseRepos();
|
|
|
|
|
}
|
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
|
|
|
|
|
*/
|
2020-05-18 01:39:53 +00:00
|
|
|
public function initialiseRepos() {
|
2007-05-30 21:02:32 +00:00
|
|
|
if ( $this->reposInitialised ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$this->reposInitialised = true;
|
|
|
|
|
|
|
|
|
|
$this->localRepo = $this->newRepo( $this->localInfo );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->foreignRepos = [];
|
2007-05-30 21:02:32 +00:00
|
|
|
foreach ( $this->foreignInfo as $key => $info ) {
|
|
|
|
|
$this->foreignRepos[$key] = $this->newRepo( $info );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 06:46:47 +00:00
|
|
|
/**
|
|
|
|
|
* Create a local repo with the specified option overrides.
|
|
|
|
|
*
|
|
|
|
|
* @param array $info
|
2021-09-03 23:09:25 +00:00
|
|
|
* @return LocalRepo
|
2021-06-04 06:46:47 +00:00
|
|
|
*/
|
|
|
|
|
public function newCustomLocalRepo( $info = [] ) {
|
2021-08-28 08:40:44 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchReturnSuperType
|
2021-06-04 06:46:47 +00:00
|
|
|
return $this->newRepo( $info + $this->localInfo );
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-31 01:43:41 +00:00
|
|
|
/**
|
|
|
|
|
* Create a repo class based on an info structure
|
2014-08-14 18:22:52 +00:00
|
|
|
* @param array $info
|
2014-08-23 20:34:25 +00:00
|
|
|
* @return FileRepo
|
2007-05-31 01:43:41 +00:00
|
|
|
*/
|
|
|
|
|
protected function newRepo( $info ) {
|
2007-05-30 21:02:32 +00:00
|
|
|
$class = $info['class'];
|
2013-11-23 20:00:11 +00:00
|
|
|
|
2019-05-01 12:54:54 +00:00
|
|
|
$info['wanCache'] = $this->wanCache;
|
2018-10-21 12:07:44 +00:00
|
|
|
|
2007-05-30 21:02:32 +00:00
|
|
|
return new $class( $info );
|
|
|
|
|
}
|
2007-06-16 02:55:25 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Split a virtual URL into repo, zone and rel parts
|
2013-12-04 16:18:05 +00:00
|
|
|
* @param string $url
|
2018-04-19 08:30:33 +00:00
|
|
|
* @return string[] Containing repo, zone and rel
|
2007-06-16 02:55:25 +00:00
|
|
|
*/
|
2020-05-18 01:39:53 +00:00
|
|
|
private function splitVirtualUrl( $url ) {
|
2022-12-12 18:54:24 +00:00
|
|
|
if ( !str_starts_with( $url, 'mwrepo://' ) ) {
|
2023-06-09 21:50:08 +00:00
|
|
|
throw new InvalidArgumentException( __METHOD__ . ': unknown protocol' );
|
2007-06-16 02:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bits = explode( '/', substr( $url, 9 ), 3 );
|
|
|
|
|
if ( count( $bits ) != 3 ) {
|
2023-06-09 21:50:08 +00:00
|
|
|
throw new InvalidArgumentException( __METHOD__ . ": invalid mwrepo URL: $url" );
|
2007-06-16 02:55:25 +00:00
|
|
|
}
|
2013-11-23 20:00:11 +00:00
|
|
|
|
2007-06-16 02:55:25 +00:00
|
|
|
return $bits;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-18 02:58:15 +00:00
|
|
|
/**
|
2013-12-04 16:18:05 +00:00
|
|
|
* @param string $fileName
|
2012-05-18 02:58:15 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2020-05-18 01:39:53 +00:00
|
|
|
public function getFileProps( $fileName ) {
|
2007-06-16 02:55:25 +00:00
|
|
|
if ( FileRepo::isVirtualUrl( $fileName ) ) {
|
2007-08-21 03:57:54 +00:00
|
|
|
[ $repoName, /* $zone */, /* $rel */ ] = $this->splitVirtualUrl( $fileName );
|
2007-06-16 02:55:25 +00:00
|
|
|
if ( $repoName === '' ) {
|
|
|
|
|
$repoName = 'local';
|
|
|
|
|
}
|
|
|
|
|
$repo = $this->getRepo( $repoName );
|
2013-11-23 20:00:11 +00:00
|
|
|
|
2007-06-16 02:55:25 +00:00
|
|
|
return $repo->getFileProps( $fileName );
|
|
|
|
|
} else {
|
2021-08-13 11:46:31 +00:00
|
|
|
$mwProps = new MWFileProps( $this->mimeAnalyzer );
|
2016-09-19 01:39:59 +00:00
|
|
|
|
|
|
|
|
return $mwProps->getPropsFromPath( $fileName, true );
|
2007-06-16 02:55:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-08-15 09:59:59 +00:00
|
|
|
|
2011-09-21 21:32:24 +00:00
|
|
|
/**
|
|
|
|
|
* Clear RepoGroup process cache used for finding a file
|
2021-04-09 15:18:14 +00:00
|
|
|
* @param PageIdentity|string|null $title File page or file name, or null to clear all files
|
2011-09-21 21:32:24 +00:00
|
|
|
*/
|
2021-04-09 15:18:14 +00:00
|
|
|
public function clearCache( $title = null ) {
|
2011-09-21 21:32:24 +00:00
|
|
|
if ( $title == null ) {
|
2013-12-03 20:24:50 +00:00
|
|
|
$this->cache->clear();
|
2021-04-09 15:18:14 +00:00
|
|
|
} elseif ( is_string( $title ) ) {
|
|
|
|
|
$this->cache->clear( $title );
|
2011-09-21 21:32:24 +00:00
|
|
|
} else {
|
2013-12-03 20:24:50 +00:00
|
|
|
$this->cache->clear( $title->getDBkey() );
|
2011-09-21 21:32:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-05-30 21:02:32 +00:00
|
|
|
}
|