Add RepoGroup::findFiles to efficiently find many files if the repo supports it....
[lhc/web/wiklou.git] / includes / filerepo / RepoGroup.php
1 <?php
2
3 /**
4 * Prioritized list of file repositories
5 * @addtogroup FileRepo
6 */
7 class RepoGroup {
8 var $localRepo, $foreignRepos, $reposInitialised = false;
9 var $localInfo, $foreignInfo;
10
11 protected static $instance;
12
13 /**
14 * Get a RepoGroup instance. At present only one instance of RepoGroup is
15 * needed in a MediaWiki invocation, this may change in the future.
16 */
17 static function singleton() {
18 if ( self::$instance ) {
19 return self::$instance;
20 }
21 global $wgLocalFileRepo, $wgForeignFileRepos;
22 self::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos );
23 return self::$instance;
24 }
25
26 /**
27 * Destroy the singleton instance, so that a new one will be created next
28 * time singleton() is called.
29 */
30 static function destroySingleton() {
31 self::$instance = null;
32 }
33
34 /**
35 * Set the singleton instance to a given object
36 */
37 static function setSingleton( $instance ) {
38 self::$instance = $instance;
39 }
40
41 /**
42 * Construct a group of file repositories.
43 * @param array $data Array of repository info arrays.
44 * Each info array is an associative array with the 'class' member
45 * giving the class name. The entire array is passed to the repository
46 * constructor as the first parameter.
47 */
48 function __construct( $localInfo, $foreignInfo ) {
49 $this->localInfo = $localInfo;
50 $this->foreignInfo = $foreignInfo;
51 }
52
53 /**
54 * Search repositories for an image.
55 * You can also use wfGetFile() to do this.
56 * @param mixed $title Title object or string
57 * @param mixed $time The 14-char timestamp the file should have
58 * been uploaded, or false for the current version
59 * @param mixed $flags FileRepo::FIND_ flags
60 * @return File object or false if it is not found
61 */
62 function findFile( $title, $time = false, $flags = 0 ) {
63 if ( !$this->reposInitialised ) {
64 $this->initialiseRepos();
65 }
66
67 $image = $this->localRepo->findFile( $title, $time, $flags );
68 if ( $image ) {
69 return $image;
70 }
71 foreach ( $this->foreignRepos as $repo ) {
72 $image = $repo->findFile( $title, $time, $flags );
73 if ( $image ) {
74 return $image;
75 }
76 }
77 return false;
78 }
79 function findFiles( $titles, $time = false, $flags = 0 ) {
80 if ( !$this->reposInitialised ) {
81 $this->initialiseRepos();
82 }
83
84 $images = $this->localRepo->findFiles( $titles, $time, $flags );
85
86 foreach ( $this->foreignRepos as $repo ) {
87 $images = array_merge( $images, $repo->findFiles( $titles, $time, $flags ) );
88 }
89 return $images;
90 }
91
92 /**
93 * Interface for FileRepo::checkRedirect()
94 */
95 function checkRedirect( $title ) {
96 if ( !$this->reposInitialised ) {
97 $this->initialiseRepos();
98 }
99
100 $redir = $this->localRepo->checkRedirect( $title );
101 if( $redir ) {
102 return $redir;
103 }
104 foreach ( $this->foreignRepos as $repo ) {
105 $redir = $repo->checkRedirect( $title );
106 if ( $redir ) {
107 return $redir;
108 }
109 }
110 return false;
111 }
112
113 function findBySha1( $hash ) {
114 if ( !$this->reposInitialised ) {
115 $this->initialiseRepos();
116 }
117
118 $result = $this->localRepo->findBySha1( $hash );
119 foreach ( $this->foreignRepos as $repo )
120 $result = array_merge( $result, $repo->findBySha1( $hash ) );
121 return $result;
122 }
123
124 /**
125 * Get the repo instance with a given key.
126 */
127 function getRepo( $index ) {
128 if ( !$this->reposInitialised ) {
129 $this->initialiseRepos();
130 }
131 if ( $index === 'local' ) {
132 return $this->localRepo;
133 } elseif ( isset( $this->foreignRepos[$index] ) ) {
134 return $this->foreignRepos[$index];
135 } else {
136 return false;
137 }
138 }
139 /**
140 * Get the repo instance by its name
141 */
142 function getRepoByName( $name ) {
143 if ( !$this->reposInitialised ) {
144 $this->initialiseRepos();
145 }
146 foreach ( $this->foreignRepos as $key => $repo ) {
147 if ( $repo->name == $name)
148 return $repo;
149 }
150 return false;
151 }
152
153 /**
154 * Get the local repository, i.e. the one corresponding to the local image
155 * table. Files are typically uploaded to the local repository.
156 */
157 function getLocalRepo() {
158 return $this->getRepo( 'local' );
159 }
160
161 function forEachForeignRepo( $callback, $params = array() ) {
162 foreach( $this->foreignRepos as $repo ) {
163 $args = array_merge( array( $repo ), $params );
164 if( call_user_func_array( $callback, $args ) ) {
165 return true;
166 }
167 }
168 return false;
169 }
170
171 function hasForeignRepos() {
172 return !empty( $this->foreignRepos );
173 }
174
175 /**
176 * Initialise the $repos array
177 */
178 function initialiseRepos() {
179 if ( $this->reposInitialised ) {
180 return;
181 }
182 $this->reposInitialised = true;
183
184 $this->localRepo = $this->newRepo( $this->localInfo );
185 $this->foreignRepos = array();
186 foreach ( $this->foreignInfo as $key => $info ) {
187 $this->foreignRepos[$key] = $this->newRepo( $info );
188 }
189 }
190
191 /**
192 * Create a repo class based on an info structure
193 */
194 protected function newRepo( $info ) {
195 $class = $info['class'];
196 return new $class( $info );
197 }
198
199 /**
200 * Split a virtual URL into repo, zone and rel parts
201 * @return an array containing repo, zone and rel
202 */
203 function splitVirtualUrl( $url ) {
204 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
205 throw new MWException( __METHOD__.': unknown protoocl' );
206 }
207
208 $bits = explode( '/', substr( $url, 9 ), 3 );
209 if ( count( $bits ) != 3 ) {
210 throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
211 }
212 return $bits;
213 }
214
215 function getFileProps( $fileName ) {
216 if ( FileRepo::isVirtualUrl( $fileName ) ) {
217 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
218 if ( $repoName === '' ) {
219 $repoName = 'local';
220 }
221 $repo = $this->getRepo( $repoName );
222 return $repo->getFileProps( $fileName );
223 } else {
224 return File::getPropsFromPath( $fileName );
225 }
226 }
227 }