Merge "Increase $wgSVGMaxSize to 5120 pixels wide (previously 2048)."
[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 Array Map of (index => 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( array( $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 $origBasePath = $this->backend->getContainerStoragePath( "{$this->repoName}-original" );
99
100 // @TODO: batching
101 $resolved = array();
102 foreach ( $paths as $i => $path ) {
103 if ( !$latest && $this->resolvedPathCache->has( $path, 'target', 10 ) ) {
104 $resolved[$i] = $this->resolvedPathCache->get( $path, 'target' );
105 continue;
106 }
107
108 list( , $container, $rel ) = FileBackend::splitStoragePath( $path );
109
110 if ( $container === "{$this->repoName}-public" ) {
111 $name = basename( $path );
112 if ( strpos( $path, '!' ) !== false ) {
113 $sha1 = $db->selectField( 'oldimage', 'oi_sha1',
114 array( 'oi_archive_name' => $name ),
115 __METHOD__
116 );
117 } else {
118 $sha1 = $db->selectField( 'image', 'img_sha1',
119 array( 'img_name' => $name ),
120 __METHOD__
121 );
122 }
123 if ( !strlen( $sha1 ) ) {
124 $resolved[$i] = $path; // give up
125 continue;
126 }
127 $resolved[$i] = $this->getPathForSHA1( $sha1 );
128 $this->resolvedPathCache->set( $path, 'target', $resolved[$i] );
129 } elseif ( $container === "{$this->repoName}-deleted" ) {
130 $name = basename( $path ); // <hash>.<ext>
131 $sha1 = substr( $name, 0, strpos( $name, '.' ) ); // ignore extension
132 $resolved[$i] = $this->getPathForSHA1( $sha1 );
133 $this->resolvedPathCache->set( $path, 'target', $resolved[$i] );
134 } else {
135 $resolved[$i] = $path;
136 }
137 }
138
139 $res = array();
140 foreach ( $paths as $i => $path ) {
141 $res[$i] = $resolved[$i];
142 }
143
144 return $res;
145 }
146
147 protected function doOperationsInternal( array $ops, array $opts ) {
148 return $this->backend->doOperationsInternal( $this->mungeOpPaths( $ops ), $opts );
149 }
150
151 protected function doQuickOperationsInternal( array $ops ) {
152 return $this->backend->doQuickOperationsInternal( $this->mungeOpPaths( $ops ) );
153 }
154
155 protected function doPrepare( array $params ) {
156 return $this->backend->doPrepare( $params );
157 }
158
159 protected function doSecure( array $params ) {
160 return $this->backend->doSecure( $params );
161 }
162
163 protected function doPublish( array $params ) {
164 return $this->backend->doPublish( $params );
165 }
166
167 protected function doClean( array $params ) {
168 return $this->backend->doClean( $params );
169 }
170
171 public function concatenate( array $params ) {
172 return $this->translateSrcParams( __FUNCTION__, $params );
173 }
174
175 public function fileExists( array $params ) {
176 return $this->translateSrcParams( __FUNCTION__, $params );
177 }
178
179 public function getFileTimestamp( array $params ) {
180 return $this->translateSrcParams( __FUNCTION__, $params );
181 }
182
183 public function getFileSize( array $params ) {
184 return $this->translateSrcParams( __FUNCTION__, $params );
185 }
186
187 public function getFileStat( array $params ) {
188 return $this->translateSrcParams( __FUNCTION__, $params );
189 }
190
191 public function getFileXAttributes( array $params ) {
192 return $this->translateSrcParams( __FUNCTION__, $params );
193 }
194
195 public function getFileSha1Base36( array $params ) {
196 return $this->translateSrcParams( __FUNCTION__, $params );
197 }
198
199 public function getFileProps( array $params ) {
200 return $this->translateSrcParams( __FUNCTION__, $params );
201 }
202
203 public function streamFile( array $params ) {
204 // The stream methods use the file extension to determine the
205 // Content-Type (as MediaWiki should already validate it on upload).
206 // The translated SHA1 path has no extension, so this needs to use
207 // the untranslated path extension.
208 $type = StreamFile::contentTypeFromPath( $params['src'] );
209 if ( $type && $type != 'unknown/unknown' ) {
210 $params['headers'][] = "Content-type: $type";
211 }
212 return $this->translateSrcParams( __FUNCTION__, $params );
213 }
214
215 public function getFileContentsMulti( array $params ) {
216 return $this->translateArrayResults( __FUNCTION__, $params );
217 }
218
219 public function getLocalReferenceMulti( array $params ) {
220 return $this->translateArrayResults( __FUNCTION__, $params );
221 }
222
223 public function getLocalCopyMulti( array $params ) {
224 return $this->translateArrayResults( __FUNCTION__, $params );
225 }
226
227 public function getFileHttpUrl( array $params ) {
228 return $this->translateSrcParams( __FUNCTION__, $params );
229 }
230
231 public function directoryExists( array $params ) {
232 return $this->backend->directoryExists( $params );
233 }
234
235 public function getDirectoryList( array $params ) {
236 return $this->backend->getDirectoryList( $params );
237 }
238
239 public function getFileList( array $params ) {
240 return $this->backend->getFileList( $params );
241 }
242
243 public function getFeatures() {
244 return $this->backend->getFeatures();
245 }
246
247 public function clearCache( array $paths = null ) {
248 $this->backend->clearCache( null ); // clear all
249 }
250
251 public function preloadCache( array $paths ) {
252 $paths = $this->getBackendPaths( $paths );
253 $this->backend->preloadCache( $paths );
254 }
255
256 public function preloadFileStat( array $params ) {
257 return $this->translateSrcParams( __FUNCTION__, $params );
258 }
259
260 public function getScopedLocksForOps( array $ops, Status $status ) {
261 return $this->backend->getScopedFileLocks( $ops, $status );
262 }
263
264 /**
265 * Get the ultimate original storage path for a file
266 *
267 * Use this when putting a new file into the system
268 *
269 * @param string $sha1 File SHA-1 base36
270 * @return string
271 */
272 public function getPathForSHA1( $sha1 ) {
273 if ( strlen( $sha1 ) < 3 ) {
274 throw new MWException( "Invalid file SHA-1." );
275 }
276 return $this->backend->getContainerStoragePath( "{$this->repoName}-original" ) .
277 "/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
278 }
279
280 /**
281 * Get a connection to the repo file registry DB
282 *
283 * @param integer $index
284 * @return DBConnRef
285 */
286 protected function getDB( $index ) {
287 if ( !isset( $this->db[$index] ) ) {
288 $func = $this->dbHandleFunc;
289 $this->db[$index] = $func( $index );
290 }
291 return $this->db[$index];
292 }
293
294 /**
295 * Translates paths found in the "src" or "srcs" keys of a params array
296 *
297 * @param string $function
298 * @param array $params
299 */
300 protected function translateSrcParams( $function, array $params ) {
301 $latest = !empty( $params['latest'] );
302
303 if ( isset( $params['src'] ) ) {
304 $params['src'] = $this->getBackendPath( $params['src'], $latest );
305 }
306
307 if ( isset( $params['srcs'] ) ) {
308 $params['srcs'] = $this->getBackendPaths( $params['srcs'], $latest );
309 }
310
311 return $this->backend->$function( $params );
312 }
313
314 /**
315 * Translates paths when the backend function returns results keyed by paths
316 *
317 * @param string $function
318 * @param array $params
319 * @return array
320 */
321 protected function translateArrayResults( $function, array $params ) {
322 $origPaths = $params['srcs'];
323 $params['srcs'] = $this->getBackendPaths( $params['srcs'], !empty( $params['latest'] ) );
324 $pathMap = array_combine( $params['srcs'], $origPaths );
325
326 $results = $this->backend->$function( $params );
327
328 $contents = array();
329 foreach ( $results as $path => $result ) {
330 $contents[$pathMap[$path]] = $result;
331 }
332
333 return $contents;
334 }
335
336 /**
337 * Translate legacy "title" source paths to their "sha1" counterparts
338 *
339 * This leaves destination paths alone since we don't want those to mutate
340 *
341 * @param array $ops
342 * @return array
343 */
344 protected function mungeOpPaths( array $ops ) {
345 // Ops that use 'src' and do not mutate core file data there
346 static $srcRefOps = array( 'store', 'copy', 'describe' );
347 foreach ( $ops as &$op ) {
348 if ( isset( $op['src'] ) && in_array( $op['op'], $srcRefOps ) ) {
349 $op['src'] = $this->getBackendPath( $op['src'], true );
350 }
351 if ( isset( $op['srcs'] ) ) {
352 $op['srcs'] = $this->getBackendPaths( $op['srcs'], true );
353 }
354 }
355 return $ops;
356 }
357 }