Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / filerepo / FileBackendDBRepoWrapper.php
1 <?php
2 /**
3 * Proxy backend that manages file layout rewriting for FileRepo.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup FileRepo
22 * @ingroup FileBackend
23 * @author Aaron Schulz
24 */
25
26 /**
27 * @brief Proxy backend that manages file layout rewriting for FileRepo.
28 *
29 * LocalRepo may be configured to store files under their title names or by SHA-1.
30 * This acts as a shim in the later case, providing backwards compatability for
31 * most callers. All "public"/"deleted" zone files actually go in an "original"
32 * container and are never changed.
33 *
34 * This requires something like thumb_handler.php and img_auth.php for client viewing of files.
35 *
36 * @ingroup FileRepo
37 * @ingroup FileBackend
38 * @since 1.25
39 */
40 class FileBackendDBRepoWrapper extends FileBackend {
41 /** @var FileBackend */
42 protected $backend;
43 /** @var string */
44 protected $repoName;
45 /** @var Closure */
46 protected $dbHandleFunc;
47 /** @var ProcessCacheLRU */
48 protected $resolvedPathCache;
49 /** @var DBConnRef[] */
50 protected $dbs;
51
52 public function __construct( array $config ) {
53 $config['name'] = $config['backend']->getName();
54 $config['wikiId'] = $config['backend']->getWikiId();
55 parent::__construct( $config );
56 $this->backend = $config['backend'];
57 $this->repoName = $config['repoName'];
58 $this->dbHandleFunc = $config['dbHandleFactory'];
59 $this->resolvedPathCache = new ProcessCacheLRU( 100 );
60 }
61
62 /**
63 * Get the underlying FileBackend that is being wrapped
64 *
65 * @return FileBackend
66 */
67 public function getInternalBackend() {
68 return $this->backend;
69 }
70
71 /**
72 * Translate a legacy "title" path to it's "sha1" counterpart
73 *
74 * E.g. mwstore://local-backend/local-public/a/ab/<name>.jpg
75 * => mwstore://local-backend/local-original/x/y/z/<sha1>.jpg
76 *
77 * @param string $path
78 * @param bool $latest
79 * @return string
80 */
81 public function getBackendPath( $path, $latest = true ) {
82 $paths = $this->getBackendPaths( [ $path ], $latest );
83 return current( $paths );
84 }
85
86 /**
87 * Translate legacy "title" paths to their "sha1" counterparts
88 *
89 * E.g. mwstore://local-backend/local-public/a/ab/<name>.jpg
90 * => mwstore://local-backend/local-original/x/y/z/<sha1>.jpg
91 *
92 * @param array $paths
93 * @param bool $latest
94 * @return array Translated paths in same order
95 */
96 public function getBackendPaths( array $paths, $latest = true ) {
97 $db = $this->getDB( $latest ? DB_MASTER : DB_SLAVE );
98
99 // @TODO: batching
100 $resolved = [];
101 foreach ( $paths as $i => $path ) {
102 if ( !$latest && $this->resolvedPathCache->has( $path, 'target', 10 ) ) {
103 $resolved[$i] = $this->resolvedPathCache->get( $path, 'target' );
104 continue;
105 }
106
107 list( , $container ) = FileBackend::splitStoragePath( $path );
108
109 if ( $container === "{$this->repoName}-public" ) {
110 $name = basename( $path );
111 if ( strpos( $path, '!' ) !== false ) {
112 $sha1 = $db->selectField( 'oldimage', 'oi_sha1',
113 [ 'oi_archive_name' => $name ],
114 __METHOD__
115 );
116 } else {
117 $sha1 = $db->selectField( 'image', 'img_sha1',
118 [ 'img_name' => $name ],
119 __METHOD__
120 );
121 }
122 if ( !strlen( $sha1 ) ) {
123 $resolved[$i] = $path; // give up
124 continue;
125 }
126 $resolved[$i] = $this->getPathForSHA1( $sha1 );
127 $this->resolvedPathCache->set( $path, 'target', $resolved[$i] );
128 } elseif ( $container === "{$this->repoName}-deleted" ) {
129 $name = basename( $path ); // <hash>.<ext>
130 $sha1 = substr( $name, 0, strpos( $name, '.' ) ); // ignore extension
131 $resolved[$i] = $this->getPathForSHA1( $sha1 );
132 $this->resolvedPathCache->set( $path, 'target', $resolved[$i] );
133 } else {
134 $resolved[$i] = $path;
135 }
136 }
137
138 $res = [];
139 foreach ( $paths as $i => $path ) {
140 $res[$i] = $resolved[$i];
141 }
142
143 return $res;
144 }
145
146 protected function doOperationsInternal( array $ops, array $opts ) {
147 return $this->backend->doOperationsInternal( $this->mungeOpPaths( $ops ), $opts );
148 }
149
150 protected function doQuickOperationsInternal( array $ops ) {
151 return $this->backend->doQuickOperationsInternal( $this->mungeOpPaths( $ops ) );
152 }
153
154 protected function doPrepare( array $params ) {
155 return $this->backend->doPrepare( $params );
156 }
157
158 protected function doSecure( array $params ) {
159 return $this->backend->doSecure( $params );
160 }
161
162 protected function doPublish( array $params ) {
163 return $this->backend->doPublish( $params );
164 }
165
166 protected function doClean( array $params ) {
167 return $this->backend->doClean( $params );
168 }
169
170 public function concatenate( array $params ) {
171 return $this->translateSrcParams( __FUNCTION__, $params );
172 }
173
174 public function fileExists( array $params ) {
175 return $this->translateSrcParams( __FUNCTION__, $params );
176 }
177
178 public function getFileTimestamp( array $params ) {
179 return $this->translateSrcParams( __FUNCTION__, $params );
180 }
181
182 public function getFileSize( array $params ) {
183 return $this->translateSrcParams( __FUNCTION__, $params );
184 }
185
186 public function getFileStat( array $params ) {
187 return $this->translateSrcParams( __FUNCTION__, $params );
188 }
189
190 public function getFileXAttributes( array $params ) {
191 return $this->translateSrcParams( __FUNCTION__, $params );
192 }
193
194 public function getFileSha1Base36( array $params ) {
195 return $this->translateSrcParams( __FUNCTION__, $params );
196 }
197
198 public function getFileProps( array $params ) {
199 return $this->translateSrcParams( __FUNCTION__, $params );
200 }
201
202 public function streamFile( array $params ) {
203 // The stream methods use the file extension to determine the
204 // Content-Type (as MediaWiki should already validate it on upload).
205 // The translated SHA1 path has no extension, so this needs to use
206 // the untranslated path extension.
207 $type = StreamFile::contentTypeFromPath( $params['src'] );
208 if ( $type && $type != 'unknown/unknown' ) {
209 $params['headers'][] = "Content-type: $type";
210 }
211 return $this->translateSrcParams( __FUNCTION__, $params );
212 }
213
214 public function getFileContentsMulti( array $params ) {
215 return $this->translateArrayResults( __FUNCTION__, $params );
216 }
217
218 public function getLocalReferenceMulti( array $params ) {
219 return $this->translateArrayResults( __FUNCTION__, $params );
220 }
221
222 public function getLocalCopyMulti( array $params ) {
223 return $this->translateArrayResults( __FUNCTION__, $params );
224 }
225
226 public function getFileHttpUrl( array $params ) {
227 return $this->translateSrcParams( __FUNCTION__, $params );
228 }
229
230 public function directoryExists( array $params ) {
231 return $this->backend->directoryExists( $params );
232 }
233
234 public function getDirectoryList( array $params ) {
235 return $this->backend->getDirectoryList( $params );
236 }
237
238 public function getFileList( array $params ) {
239 return $this->backend->getFileList( $params );
240 }
241
242 public function getFeatures() {
243 return $this->backend->getFeatures();
244 }
245
246 public function clearCache( array $paths = null ) {
247 $this->backend->clearCache( null ); // clear all
248 }
249
250 public function preloadCache( array $paths ) {
251 $paths = $this->getBackendPaths( $paths );
252 $this->backend->preloadCache( $paths );
253 }
254
255 public function preloadFileStat( array $params ) {
256 return $this->translateSrcParams( __FUNCTION__, $params );
257 }
258
259 public function getScopedLocksForOps( array $ops, Status $status ) {
260 return $this->backend->getScopedLocksForOps( $ops, $status );
261 }
262
263 /**
264 * Get the ultimate original storage path for a file
265 *
266 * Use this when putting a new file into the system
267 *
268 * @param string $sha1 File SHA-1 base36
269 * @return string
270 */
271 public function getPathForSHA1( $sha1 ) {
272 if ( strlen( $sha1 ) < 3 ) {
273 throw new InvalidArgumentException( "Invalid file SHA-1." );
274 }
275 return $this->backend->getContainerStoragePath( "{$this->repoName}-original" ) .
276 "/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
277 }
278
279 /**
280 * Get a connection to the repo file registry DB
281 *
282 * @param integer $index
283 * @return DBConnRef
284 */
285 protected function getDB( $index ) {
286 if ( !isset( $this->dbs[$index] ) ) {
287 $func = $this->dbHandleFunc;
288 $this->dbs[$index] = $func( $index );
289 }
290 return $this->dbs[$index];
291 }
292
293 /**
294 * Translates paths found in the "src" or "srcs" keys of a params array
295 *
296 * @param string $function
297 * @param array $params
298 */
299 protected function translateSrcParams( $function, array $params ) {
300 $latest = !empty( $params['latest'] );
301
302 if ( isset( $params['src'] ) ) {
303 $params['src'] = $this->getBackendPath( $params['src'], $latest );
304 }
305
306 if ( isset( $params['srcs'] ) ) {
307 $params['srcs'] = $this->getBackendPaths( $params['srcs'], $latest );
308 }
309
310 return $this->backend->$function( $params );
311 }
312
313 /**
314 * Translates paths when the backend function returns results keyed by paths
315 *
316 * @param string $function
317 * @param array $params
318 * @return array
319 */
320 protected function translateArrayResults( $function, array $params ) {
321 $origPaths = $params['srcs'];
322 $params['srcs'] = $this->getBackendPaths( $params['srcs'], !empty( $params['latest'] ) );
323 $pathMap = array_combine( $params['srcs'], $origPaths );
324
325 $results = $this->backend->$function( $params );
326
327 $contents = [];
328 foreach ( $results as $path => $result ) {
329 $contents[$pathMap[$path]] = $result;
330 }
331
332 return $contents;
333 }
334
335 /**
336 * Translate legacy "title" source paths to their "sha1" counterparts
337 *
338 * This leaves destination paths alone since we don't want those to mutate
339 *
340 * @param array $ops
341 * @return array
342 */
343 protected function mungeOpPaths( array $ops ) {
344 // Ops that use 'src' and do not mutate core file data there
345 static $srcRefOps = [ 'store', 'copy', 'describe' ];
346 foreach ( $ops as &$op ) {
347 if ( isset( $op['src'] ) && in_array( $op['op'], $srcRefOps ) ) {
348 $op['src'] = $this->getBackendPath( $op['src'], true );
349 }
350 if ( isset( $op['srcs'] ) ) {
351 $op['srcs'] = $this->getBackendPaths( $op['srcs'], true );
352 }
353 }
354 return $ops;
355 }
356 }