summaryrefslogtreecommitdiff
path: root/includes/filerepo/FileBackendDBRepoWrapper.php
blob: c83e5b1b916d54e87a6bc4dd875888b180b1d0a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
/**
 * Proxy backend that manages file layout rewriting for FileRepo.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup FileRepo
 * @ingroup FileBackend
 * @author Aaron Schulz
 */

/**
 * @brief Proxy backend that manages file layout rewriting for FileRepo.
 *
 * LocalRepo may be configured to store files under their title names or by SHA-1.
 * This acts as a shim in the later case, providing backwards compatability for
 * most callers. All "public"/"deleted" zone files actually go in an "original"
 * container and are never changed.
 *
 * This requires something like thumb_handler.php and img_auth.php for client viewing of files.
 *
 * @ingroup FileRepo
 * @ingroup FileBackend
 * @since 1.25
 */
class FileBackendDBRepoWrapper extends FileBackend {
	/** @var FileBackend */
	protected $backend;
	/** @var string */
	protected $repoName;
	/** @var Closure */
	protected $dbHandleFunc;
	/** @var ProcessCacheLRU */
	protected $resolvedPathCache;
	/** @var DBConnRef[] */
	protected $dbs;

	public function __construct( array $config ) {
		$config['name'] = $config['backend']->getName();
		$config['wikiId'] = $config['backend']->getWikiId();
		parent::__construct( $config );
		$this->backend = $config['backend'];
		$this->repoName = $config['repoName'];
		$this->dbHandleFunc = $config['dbHandleFactory'];
		$this->resolvedPathCache = new ProcessCacheLRU( 100 );
	}

	/**
	 * Get the underlying FileBackend that is being wrapped
	 *
	 * @return FileBackend
	 */
	public function getInternalBackend() {
		return $this->backend;
	}

	/**
	 * Translate a legacy "title" path to it's "sha1" counterpart
	 *
	 * E.g. mwstore://local-backend/local-public/a/ab/<name>.jpg
	 * => mwstore://local-backend/local-original/x/y/z/<sha1>.jpg
	 *
	 * @param string $path
	 * @param bool $latest
	 * @return string
	 */
	public function getBackendPath( $path, $latest = true ) {
		$paths = $this->getBackendPaths( array( $path ), $latest );
		return current( $paths );
	}

	/**
	 * Translate legacy "title" paths to their "sha1" counterparts
	 *
	 * E.g. mwstore://local-backend/local-public/a/ab/<name>.jpg
	 * => mwstore://local-backend/local-original/x/y/z/<sha1>.jpg
	 *
	 * @param array $paths
	 * @param bool $latest
	 * @return array Translated paths in same order
	 */
	public function getBackendPaths( array $paths, $latest = true ) {
		$db = $this->getDB( $latest ? DB_MASTER : DB_SLAVE );

		// @TODO: batching
		$resolved = array();
		foreach ( $paths as $i => $path ) {
			if ( !$latest && $this->resolvedPathCache->has( $path, 'target', 10 ) ) {
				$resolved[$i] = $this->resolvedPathCache->get( $path, 'target' );
				continue;
			}

			list( , $container ) = FileBackend::splitStoragePath( $path );

			if ( $container === "{$this->repoName}-public" ) {
				$name = basename( $path );
				if ( strpos( $path, '!' ) !== false ) {
					$sha1 = $db->selectField( 'oldimage', 'oi_sha1',
						array( 'oi_archive_name' => $name ),
						__METHOD__
					);
				} else {
					$sha1 = $db->selectField( 'image', 'img_sha1',
						array( 'img_name' => $name ),
						__METHOD__
					);
				}
				if ( !strlen( $sha1 ) ) {
					$resolved[$i] = $path; // give up
					continue;
				}
				$resolved[$i] = $this->getPathForSHA1( $sha1 );
				$this->resolvedPathCache->set( $path, 'target', $resolved[$i] );
			} elseif ( $container === "{$this->repoName}-deleted" ) {
				$name = basename( $path ); // <hash>.<ext>
				$sha1 = substr( $name, 0, strpos( $name, '.' ) ); // ignore extension
				$resolved[$i] = $this->getPathForSHA1( $sha1 );
				$this->resolvedPathCache->set( $path, 'target', $resolved[$i] );
			} else {
				$resolved[$i] = $path;
			}
		}

		$res = array();
		foreach ( $paths as $i => $path ) {
			$res[$i] = $resolved[$i];
		}

		return $res;
	}

	protected function doOperationsInternal( array $ops, array $opts ) {
		return $this->backend->doOperationsInternal( $this->mungeOpPaths( $ops ), $opts );
	}

	protected function doQuickOperationsInternal( array $ops ) {
		return $this->backend->doQuickOperationsInternal( $this->mungeOpPaths( $ops ) );
	}

	protected function doPrepare( array $params ) {
		return $this->backend->doPrepare( $params );
	}

	protected function doSecure( array $params ) {
		return $this->backend->doSecure( $params );
	}

	protected function doPublish( array $params ) {
		return $this->backend->doPublish( $params );
	}

	protected function doClean( array $params ) {
		return $this->backend->doClean( $params );
	}

	public function concatenate( array $params ) {
		return $this->translateSrcParams( __FUNCTION__, $params );
	}

	public function fileExists( array $params ) {
		return $this->translateSrcParams( __FUNCTION__, $params );
	}

