E_STRICT
[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
80 /**
81 * Interface for FileRepo::checkRedirect()
82 */
83 function checkRedirect( $title ) {
84 if ( !$this->reposInitialised ) {
85 $this->initialiseRepos();
86 }
87
88 $redir = $this->localRepo->checkRedirect( $title );
89 if( $redir ) {
90 return $redir;
91 }
92 foreach ( $this->foreignRepos as $repo ) {
93 $redir = $repo->checkRedirect( $title );
94 if ( $redir ) {
95 return $redir;
96 }
97 }
98 return false;
99 }
100
101 /**
102 * Get the repo instance with a given key.
103 */
104 function getRepo( $index ) {
105 if ( !$this->reposInitialised ) {
106 $this->initialiseRepos();
107 }
108 if ( $index === 'local' ) {
109 return $this->localRepo;
110 } elseif ( isset( $this->foreignRepos[$index] ) ) {
111 return $this->foreignRepos[$index];
112 } else {
113 return false;
114 }
115 }
116 /**
117 * Get the repo instance by its name
118 */
119 function getRepoByName( $name ) {
120 if ( !$this->reposInitialised ) {
121 $this->initialiseRepos();
122 }
123 foreach ( $this->foreignRepos as $key => $repo ) {
124 if ( $repo->name == $name)
125 return $repo;
126 }
127 return false;
128 }
129
130 /**
131 * Get the local repository, i.e. the one corresponding to the local image
132 * table. Files are typically uploaded to the local repository.
133 */
134 function getLocalRepo() {
135 return $this->getRepo( 'local' );
136 }
137
138 function forEachForeignRepo( $callback, $params = array() ) {
139 foreach( $this->foreignRepos as $repo ) {
140 $args = array_merge( array( $repo ), $params );
141 if( call_user_func_array( $callback, $args ) ) {
142 return true;
143 }
144 }
145 return false;
146 }
147
148 function hasForeignRepos() {
149 return !empty( $this->foreignRepos );
150 }
151
152 /**
153 * Initialise the $repos array
154 */
155 function initialiseRepos() {
156 if ( $this->reposInitialised ) {
157 return;
158 }
159 $this->reposInitialised = true;
160
161 $this->localRepo = $this->newRepo( $this->localInfo );
162 $this->foreignRepos = array();
163 foreach ( $this->foreignInfo as $key => $info ) {
164 $this->foreignRepos[$key] = $this->newRepo( $info );
165 }
166 }
167
168 /**
169 * Create a repo class based on an info structure
170 */
171 protected function newRepo( $info ) {
172 $class = $info['class'];
173 return new $class( $info );
174 }
175
176 /**
177 * Split a virtual URL into repo, zone and rel parts
178 * @return an array containing repo, zone and rel
179 */
180 function splitVirtualUrl( $url ) {
181 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
182 throw new MWException( __METHOD__.': unknown protoocl' );
183 }
184
185 $bits = explode( '/', substr( $url, 9 ), 3 );
186 if ( count( $bits ) != 3 ) {
187 throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
188 }
189 return $bits;
190 }
191
192 function getFileProps( $fileName ) {
193 if ( FileRepo::isVirtualUrl( $fileName ) ) {
194 list( $repoName, /* $zone */, /* $rel */ ) = $this->splitVirtualUrl( $fileName );
195 if ( $repoName === '' ) {
196 $repoName = 'local';
197 }
198 $repo = $this->getRepo( $repoName );
199 return $repo->getFileProps( $fileName );
200 } else {
201 return File::getPropsFromPath( $fileName );
202 }
203 }
204 }