MediaWiki  master
FileBackendDBRepoWrapper.php
Go to the documentation of this file.
1 <?php
22 
39  protected $backend;
41  protected $repoName;
43  protected $dbHandleFunc;
45  protected $resolvedPathCache;
47  protected $dbs;
48 
49  public function __construct( array $config ) {
51  $backend = $config['backend'];
52  $config['name'] = $backend->getName();
53  $config['domainId'] = $backend->getDomainId();
54  parent::__construct( $config );
55  $this->backend = $config['backend'];
56  $this->repoName = $config['repoName'];
57  $this->dbHandleFunc = $config['dbHandleFactory'];
58  $this->resolvedPathCache = new MapCacheLRU( 100 );
59  }
60 
66  public function getInternalBackend() {
67  return $this->backend;
68  }
69 
80  public function getBackendPath( $path, $latest = true ) {
81  $paths = $this->getBackendPaths( [ $path ], $latest );
82  return current( $paths );
83  }
84 
95  public function getBackendPaths( array $paths, $latest = true ) {
96  $db = $this->getDB( $latest ? DB_PRIMARY : DB_REPLICA );
97 
98  // @TODO: batching
99  $resolved = [];
100  foreach ( $paths as $i => $path ) {
101  if ( !$latest && $this->resolvedPathCache->hasField( $path, 'target', 10 ) ) {
102  $resolved[$i] = $this->resolvedPathCache->getField( $path, 'target' );
103  continue;
104  }
105 
106  [ , $container ] = FileBackend::splitStoragePath( $path );
107 
108  if ( $container === "{$this->repoName}-public" ) {
109  $name = basename( $path );
110  if ( str_contains( $path, '!' ) ) {
111  $sha1 = $db->selectField( 'oldimage', 'oi_sha1',
112  [ 'oi_archive_name' => $name ],
113  __METHOD__
114  );
115  } else {
116  $sha1 = $db->selectField( 'image', 'img_sha1',
117  [ 'img_name' => $name ],
118  __METHOD__
119  );
120  }
121  if ( $sha1 === null || !strlen( $sha1 ) ) {
122  $resolved[$i] = $path; // give up
123  continue;
124  }
125  $resolved[$i] = $this->getPathForSHA1( $sha1 );
126  $this->resolvedPathCache->setField( $path, 'target', $resolved[$i] );
127  } elseif ( $container === "{$this->repoName}-deleted" ) {
128  $name = basename( $path ); // <hash>.<ext>
129  $sha1 = substr( $name, 0, strpos( $name, '.' ) ); // ignore extension
130  $resolved[$i] = $this->getPathForSHA1( $sha1 );
131  $this->resolvedPathCache->setField( $path, 'target', $resolved[$i] );
132  } else {
133  $resolved[$i] = $path;
134  }
135  }
136 
137  $res = [];
138  foreach ( $paths as $i => $path ) {
139  $res[$i] = $resolved[$i];
140  }
141 
142  return $res;
143  }
144 
145  protected function doOperationsInternal( array $ops, array $opts ) {
146  return $this->backend->doOperationsInternal( $this->mungeOpPaths( $ops ), $opts );
147  }
148 
149  protected function doQuickOperationsInternal( array $ops, array $opts ) {
150  return $this->backend->doQuickOperationsInternal( $this->mungeOpPaths( $ops ), $opts );
151  }
152 
153  protected function doPrepare( array $params ) {
154  return $this->backend->doPrepare( $params );
155  }
156 
157  protected function doSecure( array $params ) {
158  return $this->backend->doSecure( $params );
159  }
160 
161  protected function doPublish( array $params ) {
162  return $this->backend->doPublish( $params );
163  }
164 
165  protected function doClean( array $params ) {
166  return $this->backend->doClean( $params );
167  }
168 
169  public function concatenate( array $params ) {
170  return $this->translateSrcParams( __FUNCTION__, $params );
171  }
172 
173  public function fileExists( array $params ) {
174  return $this->translateSrcParams( __FUNCTION__, $params );
175  }
176 
177  public function getFileTimestamp( array $params ) {
178  return $this->translateSrcParams( __FUNCTION__, $params );
179  }
180 
181  public function getFileSize( array $params ) {
182  return $this->translateSrcParams( __FUNCTION__, $params );
183  }
184 
185  public function getFileStat( array $params ) {
186  return $this->translateSrcParams( __FUNCTION__, $params );
187  }
188 
189  public function getFileXAttributes( array $params ) {
190  return $this->translateSrcParams( __FUNCTION__, $params );
191  }
192 
193  public function getFileSha1Base36( array $params ) {
194  return $this->translateSrcParams( __FUNCTION__, $params );
195  }
196 
197  public function getFileProps( array $params ) {
198  return $this->translateSrcParams( __FUNCTION__, $params );
199  }
200 
201  public function streamFile( array $params ) {
202  // The stream methods use the file extension to determine the
203  // Content-Type (as MediaWiki should already validate it on upload).
204  // The translated SHA1 path has no extension, so this needs to use
205  // the untranslated path extension.
206  $type = StreamFile::contentTypeFromPath( $params['src'] );
207  if ( $type && $type != 'unknown/unknown' ) {
208  $params['headers'][] = "Content-type: $type";
209  }
210  return $this->translateSrcParams( __FUNCTION__, $params );
211  }
212 
213  public function getFileContentsMulti( array $params ) {
214  return $this->translateArrayResults( __FUNCTION__, $params );
215  }
216 
217  public function getLocalReferenceMulti( array $params ) {
218  return $this->translateArrayResults( __FUNCTION__, $params );
219  }
220 
221  public function getLocalCopyMulti( array $params ) {
222  return $this->translateArrayResults( __FUNCTION__, $params );
223  }
224 
225  public function getFileHttpUrl( array $params ) {
226  return $this->translateSrcParams( __FUNCTION__, $params );
227  }
228 
229  public function directoryExists( array $params ) {
230  return $this->backend->directoryExists( $params );
231  }
232 
233  public function getDirectoryList( array $params ) {
234  return $this->backend->getDirectoryList( $params );
235  }
236 
237  public function getFileList( array $params ) {
238  return $this->backend->getFileList( $params );
239  }
240 
241  public function getFeatures() {
242  return $this->backend->getFeatures();
243  }
244 
245  public function clearCache( array $paths = null ) {
246  $this->backend->clearCache( null ); // clear all
247  }
248 
249  public function preloadCache( array $paths ) {
250  $paths = $this->getBackendPaths( $paths );
251  $this->backend->preloadCache( $paths );
252  }
253 
254  public function preloadFileStat( array $params ) {
255  return $this->translateSrcParams( __FUNCTION__, $params );
256  }
257 
258  public function getScopedLocksForOps( array $ops, StatusValue $status ) {
259  return $this->backend->getScopedLocksForOps( $ops, $status );
260  }
261 
270  public function getPathForSHA1( $sha1 ) {
271  if ( strlen( $sha1 ) < 3 ) {
272  throw new InvalidArgumentException( "Invalid file SHA-1." );
273  }
274  return $this->backend->getContainerStoragePath( "{$this->repoName}-original" ) .
275  "/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
276  }
277 
284  protected function getDB( $index ) {
285  if ( !isset( $this->dbs[$index] ) ) {
286  $func = $this->dbHandleFunc;
287  $this->dbs[$index] = $func( $index );
288  }
289  return $this->dbs[$index];
290  }
291 
299  protected function translateSrcParams( $function, array $params ) {
300  $latest = !empty( $params['latest'] );
301 
302  if ( isset( $params['src'] ) ) {
303  $params['src'] = $this->getBackendPath( $params['src'], $latest );
304  }
305 
306  if ( isset( $params['srcs'] ) ) {
307  $params['srcs'] = $this->getBackendPaths( $params['srcs'], $latest );
308  }
309 
310  return $this->backend->$function( $params );
311  }
312 
320  protected function translateArrayResults( $function, array $params ) {
321  $origPaths = $params['srcs'];
322  $params['srcs'] = $this->getBackendPaths( $params['srcs'], !empty( $params['latest'] ) );
323  $pathMap = array_combine( $params['srcs'], $origPaths );
324 
325  $results = $this->backend->$function( $params );
326 
327  $contents = [];
328  foreach ( $results as $path => $result ) {
329  $contents[$pathMap[$path]] = $result;
330  }
331 
332  return $contents;
333  }
334 
343  protected function mungeOpPaths( array $ops ) {
344  // Ops that use 'src' and do not mutate core file data there
345  static $srcRefOps = [ 'store', 'copy', 'describe' ];
346  foreach ( $ops as &$op ) {
347  if ( isset( $op['src'] ) && in_array( $op['op'], $srcRefOps ) ) {
348  $op['src'] = $this->getBackendPath( $op['src'], true );
349  }
350  if ( isset( $op['srcs'] ) ) {
351  $op['srcs'] = $this->getBackendPaths( $op['srcs'], true );
352  }
353  }
354  return $ops;
355  }
356 }
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.
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.
clearCache(array $paths=null)
Invalidate any in-process file stat and property cache.
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.
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.
Base class for all file backend classes (including multi-write backends).
Definition: FileBackend.php:99
string $name
Unique backend name.
getDomainId()
Get the domain identifier used for this backend (possibly empty).
static splitStoragePath( $storagePath)
Split a storage path into a backend name, a container name, and a relative file path.
getName()
Get the unique backend name.
Handles a simple LRU key/value map with a maximum number of entries.
Definition: MapCacheLRU.php:36
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: StatusValue.php:46
static contentTypeFromPath( $filename, $safe=true)
Determine the file type of a file based on the path.
Definition: StreamFile.php:76
Helper class used for automatically marking an IDatabase connection as reusable (once it no longer ma...
Definition: DBConnRef.php:29
const DB_REPLICA
Definition: defines.php:26
const DB_PRIMARY
Definition: defines.php:28