Fixed spacing in actions/cache/filebackend/filerepo/job folder
[mediawiki.git] / includes / filerepo / FileRepo.php
blobcfc3af7f6f5a66019fe8578c53d93001c741f3cf
1 <?php
2 /**
3 * @defgroup FileRepo File Repository
5 * @brief This module handles how MediaWiki interacts with filesystems.
7 * @details
8 */
10 /**
11 * Base code for file repositories.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 * http://www.gnu.org/copyleft/gpl.html
28 * @file
29 * @ingroup FileRepo
32 /**
33 * Base class for file repositories
35 * @ingroup FileRepo
37 class FileRepo {
38 const DELETE_SOURCE = 1;
39 const OVERWRITE = 2;
40 const OVERWRITE_SAME = 4;
41 const SKIP_LOCKING = 8;
43 /** @var FileBackend */
44 protected $backend;
45 /** @var Array Map of zones to config */
46 protected $zones = array();
48 var $thumbScriptUrl, $transformVia404;
49 var $descBaseUrl, $scriptDirUrl, $scriptExtension, $articleUrl;
50 var $fetchDescription, $initialCapital;
51 var $pathDisclosureProtection = 'simple'; // 'paranoid'
52 var $descriptionCacheExpiry, $url, $thumbUrl;
53 var $hashLevels, $deletedHashLevels;
54 protected $abbrvThreshold;
56 /**
57 * Factory functions for creating new files
58 * Override these in the base class
60 var $fileFactory = array( 'UnregisteredLocalFile', 'newFromTitle' );
61 var $oldFileFactory = false;
62 var $fileFactoryKey = false, $oldFileFactoryKey = false;
64 /**
65 * @param $info array|null
66 * @throws MWException
68 public function __construct( array $info = null ) {
69 // Verify required settings presence
70 if (
71 $info === null
72 || !array_key_exists( 'name', $info )
73 || !array_key_exists( 'backend', $info )
74 ) {
75 throw new MWException( __CLASS__ . " requires an array of options having both 'name' and 'backend' keys.\n" );
78 // Required settings
79 $this->name = $info['name'];
80 if ( $info['backend'] instanceof FileBackend ) {
81 $this->backend = $info['backend']; // useful for testing
82 } else {
83 $this->backend = FileBackendGroup::singleton()->get( $info['backend'] );
86 // Optional settings that can have no value
87 $optionalSettings = array(
88 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
89 'thumbScriptUrl', 'pathDisclosureProtection', 'descriptionCacheExpiry',
90 'scriptExtension'
92 foreach ( $optionalSettings as $var ) {
93 if ( isset( $info[$var] ) ) {
94 $this->$var = $info[$var];
98 // Optional settings that have a default
99 $this->initialCapital = isset( $info['initialCapital'] )
100 ? $info['initialCapital']
101 : MWNamespace::isCapitalized( NS_FILE );
102 $this->url = isset( $info['url'] )
103 ? $info['url']
104 : false; // a subclass may set the URL (e.g. ForeignAPIRepo)
105 if ( isset( $info['thumbUrl'] ) ) {
106 $this->thumbUrl = $info['thumbUrl'];
107 } else {
108 $this->thumbUrl = $this->url ? "{$this->url}/thumb" : false;
110 $this->hashLevels = isset( $info['hashLevels'] )
111 ? $info['hashLevels']
112 : 2;
113 $this->deletedHashLevels = isset( $info['deletedHashLevels'] )
114 ? $info['deletedHashLevels']
115 : $this->hashLevels;
116 $this->transformVia404 = !empty( $info['transformVia404'] );
117 $this->abbrvThreshold = isset( $info['abbrvThreshold'] )
118 ? $info['abbrvThreshold']
119 : 255;
120 $this->isPrivate = !empty( $info['isPrivate'] );
121 // Give defaults for the basic zones...
122 $this->zones = isset( $info['zones'] ) ? $info['zones'] : array();
123 foreach ( array( 'public', 'thumb', 'transcoded', 'temp', 'deleted' ) as $zone ) {
124 if ( !isset( $this->zones[$zone]['container'] ) ) {
125 $this->zones[$zone]['container'] = "{$this->name}-{$zone}";
127 if ( !isset( $this->zones[$zone]['directory'] ) ) {
128 $this->zones[$zone]['directory'] = '';
130 if ( !isset( $this->zones[$zone]['urlsByExt'] ) ) {
131 $this->zones[$zone]['urlsByExt'] = array();
137 * Get the file backend instance. Use this function wisely.
139 * @return FileBackend
141 public function getBackend() {
142 return $this->backend;
146 * Get an explanatory message if this repo is read-only.
147 * This checks if an administrator disabled writes to the backend.
149 * @return string|bool Returns false if the repo is not read-only
151 public function getReadOnlyReason() {
152 return $this->backend->getReadOnlyReason();
156 * Check if a single zone or list of zones is defined for usage
158 * @param array $doZones Only do a particular zones
159 * @throws MWException
160 * @return Status
162 protected function initZones( $doZones = array() ) {
163 $status = $this->newGood();
164 foreach ( (array)$doZones as $zone ) {
165 $root = $this->getZonePath( $zone );
166 if ( $root === null ) {
167 throw new MWException( "No '$zone' zone defined in the {$this->name} repo." );
170 return $status;
174 * Determine if a string is an mwrepo:// URL
176 * @param $url string
177 * @return bool
179 public static function isVirtualUrl( $url ) {
180 return substr( $url, 0, 9 ) == 'mwrepo://';
184 * Get a URL referring to this repository, with the private mwrepo protocol.
185 * The suffix, if supplied, is considered to be unencoded, and will be
186 * URL-encoded before being returned.
188 * @param $suffix string|bool
189 * @return string
191 public function getVirtualUrl( $suffix = false ) {
192 $path = 'mwrepo://' . $this->name;
193 if ( $suffix !== false ) {
194 $path .= '/' . rawurlencode( $suffix );
196 return $path;
200 * Get the URL corresponding to one of the four basic zones
202 * @param string $zone One of: public, deleted, temp, thumb
203 * @param string|null $ext Optional file extension
204 * @return String or false
206 public function getZoneUrl( $zone, $ext = null ) {
207 if ( in_array( $zone, array( 'public', 'temp', 'thumb', 'transcoded' ) ) ) { // standard public zones
208 if ( $ext !== null && isset( $this->zones[$zone]['urlsByExt'][$ext] ) ) {
209 return $this->zones[$zone]['urlsByExt'][$ext]; // custom URL for extension/zone
210 } elseif ( isset( $this->zones[$zone]['url'] ) ) {
211 return $this->zones[$zone]['url']; // custom URL for zone
214 switch ( $zone ) {
215 case 'public':
216 return $this->url;
217 case 'temp':
218 return "{$this->url}/temp";
219 case 'deleted':
220 return false; // no public URL
221 case 'thumb':
222 return $this->thumbUrl;
223 case 'transcoded':
224 return "{$this->url}/transcoded";
225 default:
226 return false;
231 * Get the thumb zone URL configured to be handled by scripts like thumb_handler.php.
232 * This is probably only useful for internal requests, such as from a fast frontend server
233 * to a slower backend server.
235 * Large sites may use a different host name for uploads than for wikis. In any case, the
236 * wiki configuration is needed in order to use thumb.php. To avoid extracting the wiki ID
237 * from the URL path, one can configure thumb_handler.php to recognize a special path on the
238 * same host name as the wiki that is used for viewing thumbnails.
240 * @param string $zone one of: public, deleted, temp, thumb
241 * @return String or false
243 public function getZoneHandlerUrl( $zone ) {
244 if ( isset( $this->zones[$zone]['handlerUrl'] )
245 && in_array( $zone, array( 'public', 'temp', 'thumb', 'transcoded' ) ) )
247 return $this->zones[$zone]['handlerUrl'];
249 return false;
253 * Get the backend storage path corresponding to a virtual URL.
254 * Use this function wisely.
256 * @param $url string
257 * @throws MWException
258 * @return string
260 public function resolveVirtualUrl( $url ) {
261 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
262 throw new MWException( __METHOD__ . ': unknown protocol' );
264 $bits = explode( '/', substr( $url, 9 ), 3 );
265 if ( count( $bits ) != 3 ) {
266 throw new MWException( __METHOD__ . ": invalid mwrepo URL: $url" );
268 list( $repo, $zone, $rel ) = $bits;
269 if ( $repo !== $this->name ) {
270 throw new MWException( __METHOD__ . ": fetching from a foreign repo is not supported" );
272 $base = $this->getZonePath( $zone );
273 if ( !$base ) {
274 throw new MWException( __METHOD__ . ": invalid zone: $zone" );
276 return $base . '/' . rawurldecode( $rel );
280 * The the storage container and base path of a zone
282 * @param $zone string
283 * @return Array (container, base path) or (null, null)
285 protected function getZoneLocation( $zone ) {
286 if ( !isset( $this->zones[$zone] ) ) {
287 return array( null, null ); // bogus
289 return array( $this->zones[$zone]['container'], $this->zones[$zone]['directory'] );
293 * Get the storage path corresponding to one of the zones
295 * @param $zone string
296 * @return string|null Returns null if the zone is not defined
298 public function getZonePath( $zone ) {
299 list( $container, $base ) = $this->getZoneLocation( $zone );
300 if ( $container === null || $base === null ) {
301 return null;
303 $backendName = $this->backend->getName();
304 if ( $base != '' ) { // may not be set
305 $base = "/{$base}";
307 return "mwstore://$backendName/{$container}{$base}";
311 * Create a new File object from the local repository
313 * @param $title Mixed: Title object or string
314 * @param $time Mixed: Time at which the image was uploaded.
315 * If this is specified, the returned object will be an
316 * instance of the repository's old file class instead of a
317 * current file. Repositories not supporting version control
318 * should return false if this parameter is set.
319 * @return File|null A File, or null if passed an invalid Title
321 public function newFile( $title, $time = false ) {
322 $title = File::normalizeTitle( $title );
323 if ( !$title ) {
324 return null;
326 if ( $time ) {
327 if ( $this->oldFileFactory ) {
328 return call_user_func( $this->oldFileFactory, $title, $this, $time );
329 } else {
330 return false;
332 } else {
333 return call_user_func( $this->fileFactory, $title, $this );
338 * Find an instance of the named file created at the specified time
339 * Returns false if the file does not exist. Repositories not supporting
340 * version control should return false if the time is specified.
342 * @param $title Mixed: Title object or string
343 * @param array $options Associative array of options:
344 * time: requested time for a specific file version, or false for the
345 * current version. An image object will be returned which was
346 * created at the specified time (which may be archived or current).
348 * ignoreRedirect: If true, do not follow file redirects
350 * private: If true, return restricted (deleted) files if the current
351 * user is allowed to view them. Otherwise, such files will not
352 * be found.
353 * @return File|bool False on failure
355 public function findFile( $title, $options = array() ) {
356 $title = File::normalizeTitle( $title );
357 if ( !$title ) {
358 return false;
360 $time = isset( $options['time'] ) ? $options['time'] : false;
361 # First try the current version of the file to see if it precedes the timestamp
362 $img = $this->newFile( $title );
363 if ( !$img ) {
364 return false;
366 if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
367 return $img;
369 # Now try an old version of the file
370 if ( $time !== false ) {
371 $img = $this->newFile( $title, $time );
372 if ( $img && $img->exists() ) {
373 if ( !$img->isDeleted( File::DELETED_FILE ) ) {
374 return $img; // always OK
375 } elseif ( !empty( $options['private'] ) && $img->userCan( File::DELETED_FILE ) ) {
376 return $img;
381 # Now try redirects
382 if ( !empty( $options['ignoreRedirect'] ) ) {
383 return false;
385 $redir = $this->checkRedirect( $title );
386 if ( $redir && $title->getNamespace() == NS_FILE) {
387 $img = $this->newFile( $redir );
388 if ( !$img ) {
389 return false;
391 if ( $img->exists() ) {
392 $img->redirectedFrom( $title->getDBkey() );
393 return $img;
396 return false;
400 * Find many files at once.
402 * @param array $items An array of titles, or an array of findFile() options with
403 * the "title" option giving the title. Example:
405 * $findItem = array( 'title' => $title, 'private' => true );
406 * $findBatch = array( $findItem );
407 * $repo->findFiles( $findBatch );
408 * @return array
410 public function findFiles( array $items ) {
411 $result = array();
412 foreach ( $items as $item ) {
413 if ( is_array( $item ) ) {
414 $title = $item['title'];
415 $options = $item;
416 unset( $options['title'] );
417 } else {
418 $title = $item;
419 $options = array();
421 $file = $this->findFile( $title, $options );
422 if ( $file ) {
423 $result[$file->getTitle()->getDBkey()] = $file;
426 return $result;
430 * Find an instance of the file with this key, created at the specified time
431 * Returns false if the file does not exist. Repositories not supporting
432 * version control should return false if the time is specified.
434 * @param string $sha1 base 36 SHA-1 hash
435 * @param array $options Option array, same as findFile().
436 * @return File|bool False on failure
438 public function findFileFromKey( $sha1, $options = array() ) {
439 $time = isset( $options['time'] ) ? $options['time'] : false;
440 # First try to find a matching current version of a file...
441 if ( $this->fileFactoryKey ) {
442 $img = call_user_func( $this->fileFactoryKey, $sha1, $this, $time );
443 } else {
444 return false; // find-by-sha1 not supported
446 if ( $img && $img->exists() ) {
447 return $img;
449 # Now try to find a matching old version of a file...
450 if ( $time !== false && $this->oldFileFactoryKey ) { // find-by-sha1 supported?
451 $img = call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
452 if ( $img && $img->exists() ) {
453 if ( !$img->isDeleted( File::DELETED_FILE ) ) {
454 return $img; // always OK
455 } elseif ( !empty( $options['private'] ) && $img->userCan( File::DELETED_FILE ) ) {
456 return $img;
460 return false;
464 * Get an array or iterator of file objects for files that have a given
465 * SHA-1 content hash.
467 * STUB
468 * @param $hash
469 * @return array
471 public function findBySha1( $hash ) {
472 return array();
476 * Get an array of arrays or iterators of file objects for files that
477 * have the given SHA-1 content hashes.
479 * @param array $hashes An array of hashes
480 * @return array An Array of arrays or iterators of file objects and the hash as key
482 public function findBySha1s( array $hashes ) {
483 $result = array();
484 foreach ( $hashes as $hash ) {
485 $files = $this->findBySha1( $hash );
486 if ( count( $files ) ) {
487 $result[$hash] = $files;
490 return $result;
494 * Return an array of files where the name starts with $prefix.
496 * STUB
497 * @param string $prefix The prefix to search for
498 * @param int $limit The maximum amount of files to return
499 * @return array
501 public function findFilesByPrefix( $prefix, $limit ) {
502 return array();
506 * Get the public root URL of the repository
508 * @deprecated since 1.20
509 * @return string
511 public function getRootUrl() {
512 return $this->getZoneUrl( 'public' );
516 * Get the URL of thumb.php
518 * @return string
520 public function getThumbScriptUrl() {
521 return $this->thumbScriptUrl;
525 * Returns true if the repository can transform files via a 404 handler
527 * @return bool
529 public function canTransformVia404() {
530 return $this->transformVia404;
534 * Get the name of an image from its title object
536 * @param $title Title
537 * @return String
539 public function getNameFromTitle( Title $title ) {
540 global $wgContLang;
541 if ( $this->initialCapital != MWNamespace::isCapitalized( NS_FILE ) ) {
542 $name = $title->getUserCaseDBKey();
543 if ( $this->initialCapital ) {
544 $name = $wgContLang->ucfirst( $name );
546 } else {
547 $name = $title->getDBkey();
549 return $name;
553 * Get the public zone root storage directory of the repository
555 * @return string
557 public function getRootDirectory() {
558 return $this->getZonePath( 'public' );
562 * Get a relative path including trailing slash, e.g. f/fa/
563 * If the repo is not hashed, returns an empty string
565 * @param string $name Name of file
566 * @return string
568 public function getHashPath( $name ) {
569 return self::getHashPathForLevel( $name, $this->hashLevels );
573 * Get a relative path including trailing slash, e.g. f/fa/
574 * If the repo is not hashed, returns an empty string
576 * @param string $suffix Basename of file from FileRepo::storeTemp()
577 * @return string
579 public function getTempHashPath( $suffix ) {
580 $parts = explode( '!', $suffix, 2 ); // format is <timestamp>!<name> or just <name>
581 $name = isset( $parts[1] ) ? $parts[1] : $suffix; // hash path is not based on timestamp
582 return self::getHashPathForLevel( $name, $this->hashLevels );
586 * @param $name
587 * @param $levels
588 * @return string
590 protected static function getHashPathForLevel( $name, $levels ) {
591 if ( $levels == 0 ) {
592 return '';
593 } else {
594 $hash = md5( $name );
595 $path = '';
596 for ( $i = 1; $i <= $levels; $i++ ) {
597 $path .= substr( $hash, 0, $i ) . '/';
599 return $path;
604 * Get the number of hash directory levels
606 * @return integer
608 public function getHashLevels() {
609 return $this->hashLevels;
613 * Get the name of this repository, as specified by $info['name]' to the constructor
615 * @return string
617 public function getName() {
618 return $this->name;
622 * Make an url to this repo
624 * @param $query mixed Query string to append
625 * @param string $entry Entry point; defaults to index
626 * @return string|bool False on failure
628 public function makeUrl( $query = '', $entry = 'index' ) {
629 if ( isset( $this->scriptDirUrl ) ) {
630 $ext = isset( $this->scriptExtension ) ? $this->scriptExtension : '.php';
631 return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}{$ext}", $query );
633 return false;
637 * Get the URL of an image description page. May return false if it is
638 * unknown or not applicable. In general this should only be called by the
639 * File class, since it may return invalid results for certain kinds of
640 * repositories. Use File::getDescriptionUrl() in user code.
642 * In particular, it uses the article paths as specified to the repository
643 * constructor, whereas local repositories use the local Title functions.
645 * @param $name string
646 * @return string
648 public function getDescriptionUrl( $name ) {
649 $encName = wfUrlencode( $name );
650 if ( !is_null( $this->descBaseUrl ) ) {
651 # "http://example.com/wiki/Image:"
652 return $this->descBaseUrl . $encName;
654 if ( !is_null( $this->articleUrl ) ) {
655 # "http://example.com/wiki/$1"
657 # We use "Image:" as the canonical namespace for
658 # compatibility across all MediaWiki versions.
659 return str_replace( '$1',
660 "Image:$encName", $this->articleUrl );
662 if ( !is_null( $this->scriptDirUrl ) ) {
663 # "http://example.com/w"
665 # We use "Image:" as the canonical namespace for
666 # compatibility across all MediaWiki versions,
667 # and just sort of hope index.php is right. ;)
668 return $this->makeUrl( "title=Image:$encName" );
670 return false;
674 * Get the URL of the content-only fragment of the description page. For
675 * MediaWiki this means action=render. This should only be called by the
676 * repository's file class, since it may return invalid results. User code
677 * should use File::getDescriptionText().
679 * @param string $name name of image to fetch
680 * @param string $lang language to fetch it in, if any.
681 * @return string
683 public function getDescriptionRenderUrl( $name, $lang = null ) {
684 $query = 'action=render';
685 if ( !is_null( $lang ) ) {
686 $query .= '&uselang=' . $lang;
688 if ( isset( $this->scriptDirUrl ) ) {
689 return $this->makeUrl(
690 'title=' .
691 wfUrlencode( 'Image:' . $name ) .
692 "&$query" );
693 } else {
694 $descUrl = $this->getDescriptionUrl( $name );
695 if ( $descUrl ) {
696 return wfAppendQuery( $descUrl, $query );
697 } else {
698 return false;
704 * Get the URL of the stylesheet to apply to description pages
706 * @return string|bool False on failure
708 public function getDescriptionStylesheetUrl() {
709 if ( isset( $this->scriptDirUrl ) ) {
710 return $this->makeUrl( 'title=MediaWiki:Filepage.css&' .
711 wfArrayToCgi( Skin::getDynamicStylesheetQuery() ) );
713 return false;
717 * Store a file to a given destination.
719 * @param string $srcPath source file system path, storage path, or virtual URL
720 * @param string $dstZone destination zone
721 * @param string $dstRel destination relative path
722 * @param $flags Integer: bitwise combination of the following flags:
723 * self::DELETE_SOURCE Delete the source file after upload
724 * self::OVERWRITE Overwrite an existing destination file instead of failing
725 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
726 * same contents as the source
727 * self::SKIP_LOCKING Skip any file locking when doing the store
728 * @return FileRepoStatus
730 public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
731 $this->assertWritableRepo(); // fail out if read-only
733 $status = $this->storeBatch( array( array( $srcPath, $dstZone, $dstRel ) ), $flags );
734 if ( $status->successCount == 0 ) {
735 $status->ok = false;
738 return $status;
742 * Store a batch of files
744 * @param array $triplets (src, dest zone, dest rel) triplets as per store()
745 * @param $flags Integer: bitwise combination of the following flags:
746 * self::DELETE_SOURCE Delete the source file after upload
747 * self::OVERWRITE Overwrite an existing destination file instead of failing
748 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
749 * same contents as the source
750 * self::SKIP_LOCKING Skip any file locking when doing the store
751 * @throws MWException
752 * @return FileRepoStatus
754 public function storeBatch( array $triplets, $flags = 0 ) {
755 $this->assertWritableRepo(); // fail out if read-only
757 $status = $this->newGood();
758 $backend = $this->backend; // convenience
760 $operations = array();
761 $sourceFSFilesToDelete = array(); // cleanup for disk source files
762 // Validate each triplet and get the store operation...
763 foreach ( $triplets as $triplet ) {
764 list( $srcPath, $dstZone, $dstRel ) = $triplet;
765 wfDebug( __METHOD__
766 . "( \$src='$srcPath', \$dstZone='$dstZone', \$dstRel='$dstRel' )\n"
769 // Resolve destination path
770 $root = $this->getZonePath( $dstZone );
771 if ( !$root ) {
772 throw new MWException( "Invalid zone: $dstZone" );
774 if ( !$this->validateFilename( $dstRel ) ) {
775 throw new MWException( 'Validation error in $dstRel' );
777 $dstPath = "$root/$dstRel";
778 $dstDir = dirname( $dstPath );
779 // Create destination directories for this triplet
780 if ( !$this->initDirectory( $dstDir )->isOK() ) {
781 return $this->newFatal( 'directorycreateerror', $dstDir );
784 // Resolve source to a storage path if virtual
785 $srcPath = $this->resolveToStoragePath( $srcPath );
787 // Get the appropriate file operation
788 if ( FileBackend::isStoragePath( $srcPath ) ) {
789 $opName = ( $flags & self::DELETE_SOURCE ) ? 'move' : 'copy';
790 } else {
791 $opName = 'store';
792 if ( $flags & self::DELETE_SOURCE ) {
793 $sourceFSFilesToDelete[] = $srcPath;
796 $operations[] = array(
797 'op' => $opName,
798 'src' => $srcPath,
799 'dst' => $dstPath,
800 'overwrite' => $flags & self::OVERWRITE,
801 'overwriteSame' => $flags & self::OVERWRITE_SAME,
805 // Execute the store operation for each triplet
806 $opts = array( 'force' => true );
807 if ( $flags & self::SKIP_LOCKING ) {
808 $opts['nonLocking'] = true;
810 $status->merge( $backend->doOperations( $operations, $opts ) );
811 // Cleanup for disk source files...
812 foreach ( $sourceFSFilesToDelete as $file ) {
813 wfSuppressWarnings();
814 unlink( $file ); // FS cleanup
815 wfRestoreWarnings();
818 return $status;
822 * Deletes a batch of files.
823 * Each file can be a (zone, rel) pair, virtual url, storage path.
824 * It will try to delete each file, but ignores any errors that may occur.
826 * @param array $files List of files to delete
827 * @param $flags Integer: bitwise combination of the following flags:
828 * self::SKIP_LOCKING Skip any file locking when doing the deletions
829 * @return FileRepoStatus
831 public function cleanupBatch( array $files, $flags = 0 ) {
832 $this->assertWritableRepo(); // fail out if read-only
834 $status = $this->newGood();
836 $operations = array();
837 foreach ( $files as $path ) {
838 if ( is_array( $path ) ) {
839 // This is a pair, extract it
840 list( $zone, $rel ) = $path;
841 $path = $this->getZonePath( $zone ) . "/$rel";
842 } else {
843 // Resolve source to a storage path if virtual
844 $path = $this->resolveToStoragePath( $path );
846 $operations[] = array( 'op' => 'delete', 'src' => $path );
848 // Actually delete files from storage...
849 $opts = array( 'force' => true );
850 if ( $flags & self::SKIP_LOCKING ) {
851 $opts['nonLocking'] = true;
853 $status->merge( $this->backend->doOperations( $operations, $opts ) );
855 return $status;
859 * Import a file from the local file system into the repo.
860 * This does no locking nor journaling and overrides existing files.
861 * This function can be used to write to otherwise read-only foreign repos.
862 * This is intended for copying generated thumbnails into the repo.
864 * @param string $src Source file system path, storage path, or virtual URL
865 * @param string $dst Virtual URL or storage path
866 * @param string|null $disposition Content-Disposition if given and supported
867 * @return FileRepoStatus
869 final public function quickImport( $src, $dst, $disposition = null ) {
870 return $this->quickImportBatch( array( array( $src, $dst, $disposition ) ) );
874 * Purge a file from the repo. This does no locking nor journaling.
875 * This function can be used to write to otherwise read-only foreign repos.
876 * This is intended for purging thumbnails.
878 * @param string $path Virtual URL or storage path
879 * @return FileRepoStatus
881 final public function quickPurge( $path ) {
882 return $this->quickPurgeBatch( array( $path ) );
886 * Deletes a directory if empty.
887 * This function can be used to write to otherwise read-only foreign repos.
889 * @param string $dir Virtual URL (or storage path) of directory to clean
890 * @return Status
892 public function quickCleanDir( $dir ) {
893 $status = $this->newGood();
894 $status->merge( $this->backend->clean(
895 array( 'dir' => $this->resolveToStoragePath( $dir ) ) ) );
897 return $status;
901 * Import a batch of files from the local file system into the repo.
902 * This does no locking nor journaling and overrides existing files.
903 * This function can be used to write to otherwise read-only foreign repos.
904 * This is intended for copying generated thumbnails into the repo.
906 * All path parameters may be a file system path, storage path, or virtual URL.
907 * When "dispositions" are given they are used as Content-Disposition if supported.
909 * @param array $triples List of (source path, destination path, disposition)
910 * @return FileRepoStatus
912 public function quickImportBatch( array $triples ) {
913 $status = $this->newGood();
914 $operations = array();
915 foreach ( $triples as $triple ) {
916 list( $src, $dst ) = $triple;
917 $src = $this->resolveToStoragePath( $src );
918 $dst = $this->resolveToStoragePath( $dst );
919 $operations[] = array(
920 'op' => FileBackend::isStoragePath( $src ) ? 'copy' : 'store',
921 'src' => $src,
922 'dst' => $dst,
923 'disposition' => isset( $triple[2] ) ? $triple[2] : null
925 $status->merge( $this->initDirectory( dirname( $dst ) ) );
927 $status->merge( $this->backend->doQuickOperations( $operations ) );
929 return $status;
933 * Purge a batch of files from the repo.
934 * This function can be used to write to otherwise read-only foreign repos.
935 * This does no locking nor journaling and is intended for purging thumbnails.
937 * @param array $paths List of virtual URLs or storage paths
938 * @return FileRepoStatus
940 public function quickPurgeBatch( array $paths ) {
941 $status = $this->newGood();
942 $operations = array();
943 foreach ( $paths as $path ) {
944 $operations[] = array(
945 'op' => 'delete',
946 'src' => $this->resolveToStoragePath( $path ),
947 'ignoreMissingSource' => true
950 $status->merge( $this->backend->doQuickOperations( $operations ) );
952 return $status;
956 * Pick a random name in the temp zone and store a file to it.
957 * Returns a FileRepoStatus object with the file Virtual URL in the value,
958 * file can later be disposed using FileRepo::freeTemp().
960 * @param string $originalName the base name of the file as specified
961 * by the user. The file extension will be maintained.
962 * @param string $srcPath the current location of the file.
963 * @return FileRepoStatus object with the URL in the value.
965 public function storeTemp( $originalName, $srcPath ) {
966 $this->assertWritableRepo(); // fail out if read-only
968 $date = gmdate( "YmdHis" );
969 $hashPath = $this->getHashPath( $originalName );
970 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
971 $virtualUrl = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
973 $result = $this->quickImport( $srcPath, $virtualUrl );
974 $result->value = $virtualUrl;
976 return $result;
980 * Remove a temporary file or mark it for garbage collection
982 * @param string $virtualUrl the virtual URL returned by FileRepo::storeTemp()
983 * @return Boolean: true on success, false on failure
985 public function freeTemp( $virtualUrl ) {
986 $this->assertWritableRepo(); // fail out if read-only
988 $temp = $this->getVirtualUrl( 'temp' );
989 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
990 wfDebug( __METHOD__ . ": Invalid temp virtual URL\n" );
991 return false;
994 return $this->quickPurge( $virtualUrl )->isOK();
998 * Concatenate a list of temporary files into a target file location.
1000 * @param array $srcPaths Ordered list of source virtual URLs/storage paths
1001 * @param string $dstPath Target file system path
1002 * @param $flags Integer: bitwise combination of the following flags:
1003 * self::DELETE_SOURCE Delete the source files
1004 * @return FileRepoStatus
1006 public function concatenate( array $srcPaths, $dstPath, $flags = 0 ) {
1007 $this->assertWritableRepo(); // fail out if read-only
1009 $status = $this->newGood();
1011 $sources = array();
1012 foreach ( $srcPaths as $srcPath ) {
1013 // Resolve source to a storage path if virtual
1014 $source = $this->resolveToStoragePath( $srcPath );
1015 $sources[] = $source; // chunk to merge
1018 // Concatenate the chunks into one FS file
1019 $params = array( 'srcs' => $sources, 'dst' => $dstPath );
1020 $status->merge( $this->backend->concatenate( $params ) );
1021 if ( !$status->isOK() ) {
1022 return $status;
1025 // Delete the sources if required
1026 if ( $flags & self::DELETE_SOURCE ) {
1027 $status->merge( $this->quickPurgeBatch( $srcPaths ) );
1030 // Make sure status is OK, despite any quickPurgeBatch() fatals
1031 $status->setResult( true );
1033 return $status;
1037 * Copy or move a file either from a storage path, virtual URL,
1038 * or file system path, into this repository at the specified destination location.
1040 * Returns a FileRepoStatus object. On success, the value contains "new" or
1041 * "archived", to indicate whether the file was new with that name.
1043 * Options to $options include:
1044 * - headers : name/value map of HTTP headers to use in response to GET/HEAD requests
1046 * @param string $srcPath the source file system path, storage path, or URL
1047 * @param string $dstRel the destination relative path
1048 * @param string $archiveRel the relative path where the existing file is to
1049 * be archived, if there is one. Relative to the public zone root.
1050 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
1051 * that the source file should be deleted if possible
1052 * @param array $options Optional additional parameters
1053 * @return FileRepoStatus
1055 public function publish(
1056 $srcPath, $dstRel, $archiveRel, $flags = 0, array $options = array()
1058 $this->assertWritableRepo(); // fail out if read-only
1060 $status = $this->publishBatch(
1061 array( array( $srcPath, $dstRel, $archiveRel, $options ) ), $flags );
1062 if ( $status->successCount == 0 ) {
1063 $status->ok = false;
1065 if ( isset( $status->value[0] ) ) {
1066 $status->value = $status->value[0];
1067 } else {
1068 $status->value = false;
1071 return $status;
1075 * Publish a batch of files
1077 * @param array $ntuples (source, dest, archive) triplets or
1078 * (source, dest, archive, options) 4-tuples as per publish().
1079 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
1080 * that the source files should be deleted if possible
1081 * @throws MWException
1082 * @return FileRepoStatus
1084 public function publishBatch( array $ntuples, $flags = 0 ) {
1085 $this->assertWritableRepo(); // fail out if read-only
1087 $backend = $this->backend; // convenience
1088 // Try creating directories
1089 $status = $this->initZones( 'public' );
1090 if ( !$status->isOK() ) {
1091 return $status;
1094 $status = $this->newGood( array() );
1096 $operations = array();
1097 $sourceFSFilesToDelete = array(); // cleanup for disk source files
1098 // Validate each triplet and get the store operation...
1099 foreach ( $ntuples as $ntuple ) {
1100 list( $srcPath, $dstRel, $archiveRel ) = $ntuple;
1101 $options = isset( $ntuple[3] ) ? $ntuple[3] : array();
1102 // Resolve source to a storage path if virtual
1103 $srcPath = $this->resolveToStoragePath( $srcPath );
1104 if ( !$this->validateFilename( $dstRel ) ) {
1105 throw new MWException( 'Validation error in $dstRel' );
1107 if ( !$this->validateFilename( $archiveRel ) ) {
1108 throw new MWException( 'Validation error in $archiveRel' );
1111 $publicRoot = $this->getZonePath( 'public' );
1112 $dstPath = "$publicRoot/$dstRel";
1113 $archivePath = "$publicRoot/$archiveRel";
1115 $dstDir = dirname( $dstPath );
1116 $archiveDir = dirname( $archivePath );
1117 // Abort immediately on directory creation errors since they're likely to be repetitive
1118 if ( !$this->initDirectory( $dstDir )->isOK() ) {
1119 return $this->newFatal( 'directorycreateerror', $dstDir );
1121 if ( !$this->initDirectory( $archiveDir )->isOK() ) {
1122 return $this->newFatal( 'directorycreateerror', $archiveDir );
1125 // Set any desired headers to be use in GET/HEAD responses
1126 $headers = isset( $options['headers'] ) ? $options['headers'] : array();
1128 // Archive destination file if it exists.
1129 // This will check if the archive file also exists and fail if does.
1130 // This is a sanity check to avoid data loss. On Windows and Linux,
1131 // copy() will overwrite, so the existence check is vulnerable to
1132 // race conditions unless an functioning LockManager is used.
1133 // LocalFile also uses SELECT FOR UPDATE for synchronization.
1134 $operations[] = array(
1135 'op' => 'copy',
1136 'src' => $dstPath,
1137 'dst' => $archivePath,
1138 'ignoreMissingSource' => true
1141 // Copy (or move) the source file to the destination
1142 if ( FileBackend::isStoragePath( $srcPath ) ) {
1143 if ( $flags & self::DELETE_SOURCE ) {
1144 $operations[] = array(
1145 'op' => 'move',
1146 'src' => $srcPath,
1147 'dst' => $dstPath,
1148 'overwrite' => true, // replace current
1149 'headers' => $headers
1151 } else {
1152 $operations[] = array(
1153 'op' => 'copy',
1154 'src' => $srcPath,
1155 'dst' => $dstPath,
1156 'overwrite' => true, // replace current
1157 'headers' => $headers
1160 } else { // FS source path
1161 $operations[] = array(
1162 'op' => 'store',
1163 'src' => $srcPath,
1164 'dst' => $dstPath,
1165 'overwrite' => true, // replace current
1166 'headers' => $headers
1168 if ( $flags & self::DELETE_SOURCE ) {
1169 $sourceFSFilesToDelete[] = $srcPath;
1174 // Execute the operations for each triplet
1175 $status->merge( $backend->doOperations( $operations ) );
1176 // Find out which files were archived...
1177 foreach ( $ntuples as $i => $ntuple ) {
1178 list( , , $archiveRel ) = $ntuple;
1179 $archivePath = $this->getZonePath( 'public' ) . "/$archiveRel";
1180 if ( $this->fileExists( $archivePath ) ) {
1181 $status->value[$i] = 'archived';
1182 } else {
1183 $status->value[$i] = 'new';
1186 // Cleanup for disk source files...
1187 foreach ( $sourceFSFilesToDelete as $file ) {
1188 wfSuppressWarnings();
1189 unlink( $file ); // FS cleanup
1190 wfRestoreWarnings();
1193 return $status;
1197 * Creates a directory with the appropriate zone permissions.
1198 * Callers are responsible for doing read-only and "writable repo" checks.
1200 * @param string $dir Virtual URL (or storage path) of directory to clean
1201 * @return Status
1203 protected function initDirectory( $dir ) {
1204 $path = $this->resolveToStoragePath( $dir );
1205 list( , $container, ) = FileBackend::splitStoragePath( $path );
1207 $params = array( 'dir' => $path );
1208 if ( $this->isPrivate || $container === $this->zones['deleted']['container'] ) {
1209 # Take all available measures to prevent web accessibility of new deleted
1210 # directories, in case the user has not configured offline storage
1211 $params = array( 'noAccess' => true, 'noListing' => true ) + $params;
1214 return $this->backend->prepare( $params );
1218 * Deletes a directory if empty.
1220 * @param string $dir Virtual URL (or storage path) of directory to clean
1221 * @return Status
1223 public function cleanDir( $dir ) {
1224 $this->assertWritableRepo(); // fail out if read-only
1226 $status = $this->newGood();
1227 $status->merge( $this->backend->clean(
1228 array( 'dir' => $this->resolveToStoragePath( $dir ) ) ) );
1230 return $status;
1234 * Checks existence of a a file
1236 * @param string $file Virtual URL (or storage path) of file to check
1237 * @return bool
1239 public function fileExists( $file ) {
1240 $result = $this->fileExistsBatch( array( $file ) );
1241 return $result[0];
1245 * Checks existence of an array of files.
1247 * @param array $files Virtual URLs (or storage paths) of files to check
1248 * @return array|bool Either array of files and existence flags, or false
1250 public function fileExistsBatch( array $files ) {
1251 $result = array();
1252 foreach ( $files as $key => $file ) {
1253 $file = $this->resolveToStoragePath( $file );
1254 $result[$key] = $this->backend->fileExists( array( 'src' => $file ) );
1256 return $result;
1260 * Move a file to the deletion archive.
1261 * If no valid deletion archive exists, this may either delete the file
1262 * or throw an exception, depending on the preference of the repository
1264 * @param $srcRel Mixed: relative path for the file to be deleted
1265 * @param $archiveRel Mixed: relative path for the archive location.
1266 * Relative to a private archive directory.
1267 * @return FileRepoStatus object
1269 public function delete( $srcRel, $archiveRel ) {
1270 $this->assertWritableRepo(); // fail out if read-only
1272 return $this->deleteBatch( array( array( $srcRel, $archiveRel ) ) );
1276 * Move a group of files to the deletion archive.
1278 * If no valid deletion archive is configured, this may either delete the
1279 * file or throw an exception, depending on the preference of the repository.
1281 * The overwrite policy is determined by the repository -- currently LocalRepo
1282 * assumes a naming scheme in the deleted zone based on content hash, as
1283 * opposed to the public zone which is assumed to be unique.
1285 * @param array $sourceDestPairs of source/destination pairs. Each element
1286 * is a two-element array containing the source file path relative to the
1287 * public root in the first element, and the archive file path relative
1288 * to the deleted zone root in the second element.
1289 * @throws MWException
1290 * @return FileRepoStatus
1292 public function deleteBatch( array $sourceDestPairs ) {
1293 $this->assertWritableRepo(); // fail out if read-only
1295 // Try creating directories
1296 $status = $this->initZones( array( 'public', 'deleted' ) );
1297 if ( !$status->isOK() ) {
1298 return $status;
1301 $status = $this->newGood();
1303 $backend = $this->backend; // convenience
1304 $operations = array();
1305 // Validate filenames and create archive directories
1306 foreach ( $sourceDestPairs as $pair ) {
1307 list( $srcRel, $archiveRel ) = $pair;
1308 if ( !$this->validateFilename( $srcRel ) ) {
1309 throw new MWException( __METHOD__ . ':Validation error in $srcRel' );
1310 } elseif ( !$this->validateFilename( $archiveRel ) ) {
1311 throw new MWException( __METHOD__ . ':Validation error in $archiveRel' );
1314 $publicRoot = $this->getZonePath( 'public' );
1315 $srcPath = "{$publicRoot}/$srcRel";
1317 $deletedRoot = $this->getZonePath( 'deleted' );
1318 $archivePath = "{$deletedRoot}/{$archiveRel}";
1319 $archiveDir = dirname( $archivePath ); // does not touch FS
1321 // Create destination directories
1322 if ( !$this->initDirectory( $archiveDir )->isOK() ) {
1323 return $this->newFatal( 'directorycreateerror', $archiveDir );
1326 $operations[] = array(
1327 'op' => 'move',
1328 'src' => $srcPath,
1329 'dst' => $archivePath,
1330 // We may have 2+ identical files being deleted,
1331 // all of which will map to the same destination file
1332 'overwriteSame' => true // also see bug 31792
1336 // Move the files by execute the operations for each pair.
1337 // We're now committed to returning an OK result, which will
1338 // lead to the files being moved in the DB also.
1339 $opts = array( 'force' => true );
1340 $status->merge( $backend->doOperations( $operations, $opts ) );
1342 return $status;
1346 * Delete files in the deleted directory if they are not referenced in the filearchive table
1348 * STUB
1350 public function cleanupDeletedBatch( array $storageKeys ) {
1351 $this->assertWritableRepo();
1355 * Get a relative path for a deletion archive key,
1356 * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
1358 * @param $key string
1359 * @throws MWException
1360 * @return string
1362 public function getDeletedHashPath( $key ) {
1363 if ( strlen( $key ) < 31 ) {
1364 throw new MWException( "Invalid storage key '$key'." );
1366 $path = '';
1367 for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) {
1368 $path .= $key[$i] . '/';
1370 return $path;
1374 * If a path is a virtual URL, resolve it to a storage path.
1375 * Otherwise, just return the path as it is.
1377 * @param $path string
1378 * @return string
1379 * @throws MWException
1381 protected function resolveToStoragePath( $path ) {
1382 if ( $this->isVirtualUrl( $path ) ) {
1383 return $this->resolveVirtualUrl( $path );
1385 return $path;
1389 * Get a local FS copy of a file with a given virtual URL/storage path.
1390 * Temporary files may be purged when the file object falls out of scope.
1392 * @param $virtualUrl string
1393 * @return TempFSFile|null Returns null on failure
1395 public function getLocalCopy( $virtualUrl ) {
1396 $path = $this->resolveToStoragePath( $virtualUrl );
1397 return $this->backend->getLocalCopy( array( 'src' => $path ) );
1401 * Get a local FS file with a given virtual URL/storage path.
1402 * The file is either an original or a copy. It should not be changed.
1403 * Temporary files may be purged when the file object falls out of scope.
1405 * @param $virtualUrl string
1406 * @return FSFile|null Returns null on failure.
1408 public function getLocalReference( $virtualUrl ) {
1409 $path = $this->resolveToStoragePath( $virtualUrl );
1410 return $this->backend->getLocalReference( array( 'src' => $path ) );
1414 * Get properties of a file with a given virtual URL/storage path.
1415 * Properties should ultimately be obtained via FSFile::getProps().
1417 * @param $virtualUrl string
1418 * @return Array
1420 public function getFileProps( $virtualUrl ) {
1421 $path = $this->resolveToStoragePath( $virtualUrl );
1422 return $this->backend->getFileProps( array( 'src' => $path ) );
1426 * Get the timestamp of a file with a given virtual URL/storage path
1428 * @param $virtualUrl string
1429 * @return string|bool False on failure
1431 public function getFileTimestamp( $virtualUrl ) {
1432 $path = $this->resolveToStoragePath( $virtualUrl );
1433 return $this->backend->getFileTimestamp( array( 'src' => $path ) );
1437 * Get the size of a file with a given virtual URL/storage path
1439 * @param $virtualUrl string
1440 * @return integer|bool False on failure
1442 public function getFileSize( $virtualUrl ) {
1443 $path = $this->resolveToStoragePath( $virtualUrl );
1444 return $this->backend->getFileSize( array( 'src' => $path ) );
1448 * Get the sha1 (base 36) of a file with a given virtual URL/storage path
1450 * @param $virtualUrl string
1451 * @return string|bool
1453 public function getFileSha1( $virtualUrl ) {
1454 $path = $this->resolveToStoragePath( $virtualUrl );
1455 return $this->backend->getFileSha1Base36( array( 'src' => $path ) );
1459 * Attempt to stream a file with the given virtual URL/storage path
1461 * @param $virtualUrl string
1462 * @param array $headers Additional HTTP headers to send on success
1463 * @return bool Success
1465 public function streamFile( $virtualUrl, $headers = array() ) {
1466 $path = $this->resolveToStoragePath( $virtualUrl );
1467 $params = array( 'src' => $path, 'headers' => $headers );
1468 return $this->backend->streamFile( $params )->isOK();
1472 * Call a callback function for every public regular file in the repository.
1473 * This only acts on the current version of files, not any old versions.
1474 * May use either the database or the filesystem.
1476 * @param $callback Array|string
1477 * @return void
1479 public function enumFiles( $callback ) {
1480 $this->enumFilesInStorage( $callback );
1484 * Call a callback function for every public file in the repository.
1485 * May use either the database or the filesystem.
1487 * @param $callback Array|string
1488 * @return void
1490 protected function enumFilesInStorage( $callback ) {
1491 $publicRoot = $this->getZonePath( 'public' );
1492 $numDirs = 1 << ( $this->hashLevels * 4 );
1493 // Use a priori assumptions about directory structure
1494 // to reduce the tree height of the scanning process.
1495 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++ ) {
1496 $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
1497 $path = $publicRoot;
1498 for ( $hexPos = 0; $hexPos < $this->hashLevels; $hexPos++ ) {
1499 $path .= '/' . substr( $hexString, 0, $hexPos + 1 );
1501 $iterator = $this->backend->getFileList( array( 'dir' => $path ) );
1502 foreach ( $iterator as $name ) {
1503 // Each item returned is a public file
1504 call_user_func( $callback, "{$path}/{$name}" );
1510 * Determine if a relative path is valid, i.e. not blank or involving directory traveral
1512 * @param $filename string
1513 * @return bool
1515 public function validateFilename( $filename ) {
1516 if ( strval( $filename ) == '' ) {
1517 return false;
1519 return FileBackend::isPathTraversalFree( $filename );
1523 * Get a callback function to use for cleaning error message parameters
1525 * @return Array
1527 function getErrorCleanupFunction() {
1528 switch ( $this->pathDisclosureProtection ) {
1529 case 'none':
1530 case 'simple': // b/c
1531 $callback = array( $this, 'passThrough' );
1532 break;
1533 default: // 'paranoid'
1534 $callback = array( $this, 'paranoidClean' );
1536 return $callback;
1540 * Path disclosure protection function
1542 * @param $param string
1543 * @return string
1545 function paranoidClean( $param ) {
1546 return '[hidden]';
1550 * Path disclosure protection function
1552 * @param $param string
1553 * @return string
1555 function passThrough( $param ) {
1556 return $param;
1560 * Create a new fatal error
1562 * @return FileRepoStatus
1564 public function newFatal( $message /*, parameters...*/ ) {
1565 $params = func_get_args();
1566 array_unshift( $params, $this );
1567 return MWInit::callStaticMethod( 'FileRepoStatus', 'newFatal', $params );
1571 * Create a new good result
1573 * @param $value null|string
1574 * @return FileRepoStatus
1576 public function newGood( $value = null ) {
1577 return FileRepoStatus::newGood( $this, $value );
1581 * Checks if there is a redirect named as $title. If there is, return the
1582 * title object. If not, return false.
1583 * STUB
1585 * @param $title Title of image
1586 * @return Bool
1588 public function checkRedirect( Title $title ) {
1589 return false;
1593 * Invalidates image redirect cache related to that image
1594 * Doesn't do anything for repositories that don't support image redirects.
1596 * STUB
1597 * @param $title Title of image
1599 public function invalidateImageRedirect( Title $title ) {}
1602 * Get the human-readable name of the repo
1604 * @return string
1606 public function getDisplayName() {
1607 // We don't name our own repo, return nothing
1608 if ( $this->isLocal() ) {
1609 return null;
1611 // 'shared-repo-name-wikimediacommons' is used when $wgUseInstantCommons = true
1612 return wfMessageFallback( 'shared-repo-name-' . $this->name, 'shared-repo' )->text();
1616 * Get the portion of the file that contains the origin file name.
1617 * If that name is too long, then the name "thumbnail.<ext>" will be given.
1619 * @param $name string
1620 * @return string
1622 public function nameForThumb( $name ) {
1623 if ( strlen( $name ) > $this->abbrvThreshold ) {
1624 $ext = FileBackend::extensionFromPath( $name );
1625 $name = ( $ext == '' ) ? 'thumbnail' : "thumbnail.$ext";
1627 return $name;
1631 * Returns true if this the local file repository.
1633 * @return bool
1635 public function isLocal() {
1636 return $this->getName() == 'local';
1640 * Get a key on the primary cache for this repository.
1641 * Returns false if the repository's cache is not accessible at this site.
1642 * The parameters are the parts of the key, as for wfMemcKey().
1644 * STUB
1645 * @return bool
1647 public function getSharedCacheKey( /*...*/ ) {
1648 return false;
1652 * Get a key for this repo in the local cache domain. These cache keys are
1653 * not shared with remote instances of the repo.
1654 * The parameters are the parts of the key, as for wfMemcKey().
1656 * @return string
1658 public function getLocalCacheKey( /*...*/ ) {
1659 $args = func_get_args();
1660 array_unshift( $args, 'filerepo', $this->getName() );
1661 return call_user_func_array( 'wfMemcKey', $args );
1665 * Get an temporary FileRepo associated with this repo.
1666 * Files will be created in the temp zone of this repo and
1667 * thumbnails in a /temp subdirectory in thumb zone of this repo.
1668 * It will have the same backend as this repo.
1670 * @return TempFileRepo
1672 public function getTempRepo() {
1673 return new TempFileRepo( array(
1674 'name' => "{$this->name}-temp",
1675 'backend' => $this->backend,
1676 'zones' => array(
1677 'public' => array(
1678 'container' => $this->zones['temp']['container'],
1679 'directory' => $this->zones['temp']['directory']
1681 'thumb' => array(
1682 'container' => $this->zones['thumb']['container'],
1683 'directory' => ( $this->zones['thumb']['directory'] == '' )
1684 ? 'temp'
1685 : $this->zones['thumb']['directory'] . '/temp'
1687 'transcoded' => array(
1688 'container' => $this->zones['transcoded']['container'],
1689 'directory' => ( $this->zones['transcoded']['directory'] == '' )
1690 ? 'temp'
1691 : $this->zones['transcoded']['directory'] . '/temp'
1694 'url' => $this->getZoneUrl( 'temp' ),
1695 'thumbUrl' => $this->getZoneUrl( 'thumb' ) . '/temp',
1696 'transcodedUrl' => $this->getZoneUrl( 'transcoded' ) . '/temp',
1697 'hashLevels' => $this->hashLevels // performance
1698 ) );
1702 * Get an UploadStash associated with this repo.
1704 * @param $user User
1705 * @return UploadStash
1707 public function getUploadStash( User $user = null ) {
1708 return new UploadStash( $this, $user );
1712 * Throw an exception if this repo is read-only by design.
1713 * This does not and should not check getReadOnlyReason().
1715 * @return void
1716 * @throws MWException
1718 protected function assertWritableRepo() {}
1722 * FileRepo for temporary files created via FileRepo::getTempRepo()
1724 class TempFileRepo extends FileRepo {
1725 public function getTempRepo() {
1726 throw new MWException( "Cannot get a temp repo from a temp repo." );