MediaWiki 1.43.1
FileBackendDBRepoWrapper.php
Go to the documentation of this file.
1<?php
22use Shellbox\Command\BoxedCommand;
25
42 protected $backend;
44 protected $repoName;
46 protected $dbHandleFunc;
50 protected $dbs;
51
52 public function __construct( array $config ) {
54 $backend = $config['backend'];
55 $config['name'] = $backend->getName();
56 $config['domainId'] = $backend->getDomainId();
57 parent::__construct( $config );
58 $this->backend = $config['backend'];
59 $this->repoName = $config['repoName'];
60 $this->dbHandleFunc = $config['dbHandleFactory'];
61 $this->resolvedPathCache = new MapCacheLRU( 100 );
62 }
63
69 public function getInternalBackend() {
70 return $this->backend;
71 }
72
83 public function getBackendPath( $path, $latest = true ) {
84 $paths = $this->getBackendPaths( [ $path ], $latest );
85 return current( $paths );
86 }
87
98 public function getBackendPaths( array $paths, $latest = true ) {
99 $db = $this->getDB( $latest ? DB_PRIMARY : DB_REPLICA );
100
101 // @TODO: batching
102 $resolved = [];
103 foreach ( $paths as $i => $path ) {
104 if ( !$latest && $this->resolvedPathCache->hasField( $path, 'target', 10 ) ) {
105 $resolved[$i] = $this->resolvedPathCache->getField( $path, 'target' );
106 continue;
107 }
108
109 [ , $container ] = FileBackend::splitStoragePath( $path );
110
111 if ( $container === "{$this->repoName}-public" ) {
112 $name = basename( $path );
113 if ( str_contains( $path, '!' ) ) {
114 $sha1 = $db->newSelectQueryBuilder()
115 ->select( 'oi_sha1' )
116 ->from( 'oldimage' )
117 ->where( [ 'oi_archive_name' => $name ] )
118 ->caller( __METHOD__ )->fetchField();
119 } else {
120 $sha1 = $db->newSelectQueryBuilder()
121 ->select( 'img_sha1' )
122 ->from( 'image' )
123 ->where( [ 'img_name' => $name ] )
124 ->caller( __METHOD__ )->fetchField();
125 }
126 if ( $sha1 === null || !strlen( $sha1 ) ) {
127 $resolved[$i] = $path; // give up
128 continue;
129 }
130 $resolved[$i] = $this->getPathForSHA1( $sha1 );
131 $this->resolvedPathCache->setField( $path, 'target', $resolved[$i] );
132 } elseif ( $container === "{$this->repoName}-deleted" ) {
133 $name = basename( $path ); // <hash>.<ext>
134 $sha1 = substr( $name, 0, strpos( $name, '.' ) ); // ignore extension
135 $resolved[$i] = $this->getPathForSHA1( $sha1 );
136 $this->resolvedPathCache->setField( $path, 'target', $resolved[$i] );
137 } else {
138 $resolved[$i] = $path;
139 }
140 }
141
142 $res = [];
143 foreach ( $paths as $i => $path ) {
144 $res[$i] = $resolved[$i];
145 }
146
147 return $res;
148 }
149
150 protected function doOperationsInternal( array $ops, array $opts ) {
151 return $this->backend->doOperationsInternal( $this->mungeOpPaths( $ops ), $opts );
152 }
153
154 protected function doQuickOperationsInternal( array $ops, array $opts ) {
155 return $this->backend->doQuickOperationsInternal( $this->mungeOpPaths( $ops ), $opts );
156 }
157
158 protected function doPrepare( array $params ) {
159 return $this->backend->doPrepare( $params );
160 }
161
162 protected function doSecure( array $params ) {
163 return $this->backend->doSecure( $params );
164 }
165
166 protected function doPublish( array $params ) {
167 return $this->backend->doPublish( $params );
168 }
169
170 protected function doClean( array $params ) {
171 return $this->backend->doClean( $params );
172 }
173
174 public function concatenate( array $params ) {
175 return $this->translateSrcParams( __FUNCTION__, $params );
176 }
177
178 public function fileExists( array $params ) {
179 return $this->translateSrcParams( __FUNCTION__, $params );
180 }
181
182 public function getFileTimestamp( array $params ) {
183 return $this->translateSrcParams( __FUNCTION__, $params );
184 }
185
186 public function getFileSize( array $params ) {
187 return $this->translateSrcParams( __FUNCTION__, $params );
188 }
189
190 public function getFileStat( array $params ) {
191 return $this->translateSrcParams( __FUNCTION__, $params );
192 }
193
194 public function getFileXAttributes( array $params ) {
195 return $this->translateSrcParams( __FUNCTION__, $params );
196 }
197
198 public function getFileSha1Base36( array $params ) {
199 return $this->translateSrcParams( __FUNCTION__, $params );
200 }
201
202 public function getFileProps( array $params ) {
203 return $this->translateSrcParams( __FUNCTION__, $params );
204 }
205
206 public function streamFile( array $params ) {
207 // The stream methods use the file extension to determine the
208 // Content-Type (as MediaWiki should already validate it on upload).
209 // The translated SHA1 path has no extension, so this needs to use
210 // the untranslated path extension.
211 $type = StreamFile::contentTypeFromPath( $params['src'] );
212 if ( $type && $type != 'unknown/unknown' ) {
213 $params['headers'][] = "Content-type: $type";
214 }
215 return $this->translateSrcParams( __FUNCTION__, $params );
216 }
217
218 public function getFileContentsMulti( array $params ) {
219 return $this->translateArrayResults( __FUNCTION__, $params );
220 }
221
222 public function getLocalReferenceMulti( array $params ) {
223 return $this->translateArrayResults( __FUNCTION__, $params );
224 }
225
226 public function getLocalCopyMulti( array $params ) {
227 return $this->translateArrayResults( __FUNCTION__, $params );
228 }
229
230 public function getFileHttpUrl( array $params ) {
231 return $this->translateSrcParams( __FUNCTION__, $params );
232 }
233
234 public function addShellboxInputFile( BoxedCommand $command, string $boxedName,
235 array $params
236 ) {
237 $params['src'] = $this->getBackendPath( $params['src'], !empty( $params['latest'] ) );
238 return $this->backend->addShellboxInputFile( $command, $boxedName, $params );
239 }
240
241 public function directoryExists( array $params ) {
242 return $this->backend->directoryExists( $params );
243 }
244
245 public function getDirectoryList( array $params ) {
246 return $this->backend->getDirectoryList( $params );
247 }
248
249 public function getFileList( array $params ) {
250 return $this->backend->getFileList( $params );
251 }
252
253 public function getFeatures() {
254 return $this->backend->getFeatures();
255 }
256
257 public function clearCache( ?array $paths = null ) {
258 $this->backend->clearCache( null ); // clear all
259 }
260
261 public function preloadCache( array $paths ) {
262 $paths = $this->getBackendPaths( $paths );
263 $this->backend->preloadCache( $paths );
264 }
265
266 public function preloadFileStat( array $params ) {
267 return $this->translateSrcParams( __FUNCTION__, $params );
268 }
269
270 public function getScopedLocksForOps( array $ops, StatusValue $status ) {
271 return $this->backend->getScopedLocksForOps( $ops, $status );
272 }
273
282 public function getPathForSHA1( $sha1 ) {
283 if ( strlen( $sha1 ) < 3 ) {
284 throw new InvalidArgumentException( "Invalid file SHA-1." );
285 }
286 return $this->backend->getContainerStoragePath( "{$this->repoName}-original" ) .
287 "/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
288 }
289
296 protected function getDB( $index ) {
297 if ( !isset( $this->dbs[$index] ) ) {
298 $func = $this->dbHandleFunc;
299 $this->dbs[$index] = $func( $index );
300 }
301 return $this->dbs[$index];
302 }
303
311 protected function translateSrcParams( $function, array $params ) {
312 $latest = !empty( $params['latest'] );
313
314 if ( isset( $params['src'] ) ) {
315 $params['src'] = $this->getBackendPath( $params['src'], $latest );
316 }
317
318 if ( isset( $params['srcs'] ) ) {
319 $params['srcs'] = $this->getBackendPaths( $params['srcs'], $latest );
320 }
321
322 return $this->backend->$function( $params );
323 }
324
332 protected function translateArrayResults( $function, array $params ) {
333 $origPaths = $params['srcs'];
334 $params['srcs'] = $this->getBackendPaths( $params['srcs'], !empty( $params['latest'] ) );
335 $pathMap = array_combine( $params['srcs'], $origPaths );
336
337 $results = $this->backend->$function( $params );
338
339 $contents = [];
340 foreach ( $results as $path => $result ) {
341 $contents[$pathMap[$path]] = $result;
342 }
343
344 return $contents;
345 }
346
355 protected function mungeOpPaths( array $ops ) {
356 // Ops that use 'src' and do not mutate core file data there
357 static $srcRefOps = [ 'store', 'copy', 'describe' ];
358 foreach ( $ops as &$op ) {
359 if ( isset( $op['src'] ) && in_array( $op['op'], $srcRefOps ) ) {
360 $op['src'] = $this->getBackendPath( $op['src'], true );
361 }
362 if ( isset( $op['srcs'] ) ) {
363 $op['srcs'] = $this->getBackendPaths( $op['srcs'], true );
364 }
365 }
366 return $ops;
367 }
368}
array $params
The job parameters.
Proxy backend that manages file layout rewriting for FileRepo.
getFileSha1Base36(array $params)
Get a SHA-1 hash of the content of the file at a storage path in the backend.
__construct(array $config)
Create a new backend instance from configuration.
concatenate(array $params)
Concatenate a list of storage files into a single file system file.
streamFile(array $params)
Stream the content of the file at a storage path in the backend.
getScopedLocksForOps(array $ops, StatusValue $status)
Get an array of scoped locks needed for a batch of file operations.
getInternalBackend()
Get the underlying FileBackend that is being wrapped.
clearCache(?array $paths=null)
Invalidate any in-process file stat and property cache.
getLocalCopyMulti(array $params)
Like getLocalCopy() except it takes an array of storage paths and yields an order preserved-map of st...
doOperationsInternal(array $ops, array $opts)
fileExists(array $params)
Check if a file exists at a storage path in the backend.
getPathForSHA1( $sha1)
Get the ultimate original storage path for a file.
getDB( $index)
Get a connection to the repo file registry DB.
translateSrcParams( $function, array $params)
Translates paths found in the "src" or "srcs" keys of a params array.
mungeOpPaths(array $ops)
Translate legacy "title" source paths to their "sha1" counterparts.
getFileHttpUrl(array $params)
Return an HTTP URL to a given file that requires no authentication to use.
getLocalReferenceMulti(array $params)
Like getLocalReference() except it takes an array of storage paths and yields an order-preserved map ...
preloadCache(array $paths)
Preload persistent file stat cache and property cache into in-process cache.
getFileSize(array $params)
Get the size (bytes) of a file at a storage path in the backend.
getFileProps(array $params)
Get the properties of the content of the file at a storage path in the backend.
addShellboxInputFile(BoxedCommand $command, string $boxedName, array $params)
Add a file to a Shellbox command as an input file.
getFeatures()
Get the a bitfield of extra features supported by the backend medium.
translateArrayResults( $function, array $params)
Translates paths when the backend function returns results keyed by paths.
preloadFileStat(array $params)
Preload file stat information (concurrently if possible) into in-process cache.
directoryExists(array $params)
Check if a directory exists at a given storage path.
getBackendPaths(array $paths, $latest=true)
Translate legacy "title" paths to their "sha1" counterparts.
getFileList(array $params)
Get an iterator to list all stored files under a storage directory.
getFileStat(array $params)
Get quick information about a file at a storage path in the backend.
getFileTimestamp(array $params)
Get the last-modified timestamp of the file at a storage path.
getFileXAttributes(array $params)
Get metadata about a file at a storage path in the backend.
getFileContentsMulti(array $params)
Like getFileContents() except it takes an array of storage paths and returns an order preserved map o...
doQuickOperationsInternal(array $ops, array $opts)
getDirectoryList(array $params)
Get an iterator to list all directories under a storage directory.
getBackendPath( $path, $latest=true)
Translate a legacy "title" path to its "sha1" counterpart.
Store key-value entries in a size-limited in-memory LRU cache.
Functions related to the output of file content.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Base class for all file backend classes (including multi-write backends).
string $name
Unique backend name.
getDomainId()
Get the domain identifier used for this backend (possibly empty).
getName()
Get the unique backend name.
Interface to a relational database.
Definition IDatabase.php:48
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28