Support plural for 'linkstoimage', 'redirectstofile' and 'duplicatesoffile'
[mediawiki.git] / includes / filerepo / RepoGroup.php
blob7cb837b3734799d37788b5269d7e44689f68076d
1 <?php
2 /**
3 * @defgroup FileRepo FileRepo
5 * @file
6 * @ingroup FileRepo
7 */
9 /**
10 * @ingroup FileRepo
11 * Prioritized list of file repositories
13 class RepoGroup {
14 var $localRepo, $foreignRepos, $reposInitialised = false;
15 var $localInfo, $foreignInfo;
17 protected static $instance;
19 /**
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;
32 /**
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;
40 /**
41 * Set the singleton instance to a given object
43 static function setSingleton( $instance ) {
44 self::$instance = $instance;
47 /**
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;
59 /**
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 );
74 if ( $image ) {
75 return $image;
77 foreach ( $this->foreignRepos as $repo ) {
78 $image = $repo->findFile( $title, $time, $flags );
79 if ( $image ) {
80 return $image;
83 return false;
85 function findFiles( $titles, $flags = 0 ) {
86 if ( !$this->reposInitialised ) {
87 $this->initialiseRepos();
90 $titleObjs = array();
91 foreach ( $titles as $title ) {
92 if ( !( $title instanceof Title ) )
93 $title = Title::makeTitleSafe( NS_IMAGE, $title );
94 $titleObjs[$title->getDBkey()] = $title;
97 $images = $this->localRepo->findFiles( $titleObjs, $flags );
99 foreach ( $this->foreignRepos as $repo ) {
100 // Remove found files from $titleObjs
101 foreach ( $images as $name => $image )
102 if ( isset( $titleObjs[$name] ) )
103 unset( $titleObjs[$name] );
105 $images = array_merge( $images, $repo->findFiles( $titleObjs, $flags ) );
107 return $images;
111 * Interface for FileRepo::checkRedirect()
113 function checkRedirect( $title ) {
114 if ( !$this->reposInitialised ) {
115 $this->initialiseRepos();
118 $redir = $this->localRepo->checkRedirect( $title );
119 if( $redir ) {
120 return $redir;
122 foreach ( $this->foreignRepos as $repo ) {
123 $redir = $repo->checkRedirect( $title );
124 if ( $redir ) {
125 return $redir;
128 return false;
131 function findBySha1( $hash ) {
132 if ( !$this->reposInitialised ) {
133 $this->initialiseRepos();
136 $result = $this->localRepo->findBySha1( $hash );
137 foreach ( $this->foreignRepos as $repo )
138 $result = array_merge( $result, $repo->findBySha1( $hash ) );
139 return $result;
143 * Get the repo instance with a given key.
145 function getRepo( $index ) {
146 if ( !$this->reposInitialised ) {
147 $this->initialiseRepos();
149 if ( $index === 'local' ) {
150 return $this->localRepo;
151 } elseif ( isset( $this->foreignRepos[$index] ) ) {
152 return $this->foreignRepos[$index];
153 } else {
154 return false;
158 * Get the repo instance by its name
160 function getRepoByName( $name ) {
161 if ( !$this->reposInitialised ) {
162 $this->initialiseRepos();
164 foreach ( $this->foreignRepos as $key => $repo ) {
165 if ( $repo->name == $name)
166 return $repo;
168 return false;
172 * Get the local repository, i.e. the one corresponding to the local image
173 * table. Files are typically uploaded to the local repository.
175 function getLocalRepo() {
176 return $this->getRepo( 'local' );
179 function forEachForeignRepo( $callback, $params = array() ) {
180 foreach( $this->foreignRepos as $repo ) {
181 $args = array_merge( array( $repo ), $params );
182 if( call_user_func_array( $callback, $args ) ) {
183 return true;
186 return false;
189 function hasForeignRepos() {
190 return !empty( $this->foreignRepos );
194 * Initialise the $repos array
196 function initialiseRepos() {
197 if ( $this->reposInitialised ) {
198 return;
200 $this->reposInitialised = true;
202 $this->localRepo = $this->newRepo( $this->localInfo );
203 $this->foreignRepos = array();
204 foreach ( $this->foreignInfo as $key => $info ) {
205 $this->foreignRepos[$key] = $this->newRepo( $info );
210 * Create a repo class based on an info structure
212 protected function newRepo( $info ) {
213 $class = $info['class'];
214 return new $class( $info );
218 * Split a virtual URL into repo, zone and rel parts
219 * @return an array containing repo, zone and rel
221 function splitVirtualUrl( $url ) {
222 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
223 throw new MWException( __METHOD__.': unknown protoocl' );
226 $bits = explode( '/', substr( $url, 9 ), 3 );
227 if ( count( $bits ) != 3 ) {
228 throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
230 return $bits;
233 function getFileProps( $fileName ) {
234 if ( FileRepo::isVirtualUrl( $fileName ) ) {
235 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
236 if ( $repoName === '' ) {
237 $repoName = 'local';
239 $repo = $this->getRepo( $repoName );
240 return $repo->getFileProps( $fileName );
241 } else {
242 return File::getPropsFromPath( $fileName );