WikitextContentHandlerTest expects the messages to be in English.
[lhc/web/wiklou.git] / includes / filebackend / FileBackendStore.php
1 <?php
2 /**
3 * Base class for all backends using particular storage medium.
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 FileBackend
22 * @author Aaron Schulz
23 */
24
25 /**
26 * @brief Base class for all backends using particular storage medium.
27 *
28 * This class defines the methods as abstract that subclasses must implement.
29 * Outside callers should *not* use functions with "Internal" in the name.
30 *
31 * The FileBackend operations are implemented using basic functions
32 * such as storeInternal(), copyInternal(), deleteInternal() and the like.
33 * This class is also responsible for path resolution and sanitization.
34 *
35 * @ingroup FileBackend
36 * @since 1.19
37 */
38 abstract class FileBackendStore extends FileBackend {
39 /** @var BagOStuff */
40 protected $memCache;
41 /** @var ProcessCacheLRU */
42 protected $cheapCache; // Map of paths to small (RAM/disk) cache items
43 /** @var ProcessCacheLRU */
44 protected $expensiveCache; // Map of paths to large (RAM/disk) cache items
45
46 /** @var Array Map of container names to sharding settings */
47 protected $shardViaHashLevels = array(); // (container name => config array)
48
49 protected $maxFileSize = 4294967296; // integer bytes (4GiB)
50
51 /**
52 * @see FileBackend::__construct()
53 *
54 * @param $config Array
55 */
56 public function __construct( array $config ) {
57 parent::__construct( $config );
58 $this->memCache = new EmptyBagOStuff(); // disabled by default
59 $this->cheapCache = new ProcessCacheLRU( 300 );
60 $this->expensiveCache = new ProcessCacheLRU( 5 );
61 }
62
63 /**
64 * Get the maximum allowable file size given backend
65 * medium restrictions and basic performance constraints.
66 * Do not call this function from places outside FileBackend and FileOp.
67 *
68 * @return integer Bytes
69 */
70 final public function maxFileSizeInternal() {
71 return $this->maxFileSize;
72 }
73
74 /**
75 * Check if a file can be created at a given storage path.
76 * FS backends should check if the parent directory exists and the file is writable.
77 * Backends using key/value stores should check if the container exists.
78 *
79 * @param $storagePath string
80 * @return bool
81 */
82 abstract public function isPathUsableInternal( $storagePath );
83
84 /**
85 * Create a file in the backend with the given contents.
86 * This will overwrite any file that exists at the destination.
87 * Do not call this function from places outside FileBackend and FileOp.
88 *
89 * $params include:
90 * - content : the raw file contents
91 * - dst : destination storage path
92 * - disposition : Content-Disposition header value for the destination
93 * - async : Status will be returned immediately if supported.
94 * If the status is OK, then its value field will be
95 * set to a FileBackendStoreOpHandle object.
96 *
97 * @param $params Array
98 * @return Status
99 */
100 final public function createInternal( array $params ) {
101 wfProfileIn( __METHOD__ );
102 wfProfileIn( __METHOD__ . '-' . $this->name );
103 if ( strlen( $params['content'] ) > $this->maxFileSizeInternal() ) {
104 $status = Status::newFatal( 'backend-fail-maxsize',
105 $params['dst'], $this->maxFileSizeInternal() );
106 } else {
107 $status = $this->doCreateInternal( $params );
108 $this->clearCache( array( $params['dst'] ) );
109 $this->deleteFileCache( $params['dst'] ); // persistent cache
110 }
111 wfProfileOut( __METHOD__ . '-' . $this->name );
112 wfProfileOut( __METHOD__ );
113 return $status;
114 }
115
116 /**
117 * @see FileBackendStore::createInternal()
118 */
119 abstract protected function doCreateInternal( array $params );
120
121 /**
122 * Store a file into the backend from a file on disk.
123 * This will overwrite any file that exists at the destination.
124 * Do not call this function from places outside FileBackend and FileOp.
125 *
126 * $params include:
127 * - src : source path on disk
128 * - dst : destination storage path
129 * - disposition : Content-Disposition header value for the destination
130 * - async : Status will be returned immediately if supported.
131 * If the status is OK, then its value field will be
132 * set to a FileBackendStoreOpHandle object.
133 *
134 * @param $params Array
135 * @return Status
136 */
137 final public function storeInternal( array $params ) {
138 wfProfileIn( __METHOD__ );
139 wfProfileIn( __METHOD__ . '-' . $this->name );
140 if ( filesize( $params['src'] ) > $this->maxFileSizeInternal() ) {
141 $status = Status::newFatal( 'backend-fail-maxsize',
142 $params['dst'], $this->maxFileSizeInternal() );
143 } else {
144 $status = $this->doStoreInternal( $params );
145 $this->clearCache( array( $params['dst'] ) );
146 $this->deleteFileCache( $params['dst'] ); // persistent cache
147 }
148 wfProfileOut( __METHOD__ . '-' . $this->name );
149 wfProfileOut( __METHOD__ );
150 return $status;
151 }
152
153 /**
154 * @see FileBackendStore::storeInternal()
155 */
156 abstract protected function doStoreInternal( array $params );
157
158 /**
159 * Copy a file from one storage path to another in the backend.
160 * This will overwrite any file that exists at the destination.
161 * Do not call this function from places outside FileBackend and FileOp.
162 *
163 * $params include:
164 * - src : source storage path
165 * - dst : destination storage path
166 * - disposition : Content-Disposition header value for the destination
167 * - async : Status will be returned immediately if supported.
168 * If the status is OK, then its value field will be
169 * set to a FileBackendStoreOpHandle object.
170 *
171 * @param $params Array
172 * @return Status
173 */
174 final public function copyInternal( array $params ) {
175 wfProfileIn( __METHOD__ );
176 wfProfileIn( __METHOD__ . '-' . $this->name );
177 $status = $this->doCopyInternal( $params );
178 $this->clearCache( array( $params['dst'] ) );
179 $this->deleteFileCache( $params['dst'] ); // persistent cache
180 wfProfileOut( __METHOD__ . '-' . $this->name );
181 wfProfileOut( __METHOD__ );
182 return $status;
183 }
184
185 /**
186 * @see FileBackendStore::copyInternal()
187 */
188 abstract protected function doCopyInternal( array $params );
189
190 /**
191 * Delete a file at the storage path.
192 * Do not call this function from places outside FileBackend and FileOp.
193 *
194 * $params include:
195 * - src : source storage path
196 * - ignoreMissingSource : do nothing if the source file does not exist
197 * - async : Status will be returned immediately if supported.
198 * If the status is OK, then its value field will be
199 * set to a FileBackendStoreOpHandle object.
200 *
201 * @param $params Array
202 * @return Status
203 */
204 final public function deleteInternal( array $params ) {
205 wfProfileIn( __METHOD__ );
206 wfProfileIn( __METHOD__ . '-' . $this->name );
207 $status = $this->doDeleteInternal( $params );
208 $this->clearCache( array( $params['src'] ) );
209 $this->deleteFileCache( $params['src'] ); // persistent cache
210 wfProfileOut( __METHOD__ . '-' . $this->name );
211 wfProfileOut( __METHOD__ );
212 return $status;
213 }
214
215 /**
216 * @see FileBackendStore::deleteInternal()
217 */
218 abstract protected function doDeleteInternal( array $params );
219
220 /**
221 * Move a file from one storage path to another in the backend.
222 * This will overwrite any file that exists at the destination.
223 * Do not call this function from places outside FileBackend and FileOp.
224 *
225 * $params include:
226 * - src : source storage path
227 * - dst : destination storage path
228 * - disposition : Content-Disposition header value for the destination
229 * - async : Status will be returned immediately if supported.
230 * If the status is OK, then its value field will be
231 * set to a FileBackendStoreOpHandle object.
232 *
233 * @param $params Array
234 * @return Status
235 */
236 final public function moveInternal( array $params ) {
237 wfProfileIn( __METHOD__ );
238 wfProfileIn( __METHOD__ . '-' . $this->name );
239 $status = $this->doMoveInternal( $params );
240 $this->clearCache( array( $params['src'], $params['dst'] ) );
241 $this->deleteFileCache( $params['src'] ); // persistent cache
242 $this->deleteFileCache( $params['dst'] ); // persistent cache
243 wfProfileOut( __METHOD__ . '-' . $this->name );
244 wfProfileOut( __METHOD__ );
245 return $status;
246 }
247
248 /**
249 * @see FileBackendStore::moveInternal()
250 * @return Status
251 */
252 protected function doMoveInternal( array $params ) {
253 unset( $params['async'] ); // two steps, won't work here :)
254 // Copy source to dest
255 $status = $this->copyInternal( $params );
256 if ( $status->isOK() ) {
257 // Delete source (only fails due to races or medium going down)
258 $status->merge( $this->deleteInternal( array( 'src' => $params['src'] ) ) );
259 $status->setResult( true, $status->value ); // ignore delete() errors
260 }
261 return $status;
262 }
263
264 /**
265 * No-op file operation that does nothing.
266 * Do not call this function from places outside FileBackend and FileOp.
267 *
268 * @param $params Array
269 * @return Status
270 */
271 final public function nullInternal( array $params ) {
272 return Status::newGood();
273 }
274
275 /**
276 * @see FileBackend::concatenate()
277 * @return Status
278 */
279 final public function concatenate( array $params ) {
280 wfProfileIn( __METHOD__ );
281 wfProfileIn( __METHOD__ . '-' . $this->name );
282 $status = Status::newGood();
283
284 // Try to lock the source files for the scope of this function
285 $scopeLockS = $this->getScopedFileLocks( $params['srcs'], LockManager::LOCK_UW, $status );
286 if ( $status->isOK() ) {
287 // Actually do the file concatenation...
288 $start_time = microtime( true );
289 $status->merge( $this->doConcatenate( $params ) );
290 $sec = microtime( true ) - $start_time;
291 if ( !$status->isOK() ) {
292 wfDebugLog( 'FileOperation', get_class( $this ) . " failed to concatenate " .
293 count( $params['srcs'] ) . " file(s) [$sec sec]" );
294 }
295 }
296
297 wfProfileOut( __METHOD__ . '-' . $this->name );
298 wfProfileOut( __METHOD__ );
299 return $status;
300 }
301
302 /**
303 * @see FileBackendStore::concatenate()
304 * @return Status
305 */
306 protected function doConcatenate( array $params ) {
307 $status = Status::newGood();
308 $tmpPath = $params['dst']; // convenience
309 unset( $params['latest'] ); // sanity
310
311 // Check that the specified temp file is valid...
312 wfSuppressWarnings();
313 $ok = ( is_file( $tmpPath ) && filesize( $tmpPath ) == 0 );
314 wfRestoreWarnings();
315 if ( !$ok ) { // not present or not empty
316 $status->fatal( 'backend-fail-opentemp', $tmpPath );
317 return $status;
318 }
319
320 // Get local FS versions of the chunks needed for the concatenation...
321 $fsFiles = $this->getLocalReferenceMulti( $params );
322 foreach ( $fsFiles as $path => &$fsFile ) {
323 if ( !$fsFile ) { // chunk failed to download?
324 $fsFile = $this->getLocalReference( array( 'src' => $path ) );
325 if ( !$fsFile ) { // retry failed?
326 $status->fatal( 'backend-fail-read', $path );
327 return $status;
328 }
329 }
330 }
331 unset( $fsFile ); // unset reference so we can reuse $fsFile
332
333 // Get a handle for the destination temp file
334 $tmpHandle = fopen( $tmpPath, 'ab' );
335 if ( $tmpHandle === false ) {
336 $status->fatal( 'backend-fail-opentemp', $tmpPath );
337 return $status;
338 }
339
340 // Build up the temp file using the source chunks (in order)...
341 foreach ( $fsFiles as $virtualSource => $fsFile ) {
342 // Get a handle to the local FS version
343 $sourceHandle = fopen( $fsFile->getPath(), 'rb' );
344 if ( $sourceHandle === false ) {
345 fclose( $tmpHandle );
346 $status->fatal( 'backend-fail-read', $virtualSource );
347 return $status;
348 }
349 // Append chunk to file (pass chunk size to avoid magic quotes)
350 if ( !stream_copy_to_stream( $sourceHandle, $tmpHandle ) ) {
351 fclose( $sourceHandle );
352 fclose( $tmpHandle );
353 $status->fatal( 'backend-fail-writetemp', $tmpPath );
354 return $status;
355 }
356 fclose( $sourceHandle );
357 }
358 if ( !fclose( $tmpHandle ) ) {
359 $status->fatal( 'backend-fail-closetemp', $tmpPath );
360 return $status;
361 }
362
363 clearstatcache(); // temp file changed
364
365 return $status;
366 }
367
368 /**
369 * @see FileBackend::doPrepare()
370 * @return Status
371 */
372 final protected function doPrepare( array $params ) {
373 wfProfileIn( __METHOD__ );
374 wfProfileIn( __METHOD__ . '-' . $this->name );
375
376 $status = Status::newGood();
377 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
378 if ( $dir === null ) {
379 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
380 wfProfileOut( __METHOD__ . '-' . $this->name );
381 wfProfileOut( __METHOD__ );
382 return $status; // invalid storage path
383 }
384
385 if ( $shard !== null ) { // confined to a single container/shard
386 $status->merge( $this->doPrepareInternal( $fullCont, $dir, $params ) );
387 } else { // directory is on several shards
388 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
389 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
390 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
391 $status->merge( $this->doPrepareInternal( "{$fullCont}{$suffix}", $dir, $params ) );
392 }
393 }
394
395 wfProfileOut( __METHOD__ . '-' . $this->name );
396 wfProfileOut( __METHOD__ );
397 return $status;
398 }
399
400 /**
401 * @see FileBackendStore::doPrepare()
402 * @return Status
403 */
404 protected function doPrepareInternal( $container, $dir, array $params ) {
405 return Status::newGood();
406 }
407
408 /**
409 * @see FileBackend::doSecure()
410 * @return Status
411 */
412 final protected function doSecure( array $params ) {
413 wfProfileIn( __METHOD__ );
414 wfProfileIn( __METHOD__ . '-' . $this->name );
415 $status = Status::newGood();
416
417 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
418 if ( $dir === null ) {
419 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
420 wfProfileOut( __METHOD__ . '-' . $this->name );
421 wfProfileOut( __METHOD__ );
422 return $status; // invalid storage path
423 }
424
425 if ( $shard !== null ) { // confined to a single container/shard
426 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
427 } else { // directory is on several shards
428 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
429 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
430 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
431 $status->merge( $this->doSecureInternal( "{$fullCont}{$suffix}", $dir, $params ) );
432 }
433 }
434
435 wfProfileOut( __METHOD__ . '-' . $this->name );
436 wfProfileOut( __METHOD__ );
437 return $status;
438 }
439
440 /**
441 * @see FileBackendStore::doSecure()
442 * @return Status
443 */
444 protected function doSecureInternal( $container, $dir, array $params ) {
445 return Status::newGood();
446 }
447
448 /**
449 * @see FileBackend::doPublish()
450 * @return Status
451 */
452 final protected function doPublish( array $params ) {
453 wfProfileIn( __METHOD__ );
454 wfProfileIn( __METHOD__ . '-' . $this->name );
455 $status = Status::newGood();
456
457 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
458 if ( $dir === null ) {
459 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
460 wfProfileOut( __METHOD__ . '-' . $this->name );
461 wfProfileOut( __METHOD__ );
462 return $status; // invalid storage path
463 }
464
465 if ( $shard !== null ) { // confined to a single container/shard
466 $status->merge( $this->doPublishInternal( $fullCont, $dir, $params ) );
467 } else { // directory is on several shards
468 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
469 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
470 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
471 $status->merge( $this->doPublishInternal( "{$fullCont}{$suffix}", $dir, $params ) );
472 }
473 }
474
475 wfProfileOut( __METHOD__ . '-' . $this->name );
476 wfProfileOut( __METHOD__ );
477 return $status;
478 }
479
480 /**
481 * @see FileBackendStore::doPublish()
482 * @return Status
483 */
484 protected function doPublishInternal( $container, $dir, array $params ) {
485 return Status::newGood();
486 }
487
488 /**
489 * @see FileBackend::doClean()
490 * @return Status
491 */
492 final protected function doClean( array $params ) {
493 wfProfileIn( __METHOD__ );
494 wfProfileIn( __METHOD__ . '-' . $this->name );
495 $status = Status::newGood();
496
497 // Recursive: first delete all empty subdirs recursively
498 if ( !empty( $params['recursive'] ) && !$this->directoriesAreVirtual() ) {
499 $subDirsRel = $this->getTopDirectoryList( array( 'dir' => $params['dir'] ) );
500 if ( $subDirsRel !== null ) { // no errors
501 foreach ( $subDirsRel as $subDirRel ) {
502 $subDir = $params['dir'] . "/{$subDirRel}"; // full path
503 $status->merge( $this->doClean( array( 'dir' => $subDir ) + $params ) );
504 }
505 }
506 }
507
508 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
509 if ( $dir === null ) {
510 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
511 wfProfileOut( __METHOD__ . '-' . $this->name );
512 wfProfileOut( __METHOD__ );
513 return $status; // invalid storage path
514 }
515
516 // Attempt to lock this directory...
517 $filesLockEx = array( $params['dir'] );
518 $scopedLockE = $this->getScopedFileLocks( $filesLockEx, LockManager::LOCK_EX, $status );
519 if ( !$status->isOK() ) {
520 wfProfileOut( __METHOD__ . '-' . $this->name );
521 wfProfileOut( __METHOD__ );
522 return $status; // abort
523 }
524
525 if ( $shard !== null ) { // confined to a single container/shard
526 $status->merge( $this->doCleanInternal( $fullCont, $dir, $params ) );
527 $this->deleteContainerCache( $fullCont ); // purge cache
528 } else { // directory is on several shards
529 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
530 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
531 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
532 $status->merge( $this->doCleanInternal( "{$fullCont}{$suffix}", $dir, $params ) );
533 $this->deleteContainerCache( "{$fullCont}{$suffix}" ); // purge cache
534 }
535 }
536
537 wfProfileOut( __METHOD__ . '-' . $this->name );
538 wfProfileOut( __METHOD__ );
539 return $status;
540 }
541
542 /**
543 * @see FileBackendStore::doClean()
544 * @return Status
545 */
546 protected function doCleanInternal( $container, $dir, array $params ) {
547 return Status::newGood();
548 }
549
550 /**
551 * @see FileBackend::fileExists()
552 * @return bool|null
553 */
554 final public function fileExists( array $params ) {
555 wfProfileIn( __METHOD__ );
556 wfProfileIn( __METHOD__ . '-' . $this->name );
557 $stat = $this->getFileStat( $params );
558 wfProfileOut( __METHOD__ . '-' . $this->name );
559 wfProfileOut( __METHOD__ );
560 return ( $stat === null ) ? null : (bool)$stat; // null => failure
561 }
562
563 /**
564 * @see FileBackend::getFileTimestamp()
565 * @return bool
566 */
567 final public function getFileTimestamp( array $params ) {
568 wfProfileIn( __METHOD__ );
569 wfProfileIn( __METHOD__ . '-' . $this->name );
570 $stat = $this->getFileStat( $params );
571 wfProfileOut( __METHOD__ . '-' . $this->name );
572 wfProfileOut( __METHOD__ );
573 return $stat ? $stat['mtime'] : false;
574 }
575
576 /**
577 * @see FileBackend::getFileSize()
578 * @return bool
579 */
580 final public function getFileSize( array $params ) {
581 wfProfileIn( __METHOD__ );
582 wfProfileIn( __METHOD__ . '-' . $this->name );
583 $stat = $this->getFileStat( $params );
584 wfProfileOut( __METHOD__ . '-' . $this->name );
585 wfProfileOut( __METHOD__ );
586 return $stat ? $stat['size'] : false;
587 }
588
589 /**
590 * @see FileBackend::getFileStat()
591 * @return bool
592 */
593 final public function getFileStat( array $params ) {
594 $path = self::normalizeStoragePath( $params['src'] );
595 if ( $path === null ) {
596 return false; // invalid storage path
597 }
598 wfProfileIn( __METHOD__ );
599 wfProfileIn( __METHOD__ . '-' . $this->name );
600 $latest = !empty( $params['latest'] ); // use latest data?
601 if ( !$this->cheapCache->has( $path, 'stat' ) ) {
602 $this->primeFileCache( array( $path ) ); // check persistent cache
603 }
604 if ( $this->cheapCache->has( $path, 'stat' ) ) {
605 $stat = $this->cheapCache->get( $path, 'stat' );
606 // If we want the latest data, check that this cached
607 // value was in fact fetched with the latest available data.
608 if ( !$latest || $stat['latest'] ) {
609 wfProfileOut( __METHOD__ . '-' . $this->name );
610 wfProfileOut( __METHOD__ );
611 return $stat;
612 }
613 }
614 wfProfileIn( __METHOD__ . '-miss' );
615 wfProfileIn( __METHOD__ . '-miss-' . $this->name );
616 $stat = $this->doGetFileStat( $params );
617 wfProfileOut( __METHOD__ . '-miss-' . $this->name );
618 wfProfileOut( __METHOD__ . '-miss' );
619 if ( is_array( $stat ) ) { // don't cache negatives
620 $stat['latest'] = $latest;
621 $this->cheapCache->set( $path, 'stat', $stat );
622 $this->setFileCache( $path, $stat ); // update persistent cache
623 if ( isset( $stat['sha1'] ) ) { // some backends store SHA-1 as metadata
624 $this->cheapCache->set( $path, 'sha1',
625 array( 'hash' => $stat['sha1'], 'latest' => $latest ) );
626 }
627 } else {
628 wfDebug( __METHOD__ . ": File $path does not exist.\n" );
629 }
630 wfProfileOut( __METHOD__ . '-' . $this->name );
631 wfProfileOut( __METHOD__ );
632 return $stat;
633 }
634
635 /**
636 * @see FileBackendStore::getFileStat()
637 */
638 abstract protected function doGetFileStat( array $params );
639
640 /**
641 * @see FileBackend::getFileContentsMulti()
642 * @return Array
643 */
644 public function getFileContentsMulti( array $params ) {
645 wfProfileIn( __METHOD__ );
646 wfProfileIn( __METHOD__ . '-' . $this->name );
647
648 $params = $this->setConcurrencyFlags( $params );
649 $contents = $this->doGetFileContentsMulti( $params );
650
651 wfProfileOut( __METHOD__ . '-' . $this->name );
652 wfProfileOut( __METHOD__ );
653 return $contents;
654 }
655
656 /**
657 * @see FileBackendStore::getFileContentsMulti()
658 * @return Array
659 */
660 protected function doGetFileContentsMulti( array $params ) {
661 $contents = array();
662 foreach ( $this->doGetLocalReferenceMulti( $params ) as $path => $fsFile ) {
663 wfSuppressWarnings();
664 $contents[$path] = $fsFile ? file_get_contents( $fsFile->getPath() ) : false;
665 wfRestoreWarnings();
666 }
667 return $contents;
668 }
669
670 /**
671 * @see FileBackend::getFileSha1Base36()
672 * @return bool|string
673 */
674 final public function getFileSha1Base36( array $params ) {
675 $path = self::normalizeStoragePath( $params['src'] );
676 if ( $path === null ) {
677 return false; // invalid storage path
678 }
679 wfProfileIn( __METHOD__ );
680 wfProfileIn( __METHOD__ . '-' . $this->name );
681 $latest = !empty( $params['latest'] ); // use latest data?
682 if ( $this->cheapCache->has( $path, 'sha1' ) ) {
683 $stat = $this->cheapCache->get( $path, 'sha1' );
684 // If we want the latest data, check that this cached
685 // value was in fact fetched with the latest available data.
686 if ( !$latest || $stat['latest'] ) {
687 wfProfileOut( __METHOD__ . '-' . $this->name );
688 wfProfileOut( __METHOD__ );
689 return $stat['hash'];
690 }
691 }
692 wfProfileIn( __METHOD__ . '-miss' );
693 wfProfileIn( __METHOD__ . '-miss-' . $this->name );
694 $hash = $this->doGetFileSha1Base36( $params );
695 wfProfileOut( __METHOD__ . '-miss-' . $this->name );
696 wfProfileOut( __METHOD__ . '-miss' );
697 if ( $hash ) { // don't cache negatives
698 $this->cheapCache->set( $path, 'sha1',
699 array( 'hash' => $hash, 'latest' => $latest ) );
700 }
701 wfProfileOut( __METHOD__ . '-' . $this->name );
702 wfProfileOut( __METHOD__ );
703 return $hash;
704 }
705
706 /**
707 * @see FileBackendStore::getFileSha1Base36()
708 * @return bool|string
709 */
710 protected function doGetFileSha1Base36( array $params ) {
711 $fsFile = $this->getLocalReference( $params );
712 if ( !$fsFile ) {
713 return false;
714 } else {
715 return $fsFile->getSha1Base36();
716 }
717 }
718
719 /**
720 * @see FileBackend::getFileProps()
721 * @return Array
722 */
723 final public function getFileProps( array $params ) {
724 wfProfileIn( __METHOD__ );
725 wfProfileIn( __METHOD__ . '-' . $this->name );
726 $fsFile = $this->getLocalReference( $params );
727 $props = $fsFile ? $fsFile->getProps() : FSFile::placeholderProps();
728 wfProfileOut( __METHOD__ . '-' . $this->name );
729 wfProfileOut( __METHOD__ );
730 return $props;
731 }
732
733 /**
734 * @see FileBackend::getLocalReferenceMulti()
735 * @return Array
736 */
737 final public function getLocalReferenceMulti( array $params ) {
738 wfProfileIn( __METHOD__ );
739 wfProfileIn( __METHOD__ . '-' . $this->name );
740
741 $params = $this->setConcurrencyFlags( $params );
742
743 $fsFiles = array(); // (path => FSFile)
744 $latest = !empty( $params['latest'] ); // use latest data?
745 // Reuse any files already in process cache...
746 foreach ( $params['srcs'] as $src ) {
747 $path = self::normalizeStoragePath( $src );
748 if ( $path === null ) {
749 $fsFiles[$src] = null; // invalid storage path
750 } elseif ( $this->expensiveCache->has( $path, 'localRef' ) ) {
751 $val = $this->expensiveCache->get( $path, 'localRef' );
752 // If we want the latest data, check that this cached
753 // value was in fact fetched with the latest available data.
754 if ( !$latest || $val['latest'] ) {
755 $fsFiles[$src] = $val['object'];
756 }
757 }
758 }
759 // Fetch local references of any remaning files...
760 $params['srcs'] = array_diff( $params['srcs'], array_keys( $fsFiles ) );
761 foreach ( $this->doGetLocalReferenceMulti( $params ) as $path => $fsFile ) {
762 $fsFiles[$path] = $fsFile;
763 if ( $fsFile ) { // update the process cache...
764 $this->expensiveCache->set( $path, 'localRef',
765 array( 'object' => $fsFile, 'latest' => $latest ) );
766 }
767 }
768
769 wfProfileOut( __METHOD__ . '-' . $this->name );
770 wfProfileOut( __METHOD__ );
771 return $fsFiles;
772 }
773
774 /**
775 * @see FileBackendStore::getLocalReferenceMulti()
776 * @return Array
777 */
778 protected function doGetLocalReferenceMulti( array $params ) {
779 return $this->doGetLocalCopyMulti( $params );
780 }
781
782 /**
783 * @see FileBackend::getLocalCopyMulti()
784 * @return Array
785 */
786 final public function getLocalCopyMulti( array $params ) {
787 wfProfileIn( __METHOD__ );
788 wfProfileIn( __METHOD__ . '-' . $this->name );
789
790 $params = $this->setConcurrencyFlags( $params );
791 $tmpFiles = $this->doGetLocalCopyMulti( $params );
792
793 wfProfileOut( __METHOD__ . '-' . $this->name );
794 wfProfileOut( __METHOD__ );
795 return $tmpFiles;
796 }
797
798 /**
799 * @see FileBackendStore::getLocalCopyMulti()
800 * @return Array
801 */
802 abstract protected function doGetLocalCopyMulti( array $params );
803
804 /**
805 * @see FileBackend::streamFile()
806 * @return Status
807 */
808 final public function streamFile( array $params ) {
809 wfProfileIn( __METHOD__ );
810 wfProfileIn( __METHOD__ . '-' . $this->name );
811 $status = Status::newGood();
812
813 $info = $this->getFileStat( $params );
814 if ( !$info ) { // let StreamFile handle the 404
815 $status->fatal( 'backend-fail-notexists', $params['src'] );
816 }
817
818 // Set output buffer and HTTP headers for stream
819 $extraHeaders = isset( $params['headers'] ) ? $params['headers'] : array();
820 $res = StreamFile::prepareForStream( $params['src'], $info, $extraHeaders );
821 if ( $res == StreamFile::NOT_MODIFIED ) {
822 // do nothing; client cache is up to date
823 } elseif ( $res == StreamFile::READY_STREAM ) {
824 wfProfileIn( __METHOD__ . '-send' );
825 wfProfileIn( __METHOD__ . '-send-' . $this->name );
826 $status = $this->doStreamFile( $params );
827 wfProfileOut( __METHOD__ . '-send-' . $this->name );
828 wfProfileOut( __METHOD__ . '-send' );
829 if ( !$status->isOK() ) {
830 // Per bug 41113, nasty things can happen if bad cache entries get
831 // stuck in cache. It's also possible that this error can come up
832 // with simple race conditions. Clear out the stat cache to be safe.
833 $this->clearCache( array( $params['src'] ) );
834 $this->deleteFileCache( $params['src'] );
835 trigger_error( "Bad stat cache or race condition for file {$params['src']}." );
836 }
837 } else {
838 $status->fatal( 'backend-fail-stream', $params['src'] );
839 }
840
841 wfProfileOut( __METHOD__ . '-' . $this->name );
842 wfProfileOut( __METHOD__ );
843 return $status;
844 }
845
846 /**
847 * @see FileBackendStore::streamFile()
848 * @return Status
849 */
850 protected function doStreamFile( array $params ) {
851 $status = Status::newGood();
852
853 $fsFile = $this->getLocalReference( $params );
854 if ( !$fsFile ) {
855 $status->fatal( 'backend-fail-stream', $params['src'] );
856 } elseif ( !readfile( $fsFile->getPath() ) ) {
857 $status->fatal( 'backend-fail-stream', $params['src'] );
858 }
859
860 return $status;
861 }
862
863 /**
864 * @see FileBackend::directoryExists()
865 * @return bool|null
866 */
867 final public function directoryExists( array $params ) {
868 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
869 if ( $dir === null ) {
870 return false; // invalid storage path
871 }
872 if ( $shard !== null ) { // confined to a single container/shard
873 return $this->doDirectoryExists( $fullCont, $dir, $params );
874 } else { // directory is on several shards
875 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
876 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
877 $res = false; // response
878 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
879 $exists = $this->doDirectoryExists( "{$fullCont}{$suffix}", $dir, $params );
880 if ( $exists ) {
881 $res = true;
882 break; // found one!
883 } elseif ( $exists === null ) { // error?
884 $res = null; // if we don't find anything, it is indeterminate
885 }
886 }
887 return $res;
888 }
889 }
890
891 /**
892 * @see FileBackendStore::directoryExists()
893 *
894 * @param $container string Resolved container name
895 * @param $dir string Resolved path relative to container
896 * @param $params Array
897 * @return bool|null
898 */
899 abstract protected function doDirectoryExists( $container, $dir, array $params );
900
901 /**
902 * @see FileBackend::getDirectoryList()
903 * @return Traversable|Array|null Returns null on failure
904 */
905 final public function getDirectoryList( array $params ) {
906 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
907 if ( $dir === null ) { // invalid storage path
908 return null;
909 }
910 if ( $shard !== null ) {
911 // File listing is confined to a single container/shard
912 return $this->getDirectoryListInternal( $fullCont, $dir, $params );
913 } else {
914 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
915 // File listing spans multiple containers/shards
916 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
917 return new FileBackendStoreShardDirIterator( $this,
918 $fullCont, $dir, $this->getContainerSuffixes( $shortCont ), $params );
919 }
920 }
921
922 /**
923 * Do not call this function from places outside FileBackend
924 *
925 * @see FileBackendStore::getDirectoryList()
926 *
927 * @param $container string Resolved container name
928 * @param $dir string Resolved path relative to container
929 * @param $params Array
930 * @return Traversable|Array|null Returns null on failure
931 */
932 abstract public function getDirectoryListInternal( $container, $dir, array $params );
933
934 /**
935 * @see FileBackend::getFileList()
936 * @return Traversable|Array|null Returns null on failure
937 */
938 final public function getFileList( array $params ) {
939 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
940 if ( $dir === null ) { // invalid storage path
941 return null;
942 }
943 if ( $shard !== null ) {
944 // File listing is confined to a single container/shard
945 return $this->getFileListInternal( $fullCont, $dir, $params );
946 } else {
947 wfDebug( __METHOD__ . ": iterating over all container shards.\n" );
948 // File listing spans multiple containers/shards
949 list( $b, $shortCont, $r ) = self::splitStoragePath( $params['dir'] );
950 return new FileBackendStoreShardFileIterator( $this,
951 $fullCont, $dir, $this->getContainerSuffixes( $shortCont ), $params );
952 }
953 }
954
955 /**
956 * Do not call this function from places outside FileBackend
957 *
958 * @see FileBackendStore::getFileList()
959 *
960 * @param $container string Resolved container name
961 * @param $dir string Resolved path relative to container
962 * @param $params Array
963 * @return Traversable|Array|null Returns null on failure
964 */
965 abstract public function getFileListInternal( $container, $dir, array $params );
966
967 /**
968 * Return a list of FileOp objects from a list of operations.
969 * Do not call this function from places outside FileBackend.
970 *
971 * The result must have the same number of items as the input.
972 * An exception is thrown if an unsupported operation is requested.
973 *
974 * @param $ops Array Same format as doOperations()
975 * @return Array List of FileOp objects
976 * @throws MWException
977 */
978 final public function getOperationsInternal( array $ops ) {
979 $supportedOps = array(
980 'store' => 'StoreFileOp',
981 'copy' => 'CopyFileOp',
982 'move' => 'MoveFileOp',
983 'delete' => 'DeleteFileOp',
984 'create' => 'CreateFileOp',
985 'null' => 'NullFileOp'
986 );
987
988 $performOps = array(); // array of FileOp objects
989 // Build up ordered array of FileOps...
990 foreach ( $ops as $operation ) {
991 $opName = $operation['op'];
992 if ( isset( $supportedOps[$opName] ) ) {
993 $class = $supportedOps[$opName];
994 // Get params for this operation
995 $params = $operation;
996 // Append the FileOp class
997 $performOps[] = new $class( $this, $params );
998 } else {
999 throw new MWException( "Operation '$opName' is not supported." );
1000 }
1001 }
1002
1003 return $performOps;
1004 }
1005
1006 /**
1007 * Get a list of storage paths to lock for a list of operations
1008 * Returns an array with 'sh' (shared) and 'ex' (exclusive) keys,
1009 * each corresponding to a list of storage paths to be locked.
1010 * All returned paths are normalized.
1011 *
1012 * @param $performOps Array List of FileOp objects
1013 * @return Array ('sh' => list of paths, 'ex' => list of paths)
1014 */
1015 final public function getPathsToLockForOpsInternal( array $performOps ) {
1016 // Build up a list of files to lock...
1017 $paths = array( 'sh' => array(), 'ex' => array() );
1018 foreach ( $performOps as $fileOp ) {
1019 $paths['sh'] = array_merge( $paths['sh'], $fileOp->storagePathsRead() );
1020 $paths['ex'] = array_merge( $paths['ex'], $fileOp->storagePathsChanged() );
1021 }
1022 // Optimization: if doing an EX lock anyway, don't also set an SH one
1023 $paths['sh'] = array_diff( $paths['sh'], $paths['ex'] );
1024 // Get a shared lock on the parent directory of each path changed
1025 $paths['sh'] = array_merge( $paths['sh'], array_map( 'dirname', $paths['ex'] ) );
1026
1027 return $paths;
1028 }
1029
1030 /**
1031 * @see FileBackend::getScopedLocksForOps()
1032 * @return Array
1033 */
1034 public function getScopedLocksForOps( array $ops, Status $status ) {
1035 $paths = $this->getPathsToLockForOpsInternal( $this->getOperationsInternal( $ops ) );
1036 return array(
1037 $this->getScopedFileLocks( $paths['sh'], LockManager::LOCK_UW, $status ),
1038 $this->getScopedFileLocks( $paths['ex'], LockManager::LOCK_EX, $status )
1039 );
1040 }
1041
1042 /**
1043 * @see FileBackend::doOperationsInternal()
1044 * @return Status
1045 */
1046 final protected function doOperationsInternal( array $ops, array $opts ) {
1047 wfProfileIn( __METHOD__ );
1048 wfProfileIn( __METHOD__ . '-' . $this->name );
1049 $status = Status::newGood();
1050
1051 // Build up a list of FileOps...
1052 $performOps = $this->getOperationsInternal( $ops );
1053
1054 // Acquire any locks as needed...
1055 if ( empty( $opts['nonLocking'] ) ) {
1056 // Build up a list of files to lock...
1057 $paths = $this->getPathsToLockForOpsInternal( $performOps );
1058 // Try to lock those files for the scope of this function...
1059 $scopeLockS = $this->getScopedFileLocks( $paths['sh'], LockManager::LOCK_UW, $status );
1060 $scopeLockE = $this->getScopedFileLocks( $paths['ex'], LockManager::LOCK_EX, $status );
1061 if ( !$status->isOK() ) {
1062 wfProfileOut( __METHOD__ . '-' . $this->name );
1063 wfProfileOut( __METHOD__ );
1064 return $status; // abort
1065 }
1066 }
1067
1068 // Clear any file cache entries (after locks acquired)
1069 if ( empty( $opts['preserveCache'] ) ) {
1070 $this->clearCache();
1071 }
1072
1073 // Load from the persistent file and container caches
1074 $this->primeFileCache( $performOps );
1075 $this->primeContainerCache( $performOps );
1076
1077 // Actually attempt the operation batch...
1078 $opts = $this->setConcurrencyFlags( $opts );
1079 $subStatus = FileOpBatch::attempt( $performOps, $opts, $this->fileJournal );
1080
1081 // Merge errors into status fields
1082 $status->merge( $subStatus );
1083 $status->success = $subStatus->success; // not done in merge()
1084
1085 wfProfileOut( __METHOD__ . '-' . $this->name );
1086 wfProfileOut( __METHOD__ );
1087 return $status;
1088 }
1089
1090 /**
1091 * @see FileBackend::doQuickOperationsInternal()
1092 * @return Status
1093 * @throws MWException
1094 */
1095 final protected function doQuickOperationsInternal( array $ops ) {
1096 wfProfileIn( __METHOD__ );
1097 wfProfileIn( __METHOD__ . '-' . $this->name );
1098 $status = Status::newGood();
1099
1100 $supportedOps = array( 'create', 'store', 'copy', 'move', 'delete', 'null' );
1101 $async = ( $this->parallelize === 'implicit' );
1102 $maxConcurrency = $this->concurrency; // throttle
1103
1104 $statuses = array(); // array of (index => Status)
1105 $fileOpHandles = array(); // list of (index => handle) arrays
1106 $curFileOpHandles = array(); // current handle batch
1107 // Perform the sync-only ops and build up op handles for the async ops...
1108 foreach ( $ops as $index => $params ) {
1109 if ( !in_array( $params['op'], $supportedOps ) ) {
1110 wfProfileOut( __METHOD__ . '-' . $this->name );
1111 wfProfileOut( __METHOD__ );
1112 throw new MWException( "Operation '{$params['op']}' is not supported." );
1113 }
1114 $method = $params['op'] . 'Internal'; // e.g. "storeInternal"
1115 $subStatus = $this->$method( array( 'async' => $async ) + $params );
1116 if ( $subStatus->value instanceof FileBackendStoreOpHandle ) { // async
1117 if ( count( $curFileOpHandles ) >= $maxConcurrency ) {
1118 $fileOpHandles[] = $curFileOpHandles; // push this batch
1119 $curFileOpHandles = array();
1120 }
1121 $curFileOpHandles[$index] = $subStatus->value; // keep index
1122 } else { // error or completed
1123 $statuses[$index] = $subStatus; // keep index
1124 }
1125 }
1126 if ( count( $curFileOpHandles ) ) {
1127 $fileOpHandles[] = $curFileOpHandles; // last batch
1128 }
1129 // Do all the async ops that can be done concurrently...
1130 foreach ( $fileOpHandles as $fileHandleBatch ) {
1131 $statuses = $statuses + $this->executeOpHandlesInternal( $fileHandleBatch );
1132 }
1133 // Marshall and merge all the responses...
1134 foreach ( $statuses as $index => $subStatus ) {
1135 $status->merge( $subStatus );
1136 if ( $subStatus->isOK() ) {
1137 $status->success[$index] = true;
1138 ++$status->successCount;
1139 } else {
1140 $status->success[$index] = false;
1141 ++$status->failCount;
1142 }
1143 }
1144
1145 wfProfileOut( __METHOD__ . '-' . $this->name );
1146 wfProfileOut( __METHOD__ );
1147 return $status;
1148 }
1149
1150 /**
1151 * Execute a list of FileBackendStoreOpHandle handles in parallel.
1152 * The resulting Status object fields will correspond
1153 * to the order in which the handles where given.
1154 *
1155 * @param $handles Array List of FileBackendStoreOpHandle objects
1156 * @return Array Map of Status objects
1157 * @throws MWException
1158 */
1159 final public function executeOpHandlesInternal( array $fileOpHandles ) {
1160 wfProfileIn( __METHOD__ );
1161 wfProfileIn( __METHOD__ . '-' . $this->name );
1162 foreach ( $fileOpHandles as $fileOpHandle ) {
1163 if ( !( $fileOpHandle instanceof FileBackendStoreOpHandle ) ) {
1164 throw new MWException( "Given a non-FileBackendStoreOpHandle object." );
1165 } elseif ( $fileOpHandle->backend->getName() !== $this->getName() ) {
1166 throw new MWException( "Given a FileBackendStoreOpHandle for the wrong backend." );
1167 }
1168 }
1169 $res = $this->doExecuteOpHandlesInternal( $fileOpHandles );
1170 foreach ( $fileOpHandles as $fileOpHandle ) {
1171 $fileOpHandle->closeResources();
1172 }
1173 wfProfileOut( __METHOD__ . '-' . $this->name );
1174 wfProfileOut( __METHOD__ );
1175 return $res;
1176 }
1177
1178 /**
1179 * @see FileBackendStore::executeOpHandlesInternal()
1180 * @param array $fileOpHandles
1181 * @throws MWException
1182 * @return Array List of corresponding Status objects
1183 */
1184 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
1185 foreach ( $fileOpHandles as $fileOpHandle ) { // OK if empty
1186 throw new MWException( "This backend supports no asynchronous operations." );
1187 }
1188 return array();
1189 }
1190
1191 /**
1192 * @see FileBackend::preloadCache()
1193 */
1194 final public function preloadCache( array $paths ) {
1195 $fullConts = array(); // full container names
1196 foreach ( $paths as $path ) {
1197 list( $fullCont, $r, $s ) = $this->resolveStoragePath( $path );
1198 $fullConts[] = $fullCont;
1199 }
1200 // Load from the persistent file and container caches
1201 $this->primeContainerCache( $fullConts );
1202 $this->primeFileCache( $paths );
1203 }
1204
1205 /**
1206 * @see FileBackend::clearCache()
1207 */
1208 final public function clearCache( array $paths = null ) {
1209 if ( is_array( $paths ) ) {
1210 $paths = array_map( 'FileBackend::normalizeStoragePath', $paths );
1211 $paths = array_filter( $paths, 'strlen' ); // remove nulls
1212 }
1213 if ( $paths === null ) {
1214 $this->cheapCache->clear();
1215 $this->expensiveCache->clear();
1216 } else {
1217 foreach ( $paths as $path ) {
1218 $this->cheapCache->clear( $path );
1219 $this->expensiveCache->clear( $path );
1220 }
1221 }
1222 $this->doClearCache( $paths );
1223 }
1224
1225 /**
1226 * Clears any additional stat caches for storage paths
1227 *
1228 * @see FileBackend::clearCache()
1229 *
1230 * @param $paths Array Storage paths (optional)
1231 * @return void
1232 */
1233 protected function doClearCache( array $paths = null ) {}
1234
1235 /**
1236 * Is this a key/value store where directories are just virtual?
1237 * Virtual directories exists in so much as files exists that are
1238 * prefixed with the directory path followed by a forward slash.
1239 *
1240 * @return bool
1241 */
1242 abstract protected function directoriesAreVirtual();
1243
1244 /**
1245 * Check if a container name is valid.
1246 * This checks for for length and illegal characters.
1247 *
1248 * @param $container string
1249 * @return bool
1250 */
1251 final protected static function isValidContainerName( $container ) {
1252 // This accounts for Swift and S3 restrictions while leaving room
1253 // for things like '.xxx' (hex shard chars) or '.seg' (segments).
1254 // This disallows directory separators or traversal characters.
1255 // Note that matching strings URL encode to the same string;
1256 // in Swift, the length restriction is *after* URL encoding.
1257 return preg_match( '/^[a-z0-9][a-z0-9-_]{0,199}$/i', $container );
1258 }
1259
1260 /**
1261 * Splits a storage path into an internal container name,
1262 * an internal relative file name, and a container shard suffix.
1263 * Any shard suffix is already appended to the internal container name.
1264 * This also checks that the storage path is valid and within this backend.
1265 *
1266 * If the container is sharded but a suffix could not be determined,
1267 * this means that the path can only refer to a directory and can only
1268 * be scanned by looking in all the container shards.
1269 *
1270 * @param $storagePath string
1271 * @return Array (container, path, container suffix) or (null, null, null) if invalid
1272 */
1273 final protected function resolveStoragePath( $storagePath ) {
1274 list( $backend, $container, $relPath ) = self::splitStoragePath( $storagePath );
1275 if ( $backend === $this->name ) { // must be for this backend
1276 $relPath = self::normalizeContainerPath( $relPath );
1277 if ( $relPath !== null ) {
1278 // Get shard for the normalized path if this container is sharded
1279 $cShard = $this->getContainerShard( $container, $relPath );
1280 // Validate and sanitize the relative path (backend-specific)
1281 $relPath = $this->resolveContainerPath( $container, $relPath );
1282 if ( $relPath !== null ) {
1283 // Prepend any wiki ID prefix to the container name
1284 $container = $this->fullContainerName( $container );
1285 if ( self::isValidContainerName( $container ) ) {
1286 // Validate and sanitize the container name (backend-specific)
1287 $container = $this->resolveContainerName( "{$container}{$cShard}" );
1288 if ( $container !== null ) {
1289 return array( $container, $relPath, $cShard );
1290 }
1291 }
1292 }
1293 }
1294 }
1295 return array( null, null, null );
1296 }
1297
1298 /**
1299 * Like resolveStoragePath() except null values are returned if
1300 * the container is sharded and the shard could not be determined.
1301 *
1302 * @see FileBackendStore::resolveStoragePath()
1303 *
1304 * @param $storagePath string
1305 * @return Array (container, path) or (null, null) if invalid
1306 */
1307 final protected function resolveStoragePathReal( $storagePath ) {
1308 list( $container, $relPath, $cShard ) = $this->resolveStoragePath( $storagePath );
1309 if ( $cShard !== null ) {
1310 return array( $container, $relPath );
1311 }
1312 return array( null, null );
1313 }
1314
1315 /**
1316 * Get the container name shard suffix for a given path.
1317 * Any empty suffix means the container is not sharded.
1318 *
1319 * @param $container string Container name
1320 * @param $relPath string Storage path relative to the container
1321 * @return string|null Returns null if shard could not be determined
1322 */
1323 final protected function getContainerShard( $container, $relPath ) {
1324 list( $levels, $base, $repeat ) = $this->getContainerHashLevels( $container );
1325 if ( $levels == 1 || $levels == 2 ) {
1326 // Hash characters are either base 16 or 36
1327 $char = ( $base == 36 ) ? '[0-9a-z]' : '[0-9a-f]';
1328 // Get a regex that represents the shard portion of paths.
1329 // The concatenation of the captures gives us the shard.
1330 if ( $levels === 1 ) { // 16 or 36 shards per container
1331 $hashDirRegex = '(' . $char . ')';
1332 } else { // 256 or 1296 shards per container
1333 if ( $repeat ) { // verbose hash dir format (e.g. "a/ab/abc")
1334 $hashDirRegex = $char . '/(' . $char . '{2})';
1335 } else { // short hash dir format (e.g. "a/b/c")
1336 $hashDirRegex = '(' . $char . ')/(' . $char . ')';
1337 }
1338 }
1339 // Allow certain directories to be above the hash dirs so as
1340 // to work with FileRepo (e.g. "archive/a/ab" or "temp/a/ab").
1341 // They must be 2+ chars to avoid any hash directory ambiguity.
1342 $m = array();
1343 if ( preg_match( "!^(?:[^/]{2,}/)*$hashDirRegex(?:/|$)!", $relPath, $m ) ) {
1344 return '.' . implode( '', array_slice( $m, 1 ) );
1345 }
1346 return null; // failed to match
1347 }
1348 return ''; // no sharding
1349 }
1350
1351 /**
1352 * Check if a storage path maps to a single shard.
1353 * Container dirs like "a", where the container shards on "x/xy",
1354 * can reside on several shards. Such paths are tricky to handle.
1355 *
1356 * @param $storagePath string Storage path
1357 * @return bool
1358 */
1359 final public function isSingleShardPathInternal( $storagePath ) {
1360 list( $c, $r, $shard ) = $this->resolveStoragePath( $storagePath );
1361 return ( $shard !== null );
1362 }
1363
1364 /**
1365 * Get the sharding config for a container.
1366 * If greater than 0, then all file storage paths within
1367 * the container are required to be hashed accordingly.
1368 *
1369 * @param $container string
1370 * @return Array (integer levels, integer base, repeat flag) or (0, 0, false)
1371 */
1372 final protected function getContainerHashLevels( $container ) {
1373 if ( isset( $this->shardViaHashLevels[$container] ) ) {
1374 $config = $this->shardViaHashLevels[$container];
1375 $hashLevels = (int)$config['levels'];
1376 if ( $hashLevels == 1 || $hashLevels == 2 ) {
1377 $hashBase = (int)$config['base'];
1378 if ( $hashBase == 16 || $hashBase == 36 ) {
1379 return array( $hashLevels, $hashBase, $config['repeat'] );
1380 }
1381 }
1382 }
1383 return array( 0, 0, false ); // no sharding
1384 }
1385
1386 /**
1387 * Get a list of full container shard suffixes for a container
1388 *
1389 * @param $container string
1390 * @return Array
1391 */
1392 final protected function getContainerSuffixes( $container ) {
1393 $shards = array();
1394 list( $digits, $base ) = $this->getContainerHashLevels( $container );
1395 if ( $digits > 0 ) {
1396 $numShards = pow( $base, $digits );
1397 for ( $index = 0; $index < $numShards; $index++ ) {
1398 $shards[] = '.' . wfBaseConvert( $index, 10, $base, $digits );
1399 }
1400 }
1401 return $shards;
1402 }
1403
1404 /**
1405 * Get the full container name, including the wiki ID prefix
1406 *
1407 * @param $container string
1408 * @return string
1409 */
1410 final protected function fullContainerName( $container ) {
1411 if ( $this->wikiId != '' ) {
1412 return "{$this->wikiId}-$container";
1413 } else {
1414 return $container;
1415 }
1416 }
1417
1418 /**
1419 * Resolve a container name, checking if it's allowed by the backend.
1420 * This is intended for internal use, such as encoding illegal chars.
1421 * Subclasses can override this to be more restrictive.
1422 *
1423 * @param $container string
1424 * @return string|null
1425 */
1426 protected function resolveContainerName( $container ) {
1427 return $container;
1428 }
1429
1430 /**
1431 * Resolve a relative storage path, checking if it's allowed by the backend.
1432 * This is intended for internal use, such as encoding illegal chars or perhaps
1433 * getting absolute paths (e.g. FS based backends). Note that the relative path
1434 * may be the empty string (e.g. the path is simply to the container).
1435 *
1436 * @param $container string Container name
1437 * @param $relStoragePath string Storage path relative to the container
1438 * @return string|null Path or null if not valid
1439 */
1440 protected function resolveContainerPath( $container, $relStoragePath ) {
1441 return $relStoragePath;
1442 }
1443
1444 /**
1445 * Get the cache key for a container
1446 *
1447 * @param $container string Resolved container name
1448 * @return string
1449 */
1450 private function containerCacheKey( $container ) {
1451 return wfMemcKey( 'backend', $this->getName(), 'container', $container );
1452 }
1453
1454 /**
1455 * Set the cached info for a container
1456 *
1457 * @param $container string Resolved container name
1458 * @param $val mixed Information to cache
1459 */
1460 final protected function setContainerCache( $container, $val ) {
1461 $this->memCache->add( $this->containerCacheKey( $container ), $val, 14*86400 );
1462 }
1463
1464 /**
1465 * Delete the cached info for a container.
1466 * The cache key is salted for a while to prevent race conditions.
1467 *
1468 * @param $container string Resolved container name
1469 */
1470 final protected function deleteContainerCache( $container ) {
1471 if ( !$this->memCache->set( $this->containerCacheKey( $container ), 'PURGED', 300 ) ) {
1472 trigger_error( "Unable to delete stat cache for container $container." );
1473 }
1474 }
1475
1476 /**
1477 * Do a batch lookup from cache for container stats for all containers
1478 * used in a list of container names, storage paths, or FileOp objects.
1479 * This loads the persistent cache values into the process cache.
1480 *
1481 * @param $items Array
1482 * @return void
1483 */
1484 final protected function primeContainerCache( array $items ) {
1485 wfProfileIn( __METHOD__ );
1486 wfProfileIn( __METHOD__ . '-' . $this->name );
1487
1488 $paths = array(); // list of storage paths
1489 $contNames = array(); // (cache key => resolved container name)
1490 // Get all the paths/containers from the items...
1491 foreach ( $items as $item ) {
1492 if ( $item instanceof FileOp ) {
1493 $paths = array_merge( $paths, $item->storagePathsRead() );
1494 $paths = array_merge( $paths, $item->storagePathsChanged() );
1495 } elseif ( self::isStoragePath( $item ) ) {
1496 $paths[] = $item;
1497 } elseif ( is_string( $item ) ) { // full container name
1498 $contNames[$this->containerCacheKey( $item )] = $item;
1499 }
1500 }
1501 // Get all the corresponding cache keys for paths...
1502 foreach ( $paths as $path ) {
1503 list( $fullCont, $r, $s ) = $this->resolveStoragePath( $path );
1504 if ( $fullCont !== null ) { // valid path for this backend
1505 $contNames[$this->containerCacheKey( $fullCont )] = $fullCont;
1506 }
1507 }
1508
1509 $contInfo = array(); // (resolved container name => cache value)
1510 // Get all cache entries for these container cache keys...
1511 $values = $this->memCache->getMulti( array_keys( $contNames ) );
1512 foreach ( $values as $cacheKey => $val ) {
1513 $contInfo[$contNames[$cacheKey]] = $val;
1514 }
1515
1516 // Populate the container process cache for the backend...
1517 $this->doPrimeContainerCache( array_filter( $contInfo, 'is_array' ) );
1518
1519 wfProfileOut( __METHOD__ . '-' . $this->name );
1520 wfProfileOut( __METHOD__ );
1521 }
1522
1523 /**
1524 * Fill the backend-specific process cache given an array of
1525 * resolved container names and their corresponding cached info.
1526 * Only containers that actually exist should appear in the map.
1527 *
1528 * @param $containerInfo Array Map of resolved container names to cached info
1529 * @return void
1530 */
1531 protected function doPrimeContainerCache( array $containerInfo ) {}
1532
1533 /**
1534 * Get the cache key for a file path
1535 *
1536 * @param $path string Normalized storage path
1537 * @return string
1538 */
1539 private function fileCacheKey( $path ) {
1540 return wfMemcKey( 'backend', $this->getName(), 'file', sha1( $path ) );
1541 }
1542
1543 /**
1544 * Set the cached stat info for a file path.
1545 * Negatives (404s) are not cached. By not caching negatives, we can skip cache
1546 * salting for the case when a file is created at a path were there was none before.
1547 *
1548 * @param $path string Storage path
1549 * @param $val mixed Information to cache
1550 */
1551 final protected function setFileCache( $path, $val ) {
1552 $path = FileBackend::normalizeStoragePath( $path );
1553 if ( $path === null ) {
1554 return; // invalid storage path
1555 }
1556 $this->memCache->add( $this->fileCacheKey( $path ), $val, 7*86400 );
1557 }
1558
1559 /**
1560 * Delete the cached stat info for a file path.
1561 * The cache key is salted for a while to prevent race conditions.
1562 *
1563 * @param $path string Storage path
1564 */
1565 final protected function deleteFileCache( $path ) {
1566 $path = FileBackend::normalizeStoragePath( $path );
1567 if ( $path === null ) {
1568 return; // invalid storage path
1569 }
1570 if ( !$this->memCache->set( $this->fileCacheKey( $path ), 'PURGED', 300 ) ) {
1571 trigger_error( "Unable to delete stat cache for file $path." );
1572 }
1573 }
1574
1575 /**
1576 * Do a batch lookup from cache for file stats for all paths
1577 * used in a list of storage paths or FileOp objects.
1578 * This loads the persistent cache values into the process cache.
1579 *
1580 * @param $items Array List of storage paths or FileOps
1581 * @return void
1582 */
1583 final protected function primeFileCache( array $items ) {
1584 wfProfileIn( __METHOD__ );
1585 wfProfileIn( __METHOD__ . '-' . $this->name );
1586
1587 $paths = array(); // list of storage paths
1588 $pathNames = array(); // (cache key => storage path)
1589 // Get all the paths/containers from the items...
1590 foreach ( $items as $item ) {
1591 if ( $item instanceof FileOp ) {
1592 $paths = array_merge( $paths, $item->storagePathsRead() );
1593 $paths = array_merge( $paths, $item->storagePathsChanged() );
1594 } elseif ( self::isStoragePath( $item ) ) {
1595 $paths[] = FileBackend::normalizeStoragePath( $item );
1596 }
1597 }
1598 // Get rid of any paths that failed normalization...
1599 $paths = array_filter( $paths, 'strlen' ); // remove nulls
1600 // Get all the corresponding cache keys for paths...
1601 foreach ( $paths as $path ) {
1602 list( $cont, $rel, $s ) = $this->resolveStoragePath( $path );
1603 if ( $rel !== null ) { // valid path for this backend
1604 $pathNames[$this->fileCacheKey( $path )] = $path;
1605 }
1606 }
1607 // Get all cache entries for these container cache keys...
1608 $values = $this->memCache->getMulti( array_keys( $pathNames ) );
1609 foreach ( $values as $cacheKey => $val ) {
1610 if ( is_array( $val ) ) {
1611 $path = $pathNames[$cacheKey];
1612 $this->cheapCache->set( $path, 'stat', $val );
1613 if ( isset( $val['sha1'] ) ) { // some backends store SHA-1 as metadata
1614 $this->cheapCache->set( $path, 'sha1',
1615 array( 'hash' => $val['sha1'], 'latest' => $val['latest'] ) );
1616 }
1617 }
1618 }
1619
1620 wfProfileOut( __METHOD__ . '-' . $this->name );
1621 wfProfileOut( __METHOD__ );
1622 }
1623
1624 /**
1625 * Set the 'concurrency' option from a list of operation options
1626 *
1627 * @param $opts array Map of operation options
1628 * @return Array
1629 */
1630 final protected function setConcurrencyFlags( array $opts ) {
1631 $opts['concurrency'] = 1; // off
1632 if ( $this->parallelize === 'implicit' ) {
1633 if ( !isset( $opts['parallelize'] ) || $opts['parallelize'] ) {
1634 $opts['concurrency'] = $this->concurrency;
1635 }
1636 } elseif ( $this->parallelize === 'explicit' ) {
1637 if ( !empty( $opts['parallelize'] ) ) {
1638 $opts['concurrency'] = $this->concurrency;
1639 }
1640 }
1641 return $opts;
1642 }
1643 }
1644
1645 /**
1646 * FileBackendStore helper class for performing asynchronous file operations.
1647 *
1648 * For example, calling FileBackendStore::createInternal() with the "async"
1649 * param flag may result in a Status that contains this object as a value.
1650 * This class is largely backend-specific and is mostly just "magic" to be
1651 * passed to FileBackendStore::executeOpHandlesInternal().
1652 */
1653 abstract class FileBackendStoreOpHandle {
1654 /** @var Array */
1655 public $params = array(); // params to caller functions
1656 /** @var FileBackendStore */
1657 public $backend;
1658 /** @var Array */
1659 public $resourcesToClose = array();
1660
1661 public $call; // string; name that identifies the function called
1662
1663 /**
1664 * Close all open file handles
1665 *
1666 * @return void
1667 */
1668 public function closeResources() {
1669 array_map( 'fclose', $this->resourcesToClose );
1670 }
1671 }
1672
1673 /**
1674 * FileBackendStore helper function to handle listings that span container shards.
1675 * Do not use this class from places outside of FileBackendStore.
1676 *
1677 * @ingroup FileBackend
1678 */
1679 abstract class FileBackendStoreShardListIterator implements Iterator {
1680 /** @var FileBackendStore */
1681 protected $backend;
1682 /** @var Array */
1683 protected $params;
1684 /** @var Array */
1685 protected $shardSuffixes;
1686 protected $container; // string; full container name
1687 protected $directory; // string; resolved relative path
1688
1689 /** @var Traversable */
1690 protected $iter;
1691 protected $curShard = 0; // integer
1692 protected $pos = 0; // integer
1693
1694 /** @var Array */
1695 protected $multiShardPaths = array(); // (rel path => 1)
1696
1697 /**
1698 * @param $backend FileBackendStore
1699 * @param $container string Full storage container name
1700 * @param $dir string Storage directory relative to container
1701 * @param $suffixes Array List of container shard suffixes
1702 * @param $params Array
1703 */
1704 public function __construct(
1705 FileBackendStore $backend, $container, $dir, array $suffixes, array $params
1706 ) {
1707 $this->backend = $backend;
1708 $this->container = $container;
1709 $this->directory = $dir;
1710 $this->shardSuffixes = $suffixes;
1711 $this->params = $params;
1712 }
1713
1714 /**
1715 * @see Iterator::key()
1716 * @return integer
1717 */
1718 public function key() {
1719 return $this->pos;
1720 }
1721
1722 /**
1723 * @see Iterator::valid()
1724 * @return bool
1725 */
1726 public function valid() {
1727 if ( $this->iter instanceof Iterator ) {
1728 return $this->iter->valid();
1729 } elseif ( is_array( $this->iter ) ) {
1730 return ( current( $this->iter ) !== false ); // no paths can have this value
1731 }
1732 return false; // some failure?
1733 }
1734
1735 /**
1736 * @see Iterator::current()
1737 * @return string|bool String or false
1738 */
1739 public function current() {
1740 return ( $this->iter instanceof Iterator )
1741 ? $this->iter->current()
1742 : current( $this->iter );
1743 }
1744
1745 /**
1746 * @see Iterator::next()
1747 * @return void
1748 */
1749 public function next() {
1750 ++$this->pos;
1751 ( $this->iter instanceof Iterator ) ? $this->iter->next() : next( $this->iter );
1752 do {
1753 $continue = false; // keep scanning shards?
1754 $this->filterViaNext(); // filter out duplicates
1755 // Find the next non-empty shard if no elements are left
1756 if ( !$this->valid() ) {
1757 $this->nextShardIteratorIfNotValid();
1758 $continue = $this->valid(); // re-filter unless we ran out of shards
1759 }
1760 } while ( $continue );
1761 }
1762
1763 /**
1764 * @see Iterator::rewind()
1765 * @return void
1766 */
1767 public function rewind() {
1768 $this->pos = 0;
1769 $this->curShard = 0;
1770 $this->setIteratorFromCurrentShard();
1771 do {
1772 $continue = false; // keep scanning shards?
1773 $this->filterViaNext(); // filter out duplicates
1774 // Find the next non-empty shard if no elements are left
1775 if ( !$this->valid() ) {
1776 $this->nextShardIteratorIfNotValid();
1777 $continue = $this->valid(); // re-filter unless we ran out of shards
1778 }
1779 } while ( $continue );
1780 }
1781
1782 /**
1783 * Filter out duplicate items by advancing to the next ones
1784 */
1785 protected function filterViaNext() {
1786 while ( $this->valid() ) {
1787 $rel = $this->iter->current(); // path relative to given directory
1788 $path = $this->params['dir'] . "/{$rel}"; // full storage path
1789 if ( $this->backend->isSingleShardPathInternal( $path ) ) {
1790 break; // path is only on one shard; no issue with duplicates
1791 } elseif ( isset( $this->multiShardPaths[$rel] ) ) {
1792 // Don't keep listing paths that are on multiple shards
1793 ( $this->iter instanceof Iterator ) ? $this->iter->next() : next( $this->iter );
1794 } else {
1795 $this->multiShardPaths[$rel] = 1;
1796 break;
1797 }
1798 }
1799 }
1800
1801 /**
1802 * If the list iterator for this container shard is out of items,
1803 * then move on to the next container that has items.
1804 * If there are none, then it advances to the last container.
1805 */
1806 protected function nextShardIteratorIfNotValid() {
1807 while ( !$this->valid() && ++$this->curShard < count( $this->shardSuffixes ) ) {
1808 $this->setIteratorFromCurrentShard();
1809 }
1810 }
1811
1812 /**
1813 * Set the list iterator to that of the current container shard
1814 */
1815 protected function setIteratorFromCurrentShard() {
1816 $this->iter = $this->listFromShard(
1817 $this->container . $this->shardSuffixes[$this->curShard],
1818 $this->directory, $this->params );
1819 // Start loading results so that current() works
1820 if ( $this->iter ) {
1821 ( $this->iter instanceof Iterator ) ? $this->iter->rewind() : reset( $this->iter );
1822 }
1823 }
1824
1825 /**
1826 * Get the list for a given container shard
1827 *
1828 * @param $container string Resolved container name
1829 * @param $dir string Resolved path relative to container
1830 * @param $params Array
1831 * @return Traversable|Array|null
1832 */
1833 abstract protected function listFromShard( $container, $dir, array $params );
1834 }
1835
1836 /**
1837 * Iterator for listing directories
1838 */
1839 class FileBackendStoreShardDirIterator extends FileBackendStoreShardListIterator {
1840 /**
1841 * @see FileBackendStoreShardListIterator::listFromShard()
1842 * @return Array|null|Traversable
1843 */
1844 protected function listFromShard( $container, $dir, array $params ) {
1845 return $this->backend->getDirectoryListInternal( $container, $dir, $params );
1846 }
1847 }
1848
1849 /**
1850 * Iterator for listing regular files
1851 */
1852 class FileBackendStoreShardFileIterator extends FileBackendStoreShardListIterator {
1853 /**
1854 * @see FileBackendStoreShardListIterator::listFromShard()
1855 * @return Array|null|Traversable
1856 */
1857 protected function listFromShard( $container, $dir, array $params ) {
1858 return $this->backend->getFileListInternal( $container, $dir, $params );
1859 }
1860 }