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