	public function getFileTimestamp( array $params ) {
		return $this->translateSrcParams( __FUNCTION__, $params );
	}

	public function getFileSize( array $params ) {
		return $this->translateSrcParams( __FUNCTION__, $params );
	}

	public function getFileStat( array $params ) {
		return $this->translateSrcParams( __FUNCTION__, $params );
	}

	public function getFileXAttributes( array $params ) {
		return $this->translateSrcParams( __FUNCTION__, $params );
	}

	public function getFileSha1Base36( array $params ) {
		return $this->translateSrcParams( __FUNCTION__, $params );
	}

	public function getFileProps( array $params ) {
		return $this->translateSrcParams( __FUNCTION__, $params );
	}

	public function streamFile( array $params ) {
		// The stream methods use the file extension to determine the
		// Content-Type (as MediaWiki should already validate it on upload).
		// The translated SHA1 path has no extension, so this needs to use
		// the untranslated path extension.
		$type = StreamFile::contentTypeFromPath( $params['src'] );
		if ( $type && $type != 'unknown/unknown' ) {
			$params['headers'][] = "Content-type: $type";
		}
		return $this->translateSrcParams( __FUNCTION__, $params );
	}

	public function getFileContentsMulti( array $params ) {
		return $this->translateArrayResults( __FUNCTION__, $params );
	}

	public function getLocalReferenceMulti( array $params ) {
		return $this->translateArrayResults( __FUNCTION__, $params );
	}

	public function getLocalCopyMulti( array $params ) {
		return $this->translateArrayResults( __FUNCTION__, $params );
	}

	public function getFileHttpUrl( array $params ) {
		return $this->translateSrcParams( __FUNCTION__, $params );
	}

	public function directoryExists( array $params ) {
		return $this->backend->directoryExists( $params );
	}

	public function getDirectoryList( array $params ) {
		return $this->backend->getDirectoryList( $params );
	}

	public function getFileList( array $params ) {
		return $this->backend->getFileList( $params );
	}

	public function getFeatures() {
		return $this->backend->getFeatures();
	}

	public function clearCache( array $paths = null ) {
		$this->backend->clearCache( null ); // clear all
	}

	public function preloadCache( array $paths ) {
		$paths = $this->getBackendPaths( $paths );
		$this->backend->preloadCache( $paths );
	}

	public function preloadFileStat( array $params ) {
		return $this->translateSrcParams( __FUNCTION__, $params );
	}

	public function getScopedLocksForOps( array $ops, Status $status ) {
		return $this->backend->getScopedLocksForOps( $ops, $status );
	}

	/**
	 * Get the ultimate original storage path for a file
	 *
	 * Use this when putting a new file into the system
	 *
	 * @param string $sha1 File SHA-1 base36
	 * @return string
	 */
	public function getPathForSHA1( $sha1 ) {
		if ( strlen( $sha1 ) < 3 ) {
			throw new InvalidArgumentException( "Invalid file SHA-1." );
		}
		return $this->backend->getContainerStoragePath( "{$this->repoName}-original" ) .
			"/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
	}

	/**
	 * Get a connection to the repo file registry DB
	 *
	 * @param integer $index
	 * @return DBConnRef
	 */
	protected function getDB( $index ) {
		if ( !isset( $this->dbs[$index] ) ) {
			$func = $this->dbHandleFunc;
			$this->dbs[$index] = $func( $index );
		}
		return $this->dbs[$index];
	}

	/**
	 * Translates paths found in the "src" or "srcs" keys of a params array
	 *
	 * @param string $function
	 * @param array $params
	 */
	protected function translateSrcParams( $function, array $params ) {
		$latest = !empty( $params['latest'] );

		if ( isset( $params['src'] ) ) {
			$params['src'] = $this->getBackendPath( $params['src'], $latest );
		}

		if ( isset( $params['srcs'] ) ) {
			$params['srcs'] = $this->getBackendPaths( $params['srcs'], $latest );
		}

		return $this->backend->$function( $params );
	}

	/**
	 * Translates paths when the backend function returns results keyed by paths
	 *
	 * @param string $function
	 * @param array $params
	 * @return array
	 */
	protected function translateArrayResults( $function, array $params ) {
		$origPaths = $params['srcs'];
		$params['srcs'] = $this->getBackendPaths( $params['srcs'], !empty( $params['latest'] ) );
		$pathMap = array_combine( $params['srcs'], $origPaths );

		$results = $this->backend->$function( $params );

		$contents = array();
		foreach ( $results as $path => $result ) {
			$contents[$pathMap[$path]] = $result;
		}

		return $contents;
	}

	/**
	 * Translate legacy "title" source paths to their "sha1" counterparts
	 *
	 * This leaves destination paths alone since we don't want those to mutate
	 *
	 * @param array $ops
	 * @return array
	 */
	protected function mungeOpPaths( array $ops ) {
		// Ops that use 'src' and do not mutate core file data there
		static $srcRefOps = array( 'store', 'copy', 'describe' );
		foreach ( $ops as &$op ) {
			if ( isset( $op['src'] ) && in_array( $op['op'], $srcRefOps ) ) {
				$op['src'] = $this->getBackendPath( $op['src'], true );
			}
			if ( isset( $op['srcs'] ) ) {
				$op['srcs'] = $this->getBackendPaths( $op['srcs'], true );
			}
		}
		return $ops;
	}
}