MediaWiki  1.34.0
FileBackendDBRepoWrapper.php
Go to the documentation of this file.
1 <?php
26 
43  protected $backend;
45  protected $repoName;
47  protected $dbHandleFunc;
49  protected $resolvedPathCache;
51  protected $dbs;
52 
53  public function __construct( array $config ) {
55  $backend = $config['backend'];
56  $config['name'] = $backend->getName();
57  $config['domainId'] = $backend->getDomainId();
58  parent::__construct( $config );
59  $this->backend = $config['backend'];
60  $this->repoName = $config['repoName'];
61  $this->dbHandleFunc = $config['dbHandleFactory'];
62  $this->resolvedPathCache = new MapCacheLRU( 100 );
63  }
64 
70  public function getInternalBackend() {
71  return $this->backend;
72  }
73 
84  public function getBackendPath( $path, $latest = true ) {
85  $paths = $this->getBackendPaths( [ $path ], $latest );
86  return current( $paths );
87  }
88 
99  public function getBackendPaths( array $paths, $latest = true ) {
100  $db = $this->getDB( $latest ? DB_MASTER : DB_REPLICA );
101 
102  // @TODO: batching
103  $resolved = [];
104  foreach ( $paths as $i => $path ) {
105  if ( !$latest && $this->resolvedPathCache->hasField( $path, 'target', 10 ) ) {
106  $resolved[$i] = $this->resolvedPathCache->getField( $path, 'target' );
107  continue;
108  }
109 
110  list( , $container ) = FileBackend::splitStoragePath( $path );
111 
112  if ( $container === "{$this->repoName}-public" ) {
113  $name = basename( $path );
114  if ( strpos( $path, '!' ) !== false ) {
115  $sha1 = $db->selectField( 'oldimage', 'oi_sha1',
116  [ 'oi_archive_name' => $name ],
117  __METHOD__
118  );
119  } else {
120  $sha1 = $db->selectField( 'image', 'img_sha1',
121  [ 'img_name' => $name ],
122  __METHOD__
123  );
124  }
125  if ( !strlen( $sha1 ) ) {
126  $resolved[$i] = $path; // give up
127  continue;
128  }
129  $resolved[$i] = $this->getPathForSHA1( $sha1 );
130  $this->resolvedPathCache->setField( $path, 'target', $resolved[$i] );
131  } elseif ( $container === "{$this->repoName}-deleted" ) {
132  $name = basename( $path ); // <hash>.<ext>
133  $sha1 = substr( $name, 0, strpos( $name, '.' ) ); // ignore extension
134  $resolved[$i] = $this->getPathForSHA1( $sha1 );
135  $this->resolvedPathCache->setField( $path, 'target', $resolved[$i] );
136  } else {
137  $resolved[$i] = $path;
138  }
139  }
140 
141  $res = [];
142  foreach ( $paths as $i => $path ) {
143  $res[$i] = $resolved[$i];
144  }
145 
146  return $res;
147  }
148 
149  protected function doOperationsInternal( array $ops, array $opts ) {
150  return $this->backend->doOperationsInternal( $this->mungeOpPaths( $ops ), $opts );
151  }
152 
153  protected function doQuickOperationsInternal( array $ops ) {
154  return $this->backend->doQuickOperationsInternal( $this->mungeOpPaths( $ops ) );
155  }
156 
157  protected function doPrepare( array $params ) {
158  return $this->backend->doPrepare( $params );
159  }
160 
161  protected function doSecure( array $params ) {
162  return $this->backend->doSecure( $params );
163  }
164 
165  protected function doPublish( array $params ) {
166  return $this->backend->doPublish( $params );
167  }
168 
169  protected function doClean( array $params ) {
170  return $this->backend->doClean( $params );
171  }
172 
173  public function concatenate( array $params ) {
174  return $this->translateSrcParams( __FUNCTION__, $params );
175  }
176 
177  public function fileExists( array $params ) {
178  return $this->translateSrcParams( __FUNCTION__, $params );
179  }
180 
181  public function getFileTimestamp( array $params ) {
182  return $this->translateSrcParams( __FUNCTION__, $params );
183  }
184 
185  public function getFileSize( array $params ) {
186  return $this->translateSrcParams( __FUNCTION__, $params );
187  }
188 
189  public function getFileStat( array $params ) {
190  return $this->translateSrcParams( __FUNCTION__, $params );
191  }
192 
193  public function getFileXAttributes( array $params ) {
194  return $this->translateSrcParams( __FUNCTION__, $params );
195  }
196 
197  public function getFileSha1Base36( array $params ) {
198  return $this->translateSrcParams( __FUNCTION__, $params );
199  }
200 
201  public function getFileProps( array $params ) {
202  return $this->translateSrcParams( __FUNCTION__, $params );
203  }
204 
205  public function streamFile( array $params ) {
206  // The stream methods use the file extension to determine the
207  // Content-Type (as MediaWiki should already validate it on upload).
208  // The translated SHA1 path has no extension, so this needs to use
209  // the untranslated path extension.
210  $type = StreamFile::contentTypeFromPath( $params['src'] );
211  if ( $type && $type != 'unknown/unknown' ) {
212  $params['headers'][] = "Content-type: $type";
213  }
214  return $this->translateSrcParams( __FUNCTION__, $params );
215  }
216 
217  public function getFileContentsMulti( array $params ) {
218  return $this->translateArrayResults( __FUNCTION__, $params );
219  }
220 
221  public function getLocalReferenceMulti( array $params ) {
222  return $this->translateArrayResults( __FUNCTION__, $params );
223  }
224 
225  public function getLocalCopyMulti( array $params ) {
226  return $this->translateArrayResults( __FUNCTION__, $params );
227  }
228 
229  public function getFileHttpUrl( array $params ) {
230  return $this->translateSrcParams( __FUNCTION__, $params );
231  }
232 
233  public function directoryExists( array $params ) {
234  return $this->backend->directoryExists( $params );
235  }
236 
237  public function getDirectoryList( array $params ) {
238  return $this->backend->getDirectoryList( $params );
239  }
240 
241  public function getFileList( array $params ) {
242  return $this->backend->getFileList( $params );
243  }
244 
245  public function getFeatures() {
246  return $this->backend->getFeatures();
247  }
248 
249  public function clearCache( array $paths = null ) {
250  $this->backend->clearCache( null ); // clear all
251  }
252 
253  public function preloadCache( array $paths ) {
254  $paths = $this->getBackendPaths( $paths );
255  $this->backend->preloadCache( $paths );
256  }
257 
258  public function preloadFileStat( array $params ) {
259  return $this->translateSrcParams( __FUNCTION__, $params );
260  }
261 
262  public function getScopedLocksForOps( array $ops, StatusValue $status ) {
263  return $this->backend->getScopedLocksForOps( $ops, $status );
264  }
265 
274  public function getPathForSHA1( $sha1 ) {
275  if ( strlen( $sha1 ) < 3 ) {
276  throw new InvalidArgumentException( "Invalid file SHA-1." );
277  }
278  return $this->backend->getContainerStoragePath( "{$this->repoName}-original" ) .
279  "/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
280  }
281 
288  protected function getDB( $index ) {
289  if ( !isset( $this->dbs[$index] ) ) {
290  $func = $this->dbHandleFunc;
291  $this->dbs[$index] = $func( $index );
292  }
293  return $this->dbs[$index];
294  }
295 
303  protected function translateSrcParams( $function, array $params ) {
304  $latest = !empty( $params['latest'] );
305 
306  if ( isset( $params['src'] ) ) {
307  $params['src'] = $this->getBackendPath( $params['src'], $latest );
308  }
309 
310  if ( isset( $params['srcs'] ) ) {
311  $params['srcs'] = $this->getBackendPaths( $params['srcs'], $latest );
312  }
313 
314  return $this->backend->$function( $params );
315  }
316 
324  protected function translateArrayResults( $function, array $params ) {
325  $origPaths = $params['srcs'];
326  $params['srcs'] = $this->getBackendPaths( $params['srcs'], !empty( $params['latest'] ) );
327  $pathMap = array_combine( $params['srcs'], $origPaths );
328 
329  $results = $this->backend->$function( $params );
330 
331  $contents = [];
332  foreach ( $results as $path => $result ) {
333  $contents[$pathMap[$path]] = $result;
334  }
335 
336  return $contents;
337  }
338 
347  protected function mungeOpPaths( array $ops ) {
348  // Ops that use 'src' and do not mutate core file data there
349  static $srcRefOps = [ 'store', 'copy', 'describe' ];
350  foreach ( $ops as &$op ) {
351  if ( isset( $op['src'] ) && in_array( $op['op'], $srcRefOps ) ) {
352  $op['src'] = $this->getBackendPath( $op['src'], true );
353  }
354  if ( isset( $op['srcs'] ) ) {
355  $op['srcs'] = $this->getBackendPaths( $op['srcs'], true );
356  }
357  }
358  return $ops;
359  }
360 }
FileBackend\splitStoragePath
static splitStoragePath( $storagePath)
Split a storage path into a backend name, a container name, and a relative file path.
Definition: FileBackend.php:1520
FileBackendDBRepoWrapper\doSecure
doSecure(array $params)
Definition: FileBackendDBRepoWrapper.php:161
FileBackendDBRepoWrapper\translateSrcParams
translateSrcParams( $function, array $params)
Translates paths found in the "src" or "srcs" keys of a params array.
Definition: FileBackendDBRepoWrapper.php:303
FileBackendDBRepoWrapper\directoryExists
directoryExists(array $params)
Check if a directory exists at a given storage path.
Definition: FileBackendDBRepoWrapper.php:233
StatusValue
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: StatusValue.php:42
FileBackendDBRepoWrapper\$backend
FileBackend $backend
Definition: FileBackendDBRepoWrapper.php:43
FileBackendDBRepoWrapper\$dbs
DBConnRef[] $dbs
Definition: FileBackendDBRepoWrapper.php:51
FileBackend
Base class for all file backend classes (including multi-write backends).
Definition: FileBackend.php:94
StreamFile\contentTypeFromPath
static contentTypeFromPath( $filename, $safe=true)
Determine the file type of a file based on the path.
Definition: StreamFile.php:99
FileBackendDBRepoWrapper\preloadFileStat
preloadFileStat(array $params)
Preload file stat information (concurrently if possible) into in-process cache.
Definition: FileBackendDBRepoWrapper.php:258
FileBackendDBRepoWrapper\getLocalReferenceMulti
getLocalReferenceMulti(array $params)
Like getLocalReference() except it takes an array of storage paths and yields an order-preserved map ...
Definition: FileBackendDBRepoWrapper.php:221
FileBackend\getName
getName()
Get the unique backend name.
Definition: FileBackend.php:242
FileBackend\getDomainId
getDomainId()
Get the domain identifier used for this backend (possibly empty).
Definition: FileBackend.php:252
FileBackendDBRepoWrapper\getScopedLocksForOps
getScopedLocksForOps(array $ops, StatusValue $status)
Get an array of scoped locks needed for a batch of file operations.
Definition: FileBackendDBRepoWrapper.php:262
FileBackendDBRepoWrapper\getFeatures
getFeatures()
Get the a bitfield of extra features supported by the backend medium.
Definition: FileBackendDBRepoWrapper.php:245
FileBackendDBRepoWrapper\$dbHandleFunc
Closure $dbHandleFunc
Definition: FileBackendDBRepoWrapper.php:47
FileBackendDBRepoWrapper\doOperationsInternal
doOperationsInternal(array $ops, array $opts)
Definition: FileBackendDBRepoWrapper.php:149
FileBackendDBRepoWrapper\doClean
doClean(array $params)
Definition: FileBackendDBRepoWrapper.php:169
$res
$res
Definition: testCompression.php:52
FileBackendDBRepoWrapper\getDirectoryList
getDirectoryList(array $params)
Get an iterator to list all directories under a storage directory.
Definition: FileBackendDBRepoWrapper.php:237
FileBackendDBRepoWrapper\getFileStat
getFileStat(array $params)
Get quick information about a file at a storage path in the backend.
Definition: FileBackendDBRepoWrapper.php:189
FileBackendDBRepoWrapper\getFileHttpUrl
getFileHttpUrl(array $params)
Return an HTTP URL to a given file that requires no authentication to use.
Definition: FileBackendDBRepoWrapper.php:229
FileBackendDBRepoWrapper\getFileXAttributes
getFileXAttributes(array $params)
Get metadata about a file at a storage path in the backend.
Definition: FileBackendDBRepoWrapper.php:193
FileBackendDBRepoWrapper\translateArrayResults
translateArrayResults( $function, array $params)
Translates paths when the backend function returns results keyed by paths.
Definition: FileBackendDBRepoWrapper.php:324
FileBackendDBRepoWrapper\getFileList
getFileList(array $params)
Get an iterator to list all stored files under a storage directory.
Definition: FileBackendDBRepoWrapper.php:241
FileBackendDBRepoWrapper\getBackendPath
getBackendPath( $path, $latest=true)
Translate a legacy "title" path to it's "sha1" counterpart.
Definition: FileBackendDBRepoWrapper.php:84
FileBackendDBRepoWrapper\getDB
getDB( $index)
Get a connection to the repo file registry DB.
Definition: FileBackendDBRepoWrapper.php:288
FileBackendDBRepoWrapper\getInternalBackend
getInternalBackend()
Get the underlying FileBackend that is being wrapped.
Definition: FileBackendDBRepoWrapper.php:70
FileBackendDBRepoWrapper\__construct
__construct(array $config)
Create a new backend instance from configuration.
Definition: FileBackendDBRepoWrapper.php:53
MapCacheLRU
Handles a simple LRU key/value map with a maximum number of entries.
Definition: MapCacheLRU.php:37
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
FileBackendDBRepoWrapper\doQuickOperationsInternal
doQuickOperationsInternal(array $ops)
Definition: FileBackendDBRepoWrapper.php:153
FileBackendDBRepoWrapper\getFileProps
getFileProps(array $params)
Get the properties of the content of the file at a storage path in the backend.
Definition: FileBackendDBRepoWrapper.php:201
FileBackendDBRepoWrapper\getFileSize
getFileSize(array $params)
Get the size (bytes) of a file at a storage path in the backend.
Definition: FileBackendDBRepoWrapper.php:185
FileBackendDBRepoWrapper\concatenate
concatenate(array $params)
Concatenate a list of storage files into a single file system file.
Definition: FileBackendDBRepoWrapper.php:173
FileBackendDBRepoWrapper\fileExists
fileExists(array $params)
Check if a file exists at a storage path in the backend.
Definition: FileBackendDBRepoWrapper.php:177
FileBackendDBRepoWrapper\mungeOpPaths
mungeOpPaths(array $ops)
Translate legacy "title" source paths to their "sha1" counterparts.
Definition: FileBackendDBRepoWrapper.php:347
FileBackendDBRepoWrapper\getLocalCopyMulti
getLocalCopyMulti(array $params)
Like getLocalCopy() except it takes an array of storage paths and yields an order preserved-map of st...
Definition: FileBackendDBRepoWrapper.php:225
FileBackendDBRepoWrapper\doPublish
doPublish(array $params)
Definition: FileBackendDBRepoWrapper.php:165
Wikimedia\Rdbms\DBConnRef
Helper class used for automatically marking an IDatabase connection as reusable (once it no longer ma...
Definition: DBConnRef.php:29
FileBackendDBRepoWrapper\getPathForSHA1
getPathForSHA1( $sha1)
Get the ultimate original storage path for a file.
Definition: FileBackendDBRepoWrapper.php:274
$status
return $status
Definition: SyntaxHighlight.php:347
FileBackend\$name
string $name
Unique backend name.
Definition: FileBackend.php:96
$path
$path
Definition: NoLocalSettings.php:25
FileBackendDBRepoWrapper\$repoName
string $repoName
Definition: FileBackendDBRepoWrapper.php:45
FileBackendDBRepoWrapper\getFileContentsMulti
getFileContentsMulti(array $params)
Like getFileContents() except it takes an array of storage paths and returns an order preserved map o...
Definition: FileBackendDBRepoWrapper.php:217
FileBackendDBRepoWrapper\preloadCache
preloadCache(array $paths)
Preload persistent file stat cache and property cache into in-process cache.
Definition: FileBackendDBRepoWrapper.php:253
FileBackendDBRepoWrapper\clearCache
clearCache(array $paths=null)
Invalidate any in-process file stat and property cache.
Definition: FileBackendDBRepoWrapper.php:249
FileBackendDBRepoWrapper
Proxy backend that manages file layout rewriting for FileRepo.
Definition: FileBackendDBRepoWrapper.php:41
FileBackendDBRepoWrapper\doPrepare
doPrepare(array $params)
Definition: FileBackendDBRepoWrapper.php:157
FileBackendDBRepoWrapper\getFileTimestamp
getFileTimestamp(array $params)
Get the last-modified timestamp of the file at a storage path.
Definition: FileBackendDBRepoWrapper.php:181
FileBackendDBRepoWrapper\getBackendPaths
getBackendPaths(array $paths, $latest=true)
Translate legacy "title" paths to their "sha1" counterparts.
Definition: FileBackendDBRepoWrapper.php:99
FileBackendDBRepoWrapper\getFileSha1Base36
getFileSha1Base36(array $params)
Get a SHA-1 hash of the content of the file at a storage path in the backend.
Definition: FileBackendDBRepoWrapper.php:197
FileBackendDBRepoWrapper\$resolvedPathCache
MapCacheLRU $resolvedPathCache
Definition: FileBackendDBRepoWrapper.php:49
FileBackendDBRepoWrapper\streamFile
streamFile(array $params)
Stream the content of the file at a storage path in the backend.
Definition: FileBackendDBRepoWrapper.php:205
$type
$type
Definition: testCompression.php:48