3 * @defgroup FileRepo FileRepo
11 * Prioritized list of file repositories
14 var $localRepo, $foreignRepos, $reposInitialised = false;
15 var $localInfo, $foreignInfo;
17 protected static $instance;
20 * Get a RepoGroup instance. At present only one instance of RepoGroup is
21 * needed in a MediaWiki invocation, this may change in the future.
23 static function singleton() {
24 if ( self
::$instance ) {
25 return self
::$instance;
27 global $wgLocalFileRepo, $wgForeignFileRepos;
28 self
::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
29 return self
::$instance;
33 * Destroy the singleton instance, so that a new one will be created next
34 * time singleton() is called.
36 static function destroySingleton() {
37 self
::$instance = null;
41 * Set the singleton instance to a given object
43 static function setSingleton( $instance ) {
44 self
::$instance = $instance;
48 * Construct a group of file repositories.
49 * @param array $data Array of repository info arrays.
50 * Each info array is an associative array with the 'class' member
51 * giving the class name. The entire array is passed to the repository
52 * constructor as the first parameter.
54 function __construct( $localInfo, $foreignInfo ) {
55 $this->localInfo
= $localInfo;
56 $this->foreignInfo
= $foreignInfo;
60 * Search repositories for an image.
61 * You can also use wfGetFile() to do this.
62 * @param mixed $title Title object or string
63 * @param mixed $time The 14-char timestamp the file should have
64 * been uploaded, or false for the current version
65 * @param mixed $flags FileRepo::FIND_ flags
66 * @return File object or false if it is not found
68 function findFile( $title, $time = false, $flags = 0 ) {
69 if ( !$this->reposInitialised
) {
70 $this->initialiseRepos();
73 $image = $this->localRepo
->findFile( $title, $time, $flags );
77 foreach ( $this->foreignRepos
as $repo ) {
78 $image = $repo->findFile( $title, $time, $flags );
85 function findFiles( $titles, $flags = 0 ) {
86 if ( !$this->reposInitialised
) {
87 $this->initialiseRepos();
90 $images = $this->localRepo
->findFiles( $titles, $flags );
92 foreach ( $this->foreignRepos
as $repo ) {
93 $images = array_merge( $images, $repo->findFiles( $titles, $flags ) );
99 * Interface for FileRepo::checkRedirect()
101 function checkRedirect( $title ) {
102 if ( !$this->reposInitialised
) {
103 $this->initialiseRepos();
106 $redir = $this->localRepo
->checkRedirect( $title );
110 foreach ( $this->foreignRepos
as $repo ) {
111 $redir = $repo->checkRedirect( $title );
119 function findBySha1( $hash ) {
120 if ( !$this->reposInitialised
) {
121 $this->initialiseRepos();
124 $result = $this->localRepo
->findBySha1( $hash );
125 foreach ( $this->foreignRepos
as $repo )
126 $result = array_merge( $result, $repo->findBySha1( $hash ) );
131 * Get the repo instance with a given key.
133 function getRepo( $index ) {
134 if ( !$this->reposInitialised
) {
135 $this->initialiseRepos();
137 if ( $index === 'local' ) {
138 return $this->localRepo
;
139 } elseif ( isset( $this->foreignRepos
[$index] ) ) {
140 return $this->foreignRepos
[$index];
146 * Get the repo instance by its name
148 function getRepoByName( $name ) {
149 if ( !$this->reposInitialised
) {
150 $this->initialiseRepos();
152 foreach ( $this->foreignRepos
as $key => $repo ) {
153 if ( $repo->name
== $name)
160 * Get the local repository, i.e. the one corresponding to the local image
161 * table. Files are typically uploaded to the local repository.
163 function getLocalRepo() {
164 return $this->getRepo( 'local' );
167 function forEachForeignRepo( $callback, $params = array() ) {
168 foreach( $this->foreignRepos
as $repo ) {
169 $args = array_merge( array( $repo ), $params );
170 if( call_user_func_array( $callback, $args ) ) {
177 function hasForeignRepos() {
178 return !empty( $this->foreignRepos
);
182 * Initialise the $repos array
184 function initialiseRepos() {
185 if ( $this->reposInitialised
) {
188 $this->reposInitialised
= true;
190 $this->localRepo
= $this->newRepo( $this->localInfo
);
191 $this->foreignRepos
= array();
192 foreach ( $this->foreignInfo
as $key => $info ) {
193 $this->foreignRepos
[$key] = $this->newRepo( $info );
198 * Create a repo class based on an info structure
200 protected function newRepo( $info ) {
201 $class = $info['class'];
202 return new $class( $info );
206 * Split a virtual URL into repo, zone and rel parts
207 * @return an array containing repo, zone and rel
209 function splitVirtualUrl( $url ) {
210 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
211 throw new MWException( __METHOD__
.': unknown protoocl' );
214 $bits = explode( '/', substr( $url, 9 ), 3 );
215 if ( count( $bits ) != 3 ) {
216 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
221 function getFileProps( $fileName ) {
222 if ( FileRepo
::isVirtualUrl( $fileName ) ) {
223 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
224 if ( $repoName === '' ) {
227 $repo = $this->getRepo( $repoName );
228 return $repo->getFileProps( $fileName );
230 return File
::getPropsFromPath( $fileName );