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