3 * Base code for file repositories.
10 * Base class for file repositories
17 const DELETE_SOURCE
= 1;
19 const OVERWRITE_SAME
= 4;
20 const SKIP_LOCKING
= 8;
22 /** @var FileBackend */
24 /** @var Array Map of zones to config */
25 protected $zones = array();
27 var $thumbScriptUrl, $transformVia404;
28 var $descBaseUrl, $scriptDirUrl, $scriptExtension, $articleUrl;
29 var $fetchDescription, $initialCapital;
30 var $pathDisclosureProtection = 'simple'; // 'paranoid'
31 var $descriptionCacheExpiry, $url, $thumbUrl;
32 var $hashLevels, $deletedHashLevels;
35 * Factory functions for creating new files
36 * Override these in the base class
38 var $fileFactory = array( 'UnregisteredLocalFile', 'newFromTitle' );
39 var $oldFileFactory = false;
40 var $fileFactoryKey = false, $oldFileFactoryKey = false;
42 function __construct( Array $info = null ) {
43 // Verify required settings presence
46 ||
!array_key_exists( 'name', $info )
47 ||
!array_key_exists( 'backend', $info )
49 throw new MWException( __CLASS__
. " requires an array of options having both 'name' and 'backend' keys.\n" );
53 $this->name
= $info['name'];
54 if ( $info['backend'] instanceof FileBackend
) {
55 $this->backend
= $info['backend']; // useful for testing
57 $this->backend
= FileBackendGroup
::singleton()->get( $info['backend'] );
60 // Optional settings that can have no value
61 $optionalSettings = array(
62 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
63 'thumbScriptUrl', 'pathDisclosureProtection', 'descriptionCacheExpiry',
66 foreach ( $optionalSettings as $var ) {
67 if ( isset( $info[$var] ) ) {
68 $this->$var = $info[$var];
72 // Optional settings that have a default
73 $this->initialCapital
= isset( $info['initialCapital'] )
74 ?
$info['initialCapital']
75 : MWNamespace
::isCapitalized( NS_FILE
);
76 $this->url
= isset( $info['url'] )
78 : false; // a subclass may set the URL (e.g. ForeignAPIRepo)
79 if ( isset( $info['thumbUrl'] ) ) {
80 $this->thumbUrl
= $info['thumbUrl'];
82 $this->thumbUrl
= $this->url ?
"{$this->url}/thumb" : false;
84 $this->hashLevels
= isset( $info['hashLevels'] )
87 $this->deletedHashLevels
= isset( $info['deletedHashLevels'] )
88 ?
$info['deletedHashLevels']
90 $this->transformVia404
= !empty( $info['transformVia404'] );
91 $this->zones
= isset( $info['zones'] )
94 // Give defaults for the basic zones...
95 foreach ( array( 'public', 'thumb', 'temp', 'deleted' ) as $zone ) {
96 if ( !isset( $this->zones
[$zone] ) ) {
97 $this->zones
[$zone] = array(
98 'container' => "{$this->name}-{$zone}",
99 'directory' => '' // container root
106 * Get the file backend instance
108 * @return FileBackend
110 public function getBackend() {
111 return $this->backend
;
115 * Prepare a single zone or list of zones for usage.
116 * See initDeletedDir() for additional setup needed for the 'deleted' zone.
118 * @param $doZones Array Only do a particular zones
121 protected function initZones( $doZones = array() ) {
122 $status = $this->newGood();
123 foreach ( (array)$doZones as $zone ) {
124 $root = $this->getZonePath( $zone );
125 if ( $root === null ) {
126 throw new MWException( "No '$zone' zone defined in the {$this->name} repo." );
133 * Take all available measures to prevent web accessibility of new deleted
134 * directories, in case the user has not configured offline storage
139 protected function initDeletedDir( $dir ) {
140 $this->backend
->secure( // prevent web access & dir listings
141 array( 'dir' => $dir, 'noAccess' => true, 'noListing' => true ) );
145 * Determine if a string is an mwrepo:// URL
150 public static function isVirtualUrl( $url ) {
151 return substr( $url, 0, 9 ) == 'mwrepo://';
155 * Get a URL referring to this repository, with the private mwrepo protocol.
156 * The suffix, if supplied, is considered to be unencoded, and will be
157 * URL-encoded before being returned.
159 * @param $suffix string
162 public function getVirtualUrl( $suffix = false ) {
163 $path = 'mwrepo://' . $this->name
;
164 if ( $suffix !== false ) {
165 $path .= '/' . rawurlencode( $suffix );
171 * Get the URL corresponding to one of the four basic zones
173 * @param $zone String: one of: public, deleted, temp, thumb
174 * @return String or false
176 public function getZoneUrl( $zone ) {
181 return "{$this->url}/temp";
183 return false; // no public URL
185 return $this->thumbUrl
;
192 * Get the backend storage path corresponding to a virtual URL
197 function resolveVirtualUrl( $url ) {
198 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
199 throw new MWException( __METHOD__
.': unknown protocol' );
201 $bits = explode( '/', substr( $url, 9 ), 3 );
202 if ( count( $bits ) != 3 ) {
203 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
205 list( $repo, $zone, $rel ) = $bits;
206 if ( $repo !== $this->name
) {
207 throw new MWException( __METHOD__
.": fetching from a foreign repo is not supported" );
209 $base = $this->getZonePath( $zone );
211 throw new MWException( __METHOD__
.": invalid zone: $zone" );
213 return $base . '/' . rawurldecode( $rel );
217 * The the storage container and base path of a zone
219 * @param $zone string
220 * @return Array (container, base path) or (null, null)
222 protected function getZoneLocation( $zone ) {
223 if ( !isset( $this->zones
[$zone] ) ) {
224 return array( null, null ); // bogus
226 return array( $this->zones
[$zone]['container'], $this->zones
[$zone]['directory'] );
230 * Get the storage path corresponding to one of the zones
232 * @param $zone string
233 * @return string|null
235 public function getZonePath( $zone ) {
236 list( $container, $base ) = $this->getZoneLocation( $zone );
237 if ( $container === null ||
$base === null ) {
240 $backendName = $this->backend
->getName();
241 if ( $base != '' ) { // may not be set
244 return "mwstore://$backendName/{$container}{$base}";
248 * Create a new File object from the local repository
250 * @param $title Mixed: Title object or string
251 * @param $time Mixed: Time at which the image was uploaded.
252 * If this is specified, the returned object will be an
253 * instance of the repository's old file class instead of a
254 * current file. Repositories not supporting version control
255 * should return false if this parameter is set.
256 * @return File|null A File, or null if passed an invalid Title
258 public function newFile( $title, $time = false ) {
259 $title = File
::normalizeTitle( $title );
264 if ( $this->oldFileFactory
) {
265 return call_user_func( $this->oldFileFactory
, $title, $this, $time );
270 return call_user_func( $this->fileFactory
, $title, $this );
275 * Find an instance of the named file created at the specified time
276 * Returns false if the file does not exist. Repositories not supporting
277 * version control should return false if the time is specified.
279 * @param $title Mixed: Title object or string
280 * @param $options array Associative array of options:
281 * time: requested time for an archived image, or false for the
282 * current version. An image object will be returned which was
283 * created at the specified time.
285 * ignoreRedirect: If true, do not follow file redirects
287 * private: If true, return restricted (deleted) files if the current
288 * user is allowed to view them. Otherwise, such files will not
292 public function findFile( $title, $options = array() ) {
293 $title = File
::normalizeTitle( $title );
297 $time = isset( $options['time'] ) ?
$options['time'] : false;
298 # First try the current version of the file to see if it precedes the timestamp
299 $img = $this->newFile( $title );
303 if ( $img->exists() && ( !$time ||
$img->getTimestamp() == $time ) ) {
306 # Now try an old version of the file
307 if ( $time !== false ) {
308 $img = $this->newFile( $title, $time );
309 if ( $img && $img->exists() ) {
310 if ( !$img->isDeleted( File
::DELETED_FILE
) ) {
311 return $img; // always OK
312 } elseif ( !empty( $options['private'] ) && $img->userCan( File
::DELETED_FILE
) ) {
319 if ( !empty( $options['ignoreRedirect'] ) ) {
322 $redir = $this->checkRedirect( $title );
323 if ( $redir && $title->getNamespace() == NS_FILE
) {
324 $img = $this->newFile( $redir );
328 if ( $img->exists() ) {
329 $img->redirectedFrom( $title->getDBkey() );
337 * Find many files at once.
339 * @param $items An array of titles, or an array of findFile() options with
340 * the "title" option giving the title. Example:
342 * $findItem = array( 'title' => $title, 'private' => true );
343 * $findBatch = array( $findItem );
344 * $repo->findFiles( $findBatch );
347 public function findFiles( $items ) {
349 foreach ( $items as $item ) {
350 if ( is_array( $item ) ) {
351 $title = $item['title'];
353 unset( $options['title'] );
358 $file = $this->findFile( $title, $options );
360 $result[$file->getTitle()->getDBkey()] = $file;
367 * Find an instance of the file with this key, created at the specified time
368 * Returns false if the file does not exist. Repositories not supporting
369 * version control should return false if the time is specified.
371 * @param $sha1 String base 36 SHA-1 hash
372 * @param $options Option array, same as findFile().
375 public function findFileFromKey( $sha1, $options = array() ) {
376 $time = isset( $options['time'] ) ?
$options['time'] : false;
378 # First try to find a matching current version of a file...
379 if ( $this->fileFactoryKey
) {
380 $img = call_user_func( $this->fileFactoryKey
, $sha1, $this, $time );
382 return false; // find-by-sha1 not supported
384 if ( $img && $img->exists() ) {
387 # Now try to find a matching old version of a file...
388 if ( $time !== false && $this->oldFileFactoryKey
) { // find-by-sha1 supported?
389 $img = call_user_func( $this->oldFileFactoryKey
, $sha1, $this, $time );
390 if ( $img && $img->exists() ) {
391 if ( !$img->isDeleted( File
::DELETED_FILE
) ) {
392 return $img; // always OK
393 } elseif ( !empty( $options['private'] ) && $img->userCan( File
::DELETED_FILE
) ) {
402 * Get an array or iterator of file objects for files that have a given
403 * SHA-1 content hash.
407 public function findBySha1( $hash ) {
412 * Get the public root URL of the repository
414 * @return string|false
416 public function getRootUrl() {
421 * Returns true if the repository uses a multi-level directory structure
425 public function isHashed() {
426 return (bool)$this->hashLevels
;
430 * Get the URL of thumb.php
434 public function getThumbScriptUrl() {
435 return $this->thumbScriptUrl
;
439 * Returns true if the repository can transform files via a 404 handler
443 public function canTransformVia404() {
444 return $this->transformVia404
;
448 * Get the name of an image from its title object
450 * @param $title Title
452 public function getNameFromTitle( Title
$title ) {
454 if ( $this->initialCapital
!= MWNamespace
::isCapitalized( NS_FILE
) ) {
455 $name = $title->getUserCaseDBKey();
456 if ( $this->initialCapital
) {
457 $name = $wgContLang->ucfirst( $name );
460 $name = $title->getDBkey();
466 * Get the public zone root storage directory of the repository
470 public function getRootDirectory() {
471 return $this->getZonePath( 'public' );
475 * Get a relative path including trailing slash, e.g. f/fa/
476 * If the repo is not hashed, returns an empty string
478 * @param $name string
481 public function getHashPath( $name ) {
482 return self
::getHashPathForLevel( $name, $this->hashLevels
);
490 static function getHashPathForLevel( $name, $levels ) {
491 if ( $levels == 0 ) {
494 $hash = md5( $name );
496 for ( $i = 1; $i <= $levels; $i++
) {
497 $path .= substr( $hash, 0, $i ) . '/';
504 * Get the number of hash directory levels
508 public function getHashLevels() {
509 return $this->hashLevels
;
513 * Get the name of this repository, as specified by $info['name]' to the constructor
517 public function getName() {
522 * Make an url to this repo
524 * @param $query mixed Query string to append
525 * @param $entry string Entry point; defaults to index
526 * @return string|false
528 public function makeUrl( $query = '', $entry = 'index' ) {
529 if ( isset( $this->scriptDirUrl
) ) {
530 $ext = isset( $this->scriptExtension
) ?
$this->scriptExtension
: '.php';
531 return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}{$ext}", $query );
537 * Get the URL of an image description page. May return false if it is
538 * unknown or not applicable. In general this should only be called by the
539 * File class, since it may return invalid results for certain kinds of
540 * repositories. Use File::getDescriptionUrl() in user code.
542 * In particular, it uses the article paths as specified to the repository
543 * constructor, whereas local repositories use the local Title functions.
545 * @param $name string
548 public function getDescriptionUrl( $name ) {
549 $encName = wfUrlencode( $name );
550 if ( !is_null( $this->descBaseUrl
) ) {
551 # "http://example.com/wiki/Image:"
552 return $this->descBaseUrl
. $encName;
554 if ( !is_null( $this->articleUrl
) ) {
555 # "http://example.com/wiki/$1"
557 # We use "Image:" as the canonical namespace for
558 # compatibility across all MediaWiki versions.
559 return str_replace( '$1',
560 "Image:$encName", $this->articleUrl
);
562 if ( !is_null( $this->scriptDirUrl
) ) {
563 # "http://example.com/w"
565 # We use "Image:" as the canonical namespace for
566 # compatibility across all MediaWiki versions,
567 # and just sort of hope index.php is right. ;)
568 return $this->makeUrl( "title=Image:$encName" );
574 * Get the URL of the content-only fragment of the description page. For
575 * MediaWiki this means action=render. This should only be called by the
576 * repository's file class, since it may return invalid results. User code
577 * should use File::getDescriptionText().
579 * @param $name String: name of image to fetch
580 * @param $lang String: language to fetch it in, if any.
583 public function getDescriptionRenderUrl( $name, $lang = null ) {
584 $query = 'action=render';
585 if ( !is_null( $lang ) ) {
586 $query .= '&uselang=' . $lang;
588 if ( isset( $this->scriptDirUrl
) ) {
589 return $this->makeUrl(
591 wfUrlencode( 'Image:' . $name ) .
594 $descUrl = $this->getDescriptionUrl( $name );
596 return wfAppendQuery( $descUrl, $query );
604 * Get the URL of the stylesheet to apply to description pages
606 * @return string|false
608 public function getDescriptionStylesheetUrl() {
609 if ( isset( $this->scriptDirUrl
) ) {
610 return $this->makeUrl( 'title=MediaWiki:Filepage.css&' .
611 wfArrayToCGI( Skin
::getDynamicStylesheetQuery() ) );
617 * Store a file to a given destination.
619 * @param $srcPath String: source FS path, storage path, or virtual URL
620 * @param $dstZone String: destination zone
621 * @param $dstRel String: destination relative path
622 * @param $flags Integer: bitwise combination of the following flags:
623 * self::DELETE_SOURCE Delete the source file after upload
624 * self::OVERWRITE Overwrite an existing destination file instead of failing
625 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
626 * same contents as the source
627 * self::SKIP_LOCKING Skip any file locking when doing the store
628 * @return FileRepoStatus
630 public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
631 $status = $this->storeBatch( array( array( $srcPath, $dstZone, $dstRel ) ), $flags );
632 if ( $status->successCount
== 0 ) {
639 * Store a batch of files
641 * @param $triplets Array: (src, dest zone, dest rel) triplets as per store()
642 * @param $flags Integer: bitwise combination of the following flags:
643 * self::DELETE_SOURCE Delete the source file after upload
644 * self::OVERWRITE Overwrite an existing destination file instead of failing
645 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
646 * same contents as the source
647 * self::SKIP_LOCKING Skip any file locking when doing the store
648 * @return FileRepoStatus
650 public function storeBatch( $triplets, $flags = 0 ) {
651 $backend = $this->backend
; // convenience
653 $status = $this->newGood();
655 $operations = array();
656 $sourceFSFilesToDelete = array(); // cleanup for disk source files
657 // Validate each triplet and get the store operation...
658 foreach ( $triplets as $triplet ) {
659 list( $srcPath, $dstZone, $dstRel ) = $triplet;
661 . "( \$src='$srcPath', \$dstZone='$dstZone', \$dstRel='$dstRel' )\n"
664 // Resolve destination path
665 $root = $this->getZonePath( $dstZone );
667 throw new MWException( "Invalid zone: $dstZone" );
669 if ( !$this->validateFilename( $dstRel ) ) {
670 throw new MWException( 'Validation error in $dstRel' );
672 $dstPath = "$root/$dstRel";
673 $dstDir = dirname( $dstPath );
674 // Create destination directories for this triplet
675 if ( !$backend->prepare( array( 'dir' => $dstDir ) )->isOK() ) {
676 return $this->newFatal( 'directorycreateerror', $dstDir );
679 if ( $dstZone == 'deleted' ) {
680 $this->initDeletedDir( $dstDir );
683 // Resolve source to a storage path if virtual
684 if ( self
::isVirtualUrl( $srcPath ) ) {
685 $srcPath = $this->resolveVirtualUrl( $srcPath );
688 // Get the appropriate file operation
689 if ( FileBackend
::isStoragePath( $srcPath ) ) {
690 $opName = ( $flags & self
::DELETE_SOURCE
) ?
'move' : 'copy';
693 if ( $flags & self
::DELETE_SOURCE
) {
694 $sourceFSFilesToDelete[] = $srcPath;
697 $operations[] = array(
701 'overwrite' => $flags & self
::OVERWRITE
,
702 'overwriteSame' => $flags & self
::OVERWRITE_SAME
,
706 // Execute the store operation for each triplet
707 $opts = array( 'force' => true );
708 if ( $flags & self
::SKIP_LOCKING
) {
709 $opts['nonLocking'] = true;
711 $status->merge( $backend->doOperations( $operations, $opts ) );
712 // Cleanup for disk source files...
713 foreach ( $sourceFSFilesToDelete as $file ) {
714 wfSuppressWarnings();
715 unlink( $file ); // FS cleanup
723 * Deletes a batch of files.
724 * Each file can be a (zone, rel) pair, virtual url, storage path, or FS path.
725 * It will try to delete each file, but ignores any errors that may occur.
727 * @param $pairs array List of files to delete
730 public function cleanupBatch( $files ) {
731 $operations = array();
732 $sourceFSFilesToDelete = array(); // cleanup for disk source files
733 foreach ( $files as $file ) {
734 if ( is_array( $file ) ) {
735 // This is a pair, extract it
736 list( $zone, $rel ) = $file;
737 $root = $this->getZonePath( $zone );
738 $path = "$root/$rel";
740 if ( self
::isVirtualUrl( $file ) ) {
741 // This is a virtual url, resolve it
742 $path = $this->resolveVirtualUrl( $file );
744 // This is a full file name
748 // Get a file operation if needed
749 if ( FileBackend
::isStoragePath( $path ) ) {
750 $operations[] = array(
755 $sourceFSFilesToDelete[] = $path;
758 // Actually delete files from storage...
759 $opts = array( 'force' => true );
760 $this->backend
->doOperations( $operations, $opts );
761 // Cleanup for disk source files...
762 foreach ( $sourceFSFilesToDelete as $file ) {
763 wfSuppressWarnings();
764 unlink( $file ); // FS cleanup
770 * Pick a random name in the temp zone and store a file to it.
771 * Returns a FileRepoStatus object with the file Virtual URL in the value,
772 * file can later be disposed using FileRepo::freeTemp().
775 * @param $originalName String: the base name of the file as specified
776 * by the user. The file extension will be maintained.
777 * @param $srcPath String: the current location of the file.
778 * @return FileRepoStatus object with the URL in the value.
780 public function storeTemp( $originalName, $srcPath ) {
781 $date = gmdate( "YmdHis" );
782 $hashPath = $this->getHashPath( $originalName );
783 $dstRel = "{$hashPath}{$date}!{$originalName}";
784 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
786 $result = $this->store( $srcPath, 'temp', $dstRel, self
::SKIP_LOCKING
);
787 $result->value
= $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
792 * Concatenate a list of files into a target file location.
794 * @param $srcPaths Array Ordered list of source virtual URLs/storage paths
795 * @param $dstPath String Target file system path
796 * @param $flags Integer: bitwise combination of the following flags:
797 * self::DELETE_SOURCE Delete the source files
798 * @return FileRepoStatus
800 function concatenate( $srcPaths, $dstPath, $flags = 0 ) {
801 $status = $this->newGood();
804 $deleteOperations = array(); // post-concatenate ops
805 foreach ( $srcPaths as $srcPath ) {
806 // Resolve source to a storage path if virtual
807 $source = $this->resolveToStoragePath( $srcPath );
808 $sources[] = $source; // chunk to merge
809 if ( $flags & self
::DELETE_SOURCE
) {
810 $deleteOperations[] = array( 'op' => 'delete', 'src' => $source );
814 // Concatenate the chunks into one FS file
815 $params = array( 'srcs' => $sources, 'dst' => $dstPath );
816 $status->merge( $this->backend
->concatenate( $params ) );
817 if ( !$status->isOK() ) {
821 // Delete the sources if required
822 if ( $deleteOperations ) {
823 $opts = array( 'force' => true );
824 $status->merge( $this->backend
->doOperations( $deleteOperations, $opts ) );
827 // Make sure status is OK, despite any $deleteOperations fatals
828 $status->setResult( true );
834 * Remove a temporary file or mark it for garbage collection
836 * @param $virtualUrl String: the virtual URL returned by FileRepo::storeTemp()
837 * @return Boolean: true on success, false on failure
839 public function freeTemp( $virtualUrl ) {
840 $temp = "mwrepo://{$this->name}/temp";
841 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
842 wfDebug( __METHOD__
.": Invalid temp virtual URL\n" );
845 $path = $this->resolveVirtualUrl( $virtualUrl );
846 $op = array( 'op' => 'delete', 'src' => $path );
847 $status = $this->backend
->doOperation( $op );
848 return $status->isOK();
852 * Copy or move a file either from a storage path, virtual URL,
853 * or FS path, into this repository at the specified destination location.
855 * Returns a FileRepoStatus object. On success, the value contains "new" or
856 * "archived", to indicate whether the file was new with that name.
858 * @param $srcPath String: the source FS path, storage path, or URL
859 * @param $dstRel String: the destination relative path
860 * @param $archiveRel String: the relative path where the existing file is to
861 * be archived, if there is one. Relative to the public zone root.
862 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
863 * that the source file should be deleted if possible
865 public function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 ) {
866 $status = $this->publishBatch( array( array( $srcPath, $dstRel, $archiveRel ) ), $flags );
867 if ( $status->successCount
== 0 ) {
870 if ( isset( $status->value
[0] ) ) {
871 $status->value
= $status->value
[0];
873 $status->value
= false;
879 * Publish a batch of files
881 * @param $triplets Array: (source, dest, archive) triplets as per publish()
882 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
883 * that the source files should be deleted if possible
884 * @return FileRepoStatus
886 public function publishBatch( $triplets, $flags = 0 ) {
887 $backend = $this->backend
; // convenience
889 // Try creating directories
890 $status = $this->initZones( 'public' );
891 if ( !$status->isOK() ) {
895 $status = $this->newGood( array() );
897 $operations = array();
898 $sourceFSFilesToDelete = array(); // cleanup for disk source files
899 // Validate each triplet and get the store operation...
900 foreach ( $triplets as $i => $triplet ) {
901 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
902 // Resolve source to a storage path if virtual
903 if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) {
904 $srcPath = $this->resolveVirtualUrl( $srcPath );
906 if ( !$this->validateFilename( $dstRel ) ) {
907 throw new MWException( 'Validation error in $dstRel' );
909 if ( !$this->validateFilename( $archiveRel ) ) {
910 throw new MWException( 'Validation error in $archiveRel' );
913 $publicRoot = $this->getZonePath( 'public' );
914 $dstPath = "$publicRoot/$dstRel";
915 $archivePath = "$publicRoot/$archiveRel";
917 $dstDir = dirname( $dstPath );
918 $archiveDir = dirname( $archivePath );
919 // Abort immediately on directory creation errors since they're likely to be repetitive
920 if ( !$backend->prepare( array( 'dir' => $dstDir ) )->isOK() ) {
921 return $this->newFatal( 'directorycreateerror', $dstDir );
923 if ( !$backend->prepare( array( 'dir' => $archiveDir ) )->isOK() ) {
924 return $this->newFatal( 'directorycreateerror', $archiveDir );
927 // Archive destination file if it exists
928 if ( $backend->fileExists( array( 'src' => $dstPath ) ) ) {
929 // Check if the archive file exists
930 // This is a sanity check to avoid data loss. In UNIX, the rename primitive
931 // unlinks the destination file if it exists. DB-based synchronisation in
932 // publishBatch's caller should prevent races. In Windows there's no
933 // problem because the rename primitive fails if the destination exists.
934 if ( $backend->fileExists( array( 'src' => $archivePath ) ) ) {
935 $operations[] = array( 'op' => 'null' );
938 $operations[] = array(
941 'dst' => $archivePath
944 $status->value
[$i] = 'archived';
946 $status->value
[$i] = 'new';
948 // Copy (or move) the source file to the destination
949 if ( FileBackend
::isStoragePath( $srcPath ) ) {
950 if ( $flags & self
::DELETE_SOURCE
) {
951 $operations[] = array(
957 $operations[] = array(
963 } else { // FS source path
964 $operations[] = array(
969 if ( $flags & self
::DELETE_SOURCE
) {
970 $sourceFSFilesToDelete[] = $srcPath;
975 // Execute the operations for each triplet
976 $opts = array( 'force' => true );
977 $status->merge( $backend->doOperations( $operations, $opts ) );
978 // Cleanup for disk source files...
979 foreach ( $sourceFSFilesToDelete as $file ) {
980 wfSuppressWarnings();
981 unlink( $file ); // FS cleanup
989 * Checks existence of a a file
991 * @param $file Virtual URL (or storage path) of file to check
992 * @param $flags Integer: bitwise combination of the following flags:
993 * self::FILES_ONLY Mark file as existing only if it is a file (not directory)
996 public function fileExists( $file, $flags = 0 ) {
997 $result = $this->fileExistsBatch( array( $file ), $flags );
1002 * Checks existence of an array of files.
1004 * @param $files Array: Virtual URLs (or storage paths) of files to check
1005 * @param $flags Integer: bitwise combination of the following flags:
1006 * self::FILES_ONLY Mark file as existing only if it is a file (not directory)
1007 * @return Either array of files and existence flags, or false
1009 public function fileExistsBatch( $files, $flags = 0 ) {
1011 foreach ( $files as $key => $file ) {
1012 if ( self
::isVirtualUrl( $file ) ) {
1013 $file = $this->resolveVirtualUrl( $file );
1015 if ( FileBackend
::isStoragePath( $file ) ) {
1016 $result[$key] = $this->backend
->fileExists( array( 'src' => $file ) );
1018 if ( $flags & self
::FILES_ONLY
) {
1019 $result[$key] = is_file( $file ); // FS only
1021 $result[$key] = file_exists( $file ); // FS only
1030 * Move a file to the deletion archive.
1031 * If no valid deletion archive exists, this may either delete the file
1032 * or throw an exception, depending on the preference of the repository
1034 * @param $srcRel Mixed: relative path for the file to be deleted
1035 * @param $archiveRel Mixed: relative path for the archive location.
1036 * Relative to a private archive directory.
1037 * @return FileRepoStatus object
1039 public function delete( $srcRel, $archiveRel ) {
1040 return $this->deleteBatch( array( array( $srcRel, $archiveRel ) ) );
1044 * Move a group of files to the deletion archive.
1046 * If no valid deletion archive is configured, this may either delete the
1047 * file or throw an exception, depending on the preference of the repository.
1049 * The overwrite policy is determined by the repository -- currently LocalRepo
1050 * assumes a naming scheme in the deleted zone based on content hash, as
1051 * opposed to the public zone which is assumed to be unique.
1053 * @param $sourceDestPairs Array of source/destination pairs. Each element
1054 * is a two-element array containing the source file path relative to the
1055 * public root in the first element, and the archive file path relative
1056 * to the deleted zone root in the second element.
1057 * @return FileRepoStatus
1059 public function deleteBatch( $sourceDestPairs ) {
1060 $backend = $this->backend
; // convenience
1062 // Try creating directories
1063 $status = $this->initZones( array( 'public', 'deleted' ) );
1064 if ( !$status->isOK() ) {
1068 $status = $this->newGood();
1070 $operations = array();
1071 // Validate filenames and create archive directories
1072 foreach ( $sourceDestPairs as $pair ) {
1073 list( $srcRel, $archiveRel ) = $pair;
1074 if ( !$this->validateFilename( $srcRel ) ) {
1075 throw new MWException( __METHOD__
.':Validation error in $srcRel' );
1077 if ( !$this->validateFilename( $archiveRel ) ) {
1078 throw new MWException( __METHOD__
.':Validation error in $archiveRel' );
1081 $publicRoot = $this->getZonePath( 'public' );
1082 $srcPath = "{$publicRoot}/$srcRel";
1084 $deletedRoot = $this->getZonePath( 'deleted' );
1085 $archivePath = "{$deletedRoot}/{$archiveRel}";
1086 $archiveDir = dirname( $archivePath ); // does not touch FS
1088 // Create destination directories
1089 if ( !$backend->prepare( array( 'dir' => $archiveDir ) )->isOK() ) {
1090 return $this->newFatal( 'directorycreateerror', $archiveDir );
1092 $this->initDeletedDir( $archiveDir );
1094 $operations[] = array(
1097 'dst' => $archivePath,
1098 // We may have 2+ identical files being deleted,
1099 // all of which will map to the same destination file
1100 'overwriteSame' => true // also see bug 31792
1104 // Move the files by execute the operations for each pair.
1105 // We're now committed to returning an OK result, which will
1106 // lead to the files being moved in the DB also.
1107 $opts = array( 'force' => true );
1108 $status->merge( $backend->doOperations( $operations, $opts ) );
1114 * Get a relative path for a deletion archive key,
1115 * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
1119 public function getDeletedHashPath( $key ) {
1121 for ( $i = 0; $i < $this->deletedHashLevels
; $i++
) {
1122 $path .= $key[$i] . '/';
1128 * If a path is a virtual URL, resolve it to a storage path.
1129 * Otherwise, just return the path as it is.
1131 * @param $path string
1133 * @throws MWException
1135 protected function resolveToStoragePath( $path ) {
1136 if ( $this->isVirtualUrl( $path ) ) {
1137 return $this->resolveVirtualUrl( $path );
1143 * Get a local FS copy of a file with a given virtual URL/storage path.
1144 * Temporary files may be purged when the file object falls out of scope.
1146 * @param $virtualUrl string
1147 * @return TempFSFile|null Returns null on failure
1149 public function getLocalCopy( $virtualUrl ) {
1150 $path = $this->resolveToStoragePath( $virtualUrl );
1151 return $this->backend
->getLocalCopy( array( 'src' => $path ) );
1155 * Get a local FS file with a given virtual URL/storage path.
1156 * The file is either an original or a copy. It should not be changed.
1157 * Temporary files may be purged when the file object falls out of scope.
1159 * @param $virtualUrl string
1160 * @return FSFile|null Returns null on failure.
1162 public function getLocalReference( $virtualUrl ) {
1163 $path = $this->resolveToStoragePath( $virtualUrl );
1164 return $this->backend
->getLocalReference( array( 'src' => $path ) );
1168 * Get properties of a file with a given virtual URL/storage path.
1169 * Properties should ultimately be obtained via FSFile::getProps().
1171 * @param $virtualUrl string
1174 public function getFileProps( $virtualUrl ) {
1175 $path = $this->resolveToStoragePath( $virtualUrl );
1176 return $this->backend
->getFileProps( array( 'src' => $path ) );
1180 * Get the timestamp of a file with a given virtual URL/storage path
1182 * @param $virtualUrl string
1183 * @return string|false
1185 public function getFileTimestamp( $virtualUrl ) {
1186 $path = $this->resolveToStoragePath( $virtualUrl );
1187 return $this->backend
->getFileTimestamp( array( 'src' => $path ) );
1191 * Get the sha1 of a file with a given virtual URL/storage path
1193 * @param $virtualUrl string
1194 * @return string|false
1196 public function getFileSha1( $virtualUrl ) {
1197 $path = $this->resolveToStoragePath( $virtualUrl );
1198 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $path ) );
1202 return $tmpFile->getSha1Base36();
1206 * Attempt to stream a file with the given virtual URL/storage path
1208 * @param $virtualUrl string
1209 * @param $headers Array Additional HTTP headers to send on success
1210 * @return bool Success
1212 public function streamFile( $virtualUrl, $headers = array() ) {
1213 $path = $this->resolveToStoragePath( $virtualUrl );
1214 $params = array( 'src' => $path, 'headers' => $headers );
1215 return $this->backend
->streamFile( $params )->isOK();
1219 * Call a callback function for every public regular file in the repository.
1220 * This only acts on the current version of files, not any old versions.
1221 * May use either the database or the filesystem.
1223 * @param $callback Array|string
1226 public function enumFiles( $callback ) {
1227 $this->enumFilesInStorage( $callback );
1231 * Call a callback function for every public file in the repository.
1232 * May use either the database or the filesystem.
1234 * @param $callback Array|string
1237 protected function enumFilesInStorage( $callback ) {
1238 $publicRoot = $this->getZonePath( 'public' );
1239 $numDirs = 1 << ( $this->hashLevels
* 4 );
1240 // Use a priori assumptions about directory structure
1241 // to reduce the tree height of the scanning process.
1242 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++
) {
1243 $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
1244 $path = $publicRoot;
1245 for ( $hexPos = 0; $hexPos < $this->hashLevels
; $hexPos++
) {
1246 $path .= '/' . substr( $hexString, 0, $hexPos +
1 );
1248 $iterator = $this->backend
->getFileList( array( 'dir' => $path ) );
1249 foreach ( $iterator as $name ) {
1250 // Each item returned is a public file
1251 call_user_func( $callback, "{$path}/{$name}" );
1257 * Determine if a relative path is valid, i.e. not blank or involving directory traveral
1259 * @param $filename string
1262 public function validateFilename( $filename ) {
1263 if ( strval( $filename ) == '' ) {
1266 if ( wfIsWindows() ) {
1267 $filename = strtr( $filename, '\\', '/' );
1270 * Use the same traversal protection as Title::secureAndSplit()
1272 if ( strpos( $filename, '.' ) !== false &&
1273 ( $filename === '.' ||
$filename === '..' ||
1274 strpos( $filename, './' ) === 0 ||
1275 strpos( $filename, '../' ) === 0 ||
1276 strpos( $filename, '/./' ) !== false ||
1277 strpos( $filename, '/../' ) !== false ) )
1286 * Get a callback function to use for cleaning error message parameters
1290 function getErrorCleanupFunction() {
1291 switch ( $this->pathDisclosureProtection
) {
1293 $callback = array( $this, 'passThrough' );
1296 $callback = array( $this, 'simpleClean' );
1298 default: // 'paranoid'
1299 $callback = array( $this, 'paranoidClean' );
1305 * Path disclosure protection function
1307 * @param $param string
1310 function paranoidClean( $param ) {
1315 * Path disclosure protection function
1317 * @param $param string
1320 function simpleClean( $param ) {
1322 if ( !isset( $this->simpleCleanPairs
) ) {
1323 $this->simpleCleanPairs
= array(
1324 $IP => '$IP', // sanity
1327 return strtr( $param, $this->simpleCleanPairs
);
1331 * Path disclosure protection function
1333 * @param $param string
1336 function passThrough( $param ) {
1341 * Create a new fatal error
1343 * @return FileRepoStatus
1345 function newFatal( $message /*, parameters...*/ ) {
1346 $params = func_get_args();
1347 array_unshift( $params, $this );
1348 return MWInit
::callStaticMethod( 'FileRepoStatus', 'newFatal', $params );
1352 * Create a new good result
1354 * @return FileRepoStatus
1356 function newGood( $value = null ) {
1357 return FileRepoStatus
::newGood( $this, $value );
1361 * Delete files in the deleted directory if they are not referenced in the filearchive table
1365 public function cleanupDeletedBatch( $storageKeys ) {}
1368 * Checks if there is a redirect named as $title. If there is, return the
1369 * title object. If not, return false.
1372 * @param $title Title of image
1375 public function checkRedirect( Title
$title ) {
1380 * Invalidates image redirect cache related to that image
1381 * Doesn't do anything for repositories that don't support image redirects.
1384 * @param $title Title of image
1386 public function invalidateImageRedirect( Title
$title ) {}
1389 * Get the human-readable name of the repo
1393 public function getDisplayName() {
1394 // We don't name our own repo, return nothing
1395 if ( $this->isLocal() ) {
1398 // 'shared-repo-name-wikimediacommons' is used when $wgUseInstantCommons = true
1399 return wfMessageFallback( 'shared-repo-name-' . $this->name
, 'shared-repo' )->text();
1403 * Returns true if this the local file repository.
1407 public function isLocal() {
1408 return $this->getName() == 'local';
1412 * Get a key on the primary cache for this repository.
1413 * Returns false if the repository's cache is not accessible at this site.
1414 * The parameters are the parts of the key, as for wfMemcKey().
1418 function getSharedCacheKey( /*...*/ ) {
1423 * Get a key for this repo in the local cache domain. These cache keys are
1424 * not shared with remote instances of the repo.
1425 * The parameters are the parts of the key, as for wfMemcKey().
1429 function getLocalCacheKey( /*...*/ ) {
1430 $args = func_get_args();
1431 array_unshift( $args, 'filerepo', $this->getName() );
1432 return call_user_func_array( 'wfMemcKey', $args );
1436 * Get an UploadStash associated with this repo.
1438 * @return UploadStash
1440 public function getUploadStash() {
1441 return new UploadStash( $this );