Consistent casing for wfGetDB(), getDB(), and getDBKey()
[lhc/web/wiklou.git] / includes / filerepo / RepoGroup.php
1 <?php
2 /**
3 * @defgroup FileRepo FileRepo
4 *
5 * @file
6 * @ingroup FileRepo
7 */
8
9 /**
10 * @ingroup FileRepo
11 * Prioritized list of file repositories
12 */
13 class RepoGroup {
14 var $localRepo, $foreignRepos, $reposInitialised = false;
15 var $localInfo, $foreignInfo;
16
17 protected static $instance;
18
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.
22 */
23 static function singleton() {
24 if ( self::$instance ) {
25 return self::$instance;
26 }
27 global $wgLocalFileRepo, $wgForeignFileRepos;
28 self::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
29 return self::$instance;
30 }
31
32 /**
33 * Destroy the singleton instance, so that a new one will be created next
34 * time singleton() is called.
35 */
36 static function destroySingleton() {
37 self::$instance = null;
38 }
39
40 /**
41 * Set the singleton instance to a given object
42 */
43 static function setSingleton( $instance ) {
44 self::$instance = $instance;
45 }
46
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.
53 */
54 function __construct( $localInfo, $foreignInfo ) {
55 $this->localInfo = $localInfo;
56 $this->foreignInfo = $foreignInfo;
57 }
58
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
67 */
68 function findFile( $title, $time = false, $flags = 0 ) {
69 if ( !$this->reposInitialised ) {
70 $this->initialiseRepos();
71 }
72
73 $image = $this->localRepo->findFile( $title, $time, $flags );
74 if ( $image ) {
75 return $image;
76 }
77 foreach ( $this->foreignRepos as $repo ) {
78 $image = $repo->findFile( $title, $time, $flags );
79 if ( $image ) {
80 return $image;
81 }
82 }
83 return false;
84 }
85 function findFiles( $titles, $flags = 0 ) {
86 if ( !$this->reposInitialised ) {
87 $this->initialiseRepos();
88 }
89
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;
95 }
96
97 $images = $this->localRepo->findFiles( $titleObjs, $flags );
98
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] );
104
105 $images = array_merge( $images, $repo->findFiles( $titleObjs, $flags ) );
106 }
107 return $images;
108 }
109
110 /**
111 * Interface for FileRepo::checkRedirect()
112 */
113 function checkRedirect( $title ) {
114 if ( !$this->reposInitialised ) {
115 $this->initialiseRepos();
116 }
117
118 $redir = $this->localRepo->checkRedirect( $title );
119 if( $redir ) {
120 return $redir;
121 }
122 foreach ( $this->foreignRepos as $repo ) {
123 $redir = $repo->checkRedirect( $title );
124 if ( $redir ) {
125 return $redir;
126 }
127 }
128 return false;
129 }
130
131 function findBySha1( $hash ) {
132 if ( !$this->reposInitialised ) {
133 $this->initialiseRepos();
134 }
135
136 $result = $this->localRepo->findBySha1( $hash );
137 foreach ( $this->foreignRepos as $repo )
138 $result = array_merge( $result, $repo->findBySha1( $hash ) );
139 return $result;
140 }
141
142 /**
143 * Get the repo instance with a given key.
144 */
145 function getRepo( $index ) {
146 if ( !$this->reposInitialised ) {
147 $this->initialiseRepos();
148 }
149 if ( $index === 'local' ) {
150 return $this->localRepo;
151 } elseif ( isset( $this->foreignRepos[$index] ) ) {
152 return $this->foreignRepos[$index];
153 } else {
154 return false;
155 }
156 }
157 /**
158 * Get the repo instance by its name
159 */
160 function getRepoByName( $name ) {
161 if ( !$this->reposInitialised ) {
162 $this->initialiseRepos();
163 }
164 foreach ( $this->foreignRepos as $key => $repo ) {
165 if ( $repo->name == $name)
166 return $repo;
167 }
168 return false;
169 }
170
171 /**
172 * Get the local repository, i.e. the one corresponding to the local image
173 * table. Files are typically uploaded to the local repository.
174 */
175 function getLocalRepo() {
176 return $this->getRepo( 'local' );
177 }
178
179 /**
180 * Call a function for each foreign repo, with the repo object as the
181 * first parameter.
182 *
183 * @param $callback callback The function to call
184 * @param $params array Optional additional parameters to pass to the function
185 */
186 function forEachForeignRepo( $callback, $params = array() ) {
187 foreach( $this->foreignRepos as $repo ) {
188 $args = array_merge( array( $repo ), $params );
189 if( call_user_func_array( $callback, $args ) ) {
190 return true;
191 }
192 }
193 return false;
194 }
195
196 /**
197 * Does the installation have any foreign repos set up?
198 * @return bool
199 */
200 function hasForeignRepos() {
201 return (bool)$this->foreignRepos;
202 }
203
204 /**
205 * Initialise the $repos array
206 */
207 function initialiseRepos() {
208 if ( $this->reposInitialised ) {
209 return;
210 }
211 $this->reposInitialised = true;
212
213 $this->localRepo = $this->newRepo( $this->localInfo );
214 $this->foreignRepos = array();
215 foreach ( $this->foreignInfo as $key => $info ) {
216 $this->foreignRepos[$key] = $this->newRepo( $info );
217 }
218 }
219
220 /**
221 * Create a repo class based on an info structure
222 */
223 protected function newRepo( $info ) {
224 $class = $info['class'];
225 return new $class( $info );
226 }
227
228 /**
229 * Split a virtual URL into repo, zone and rel parts
230 * @return an array containing repo, zone and rel
231 */
232 function splitVirtualUrl( $url ) {
233 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
234 throw new MWException( __METHOD__.': unknown protoocl' );
235 }
236
237 $bits = explode( '/', substr( $url, 9 ), 3 );
238 if ( count( $bits ) != 3 ) {
239 throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
240 }
241 return $bits;
242 }
243
244 function getFileProps( $fileName ) {
245 if ( FileRepo::isVirtualUrl( $fileName ) ) {
246 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
247 if ( $repoName === '' ) {
248 $repoName = 'local';
249 }
250 $repo = $this->getRepo( $repoName );
251 return $repo->getFileProps( $fileName );
252 } else {
253 return File::getPropsFromPath( $fileName );
254 }
255 }
256 }