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 );
81 * Interface for FileRepo::checkRedirect()
83 function checkRedirect( $title ) {
84 if ( !$this->reposInitialised
) {
85 $this->initialiseRepos();
88 $redir = $this->localRepo
->checkRedirect( $title );
92 foreach ( $this->foreignRepos
as $repo ) {
93 $redir = $repo->checkRedirect( $title );
102 * Get the repo instance with a given key.
104 function getRepo( $index ) {
105 if ( !$this->reposInitialised
) {
106 $this->initialiseRepos();
108 if ( $index === 'local' ) {
109 return $this->localRepo
;
110 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
111 return $this->foreignRepos
[$index];
117 * Get the repo instance by its name
119 function getRepoByName( $name ) {
120 if ( !$this->reposInitialised
) {
121 $this->initialiseRepos();
123 foreach ( $this->foreignRepos
as $key => $repo ) {
124 if ( $repo->name
== $name)
131 * Get the local repository, i.e. the one corresponding to the local image
132 * table. Files are typically uploaded to the local repository.
134 function getLocalRepo() {
135 return $this->getRepo( 'local' );
138 function forEachForeignRepo( $callback, $params = array() ) {
139 foreach( $this->foreignRepos
as $repo ) {
140 $args = array_merge( array( $repo ), $params );
141 if( call_user_func_array( $callback, $args ) ) {
148 function hasForeignRepos() {
149 return !empty( $this->foreignRepos
);
153 * Initialise the $repos array
155 function initialiseRepos() {
156 if ( $this->reposInitialised
) {
159 $this->reposInitialised
= true;
161 $this->localRepo
= $this->newRepo( $this->localInfo
);
162 $this->foreignRepos
= array();
163 foreach ( $this->foreignInfo
as $key => $info ) {
164 $this->foreignRepos
[$key] = $this->newRepo( $info );
169 * Create a repo class based on an info structure
171 protected function newRepo( $info ) {
172 $class = $info['class'];
173 return new $class( $info );
177 * Split a virtual URL into repo, zone and rel parts
178 * @return an array containing repo, zone and rel
180 function splitVirtualUrl( $url ) {
181 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
182 throw new MWException( __METHOD__
.': unknown protoocl' );
185 $bits = explode( '/', substr( $url, 9 ), 3 );
186 if ( count( $bits ) != 3 ) {
187 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
192 function getFileProps( $fileName ) {
193 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
194 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
195 if ( $repoName === '' ) {
198 $repo = $this->getRepo( $repoName );
199 return $repo->getFileProps( $fileName );
201 return File
::getPropsFromPath( $fileName );