Add bug and comment for r35609: * (bug 13434) Show a warning when hash identical...
[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 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;
184 }
185 }
186 return false;
187 }
188
189 function hasForeignRepos() {
190 return !empty( $this->foreignRepos );
191 }
192
193 /**
194 * Initialise the $repos array
195 */
196 function initialiseRepos() {
197 if ( $this->reposInitialised ) {
198 return;
199 }
200 $this->reposInitialised = true;
201
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 );
206 }
207 }
208
209 /**
210 * Create a repo class based on an info structure
211 */
212 protected function newRepo( $info ) {
213 $class = $info['class'];
214 return new $class( $info );
215 }
216
217 /**
218 * Split a virtual URL into repo, zone and rel parts
219 * @return an array containing repo, zone and rel
220 */
221 function splitVirtualUrl( $url ) {
222 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
223 throw new MWException( __METHOD__.': unknown protoocl' );
224 }
225
226 $bits = explode( '/', substr( $url, 9 ), 3 );
227 if ( count( $bits ) != 3 ) {
228 throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
229 }
230 return $bits;
231 }
232
233 function getFileProps( $fileName ) {
234 if ( FileRepo::isVirtualUrl( $fileName ) ) {
235 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
236 if ( $repoName === '' ) {
237 $repoName = 'local';
238 }
239 $repo = $this->getRepo( $repoName );
240 return $repo->getFileProps( $fileName );
241 } else {
242 return File::getPropsFromPath( $fileName );
243 }
244 }
245 }