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