ForeignAPIRepo fixes:
[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 $images = $this->localRepo->findFiles( $titles, $flags );
91
92 foreach ( $this->foreignRepos as $repo ) {
93 $images = array_merge( $images, $repo->findFiles( $titles, $flags ) );
94 }
95 return $images;
96 }
97
98 /**
99 * Interface for FileRepo::checkRedirect()
100 */
101 function checkRedirect( $title ) {
102 if ( !$this->reposInitialised ) {
103 $this->initialiseRepos();
104 }
105
106 $redir = $this->localRepo->checkRedirect( $title );
107 if( $redir ) {
108 return $redir;
109 }
110 foreach ( $this->foreignRepos as $repo ) {
111 $redir = $repo->checkRedirect( $title );
112 if ( $redir ) {
113 return $redir;
114 }
115 }
116 return false;
117 }
118
119 function findBySha1( $hash ) {
120 if ( !$this->reposInitialised ) {
121 $this->initialiseRepos();
122 }
123
124 $result = $this->localRepo->findBySha1( $hash );
125 foreach ( $this->foreignRepos as $repo )
126 $result = array_merge( $result, $repo->findBySha1( $hash ) );
127 return $result;
128 }
129
130 /**
131 * Get the repo instance with a given key.
132 */
133 function getRepo( $index ) {
134 if ( !$this->reposInitialised ) {
135 $this->initialiseRepos();
136 }
137 if ( $index === 'local' ) {
138 return $this->localRepo;
139 } elseif ( isset( $this->foreignRepos[$index] ) ) {
140 return $this->foreignRepos[$index];
141 } else {
142 return false;
143 }
144 }
145 /**
146 * Get the repo instance by its name
147 */
148 function getRepoByName( $name ) {
149 if ( !$this->reposInitialised ) {
150 $this->initialiseRepos();
151 }
152 foreach ( $this->foreignRepos as $key => $repo ) {
153 if ( $repo->name == $name)
154 return $repo;
155 }
156 return false;
157 }
158
159 /**
160 * Get the local repository, i.e. the one corresponding to the local image
161 * table. Files are typically uploaded to the local repository.
162 */
163 function getLocalRepo() {
164 return $this->getRepo( 'local' );
165 }
166
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 ) ) {
171 return true;
172 }
173 }
174 return false;
175 }
176
177 function hasForeignRepos() {
178 return !empty( $this->foreignRepos );
179 }
180
181 /**
182 * Initialise the $repos array
183 */
184 function initialiseRepos() {
185 if ( $this->reposInitialised ) {
186 return;
187 }
188 $this->reposInitialised = true;
189
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 );
194 }
195 }
196
197 /**
198 * Create a repo class based on an info structure
199 */
200 protected function newRepo( $info ) {
201 $class = $info['class'];
202 return new $class( $info );
203 }
204
205 /**
206 * Split a virtual URL into repo, zone and rel parts
207 * @return an array containing repo, zone and rel
208 */
209 function splitVirtualUrl( $url ) {
210 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
211 throw new MWException( __METHOD__.': unknown protoocl' );
212 }
213
214 $bits = explode( '/', substr( $url, 9 ), 3 );
215 if ( count( $bits ) != 3 ) {
216 throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
217 }
218 return $bits;
219 }
220
221 function getFileProps( $fileName ) {
222 if ( FileRepo::isVirtualUrl( $fileName ) ) {
223 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
224 if ( $repoName === '' ) {
225 $repoName = 'local';
226 }
227 $repo = $this->getRepo( $repoName );
228 return $repo->getFileProps( $fileName );
229 } else {
230 return File::getPropsFromPath( $fileName );
231 }
232 }
233 }