4 * Prioritized list of file repositories
8 var $localRepo, $foreignRepos, $reposInitialised = false;
9 var $localInfo, $foreignInfo;
11 protected static $instance;
14 * Get a RepoGroup instance. At present only one instance of RepoGroup is
15 * needed in a MediaWiki invocation, this may change in the future.
17 static function singleton() {
18 if ( self
::$instance ) {
19 return self
::$instance;
21 global $wgLocalFileRepo, $wgForeignFileRepos;
22 self
::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
23 return self
::$instance;
27 * Destroy the singleton instance, so that a new one will be created next
28 * time singleton() is called.
30 static function destroySingleton() {
31 self
::$instance = null;
35 * Set the singleton instance to a given object
37 static function setSingleton( $instance ) {
38 self
::$instance = $instance;
42 * Construct a group of file repositories.
43 * @param array $data Array of repository info arrays.
44 * Each info array is an associative array with the 'class' member
45 * giving the class name. The entire array is passed to the repository
46 * constructor as the first parameter.
48 function __construct( $localInfo, $foreignInfo ) {
49 $this->localInfo
= $localInfo;
50 $this->foreignInfo
= $foreignInfo;
54 * Search repositories for an image.
55 * You can also use wfGetFile() to do this.
56 * @param mixed $title Title object or string
57 * @param mixed $time The 14-char timestamp the file should have
58 * been uploaded, or false for the current version
59 * @param mixed $flags FileRepo::FIND_ flags
60 * @return File object or false if it is not found
62 function findFile( $title, $time = false, $flags = 0 ) {
63 if ( !$this->reposInitialised
) {
64 $this->initialiseRepos();
67 $image = $this->localRepo
->findFile( $title, $time, $flags );
71 foreach ( $this->foreignRepos
as $repo ) {
72 $image = $repo->findFile( $title, $time, $flags );
79 function findFiles( $titles, $time = false, $flags = 0 ) {
80 if ( !$this->reposInitialised
) {
81 $this->initialiseRepos();
84 $images = $this->localRepo
->findFiles( $titles, $time, $flags );
86 foreach ( $this->foreignRepos
as $repo ) {
87 $images = array_merge( $images, $repo->findFiles( $titles, $time, $flags ) );
93 * Interface for FileRepo::checkRedirect()
95 function checkRedirect( $title ) {
96 if ( !$this->reposInitialised
) {
97 $this->initialiseRepos();
100 $redir = $this->localRepo
->checkRedirect( $title );
104 foreach ( $this->foreignRepos
as $repo ) {
105 $redir = $repo->checkRedirect( $title );
113 function findBySha1( $hash ) {
114 if ( !$this->reposInitialised
) {
115 $this->initialiseRepos();
118 $result = $this->localRepo
->findBySha1( $hash );
119 foreach ( $this->foreignRepos
as $repo )
120 $result = array_merge( $result, $repo->findBySha1( $hash ) );
125 * Get the repo instance with a given key.
127 function getRepo( $index ) {
128 if ( !$this->reposInitialised
) {
129 $this->initialiseRepos();
131 if ( $index === 'local' ) {
132 return $this->localRepo
;
133 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
134 return $this->foreignRepos
[$index];
140 * Get the repo instance by its name
142 function getRepoByName( $name ) {
143 if ( !$this->reposInitialised
) {
144 $this->initialiseRepos();
146 foreach ( $this->foreignRepos
as $key => $repo ) {
147 if ( $repo->name
== $name)
154 * Get the local repository, i.e. the one corresponding to the local image
155 * table. Files are typically uploaded to the local repository.
157 function getLocalRepo() {
158 return $this->getRepo( 'local' );
161 function forEachForeignRepo( $callback, $params = array() ) {
162 foreach( $this->foreignRepos
as $repo ) {
163 $args = array_merge( array( $repo ), $params );
164 if( call_user_func_array( $callback, $args ) ) {
171 function hasForeignRepos() {
172 return !empty( $this->foreignRepos
);
176 * Initialise the $repos array
178 function initialiseRepos() {
179 if ( $this->reposInitialised
) {
182 $this->reposInitialised
= true;
184 $this->localRepo
= $this->newRepo( $this->localInfo
);
185 $this->foreignRepos
= array();
186 foreach ( $this->foreignInfo
as $key => $info ) {
187 $this->foreignRepos
[$key] = $this->newRepo( $info );
192 * Create a repo class based on an info structure
194 protected function newRepo( $info ) {
195 $class = $info['class'];
196 return new $class( $info );
200 * Split a virtual URL into repo, zone and rel parts
201 * @return an array containing repo, zone and rel
203 function splitVirtualUrl( $url ) {
204 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
205 throw new MWException( __METHOD__
.': unknown protoocl' );
208 $bits = explode( '/', substr( $url, 9 ), 3 );
209 if ( count( $bits ) != 3 ) {
210 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
215 function getFileProps( $fileName ) {
216 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
217 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
218 if ( $repoName === '' ) {
221 $repo = $this->getRepo( $repoName );
222 return $repo->getFileProps( $fileName );
224 return File
::getPropsFromPath( $fileName );