3 * @defgroup FileRepo File Repository
5 * @brief This module handles how MediaWiki interacts with filesystems.
11 * Base code for file repositories.
18 * Base class for file repositories
25 const DELETE_SOURCE
= 1;
27 const OVERWRITE_SAME
= 4;
28 const SKIP_LOCKING
= 8;
30 /** @var FileBackend */
32 /** @var Array Map of zones to config */
33 protected $zones = array();
35 var $thumbScriptUrl, $transformVia404;
36 var $descBaseUrl, $scriptDirUrl, $scriptExtension, $articleUrl;
37 var $fetchDescription, $initialCapital;
38 var $pathDisclosureProtection = 'simple'; // 'paranoid'
39 var $descriptionCacheExpiry, $url, $thumbUrl;
40 var $hashLevels, $deletedHashLevels;
43 * Factory functions for creating new files
44 * Override these in the base class
46 var $fileFactory = array( 'UnregisteredLocalFile', 'newFromTitle' );
47 var $oldFileFactory = false;
48 var $fileFactoryKey = false, $oldFileFactoryKey = false;
50 function __construct( Array $info = null ) {
51 // Verify required settings presence
54 ||
!array_key_exists( 'name', $info )
55 ||
!array_key_exists( 'backend', $info )
57 throw new MWException( __CLASS__
. " requires an array of options having both 'name' and 'backend' keys.\n" );
61 $this->name
= $info['name'];
62 if ( $info['backend'] instanceof FileBackend
) {
63 $this->backend
= $info['backend']; // useful for testing
65 $this->backend
= FileBackendGroup
::singleton()->get( $info['backend'] );
68 // Optional settings that can have no value
69 $optionalSettings = array(
70 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
71 'thumbScriptUrl', 'pathDisclosureProtection', 'descriptionCacheExpiry',
74 foreach ( $optionalSettings as $var ) {
75 if ( isset( $info[$var] ) ) {
76 $this->$var = $info[$var];
80 // Optional settings that have a default
81 $this->initialCapital
= isset( $info['initialCapital'] )
82 ?
$info['initialCapital']
83 : MWNamespace
::isCapitalized( NS_FILE
);
84 $this->url
= isset( $info['url'] )
86 : false; // a subclass may set the URL (e.g. ForeignAPIRepo)
87 if ( isset( $info['thumbUrl'] ) ) {
88 $this->thumbUrl
= $info['thumbUrl'];
90 $this->thumbUrl
= $this->url ?
"{$this->url}/thumb" : false;
92 $this->hashLevels
= isset( $info['hashLevels'] )
95 $this->deletedHashLevels
= isset( $info['deletedHashLevels'] )
96 ?
$info['deletedHashLevels']
98 $this->transformVia404
= !empty( $info['transformVia404'] );
99 $this->zones
= isset( $info['zones'] )
102 // Give defaults for the basic zones...
103 foreach ( array( 'public', 'thumb', 'temp', 'deleted' ) as $zone ) {
104 if ( !isset( $this->zones
[$zone] ) ) {
105 $this->zones
[$zone] = array(
106 'container' => "{$this->name}-{$zone}",
107 'directory' => '' // container root
114 * Get the file backend instance
116 * @return FileBackend
118 public function getBackend() {
119 return $this->backend
;
123 * Prepare a single zone or list of zones for usage.
124 * See initDeletedDir() for additional setup needed for the 'deleted' zone.
126 * @param $doZones Array Only do a particular zones
129 protected function initZones( $doZones = array() ) {
130 $status = $this->newGood();
131 foreach ( (array)$doZones as $zone ) {
132 $root = $this->getZonePath( $zone );
133 if ( $root === null ) {
134 throw new MWException( "No '$zone' zone defined in the {$this->name} repo." );
141 * Take all available measures to prevent web accessibility of new deleted
142 * directories, in case the user has not configured offline storage
147 protected function initDeletedDir( $dir ) {
148 $this->backend
->secure( // prevent web access & dir listings
149 array( 'dir' => $dir, 'noAccess' => true, 'noListing' => true ) );
153 * Determine if a string is an mwrepo:// URL
158 public static function isVirtualUrl( $url ) {
159 return substr( $url, 0, 9 ) == 'mwrepo://';
163 * Get a URL referring to this repository, with the private mwrepo protocol.
164 * The suffix, if supplied, is considered to be unencoded, and will be
165 * URL-encoded before being returned.
167 * @param $suffix string
170 public function getVirtualUrl( $suffix = false ) {
171 $path = 'mwrepo://' . $this->name
;
172 if ( $suffix !== false ) {
173 $path .= '/' . rawurlencode( $suffix );
179 * Get the URL corresponding to one of the four basic zones
181 * @param $zone String: one of: public, deleted, temp, thumb
182 * @return String or false
184 public function getZoneUrl( $zone ) {
189 return "{$this->url}/temp";
191 return false; // no public URL
193 return $this->thumbUrl
;
200 * Get the backend storage path corresponding to a virtual URL
205 function resolveVirtualUrl( $url ) {
206 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
207 throw new MWException( __METHOD__
.': unknown protocol' );
209 $bits = explode( '/', substr( $url, 9 ), 3 );
210 if ( count( $bits ) != 3 ) {
211 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
213 list( $repo, $zone, $rel ) = $bits;
214 if ( $repo !== $this->name
) {
215 throw new MWException( __METHOD__
.": fetching from a foreign repo is not supported" );
217 $base = $this->getZonePath( $zone );
219 throw new MWException( __METHOD__
.": invalid zone: $zone" );
221 return $base . '/' . rawurldecode( $rel );
225 * The the storage container and base path of a zone
227 * @param $zone string
228 * @return Array (container, base path) or (null, null)
230 protected function getZoneLocation( $zone ) {
231 if ( !isset( $this->zones
[$zone] ) ) {
232 return array( null, null ); // bogus
234 return array( $this->zones
[$zone]['container'], $this->zones
[$zone]['directory'] );
238 * Get the storage path corresponding to one of the zones
240 * @param $zone string
241 * @return string|null
243 public function getZonePath( $zone ) {
244 list( $container, $base ) = $this->getZoneLocation( $zone );
245 if ( $container === null ||
$base === null ) {
248 $backendName = $this->backend
->getName();
249 if ( $base != '' ) { // may not be set
252 return "mwstore://$backendName/{$container}{$base}";
256 * Create a new File object from the local repository
258 * @param $title Mixed: Title object or string
259 * @param $time Mixed: Time at which the image was uploaded.
260 * If this is specified, the returned object will be an
261 * instance of the repository's old file class instead of a
262 * current file. Repositories not supporting version control
263 * should return false if this parameter is set.
264 * @return File|null A File, or null if passed an invalid Title
266 public function newFile( $title, $time = false ) {
267 $title = File
::normalizeTitle( $title );
272 if ( $this->oldFileFactory
) {
273 return call_user_func( $this->oldFileFactory
, $title, $this, $time );
278 return call_user_func( $this->fileFactory
, $title, $this );
283 * Find an instance of the named file created at the specified time
284 * Returns false if the file does not exist. Repositories not supporting
285 * version control should return false if the time is specified.
287 * @param $title Mixed: Title object or string
288 * @param $options array Associative array of options:
289 * time: requested time for an archived image, or false for the
290 * current version. An image object will be returned which was
291 * created at the specified time.
293 * ignoreRedirect: If true, do not follow file redirects
295 * private: If true, return restricted (deleted) files if the current
296 * user is allowed to view them. Otherwise, such files will not
300 public function findFile( $title, $options = array() ) {
301 $title = File
::normalizeTitle( $title );
305 $time = isset( $options['time'] ) ?
$options['time'] : false;
306 # First try the current version of the file to see if it precedes the timestamp
307 $img = $this->newFile( $title );
311 if ( $img->exists() && ( !$time ||
$img->getTimestamp() == $time ) ) {
314 # Now try an old version of the file
315 if ( $time !== false ) {
316 $img = $this->newFile( $title, $time );
317 if ( $img && $img->exists() ) {
318 if ( !$img->isDeleted( File
::DELETED_FILE
) ) {
319 return $img; // always OK
320 } elseif ( !empty( $options['private'] ) && $img->userCan( File
::DELETED_FILE
) ) {
327 if ( !empty( $options['ignoreRedirect'] ) ) {
330 $redir = $this->checkRedirect( $title );
331 if ( $redir && $title->getNamespace() == NS_FILE
) {
332 $img = $this->newFile( $redir );
336 if ( $img->exists() ) {
337 $img->redirectedFrom( $title->getDBkey() );
345 * Find many files at once.
347 * @param $items An array of titles, or an array of findFile() options with
348 * the "title" option giving the title. Example:
350 * $findItem = array( 'title' => $title, 'private' => true );
351 * $findBatch = array( $findItem );
352 * $repo->findFiles( $findBatch );
355 public function findFiles( $items ) {
357 foreach ( $items as $item ) {
358 if ( is_array( $item ) ) {
359 $title = $item['title'];
361 unset( $options['title'] );
366 $file = $this->findFile( $title, $options );
368 $result[$file->getTitle()->getDBkey()] = $file;
375 * Find an instance of the file with this key, created at the specified time
376 * Returns false if the file does not exist. Repositories not supporting
377 * version control should return false if the time is specified.
379 * @param $sha1 String base 36 SHA-1 hash
380 * @param $options Option array, same as findFile().
383 public function findFileFromKey( $sha1, $options = array() ) {
384 $time = isset( $options['time'] ) ?
$options['time'] : false;
386 # First try to find a matching current version of a file...
387 if ( $this->fileFactoryKey
) {
388 $img = call_user_func( $this->fileFactoryKey
, $sha1, $this, $time );
390 return false; // find-by-sha1 not supported
392 if ( $img && $img->exists() ) {
395 # Now try to find a matching old version of a file...
396 if ( $time !== false && $this->oldFileFactoryKey
) { // find-by-sha1 supported?
397 $img = call_user_func( $this->oldFileFactoryKey
, $sha1, $this, $time );
398 if ( $img && $img->exists() ) {
399 if ( !$img->isDeleted( File
::DELETED_FILE
) ) {
400 return $img; // always OK
401 } elseif ( !empty( $options['private'] ) && $img->userCan( File
::DELETED_FILE
) ) {
410 * Get an array or iterator of file objects for files that have a given
411 * SHA-1 content hash.
415 public function findBySha1( $hash ) {
420 * Get the public root URL of the repository
422 * @return string|false
424 public function getRootUrl() {
429 * Returns true if the repository uses a multi-level directory structure
433 public function isHashed() {
434 return (bool)$this->hashLevels
;
438 * Get the URL of thumb.php
442 public function getThumbScriptUrl() {
443 return $this->thumbScriptUrl
;
447 * Returns true if the repository can transform files via a 404 handler
451 public function canTransformVia404() {
452 return $this->transformVia404
;
456 * Get the name of an image from its title object
458 * @param $title Title
460 public function getNameFromTitle( Title
$title ) {
462 if ( $this->initialCapital
!= MWNamespace
::isCapitalized( NS_FILE
) ) {
463 $name = $title->getUserCaseDBKey();
464 if ( $this->initialCapital
) {
465 $name = $wgContLang->ucfirst( $name );
468 $name = $title->getDBkey();
474 * Get the public zone root storage directory of the repository
478 public function getRootDirectory() {
479 return $this->getZonePath( 'public' );
483 * Get a relative path including trailing slash, e.g. f/fa/
484 * If the repo is not hashed, returns an empty string
486 * @param $name string
489 public function getHashPath( $name ) {
490 return self
::getHashPathForLevel( $name, $this->hashLevels
);
498 static function getHashPathForLevel( $name, $levels ) {
499 if ( $levels == 0 ) {
502 $hash = md5( $name );
504 for ( $i = 1; $i <= $levels; $i++
) {
505 $path .= substr( $hash, 0, $i ) . '/';
512 * Get the number of hash directory levels
516 public function getHashLevels() {
517 return $this->hashLevels
;
521 * Get the name of this repository, as specified by $info['name]' to the constructor
525 public function getName() {
530 * Make an url to this repo
532 * @param $query mixed Query string to append
533 * @param $entry string Entry point; defaults to index
534 * @return string|false
536 public function makeUrl( $query = '', $entry = 'index' ) {
537 if ( isset( $this->scriptDirUrl
) ) {
538 $ext = isset( $this->scriptExtension
) ?
$this->scriptExtension
: '.php';
539 return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}{$ext}", $query );
545 * Get the URL of an image description page. May return false if it is
546 * unknown or not applicable. In general this should only be called by the
547 * File class, since it may return invalid results for certain kinds of
548 * repositories. Use File::getDescriptionUrl() in user code.
550 * In particular, it uses the article paths as specified to the repository
551 * constructor, whereas local repositories use the local Title functions.
553 * @param $name string
556 public function getDescriptionUrl( $name ) {
557 $encName = wfUrlencode( $name );
558 if ( !is_null( $this->descBaseUrl
) ) {
559 # "http://example.com/wiki/Image:"
560 return $this->descBaseUrl
. $encName;
562 if ( !is_null( $this->articleUrl
) ) {
563 # "http://example.com/wiki/$1"
565 # We use "Image:" as the canonical namespace for
566 # compatibility across all MediaWiki versions.
567 return str_replace( '$1',
568 "Image:$encName", $this->articleUrl
);
570 if ( !is_null( $this->scriptDirUrl
) ) {
571 # "http://example.com/w"
573 # We use "Image:" as the canonical namespace for
574 # compatibility across all MediaWiki versions,
575 # and just sort of hope index.php is right. ;)
576 return $this->makeUrl( "title=Image:$encName" );
582 * Get the URL of the content-only fragment of the description page. For
583 * MediaWiki this means action=render. This should only be called by the
584 * repository's file class, since it may return invalid results. User code
585 * should use File::getDescriptionText().
587 * @param $name String: name of image to fetch
588 * @param $lang String: language to fetch it in, if any.
591 public function getDescriptionRenderUrl( $name, $lang = null ) {
592 $query = 'action=render';
593 if ( !is_null( $lang ) ) {
594 $query .= '&uselang=' . $lang;
596 if ( isset( $this->scriptDirUrl
) ) {
597 return $this->makeUrl(
599 wfUrlencode( 'Image:' . $name ) .
602 $descUrl = $this->getDescriptionUrl( $name );
604 return wfAppendQuery( $descUrl, $query );
612 * Get the URL of the stylesheet to apply to description pages
614 * @return string|false
616 public function getDescriptionStylesheetUrl() {
617 if ( isset( $this->scriptDirUrl
) ) {
618 return $this->makeUrl( 'title=MediaWiki:Filepage.css&' .
619 wfArrayToCGI( Skin
::getDynamicStylesheetQuery() ) );
625 * Store a file to a given destination.
627 * @param $srcPath String: source FS path, storage path, or virtual URL
628 * @param $dstZone String: destination zone
629 * @param $dstRel String: destination relative path
630 * @param $flags Integer: bitwise combination of the following flags:
631 * self::DELETE_SOURCE Delete the source file after upload
632 * self::OVERWRITE Overwrite an existing destination file instead of failing
633 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
634 * same contents as the source
635 * self::SKIP_LOCKING Skip any file locking when doing the store
636 * @return FileRepoStatus
638 public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
639 $status = $this->storeBatch( array( array( $srcPath, $dstZone, $dstRel ) ), $flags );
640 if ( $status->successCount
== 0 ) {
647 * Store a batch of files
649 * @param $triplets Array: (src, dest zone, dest rel) triplets as per store()
650 * @param $flags Integer: bitwise combination of the following flags:
651 * self::DELETE_SOURCE Delete the source file after upload
652 * self::OVERWRITE Overwrite an existing destination file instead of failing
653 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
654 * same contents as the source
655 * self::SKIP_LOCKING Skip any file locking when doing the store
656 * @return FileRepoStatus
658 public function storeBatch( $triplets, $flags = 0 ) {
659 $backend = $this->backend
; // convenience
661 $status = $this->newGood();
663 $operations = array();
664 $sourceFSFilesToDelete = array(); // cleanup for disk source files
665 // Validate each triplet and get the store operation...
666 foreach ( $triplets as $triplet ) {
667 list( $srcPath, $dstZone, $dstRel ) = $triplet;
669 . "( \$src='$srcPath', \$dstZone='$dstZone', \$dstRel='$dstRel' )\n"
672 // Resolve destination path
673 $root = $this->getZonePath( $dstZone );
675 throw new MWException( "Invalid zone: $dstZone" );
677 if ( !$this->validateFilename( $dstRel ) ) {
678 throw new MWException( 'Validation error in $dstRel' );
680 $dstPath = "$root/$dstRel";
681 $dstDir = dirname( $dstPath );
682 // Create destination directories for this triplet
683 if ( !$backend->prepare( array( 'dir' => $dstDir ) )->isOK() ) {
684 return $this->newFatal( 'directorycreateerror', $dstDir );
687 if ( $dstZone == 'deleted' ) {
688 $this->initDeletedDir( $dstDir );
691 // Resolve source to a storage path if virtual
692 if ( self
::isVirtualUrl( $srcPath ) ) {
693 $srcPath = $this->resolveVirtualUrl( $srcPath );
696 // Get the appropriate file operation
697 if ( FileBackend
::isStoragePath( $srcPath ) ) {
698 $opName = ( $flags & self
::DELETE_SOURCE
) ?
'move' : 'copy';
701 if ( $flags & self
::DELETE_SOURCE
) {
702 $sourceFSFilesToDelete[] = $srcPath;
705 $operations[] = array(
709 'overwrite' => $flags & self
::OVERWRITE
,
710 'overwriteSame' => $flags & self
::OVERWRITE_SAME
,
714 // Execute the store operation for each triplet
715 $opts = array( 'force' => true );
716 if ( $flags & self
::SKIP_LOCKING
) {
717 $opts['nonLocking'] = true;
719 $status->merge( $backend->doOperations( $operations, $opts ) );
720 // Cleanup for disk source files...
721 foreach ( $sourceFSFilesToDelete as $file ) {
722 wfSuppressWarnings();
723 unlink( $file ); // FS cleanup
731 * Deletes a batch of files.
732 * Each file can be a (zone, rel) pair, virtual url, storage path, or FS path.
733 * It will try to delete each file, but ignores any errors that may occur.
735 * @param $pairs array List of files to delete
738 public function cleanupBatch( $files ) {
739 $operations = array();
740 $sourceFSFilesToDelete = array(); // cleanup for disk source files
741 foreach ( $files as $file ) {
742 if ( is_array( $file ) ) {
743 // This is a pair, extract it
744 list( $zone, $rel ) = $file;
745 $root = $this->getZonePath( $zone );
746 $path = "$root/$rel";
748 if ( self
::isVirtualUrl( $file ) ) {
749 // This is a virtual url, resolve it
750 $path = $this->resolveVirtualUrl( $file );
752 // This is a full file name
756 // Get a file operation if needed
757 if ( FileBackend
::isStoragePath( $path ) ) {
758 $operations[] = array(
763 $sourceFSFilesToDelete[] = $path;
766 // Actually delete files from storage...
767 $opts = array( 'force' => true );
768 $this->backend
->doOperations( $operations, $opts );
769 // Cleanup for disk source files...
770 foreach ( $sourceFSFilesToDelete as $file ) {
771 wfSuppressWarnings();
772 unlink( $file ); // FS cleanup
778 * Pick a random name in the temp zone and store a file to it.
779 * Returns a FileRepoStatus object with the file Virtual URL in the value,
780 * file can later be disposed using FileRepo::freeTemp().
783 * @param $originalName String: the base name of the file as specified
784 * by the user. The file extension will be maintained.
785 * @param $srcPath String: the current location of the file.
786 * @return FileRepoStatus object with the URL in the value.
788 public function storeTemp( $originalName, $srcPath ) {
789 $date = gmdate( "YmdHis" );
790 $hashPath = $this->getHashPath( $originalName );
791 $dstRel = "{$hashPath}{$date}!{$originalName}";
792 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
794 $result = $this->store( $srcPath, 'temp', $dstRel, self
::SKIP_LOCKING
);
795 $result->value
= $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
800 * Concatenate a list of files into a target file location.
802 * @param $srcPaths Array Ordered list of source virtual URLs/storage paths
803 * @param $dstPath String Target file system path
804 * @param $flags Integer: bitwise combination of the following flags:
805 * self::DELETE_SOURCE Delete the source files
806 * @return FileRepoStatus
808 function concatenate( $srcPaths, $dstPath, $flags = 0 ) {
809 $status = $this->newGood();
812 $deleteOperations = array(); // post-concatenate ops
813 foreach ( $srcPaths as $srcPath ) {
814 // Resolve source to a storage path if virtual
815 $source = $this->resolveToStoragePath( $srcPath );
816 $sources[] = $source; // chunk to merge
817 if ( $flags & self
::DELETE_SOURCE
) {
818 $deleteOperations[] = array( 'op' => 'delete', 'src' => $source );
822 // Concatenate the chunks into one FS file
823 $params = array( 'srcs' => $sources, 'dst' => $dstPath );
824 $status->merge( $this->backend
->concatenate( $params ) );
825 if ( !$status->isOK() ) {
829 // Delete the sources if required
830 if ( $deleteOperations ) {
831 $opts = array( 'force' => true );
832 $status->merge( $this->backend
->doOperations( $deleteOperations, $opts ) );
835 // Make sure status is OK, despite any $deleteOperations fatals
836 $status->setResult( true );
842 * Remove a temporary file or mark it for garbage collection
844 * @param $virtualUrl String: the virtual URL returned by FileRepo::storeTemp()
845 * @return Boolean: true on success, false on failure
847 public function freeTemp( $virtualUrl ) {
848 $temp = "mwrepo://{$this->name}/temp";
849 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
850 wfDebug( __METHOD__
.": Invalid temp virtual URL\n" );
853 $path = $this->resolveVirtualUrl( $virtualUrl );
854 $op = array( 'op' => 'delete', 'src' => $path );
855 $status = $this->backend
->doOperation( $op );
856 return $status->isOK();
860 * Copy or move a file either from a storage path, virtual URL,
861 * or FS path, into this repository at the specified destination location.
863 * Returns a FileRepoStatus object. On success, the value contains "new" or
864 * "archived", to indicate whether the file was new with that name.
866 * @param $srcPath String: the source FS path, storage path, or URL
867 * @param $dstRel String: the destination relative path
868 * @param $archiveRel String: the relative path where the existing file is to
869 * be archived, if there is one. Relative to the public zone root.
870 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
871 * that the source file should be deleted if possible
873 public function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 ) {
874 $status = $this->publishBatch( array( array( $srcPath, $dstRel, $archiveRel ) ), $flags );
875 if ( $status->successCount
== 0 ) {
878 if ( isset( $status->value
[0] ) ) {
879 $status->value
= $status->value
[0];
881 $status->value
= false;
887 * Publish a batch of files
889 * @param $triplets Array: (source, dest, archive) triplets as per publish()
890 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
891 * that the source files should be deleted if possible
892 * @return FileRepoStatus
894 public function publishBatch( $triplets, $flags = 0 ) {
895 $backend = $this->backend
; // convenience
897 // Try creating directories
898 $status = $this->initZones( 'public' );
899 if ( !$status->isOK() ) {
903 $status = $this->newGood( array() );
905 $operations = array();
906 $sourceFSFilesToDelete = array(); // cleanup for disk source files
907 // Validate each triplet and get the store operation...
908 foreach ( $triplets as $i => $triplet ) {
909 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
910 // Resolve source to a storage path if virtual
911 if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) {
912 $srcPath = $this->resolveVirtualUrl( $srcPath );
914 if ( !$this->validateFilename( $dstRel ) ) {
915 throw new MWException( 'Validation error in $dstRel' );
917 if ( !$this->validateFilename( $archiveRel ) ) {
918 throw new MWException( 'Validation error in $archiveRel' );
921 $publicRoot = $this->getZonePath( 'public' );
922 $dstPath = "$publicRoot/$dstRel";
923 $archivePath = "$publicRoot/$archiveRel";
925 $dstDir = dirname( $dstPath );
926 $archiveDir = dirname( $archivePath );
927 // Abort immediately on directory creation errors since they're likely to be repetitive
928 if ( !$backend->prepare( array( 'dir' => $dstDir ) )->isOK() ) {
929 return $this->newFatal( 'directorycreateerror', $dstDir );
931 if ( !$backend->prepare( array( 'dir' => $archiveDir ) )->isOK() ) {
932 return $this->newFatal( 'directorycreateerror', $archiveDir );
935 // Archive destination file if it exists
936 if ( $backend->fileExists( array( 'src' => $dstPath ) ) ) {
937 // Check if the archive file exists
938 // This is a sanity check to avoid data loss. In UNIX, the rename primitive
939 // unlinks the destination file if it exists. DB-based synchronisation in
940 // publishBatch's caller should prevent races. In Windows there's no
941 // problem because the rename primitive fails if the destination exists.
942 if ( $backend->fileExists( array( 'src' => $archivePath ) ) ) {
943 $operations[] = array( 'op' => 'null' );
946 $operations[] = array(
949 'dst' => $archivePath
952 $status->value
[$i] = 'archived';
954 $status->value
[$i] = 'new';
956 // Copy (or move) the source file to the destination
957 if ( FileBackend
::isStoragePath( $srcPath ) ) {
958 if ( $flags & self
::DELETE_SOURCE
) {
959 $operations[] = array(
965 $operations[] = array(
971 } else { // FS source path
972 $operations[] = array(
977 if ( $flags & self
::DELETE_SOURCE
) {
978 $sourceFSFilesToDelete[] = $srcPath;
983 // Execute the operations for each triplet
984 $opts = array( 'force' => true );
985 $status->merge( $backend->doOperations( $operations, $opts ) );
986 // Cleanup for disk source files...
987 foreach ( $sourceFSFilesToDelete as $file ) {
988 wfSuppressWarnings();
989 unlink( $file ); // FS cleanup
997 * Checks existence of a a file
999 * @param $file Virtual URL (or storage path) of file to check
1000 * @param $flags Integer: bitwise combination of the following flags:
1001 * self::FILES_ONLY Mark file as existing only if it is a file (not directory)
1004 public function fileExists( $file, $flags = 0 ) {
1005 $result = $this->fileExistsBatch( array( $file ), $flags );
1010 * Checks existence of an array of files.
1012 * @param $files Array: Virtual URLs (or storage paths) of files to check
1013 * @param $flags Integer: bitwise combination of the following flags:
1014 * self::FILES_ONLY Mark file as existing only if it is a file (not directory)
1015 * @return Either array of files and existence flags, or false
1017 public function fileExistsBatch( $files, $flags = 0 ) {
1019 foreach ( $files as $key => $file ) {
1020 if ( self
::isVirtualUrl( $file ) ) {
1021 $file = $this->resolveVirtualUrl( $file );
1023 if ( FileBackend
::isStoragePath( $file ) ) {
1024 $result[$key] = $this->backend
->fileExists( array( 'src' => $file ) );
1026 if ( $flags & self
::FILES_ONLY
) {
1027 $result[$key] = is_file( $file ); // FS only
1029 $result[$key] = file_exists( $file ); // FS only
1038 * Move a file to the deletion archive.
1039 * If no valid deletion archive exists, this may either delete the file
1040 * or throw an exception, depending on the preference of the repository
1042 * @param $srcRel Mixed: relative path for the file to be deleted
1043 * @param $archiveRel Mixed: relative path for the archive location.
1044 * Relative to a private archive directory.
1045 * @return FileRepoStatus object
1047 public function delete( $srcRel, $archiveRel ) {
1048 return $this->deleteBatch( array( array( $srcRel, $archiveRel ) ) );
1052 * Move a group of files to the deletion archive.
1054 * If no valid deletion archive is configured, this may either delete the
1055 * file or throw an exception, depending on the preference of the repository.
1057 * The overwrite policy is determined by the repository -- currently LocalRepo
1058 * assumes a naming scheme in the deleted zone based on content hash, as
1059 * opposed to the public zone which is assumed to be unique.
1061 * @param $sourceDestPairs Array of source/destination pairs. Each element
1062 * is a two-element array containing the source file path relative to the
1063 * public root in the first element, and the archive file path relative
1064 * to the deleted zone root in the second element.
1065 * @return FileRepoStatus
1067 public function deleteBatch( $sourceDestPairs ) {
1068 $backend = $this->backend
; // convenience
1070 // Try creating directories
1071 $status = $this->initZones( array( 'public', 'deleted' ) );
1072 if ( !$status->isOK() ) {
1076 $status = $this->newGood();
1078 $operations = array();
1079 // Validate filenames and create archive directories
1080 foreach ( $sourceDestPairs as $pair ) {
1081 list( $srcRel, $archiveRel ) = $pair;
1082 if ( !$this->validateFilename( $srcRel ) ) {
1083 throw new MWException( __METHOD__
.':Validation error in $srcRel' );
1085 if ( !$this->validateFilename( $archiveRel ) ) {
1086 throw new MWException( __METHOD__
.':Validation error in $archiveRel' );
1089 $publicRoot = $this->getZonePath( 'public' );
1090 $srcPath = "{$publicRoot}/$srcRel";
1092 $deletedRoot = $this->getZonePath( 'deleted' );
1093 $archivePath = "{$deletedRoot}/{$archiveRel}";
1094 $archiveDir = dirname( $archivePath ); // does not touch FS
1096 // Create destination directories
1097 if ( !$backend->prepare( array( 'dir' => $archiveDir ) )->isOK() ) {
1098 return $this->newFatal( 'directorycreateerror', $archiveDir );
1100 $this->initDeletedDir( $archiveDir );
1102 $operations[] = array(
1105 'dst' => $archivePath,
1106 // We may have 2+ identical files being deleted,
1107 // all of which will map to the same destination file
1108 'overwriteSame' => true // also see bug 31792
1112 // Move the files by execute the operations for each pair.
1113 // We're now committed to returning an OK result, which will
1114 // lead to the files being moved in the DB also.
1115 $opts = array( 'force' => true );
1116 $status->merge( $backend->doOperations( $operations, $opts ) );
1122 * Get a relative path for a deletion archive key,
1123 * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
1127 public function getDeletedHashPath( $key ) {
1129 for ( $i = 0; $i < $this->deletedHashLevels
; $i++
) {
1130 $path .= $key[$i] . '/';
1136 * If a path is a virtual URL, resolve it to a storage path.
1137 * Otherwise, just return the path as it is.
1139 * @param $path string
1141 * @throws MWException
1143 protected function resolveToStoragePath( $path ) {
1144 if ( $this->isVirtualUrl( $path ) ) {
1145 return $this->resolveVirtualUrl( $path );
1151 * Get a local FS copy of a file with a given virtual URL/storage path.
1152 * Temporary files may be purged when the file object falls out of scope.
1154 * @param $virtualUrl string
1155 * @return TempFSFile|null Returns null on failure
1157 public function getLocalCopy( $virtualUrl ) {
1158 $path = $this->resolveToStoragePath( $virtualUrl );
1159 return $this->backend
->getLocalCopy( array( 'src' => $path ) );
1163 * Get a local FS file with a given virtual URL/storage path.
1164 * The file is either an original or a copy. It should not be changed.
1165 * Temporary files may be purged when the file object falls out of scope.
1167 * @param $virtualUrl string
1168 * @return FSFile|null Returns null on failure.
1170 public function getLocalReference( $virtualUrl ) {
1171 $path = $this->resolveToStoragePath( $virtualUrl );
1172 return $this->backend
->getLocalReference( array( 'src' => $path ) );
1176 * Get properties of a file with a given virtual URL/storage path.
1177 * Properties should ultimately be obtained via FSFile::getProps().
1179 * @param $virtualUrl string
1182 public function getFileProps( $virtualUrl ) {
1183 $path = $this->resolveToStoragePath( $virtualUrl );
1184 return $this->backend
->getFileProps( array( 'src' => $path ) );
1188 * Get the timestamp of a file with a given virtual URL/storage path
1190 * @param $virtualUrl string
1191 * @return string|false
1193 public function getFileTimestamp( $virtualUrl ) {
1194 $path = $this->resolveToStoragePath( $virtualUrl );
1195 return $this->backend
->getFileTimestamp( array( 'src' => $path ) );
1199 * Get the sha1 of a file with a given virtual URL/storage path
1201 * @param $virtualUrl string
1202 * @return string|false
1204 public function getFileSha1( $virtualUrl ) {
1205 $path = $this->resolveToStoragePath( $virtualUrl );
1206 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $path ) );
1210 return $tmpFile->getSha1Base36();
1214 * Attempt to stream a file with the given virtual URL/storage path
1216 * @param $virtualUrl string
1217 * @param $headers Array Additional HTTP headers to send on success
1218 * @return bool Success
1220 public function streamFile( $virtualUrl, $headers = array() ) {
1221 $path = $this->resolveToStoragePath( $virtualUrl );
1222 $params = array( 'src' => $path, 'headers' => $headers );
1223 return $this->backend
->streamFile( $params )->isOK();
1227 * Call a callback function for every public regular file in the repository.
1228 * This only acts on the current version of files, not any old versions.
1229 * May use either the database or the filesystem.
1231 * @param $callback Array|string
1234 public function enumFiles( $callback ) {
1235 $this->enumFilesInStorage( $callback );
1239 * Call a callback function for every public file in the repository.
1240 * May use either the database or the filesystem.
1242 * @param $callback Array|string
1245 protected function enumFilesInStorage( $callback ) {
1246 $publicRoot = $this->getZonePath( 'public' );
1247 $numDirs = 1 << ( $this->hashLevels
* 4 );
1248 // Use a priori assumptions about directory structure
1249 // to reduce the tree height of the scanning process.
1250 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++
) {
1251 $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
1252 $path = $publicRoot;
1253 for ( $hexPos = 0; $hexPos < $this->hashLevels
; $hexPos++
) {
1254 $path .= '/' . substr( $hexString, 0, $hexPos +
1 );
1256 $iterator = $this->backend
->getFileList( array( 'dir' => $path ) );
1257 foreach ( $iterator as $name ) {
1258 // Each item returned is a public file
1259 call_user_func( $callback, "{$path}/{$name}" );
1265 * Determine if a relative path is valid, i.e. not blank or involving directory traveral
1267 * @param $filename string
1270 public function validateFilename( $filename ) {
1271 if ( strval( $filename ) == '' ) {
1274 if ( wfIsWindows() ) {
1275 $filename = strtr( $filename, '\\', '/' );
1278 * Use the same traversal protection as Title::secureAndSplit()
1280 if ( strpos( $filename, '.' ) !== false &&
1281 ( $filename === '.' ||
$filename === '..' ||
1282 strpos( $filename, './' ) === 0 ||
1283 strpos( $filename, '../' ) === 0 ||
1284 strpos( $filename, '/./' ) !== false ||
1285 strpos( $filename, '/../' ) !== false ) )
1294 * Get a callback function to use for cleaning error message parameters
1298 function getErrorCleanupFunction() {
1299 switch ( $this->pathDisclosureProtection
) {
1301 $callback = array( $this, 'passThrough' );
1304 $callback = array( $this, 'simpleClean' );
1306 default: // 'paranoid'
1307 $callback = array( $this, 'paranoidClean' );
1313 * Path disclosure protection function
1315 * @param $param string
1318 function paranoidClean( $param ) {
1323 * Path disclosure protection function
1325 * @param $param string
1328 function simpleClean( $param ) {
1330 if ( !isset( $this->simpleCleanPairs
) ) {
1331 $this->simpleCleanPairs
= array(
1332 $IP => '$IP', // sanity
1335 return strtr( $param, $this->simpleCleanPairs
);
1339 * Path disclosure protection function
1341 * @param $param string
1344 function passThrough( $param ) {
1349 * Create a new fatal error
1351 * @return FileRepoStatus
1353 function newFatal( $message /*, parameters...*/ ) {
1354 $params = func_get_args();
1355 array_unshift( $params, $this );
1356 return MWInit
::callStaticMethod( 'FileRepoStatus', 'newFatal', $params );
1360 * Create a new good result
1362 * @return FileRepoStatus
1364 function newGood( $value = null ) {
1365 return FileRepoStatus
::newGood( $this, $value );
1369 * Delete files in the deleted directory if they are not referenced in the filearchive table
1373 public function cleanupDeletedBatch( $storageKeys ) {}
1376 * Checks if there is a redirect named as $title. If there is, return the
1377 * title object. If not, return false.
1380 * @param $title Title of image
1383 public function checkRedirect( Title
$title ) {
1388 * Invalidates image redirect cache related to that image
1389 * Doesn't do anything for repositories that don't support image redirects.
1392 * @param $title Title of image
1394 public function invalidateImageRedirect( Title
$title ) {}
1397 * Get the human-readable name of the repo
1401 public function getDisplayName() {
1402 // We don't name our own repo, return nothing
1403 if ( $this->isLocal() ) {
1406 // 'shared-repo-name-wikimediacommons' is used when $wgUseInstantCommons = true
1407 return wfMessageFallback( 'shared-repo-name-' . $this->name
, 'shared-repo' )->text();
1411 * Returns true if this the local file repository.
1415 public function isLocal() {
1416 return $this->getName() == 'local';
1420 * Get a key on the primary cache for this repository.
1421 * Returns false if the repository's cache is not accessible at this site.
1422 * The parameters are the parts of the key, as for wfMemcKey().
1426 function getSharedCacheKey( /*...*/ ) {
1431 * Get a key for this repo in the local cache domain. These cache keys are
1432 * not shared with remote instances of the repo.
1433 * The parameters are the parts of the key, as for wfMemcKey().
1437 function getLocalCacheKey( /*...*/ ) {
1438 $args = func_get_args();
1439 array_unshift( $args, 'filerepo', $this->getName() );
1440 return call_user_func_array( 'wfMemcKey', $args );
1444 * Get an UploadStash associated with this repo.
1446 * @return UploadStash
1448 public function getUploadStash() {
1449 return new UploadStash( $this );