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