Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
25.00% |
31 / 124 |
|
2.70% |
1 / 37 |
CRAP | |
0.00% |
0 / 1 |
FileBackendDBRepoWrapper | |
25.00% |
31 / 124 |
|
2.70% |
1 / 37 |
1578.75 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
getInternalBackend | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getBackendPath | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getBackendPaths | |
62.50% |
30 / 48 |
|
0.00% |
0 / 1 |
21.91 | |||
doOperationsInternal | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
doQuickOperationsInternal | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
doPrepare | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
doSecure | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
doPublish | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
doClean | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
concatenate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
fileExists | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFileTimestamp | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFileSize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFileStat | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFileXAttributes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFileSha1Base36 | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFileProps | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
streamFile | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
getFileContentsMulti | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLocalReferenceMulti | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getLocalCopyMulti | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFileHttpUrl | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addShellboxInputFile | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
directoryExists | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDirectoryList | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFileList | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFeatures | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
clearCache | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
preloadCache | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
preloadFileStat | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getScopedLocksForOps | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getPathForSHA1 | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getDB | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
translateSrcParams | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
translateArrayResults | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
mungeOpPaths | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | use MediaWiki\MainConfigNames; |
22 | use MediaWiki\MediaWikiServices; |
23 | use MediaWiki\Output\StreamFile; |
24 | use Shellbox\Command\BoxedCommand; |
25 | use Wikimedia\FileBackend\FileBackend; |
26 | use Wikimedia\Rdbms\IDatabase; |
27 | |
28 | /** |
29 | * 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 MapCacheLRU */ |
50 | protected $resolvedPathCache; |
51 | /** @var IDatabase[] */ |
52 | protected $dbs; |
53 | private int $migrationStage; |
54 | |
55 | public function __construct( array $config ) { |
56 | /** @var FileBackend $backend */ |
57 | $backend = $config['backend']; |
58 | $config['name'] = $backend->getName(); |
59 | $config['domainId'] = $backend->getDomainId(); |
60 | parent::__construct( $config ); |
61 | $this->backend = $config['backend']; |
62 | $this->repoName = $config['repoName']; |
63 | $this->dbHandleFunc = $config['dbHandleFactory']; |
64 | $this->resolvedPathCache = new MapCacheLRU( 100 ); |
65 | $this->migrationStage = MediaWikiServices::getInstance()->getMainConfig()->get( |
66 | MainConfigNames::FileSchemaMigrationStage |
67 | ); |
68 | } |
69 | |
70 | /** |
71 | * Get the underlying FileBackend that is being wrapped |
72 | * |
73 | * @return FileBackend |
74 | */ |
75 | public function getInternalBackend() { |
76 | return $this->backend; |
77 | } |
78 | |
79 | /** |
80 | * Translate a legacy "title" path to its "sha1" counterpart |
81 | * |
82 | * E.g. mwstore://local-backend/local-public/a/ab/<name>.jpg |
83 | * => mwstore://local-backend/local-original/x/y/z/<sha1>.jpg |
84 | * |
85 | * @param string $path |
86 | * @param bool $latest |
87 | * @return string |
88 | */ |
89 | public function getBackendPath( $path, $latest = true ) { |
90 | $paths = $this->getBackendPaths( [ $path ], $latest ); |
91 | return current( $paths ); |
92 | } |
93 | |
94 | /** |
95 | * Translate legacy "title" paths to their "sha1" counterparts |
96 | * |
97 | * E.g. mwstore://local-backend/local-public/a/ab/<name>.jpg |
98 | * => mwstore://local-backend/local-original/x/y/z/<sha1>.jpg |
99 | * |
100 | * @param string[] $paths |
101 | * @param bool $latest |
102 | * @return string[] Translated paths in same order |
103 | */ |
104 | public function getBackendPaths( array $paths, $latest = true ) { |
105 | $db = $this->getDB( $latest ? DB_PRIMARY : DB_REPLICA ); |
106 | |
107 | // @TODO: batching |
108 | $resolved = []; |
109 | foreach ( $paths as $i => $path ) { |
110 | if ( !$latest && $this->resolvedPathCache->hasField( $path, 'target', 10 ) ) { |
111 | $resolved[$i] = $this->resolvedPathCache->getField( $path, 'target' ); |
112 | continue; |
113 | } |
114 | |
115 | [ , $container ] = FileBackend::splitStoragePath( $path ); |
116 | |
117 | if ( $container === "{$this->repoName}-public" ) { |
118 | $name = basename( $path ); |
119 | if ( $this->migrationStage & SCHEMA_COMPAT_READ_OLD ) { |
120 | if ( str_contains( $path, '!' ) ) { |
121 | $sha1 = $db->newSelectQueryBuilder() |
122 | ->select( 'oi_sha1' ) |
123 | ->from( 'oldimage' ) |
124 | ->where( [ 'oi_archive_name' => $name ] ) |
125 | ->caller( __METHOD__ )->fetchField(); |
126 | } else { |
127 | $sha1 = $db->newSelectQueryBuilder() |
128 | ->select( 'img_sha1' ) |
129 | ->from( 'image' ) |
130 | ->where( [ 'img_name' => $name ] ) |
131 | ->caller( __METHOD__ )->fetchField(); |
132 | } |
133 | } else { |
134 | if ( str_contains( $path, '!' ) ) { |
135 | $sha1 = $db->newSelectQueryBuilder() |
136 | ->select( 'fr_sha1' ) |
137 | ->from( 'filerevision' ) |
138 | ->where( [ 'fr_archive_name' => $name ] ) |
139 | ->caller( __METHOD__ )->fetchField(); |
140 | } else { |
141 | $sha1 = $db->newSelectQueryBuilder() |
142 | ->select( 'fr_sha1' ) |
143 | ->from( 'file' ) |
144 | ->join( 'filerevision', null, 'file_latest = fr_id' ) |
145 | ->where( [ 'file_name' => $name ] ) |
146 | ->caller( __METHOD__ )->fetchField(); |
147 | } |
148 | } |
149 | |
150 | if ( !is_string( $sha1 ) || $sha1 === '' ) { |
151 | $resolved[$i] = $path; // give up |
152 | continue; |
153 | } |
154 | $resolved[$i] = $this->getPathForSHA1( $sha1 ); |
155 | $this->resolvedPathCache->setField( $path, 'target', $resolved[$i] ); |
156 | } elseif ( $container === "{$this->repoName}-deleted" ) { |
157 | $name = basename( $path ); // <hash>.<ext> |
158 | $sha1 = substr( $name, 0, strpos( $name, '.' ) ); // ignore extension |
159 | $resolved[$i] = $this->getPathForSHA1( $sha1 ); |
160 | $this->resolvedPathCache->setField( $path, 'target', $resolved[$i] ); |
161 | } else { |
162 | $resolved[$i] = $path; |
163 | } |
164 | } |
165 | |
166 | $res = []; |
167 | foreach ( $paths as $i => $path ) { |
168 | $res[$i] = $resolved[$i]; |
169 | } |
170 | |
171 | return $res; |
172 | } |
173 | |
174 | protected function doOperationsInternal( array $ops, array $opts ) { |
175 | return $this->backend->doOperationsInternal( $this->mungeOpPaths( $ops ), $opts ); |
176 | } |
177 | |
178 | protected function doQuickOperationsInternal( array $ops, array $opts ) { |
179 | return $this->backend->doQuickOperationsInternal( $this->mungeOpPaths( $ops ), $opts ); |
180 | } |
181 | |
182 | protected function doPrepare( array $params ) { |
183 | return $this->backend->doPrepare( $params ); |
184 | } |
185 | |
186 | protected function doSecure( array $params ) { |
187 | return $this->backend->doSecure( $params ); |
188 | } |
189 | |
190 | protected function doPublish( array $params ) { |
191 | return $this->backend->doPublish( $params ); |
192 | } |
193 | |
194 | protected function doClean( array $params ) { |
195 | return $this->backend->doClean( $params ); |
196 | } |
197 | |
198 | public function concatenate( array $params ) { |
199 | return $this->translateSrcParams( __FUNCTION__, $params ); |
200 | } |
201 | |
202 | public function fileExists( array $params ) { |
203 | return $this->translateSrcParams( __FUNCTION__, $params ); |
204 | } |
205 | |
206 | public function getFileTimestamp( array $params ) { |
207 | return $this->translateSrcParams( __FUNCTION__, $params ); |
208 | } |
209 | |
210 | public function getFileSize( array $params ) { |
211 | return $this->translateSrcParams( __FUNCTION__, $params ); |
212 | } |
213 | |
214 | public function getFileStat( array $params ) { |
215 | return $this->translateSrcParams( __FUNCTION__, $params ); |
216 | } |
217 | |
218 | public function getFileXAttributes( array $params ) { |
219 | return $this->translateSrcParams( __FUNCTION__, $params ); |
220 | } |
221 | |
222 | public function getFileSha1Base36( array $params ) { |
223 | return $this->translateSrcParams( __FUNCTION__, $params ); |
224 | } |
225 | |
226 | public function getFileProps( array $params ) { |
227 | return $this->translateSrcParams( __FUNCTION__, $params ); |
228 | } |
229 | |
230 | public function streamFile( array $params ) { |
231 | // The stream methods use the file extension to determine the |
232 | // Content-Type (as MediaWiki should already validate it on upload). |
233 | // The translated SHA1 path has no extension, so this needs to use |
234 | // the untranslated path extension. |
235 | $type = StreamFile::contentTypeFromPath( $params['src'] ); |
236 | if ( $type && $type != 'unknown/unknown' ) { |
237 | $params['headers'][] = "Content-type: $type"; |
238 | } |
239 | return $this->translateSrcParams( __FUNCTION__, $params ); |
240 | } |
241 | |
242 | public function getFileContentsMulti( array $params ) { |
243 | return $this->translateArrayResults( __FUNCTION__, $params ); |
244 | } |
245 | |
246 | public function getLocalReferenceMulti( array $params ) { |
247 | return $this->translateArrayResults( __FUNCTION__, $params ); |
248 | } |
249 | |
250 | public function getLocalCopyMulti( array $params ) { |
251 | return $this->translateArrayResults( __FUNCTION__, $params ); |
252 | } |
253 | |
254 | public function getFileHttpUrl( array $params ) { |
255 | return $this->translateSrcParams( __FUNCTION__, $params ); |
256 | } |
257 | |
258 | public function addShellboxInputFile( BoxedCommand $command, string $boxedName, |
259 | array $params |
260 | ) { |
261 | $params['src'] = $this->getBackendPath( $params['src'], !empty( $params['latest'] ) ); |
262 | return $this->backend->addShellboxInputFile( $command, $boxedName, $params ); |
263 | } |
264 | |
265 | public function directoryExists( array $params ) { |
266 | return $this->backend->directoryExists( $params ); |
267 | } |
268 | |
269 | public function getDirectoryList( array $params ) { |
270 | return $this->backend->getDirectoryList( $params ); |
271 | } |
272 | |
273 | public function getFileList( array $params ) { |
274 | return $this->backend->getFileList( $params ); |
275 | } |
276 | |
277 | public function getFeatures() { |
278 | return $this->backend->getFeatures(); |
279 | } |
280 | |
281 | public function clearCache( ?array $paths = null ) { |
282 | $this->backend->clearCache( null ); // clear all |
283 | } |
284 | |
285 | public function preloadCache( array $paths ) { |
286 | $paths = $this->getBackendPaths( $paths ); |
287 | $this->backend->preloadCache( $paths ); |
288 | } |
289 | |
290 | public function preloadFileStat( array $params ) { |
291 | return $this->translateSrcParams( __FUNCTION__, $params ); |
292 | } |
293 | |
294 | public function getScopedLocksForOps( array $ops, StatusValue $status ) { |
295 | return $this->backend->getScopedLocksForOps( $ops, $status ); |
296 | } |
297 | |
298 | /** |
299 | * Get the ultimate original storage path for a file |
300 | * |
301 | * Use this when putting a new file into the system |
302 | * |
303 | * @param string $sha1 File SHA-1 base36 |
304 | * @return string |
305 | */ |
306 | public function getPathForSHA1( $sha1 ) { |
307 | if ( strlen( $sha1 ) < 3 ) { |
308 | throw new InvalidArgumentException( "Invalid file SHA-1." ); |
309 | } |
310 | return $this->backend->getContainerStoragePath( "{$this->repoName}-original" ) . |
311 | "/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}"; |
312 | } |
313 | |
314 | /** |
315 | * Get a connection to the repo file registry DB |
316 | * |
317 | * @param int $index |
318 | * @return IDatabase |
319 | */ |
320 | protected function getDB( $index ) { |
321 | if ( !isset( $this->dbs[$index] ) ) { |
322 | $func = $this->dbHandleFunc; |
323 | $this->dbs[$index] = $func( $index ); |
324 | } |
325 | return $this->dbs[$index]; |
326 | } |
327 | |
328 | /** |
329 | * Translates paths found in the "src" or "srcs" keys of a params array |
330 | * |
331 | * @param string $function |
332 | * @param array $params |
333 | * @return mixed |
334 | */ |
335 | protected function translateSrcParams( $function, array $params ) { |
336 | $latest = !empty( $params['latest'] ); |
337 | |
338 | if ( isset( $params['src'] ) ) { |
339 | $params['src'] = $this->getBackendPath( $params['src'], $latest ); |
340 | } |
341 | |
342 | if ( isset( $params['srcs'] ) ) { |
343 | $params['srcs'] = $this->getBackendPaths( $params['srcs'], $latest ); |
344 | } |
345 | |
346 | return $this->backend->$function( $params ); |
347 | } |
348 | |
349 | /** |
350 | * Translates paths when the backend function returns results keyed by paths |
351 | * |
352 | * @param string $function |
353 | * @param array $params |
354 | * @return array |
355 | */ |
356 | protected function translateArrayResults( $function, array $params ) { |
357 | $origPaths = $params['srcs']; |
358 | $params['srcs'] = $this->getBackendPaths( $params['srcs'], !empty( $params['latest'] ) ); |
359 | $pathMap = array_combine( $params['srcs'], $origPaths ); |
360 | |
361 | $results = $this->backend->$function( $params ); |
362 | |
363 | $contents = []; |
364 | foreach ( $results as $path => $result ) { |
365 | $contents[$pathMap[$path]] = $result; |
366 | } |
367 | |
368 | return $contents; |
369 | } |
370 | |
371 | /** |
372 | * Translate legacy "title" source paths to their "sha1" counterparts |
373 | * |
374 | * This leaves destination paths alone since we don't want those to mutate |
375 | * |
376 | * @param array[] $ops |
377 | * @return array[] |
378 | */ |
379 | protected function mungeOpPaths( array $ops ) { |
380 | // Ops that use 'src' and do not mutate core file data there |
381 | static $srcRefOps = [ 'store', 'copy', 'describe' ]; |
382 | foreach ( $ops as &$op ) { |
383 | if ( isset( $op['src'] ) && in_array( $op['op'], $srcRefOps ) ) { |
384 | $op['src'] = $this->getBackendPath( $op['src'], true ); |
385 | } |
386 | if ( isset( $op['srcs'] ) ) { |
387 | $op['srcs'] = $this->getBackendPaths( $op['srcs'], true ); |
388 | } |
389 | } |
390 | return $ops; |
391 | } |
392 | } |