3 * @defgroup FileRepo File Repository
5 * @brief This module handles how MediaWiki interacts with filesystems.
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
33 * Base class for file repositories
38 const DELETE_SOURCE
= 1;
40 const OVERWRITE_SAME
= 4;
41 const SKIP_LOCKING
= 8;
43 /** @var FileBackend */
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;
56 * Factory functions for creating new files
57 * Override these in the base class
59 var $fileFactory = array( 'UnregisteredLocalFile', 'newFromTitle' );
60 var $oldFileFactory = false;
61 var $fileFactoryKey = false, $oldFileFactoryKey = false;
64 * @param $info array|null
67 public function __construct( array $info = null ) {
68 // Verify required settings presence
71 ||
!array_key_exists( 'name', $info )
72 ||
!array_key_exists( 'backend', $info )
74 throw new MWException( __CLASS__
. " requires an array of options having both 'name' and 'backend' keys.\n" );
78 $this->name
= $info['name'];
79 if ( $info['backend'] instanceof FileBackend
) {
80 $this->backend
= $info['backend']; // useful for testing
82 $this->backend
= FileBackendGroup
::singleton()->get( $info['backend'] );
85 // Optional settings that can have no value
86 $optionalSettings = array(
87 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
88 'thumbScriptUrl', 'pathDisclosureProtection', 'descriptionCacheExpiry',
91 foreach ( $optionalSettings as $var ) {
92 if ( isset( $info[$var] ) ) {
93 $this->$var = $info[$var];
97 // Optional settings that have a default
98 $this->initialCapital
= isset( $info['initialCapital'] )
99 ?
$info['initialCapital']
100 : MWNamespace
::isCapitalized( NS_FILE
);
101 $this->url
= isset( $info['url'] )
103 : false; // a subclass may set the URL (e.g. ForeignAPIRepo)
104 if ( isset( $info['thumbUrl'] ) ) {
105 $this->thumbUrl
= $info['thumbUrl'];
107 $this->thumbUrl
= $this->url ?
"{$this->url}/thumb" : false;
109 $this->hashLevels
= isset( $info['hashLevels'] )
110 ?
$info['hashLevels']
112 $this->deletedHashLevels
= isset( $info['deletedHashLevels'] )
113 ?
$info['deletedHashLevels']
115 $this->transformVia404
= !empty( $info['transformVia404'] );
116 $this->zones
= isset( $info['zones'] )
119 // Give defaults for the basic zones...
120 foreach ( array( 'public', 'thumb', 'temp', 'deleted' ) as $zone ) {
121 if ( !isset( $this->zones
[$zone]['container'] ) ) {
122 $this->zones
[$zone]['container'] = "{$this->name}-{$zone}";
124 if ( !isset( $this->zones
[$zone]['directory'] ) ) {
125 $this->zones
[$zone]['directory'] = '';
131 * Get the file backend instance. Use this function wisely.
133 * @return FileBackend
135 public function getBackend() {
136 return $this->backend
;
140 * Get an explanatory message if this repo is read-only.
141 * This checks if an administrator disabled writes to the backend.
143 * @return string|bool Returns false if the repo is not read-only
145 public function getReadOnlyReason() {
146 return $this->backend
->getReadOnlyReason();
150 * Check if a single zone or list of zones is defined for usage
152 * @param $doZones Array Only do a particular zones
153 * @throws MWException
156 protected function initZones( $doZones = array() ) {
157 $status = $this->newGood();
158 foreach ( (array)$doZones as $zone ) {
159 $root = $this->getZonePath( $zone );
160 if ( $root === null ) {
161 throw new MWException( "No '$zone' zone defined in the {$this->name} repo." );
168 * Determine if a string is an mwrepo:// URL
173 public static function isVirtualUrl( $url ) {
174 return substr( $url, 0, 9 ) == 'mwrepo://';
178 * Get a URL referring to this repository, with the private mwrepo protocol.
179 * The suffix, if supplied, is considered to be unencoded, and will be
180 * URL-encoded before being returned.
182 * @param $suffix string|bool
185 public function getVirtualUrl( $suffix = false ) {
186 $path = 'mwrepo://' . $this->name
;
187 if ( $suffix !== false ) {
188 $path .= '/' . rawurlencode( $suffix );
194 * Get the URL corresponding to one of the four basic zones
196 * @param $zone String: one of: public, deleted, temp, thumb
197 * @return String or false
199 public function getZoneUrl( $zone ) {
200 if ( isset( $this->zones
[$zone]['url'] )
201 && in_array( $zone, array( 'public', 'temp', 'thumb' ) ) )
203 return $this->zones
[$zone]['url']; // custom URL
209 return "{$this->url}/temp";
211 return false; // no public URL
213 return $this->thumbUrl
;
220 * Get the thumb zone URL configured to be handled by scripts like thumb_handler.php.
221 * This is probably only useful for internal requests, such as from a fast frontend server
222 * to a slower backend server.
224 * Large sites may use a different host name for uploads than for wikis. In any case, the
225 * wiki configuration is needed in order to use thumb.php. To avoid extracting the wiki ID
226 * from the URL path, one can configure thumb_handler.php to recognize a special path on the
227 * same host name as the wiki that is used for viewing thumbnails.
229 * @param $zone String: one of: public, deleted, temp, thumb
230 * @return String or false
232 public function getZoneHandlerUrl( $zone ) {
233 if ( isset( $this->zones
[$zone]['handlerUrl'] )
234 && in_array( $zone, array( 'public', 'temp', 'thumb' ) ) )
236 return $this->zones
[$zone]['handlerUrl'];
242 * Get the backend storage path corresponding to a virtual URL.
243 * Use this function wisely.
246 * @throws MWException
249 public function resolveVirtualUrl( $url ) {
250 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
251 throw new MWException( __METHOD__
.': unknown protocol' );
253 $bits = explode( '/', substr( $url, 9 ), 3 );
254 if ( count( $bits ) != 3 ) {
255 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
257 list( $repo, $zone, $rel ) = $bits;
258 if ( $repo !== $this->name
) {
259 throw new MWException( __METHOD__
.": fetching from a foreign repo is not supported" );
261 $base = $this->getZonePath( $zone );
263 throw new MWException( __METHOD__
.": invalid zone: $zone" );
265 return $base . '/' . rawurldecode( $rel );
269 * The the storage container and base path of a zone
271 * @param $zone string
272 * @return Array (container, base path) or (null, null)
274 protected function getZoneLocation( $zone ) {
275 if ( !isset( $this->zones
[$zone] ) ) {
276 return array( null, null ); // bogus
278 return array( $this->zones
[$zone]['container'], $this->zones
[$zone]['directory'] );
282 * Get the storage path corresponding to one of the zones
284 * @param $zone string
285 * @return string|null Returns null if the zone is not defined
287 public function getZonePath( $zone ) {
288 list( $container, $base ) = $this->getZoneLocation( $zone );
289 if ( $container === null ||
$base === null ) {
292 $backendName = $this->backend
->getName();
293 if ( $base != '' ) { // may not be set
296 return "mwstore://$backendName/{$container}{$base}";
300 * Create a new File object from the local repository
302 * @param $title Mixed: Title object or string
303 * @param $time Mixed: Time at which the image was uploaded.
304 * If this is specified, the returned object will be an
305 * instance of the repository's old file class instead of a
306 * current file. Repositories not supporting version control
307 * should return false if this parameter is set.
308 * @return File|null A File, or null if passed an invalid Title
310 public function newFile( $title, $time = false ) {
311 $title = File
::normalizeTitle( $title );
316 if ( $this->oldFileFactory
) {
317 return call_user_func( $this->oldFileFactory
, $title, $this, $time );
322 return call_user_func( $this->fileFactory
, $title, $this );
327 * Find an instance of the named file created at the specified time
328 * Returns false if the file does not exist. Repositories not supporting
329 * version control should return false if the time is specified.
331 * @param $title Mixed: Title object or string
332 * @param $options array Associative array of options:
333 * time: requested time for a specific file version, or false for the
334 * current version. An image object will be returned which was
335 * created at the specified time (which may be archived or current).
337 * ignoreRedirect: If true, do not follow file redirects
339 * private: If true, return restricted (deleted) files if the current
340 * user is allowed to view them. Otherwise, such files will not
342 * @return File|bool False on failure
344 public function findFile( $title, $options = array() ) {
345 $title = File
::normalizeTitle( $title );
349 $time = isset( $options['time'] ) ?
$options['time'] : false;
350 # First try the current version of the file to see if it precedes the timestamp
351 $img = $this->newFile( $title );
355 if ( $img->exists() && ( !$time ||
$img->getTimestamp() == $time ) ) {
358 # Now try an old version of the file
359 if ( $time !== false ) {
360 $img = $this->newFile( $title, $time );
361 if ( $img && $img->exists() ) {
362 if ( !$img->isDeleted( File
::DELETED_FILE
) ) {
363 return $img; // always OK
364 } elseif ( !empty( $options['private'] ) && $img->userCan( File
::DELETED_FILE
) ) {
371 if ( !empty( $options['ignoreRedirect'] ) ) {
374 $redir = $this->checkRedirect( $title );
375 if ( $redir && $title->getNamespace() == NS_FILE
) {
376 $img = $this->newFile( $redir );
380 if ( $img->exists() ) {
381 $img->redirectedFrom( $title->getDBkey() );
389 * Find many files at once.
391 * @param $items array An array of titles, or an array of findFile() options with
392 * the "title" option giving the title. Example:
394 * $findItem = array( 'title' => $title, 'private' => true );
395 * $findBatch = array( $findItem );
396 * $repo->findFiles( $findBatch );
399 public function findFiles( array $items ) {
401 foreach ( $items as $item ) {
402 if ( is_array( $item ) ) {
403 $title = $item['title'];
405 unset( $options['title'] );
410 $file = $this->findFile( $title, $options );
412 $result[$file->getTitle()->getDBkey()] = $file;
419 * Find an instance of the file with this key, created at the specified time
420 * Returns false if the file does not exist. Repositories not supporting
421 * version control should return false if the time is specified.
423 * @param $sha1 String base 36 SHA-1 hash
424 * @param $options array Option array, same as findFile().
425 * @return File|bool False on failure
427 public function findFileFromKey( $sha1, $options = array() ) {
428 $time = isset( $options['time'] ) ?
$options['time'] : false;
429 # First try to find a matching current version of a file...
430 if ( $this->fileFactoryKey
) {
431 $img = call_user_func( $this->fileFactoryKey
, $sha1, $this, $time );
433 return false; // find-by-sha1 not supported
435 if ( $img && $img->exists() ) {
438 # Now try to find a matching old version of a file...
439 if ( $time !== false && $this->oldFileFactoryKey
) { // find-by-sha1 supported?
440 $img = call_user_func( $this->oldFileFactoryKey
, $sha1, $this, $time );
441 if ( $img && $img->exists() ) {
442 if ( !$img->isDeleted( File
::DELETED_FILE
) ) {
443 return $img; // always OK
444 } elseif ( !empty( $options['private'] ) && $img->userCan( File
::DELETED_FILE
) ) {
453 * Get an array or iterator of file objects for files that have a given
454 * SHA-1 content hash.
460 public function findBySha1( $hash ) {
465 * Get an array of arrays or iterators of file objects for files that
466 * have the given SHA-1 content hashes.
468 * @param $hashes array An array of hashes
469 * @return array An Array of arrays or iterators of file objects and the hash as key
471 public function findBySha1s( array $hashes ) {
473 foreach ( $hashes as $hash ) {
474 $files = $this->findBySha1( $hash );
475 if ( count( $files ) ) {
476 $result[$hash] = $files;
483 * Get the public root URL of the repository
485 * @deprecated since 1.20
488 public function getRootUrl() {
489 return $this->getZoneUrl( 'public' );
493 * Get the URL of thumb.php
497 public function getThumbScriptUrl() {
498 return $this->thumbScriptUrl
;
502 * Returns true if the repository can transform files via a 404 handler
506 public function canTransformVia404() {
507 return $this->transformVia404
;
511 * Get the name of an image from its title object
513 * @param $title Title
516 public function getNameFromTitle( Title
$title ) {
518 if ( $this->initialCapital
!= MWNamespace
::isCapitalized( NS_FILE
) ) {
519 $name = $title->getUserCaseDBKey();
520 if ( $this->initialCapital
) {
521 $name = $wgContLang->ucfirst( $name );
524 $name = $title->getDBkey();
530 * Get the public zone root storage directory of the repository
534 public function getRootDirectory() {
535 return $this->getZonePath( 'public' );
539 * Get a relative path including trailing slash, e.g. f/fa/
540 * If the repo is not hashed, returns an empty string
542 * @param $name string Name of file
545 public function getHashPath( $name ) {
546 return self
::getHashPathForLevel( $name, $this->hashLevels
);
550 * Get a relative path including trailing slash, e.g. f/fa/
551 * If the repo is not hashed, returns an empty string
553 * @param $suffix string Basename of file from FileRepo::storeTemp()
556 public function getTempHashPath( $suffix ) {
557 $parts = explode( '!', $suffix, 2 ); // format is <timestamp>!<name> or just <name>
558 $name = isset( $parts[1] ) ?
$parts[1] : $suffix; // hash path is not based on timestamp
559 return self
::getHashPathForLevel( $name, $this->hashLevels
);
567 protected static function getHashPathForLevel( $name, $levels ) {
568 if ( $levels == 0 ) {
571 $hash = md5( $name );
573 for ( $i = 1; $i <= $levels; $i++
) {
574 $path .= substr( $hash, 0, $i ) . '/';
581 * Get the number of hash directory levels
585 public function getHashLevels() {
586 return $this->hashLevels
;
590 * Get the name of this repository, as specified by $info['name]' to the constructor
594 public function getName() {
599 * Make an url to this repo
601 * @param $query mixed Query string to append
602 * @param $entry string Entry point; defaults to index
603 * @return string|bool False on failure
605 public function makeUrl( $query = '', $entry = 'index' ) {
606 if ( isset( $this->scriptDirUrl
) ) {
607 $ext = isset( $this->scriptExtension
) ?
$this->scriptExtension
: '.php';
608 return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}{$ext}", $query );
614 * Get the URL of an image description page. May return false if it is
615 * unknown or not applicable. In general this should only be called by the
616 * File class, since it may return invalid results for certain kinds of
617 * repositories. Use File::getDescriptionUrl() in user code.
619 * In particular, it uses the article paths as specified to the repository
620 * constructor, whereas local repositories use the local Title functions.
622 * @param $name string
625 public function getDescriptionUrl( $name ) {
626 $encName = wfUrlencode( $name );
627 if ( !is_null( $this->descBaseUrl
) ) {
628 # "http://example.com/wiki/Image:"
629 return $this->descBaseUrl
. $encName;
631 if ( !is_null( $this->articleUrl
) ) {
632 # "http://example.com/wiki/$1"
634 # We use "Image:" as the canonical namespace for
635 # compatibility across all MediaWiki versions.
636 return str_replace( '$1',
637 "Image:$encName", $this->articleUrl
);
639 if ( !is_null( $this->scriptDirUrl
) ) {
640 # "http://example.com/w"
642 # We use "Image:" as the canonical namespace for
643 # compatibility across all MediaWiki versions,
644 # and just sort of hope index.php is right. ;)
645 return $this->makeUrl( "title=Image:$encName" );
651 * Get the URL of the content-only fragment of the description page. For
652 * MediaWiki this means action=render. This should only be called by the
653 * repository's file class, since it may return invalid results. User code
654 * should use File::getDescriptionText().
656 * @param $name String: name of image to fetch
657 * @param $lang String: language to fetch it in, if any.
660 public function getDescriptionRenderUrl( $name, $lang = null ) {
661 $query = 'action=render';
662 if ( !is_null( $lang ) ) {
663 $query .= '&uselang=' . $lang;
665 if ( isset( $this->scriptDirUrl
) ) {
666 return $this->makeUrl(
668 wfUrlencode( 'Image:' . $name ) .
671 $descUrl = $this->getDescriptionUrl( $name );
673 return wfAppendQuery( $descUrl, $query );
681 * Get the URL of the stylesheet to apply to description pages
683 * @return string|bool False on failure
685 public function getDescriptionStylesheetUrl() {
686 if ( isset( $this->scriptDirUrl
) ) {
687 return $this->makeUrl( 'title=MediaWiki:Filepage.css&' .
688 wfArrayToCGI( Skin
::getDynamicStylesheetQuery() ) );
694 * Store a file to a given destination.
696 * @param $srcPath String: source FS path, storage path, or virtual URL
697 * @param $dstZone String: destination zone
698 * @param $dstRel String: destination relative path
699 * @param $flags Integer: bitwise combination of the following flags:
700 * self::DELETE_SOURCE Delete the source file after upload
701 * self::OVERWRITE Overwrite an existing destination file instead of failing
702 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
703 * same contents as the source
704 * self::SKIP_LOCKING Skip any file locking when doing the store
705 * @return FileRepoStatus
707 public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
708 $this->assertWritableRepo(); // fail out if read-only
710 $status = $this->storeBatch( array( array( $srcPath, $dstZone, $dstRel ) ), $flags );
711 if ( $status->successCount
== 0 ) {
719 * Store a batch of files
721 * @param $triplets Array: (src, dest zone, dest rel) triplets as per store()
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 * @throws MWException
729 * @return FileRepoStatus
731 public function storeBatch( array $triplets, $flags = 0 ) {
732 $this->assertWritableRepo(); // fail out if read-only
734 $status = $this->newGood();
735 $backend = $this->backend
; // convenience
737 $operations = array();
738 $sourceFSFilesToDelete = array(); // cleanup for disk source files
739 // Validate each triplet and get the store operation...
740 foreach ( $triplets as $triplet ) {
741 list( $srcPath, $dstZone, $dstRel ) = $triplet;
743 . "( \$src='$srcPath', \$dstZone='$dstZone', \$dstRel='$dstRel' )\n"
746 // Resolve destination path
747 $root = $this->getZonePath( $dstZone );
749 throw new MWException( "Invalid zone: $dstZone" );
751 if ( !$this->validateFilename( $dstRel ) ) {
752 throw new MWException( 'Validation error in $dstRel' );
754 $dstPath = "$root/$dstRel";
755 $dstDir = dirname( $dstPath );
756 // Create destination directories for this triplet
757 if ( !$this->initDirectory( $dstDir )->isOK() ) {
758 return $this->newFatal( 'directorycreateerror', $dstDir );
761 // Resolve source to a storage path if virtual
762 $srcPath = $this->resolveToStoragePath( $srcPath );
764 // Get the appropriate file operation
765 if ( FileBackend
::isStoragePath( $srcPath ) ) {
766 $opName = ( $flags & self
::DELETE_SOURCE
) ?
'move' : 'copy';
769 if ( $flags & self
::DELETE_SOURCE
) {
770 $sourceFSFilesToDelete[] = $srcPath;
773 $operations[] = array(
777 'overwrite' => $flags & self
::OVERWRITE
,
778 'overwriteSame' => $flags & self
::OVERWRITE_SAME
,
782 // Execute the store operation for each triplet
783 $opts = array( 'force' => true );
784 if ( $flags & self
::SKIP_LOCKING
) {
785 $opts['nonLocking'] = true;
787 $status->merge( $backend->doOperations( $operations, $opts ) );
788 // Cleanup for disk source files...
789 foreach ( $sourceFSFilesToDelete as $file ) {
790 wfSuppressWarnings();
791 unlink( $file ); // FS cleanup
799 * Deletes a batch of files.
800 * Each file can be a (zone, rel) pair, virtual url, storage path.
801 * It will try to delete each file, but ignores any errors that may occur.
803 * @param $files array List of files to delete
804 * @param $flags Integer: bitwise combination of the following flags:
805 * self::SKIP_LOCKING Skip any file locking when doing the deletions
806 * @return FileRepoStatus
808 public function cleanupBatch( array $files, $flags = 0 ) {
809 $this->assertWritableRepo(); // fail out if read-only
811 $status = $this->newGood();
813 $operations = array();
814 foreach ( $files as $path ) {
815 if ( is_array( $path ) ) {
816 // This is a pair, extract it
817 list( $zone, $rel ) = $path;
818 $path = $this->getZonePath( $zone ) . "/$rel";
820 // Resolve source to a storage path if virtual
821 $path = $this->resolveToStoragePath( $path );
823 $operations[] = array( 'op' => 'delete', 'src' => $path );
825 // Actually delete files from storage...
826 $opts = array( 'force' => true );
827 if ( $flags & self
::SKIP_LOCKING
) {
828 $opts['nonLocking'] = true;
830 $status->merge( $this->backend
->doOperations( $operations, $opts ) );
836 * Import a file from the local file system into the repo.
837 * This does no locking nor journaling and overrides existing files.
838 * This function can be used to write to otherwise read-only foreign repos.
839 * This is intended for copying generated thumbnails into the repo.
841 * @param $src string File system path
842 * @param $dst string Virtual URL or storage path
843 * @return FileRepoStatus
845 final public function quickImport( $src, $dst ) {
846 return $this->quickImportBatch( array( array( $src, $dst ) ) );
850 * Purge a file from the repo. This does no locking nor journaling.
851 * This function can be used to write to otherwise read-only foreign repos.
852 * This is intended for purging thumbnails.
854 * @param $path string Virtual URL or storage path
855 * @return FileRepoStatus
857 final public function quickPurge( $path ) {
858 return $this->quickPurgeBatch( array( $path ) );
862 * Deletes a directory if empty.
863 * This function can be used to write to otherwise read-only foreign repos.
865 * @param $dir string Virtual URL (or storage path) of directory to clean
868 public function quickCleanDir( $dir ) {
869 $status = $this->newGood();
870 $status->merge( $this->backend
->clean(
871 array( 'dir' => $this->resolveToStoragePath( $dir ) ) ) );
877 * Import a batch of files from the local file system into the repo.
878 * This does no locking nor journaling and overrides existing files.
879 * This function can be used to write to otherwise read-only foreign repos.
880 * This is intended for copying generated thumbnails into the repo.
882 * @param $pairs Array List of tuples (file system path, virtual URL or storage path)
883 * @return FileRepoStatus
885 public function quickImportBatch( array $pairs ) {
886 $status = $this->newGood();
887 $operations = array();
888 foreach ( $pairs as $pair ) {
889 list ( $src, $dst ) = $pair;
890 $dst = $this->resolveToStoragePath( $dst );
891 $operations[] = array(
896 $status->merge( $this->initDirectory( dirname( $dst ) ) );
898 $status->merge( $this->backend
->doQuickOperations( $operations ) );
904 * Purge a batch of files from the repo.
905 * This function can be used to write to otherwise read-only foreign repos.
906 * This does no locking nor journaling and is intended for purging thumbnails.
908 * @param $paths Array List of virtual URLs or storage paths
909 * @return FileRepoStatus
911 public function quickPurgeBatch( array $paths ) {
912 $status = $this->newGood();
913 $operations = array();
914 foreach ( $paths as $path ) {
915 $operations[] = array(
917 'src' => $this->resolveToStoragePath( $path ),
918 'ignoreMissingSource' => true
921 $status->merge( $this->backend
->doQuickOperations( $operations ) );
927 * Pick a random name in the temp zone and store a file to it.
928 * Returns a FileRepoStatus object with the file Virtual URL in the value,
929 * file can later be disposed using FileRepo::freeTemp().
931 * @param $originalName String: the base name of the file as specified
932 * by the user. The file extension will be maintained.
933 * @param $srcPath String: the current location of the file.
934 * @return FileRepoStatus object with the URL in the value.
936 public function storeTemp( $originalName, $srcPath ) {
937 $this->assertWritableRepo(); // fail out if read-only
939 $date = gmdate( "YmdHis" );
940 $hashPath = $this->getHashPath( $originalName );
941 $dstRel = "{$hashPath}{$date}!{$originalName}";
942 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
944 $result = $this->store( $srcPath, 'temp', $dstRel, self
::SKIP_LOCKING
);
945 $result->value
= $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
951 * Concatenate a list of files into a target file location.
953 * @param $srcPaths Array Ordered list of source virtual URLs/storage paths
954 * @param $dstPath String Target file system path
955 * @param $flags Integer: bitwise combination of the following flags:
956 * self::DELETE_SOURCE Delete the source files
957 * @return FileRepoStatus
959 public function concatenate( array $srcPaths, $dstPath, $flags = 0 ) {
960 $this->assertWritableRepo(); // fail out if read-only
962 $status = $this->newGood();
965 $deleteOperations = array(); // post-concatenate ops
966 foreach ( $srcPaths as $srcPath ) {
967 // Resolve source to a storage path if virtual
968 $source = $this->resolveToStoragePath( $srcPath );
969 $sources[] = $source; // chunk to merge
970 if ( $flags & self
::DELETE_SOURCE
) {
971 $deleteOperations[] = array( 'op' => 'delete', 'src' => $source );
975 // Concatenate the chunks into one FS file
976 $params = array( 'srcs' => $sources, 'dst' => $dstPath );
977 $status->merge( $this->backend
->concatenate( $params ) );
978 if ( !$status->isOK() ) {
982 // Delete the sources if required
983 if ( $deleteOperations ) {
984 $opts = array( 'force' => true );
985 $status->merge( $this->backend
->doOperations( $deleteOperations, $opts ) );
988 // Make sure status is OK, despite any $deleteOperations fatals
989 $status->setResult( true );
995 * Remove a temporary file or mark it for garbage collection
997 * @param $virtualUrl String: the virtual URL returned by FileRepo::storeTemp()
998 * @return Boolean: true on success, false on failure
1000 public function freeTemp( $virtualUrl ) {
1001 $this->assertWritableRepo(); // fail out if read-only
1003 $temp = "mwrepo://{$this->name}/temp";
1004 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
1005 wfDebug( __METHOD__
.": Invalid temp virtual URL\n" );
1008 $path = $this->resolveVirtualUrl( $virtualUrl );
1010 return $this->cleanupBatch( array( $path ), self
::SKIP_LOCKING
)->isOK();
1014 * Copy or move a file either from a storage path, virtual URL,
1015 * or FS path, into this repository at the specified destination location.
1017 * Returns a FileRepoStatus object. On success, the value contains "new" or
1018 * "archived", to indicate whether the file was new with that name.
1020 * @param $srcPath String: the source FS path, storage path, or URL
1021 * @param $dstRel String: the destination relative path
1022 * @param $archiveRel String: the relative path where the existing file is to
1023 * be archived, if there is one. Relative to the public zone root.
1024 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
1025 * that the source file should be deleted if possible
1026 * @return FileRepoStatus
1028 public function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 ) {
1029 $this->assertWritableRepo(); // fail out if read-only
1031 $status = $this->publishBatch( array( array( $srcPath, $dstRel, $archiveRel ) ), $flags );
1032 if ( $status->successCount
== 0 ) {
1033 $status->ok
= false;
1035 if ( isset( $status->value
[0] ) ) {
1036 $status->value
= $status->value
[0];
1038 $status->value
= false;
1045 * Publish a batch of files
1047 * @param $triplets Array: (source, dest, archive) triplets as per publish()
1048 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
1049 * that the source files should be deleted if possible
1050 * @throws MWException
1051 * @return FileRepoStatus
1053 public function publishBatch( array $triplets, $flags = 0 ) {
1054 $this->assertWritableRepo(); // fail out if read-only
1056 $backend = $this->backend
; // convenience
1057 // Try creating directories
1058 $status = $this->initZones( 'public' );
1059 if ( !$status->isOK() ) {
1063 $status = $this->newGood( array() );
1065 $operations = array();
1066 $sourceFSFilesToDelete = array(); // cleanup for disk source files
1067 // Validate each triplet and get the store operation...
1068 foreach ( $triplets as $i => $triplet ) {
1069 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
1070 // Resolve source to a storage path if virtual
1071 $srcPath = $this->resolveToStoragePath( $srcPath );
1072 if ( !$this->validateFilename( $dstRel ) ) {
1073 throw new MWException( 'Validation error in $dstRel' );
1075 if ( !$this->validateFilename( $archiveRel ) ) {
1076 throw new MWException( 'Validation error in $archiveRel' );
1079 $publicRoot = $this->getZonePath( 'public' );
1080 $dstPath = "$publicRoot/$dstRel";
1081 $archivePath = "$publicRoot/$archiveRel";
1083 $dstDir = dirname( $dstPath );
1084 $archiveDir = dirname( $archivePath );
1085 // Abort immediately on directory creation errors since they're likely to be repetitive
1086 if ( !$this->initDirectory( $dstDir )->isOK() ) {
1087 return $this->newFatal( 'directorycreateerror', $dstDir );
1089 if ( !$this->initDirectory($archiveDir )->isOK() ) {
1090 return $this->newFatal( 'directorycreateerror', $archiveDir );
1093 // Archive destination file if it exists
1094 if ( $backend->fileExists( array( 'src' => $dstPath ) ) ) {
1095 // Check if the archive file exists
1096 // This is a sanity check to avoid data loss. In UNIX, the rename primitive
1097 // unlinks the destination file if it exists. DB-based synchronisation in
1098 // publishBatch's caller should prevent races. In Windows there's no
1099 // problem because the rename primitive fails if the destination exists.
1100 if ( $backend->fileExists( array( 'src' => $archivePath ) ) ) {
1101 $operations[] = array( 'op' => 'null' );
1104 $operations[] = array(
1107 'dst' => $archivePath
1110 $status->value
[$i] = 'archived';
1112 $status->value
[$i] = 'new';
1114 // Copy (or move) the source file to the destination
1115 if ( FileBackend
::isStoragePath( $srcPath ) ) {
1116 if ( $flags & self
::DELETE_SOURCE
) {
1117 $operations[] = array(
1123 $operations[] = array(
1129 } else { // FS source path
1130 $operations[] = array(
1135 if ( $flags & self
::DELETE_SOURCE
) {
1136 $sourceFSFilesToDelete[] = $srcPath;
1141 // Execute the operations for each triplet
1142 $opts = array( 'force' => true );
1143 $status->merge( $backend->doOperations( $operations, $opts ) );
1144 // Cleanup for disk source files...
1145 foreach ( $sourceFSFilesToDelete as $file ) {
1146 wfSuppressWarnings();
1147 unlink( $file ); // FS cleanup
1148 wfRestoreWarnings();
1155 * Creates a directory with the appropriate zone permissions.
1156 * Callers are responsible for doing read-only and "writable repo" checks.
1158 * @param $dir string Virtual URL (or storage path) of directory to clean
1161 protected function initDirectory( $dir ) {
1162 $path = $this->resolveToStoragePath( $dir );
1163 list( $b, $container, $r ) = FileBackend
::splitStoragePath( $path );
1165 $params = array( 'dir' => $path );
1166 if ( $container === $this->zones
['deleted']['container'] ) {
1167 # Take all available measures to prevent web accessibility of new deleted
1168 # directories, in case the user has not configured offline storage
1169 $params = array( 'noAccess' => true, 'noListing' => true ) +
$params;
1172 return $this->backend
->prepare( $params );
1176 * Deletes a directory if empty.
1178 * @param $dir string Virtual URL (or storage path) of directory to clean
1181 public function cleanDir( $dir ) {
1182 $this->assertWritableRepo(); // fail out if read-only
1184 $status = $this->newGood();
1185 $status->merge( $this->backend
->clean(
1186 array( 'dir' => $this->resolveToStoragePath( $dir ) ) ) );
1192 * Checks existence of a a file
1194 * @param $file string Virtual URL (or storage path) of file to check
1197 public function fileExists( $file ) {
1198 $result = $this->fileExistsBatch( array( $file ) );
1203 * Checks existence of an array of files.
1205 * @param $files Array: Virtual URLs (or storage paths) of files to check
1206 * @return array|bool Either array of files and existence flags, or false
1208 public function fileExistsBatch( array $files ) {
1210 foreach ( $files as $key => $file ) {
1211 $file = $this->resolveToStoragePath( $file );
1212 $result[$key] = $this->backend
->fileExists( array( 'src' => $file ) );
1218 * Move a file to the deletion archive.
1219 * If no valid deletion archive exists, this may either delete the file
1220 * or throw an exception, depending on the preference of the repository
1222 * @param $srcRel Mixed: relative path for the file to be deleted
1223 * @param $archiveRel Mixed: relative path for the archive location.
1224 * Relative to a private archive directory.
1225 * @return FileRepoStatus object
1227 public function delete( $srcRel, $archiveRel ) {
1228 $this->assertWritableRepo(); // fail out if read-only
1230 return $this->deleteBatch( array( array( $srcRel, $archiveRel ) ) );
1234 * Move a group of files to the deletion archive.
1236 * If no valid deletion archive is configured, this may either delete the
1237 * file or throw an exception, depending on the preference of the repository.
1239 * The overwrite policy is determined by the repository -- currently LocalRepo
1240 * assumes a naming scheme in the deleted zone based on content hash, as
1241 * opposed to the public zone which is assumed to be unique.
1243 * @param $sourceDestPairs Array of source/destination pairs. Each element
1244 * is a two-element array containing the source file path relative to the
1245 * public root in the first element, and the archive file path relative
1246 * to the deleted zone root in the second element.
1247 * @throws MWException
1248 * @return FileRepoStatus
1250 public function deleteBatch( array $sourceDestPairs ) {
1251 $this->assertWritableRepo(); // fail out if read-only
1253 // Try creating directories
1254 $status = $this->initZones( array( 'public', 'deleted' ) );
1255 if ( !$status->isOK() ) {
1259 $status = $this->newGood();
1261 $backend = $this->backend
; // convenience
1262 $operations = array();
1263 // Validate filenames and create archive directories
1264 foreach ( $sourceDestPairs as $pair ) {
1265 list( $srcRel, $archiveRel ) = $pair;
1266 if ( !$this->validateFilename( $srcRel ) ) {
1267 throw new MWException( __METHOD__
.':Validation error in $srcRel' );
1268 } elseif ( !$this->validateFilename( $archiveRel ) ) {
1269 throw new MWException( __METHOD__
.':Validation error in $archiveRel' );
1272 $publicRoot = $this->getZonePath( 'public' );
1273 $srcPath = "{$publicRoot}/$srcRel";
1275 $deletedRoot = $this->getZonePath( 'deleted' );
1276 $archivePath = "{$deletedRoot}/{$archiveRel}";
1277 $archiveDir = dirname( $archivePath ); // does not touch FS
1279 // Create destination directories
1280 if ( !$this->initDirectory( $archiveDir )->isOK() ) {
1281 return $this->newFatal( 'directorycreateerror', $archiveDir );
1284 $operations[] = array(
1287 'dst' => $archivePath,
1288 // We may have 2+ identical files being deleted,
1289 // all of which will map to the same destination file
1290 'overwriteSame' => true // also see bug 31792
1294 // Move the files by execute the operations for each pair.
1295 // We're now committed to returning an OK result, which will
1296 // lead to the files being moved in the DB also.
1297 $opts = array( 'force' => true );
1298 $status->merge( $backend->doOperations( $operations, $opts ) );
1304 * Delete files in the deleted directory if they are not referenced in the filearchive table
1308 public function cleanupDeletedBatch( array $storageKeys ) {
1309 $this->assertWritableRepo();
1313 * Get a relative path for a deletion archive key,
1314 * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
1316 * @param $key string
1319 public function getDeletedHashPath( $key ) {
1321 for ( $i = 0; $i < $this->deletedHashLevels
; $i++
) {
1322 $path .= $key[$i] . '/';
1328 * If a path is a virtual URL, resolve it to a storage path.
1329 * Otherwise, just return the path as it is.
1331 * @param $path string
1333 * @throws MWException
1335 protected function resolveToStoragePath( $path ) {
1336 if ( $this->isVirtualUrl( $path ) ) {
1337 return $this->resolveVirtualUrl( $path );
1343 * Get a local FS copy of a file with a given virtual URL/storage path.
1344 * Temporary files may be purged when the file object falls out of scope.
1346 * @param $virtualUrl string
1347 * @return TempFSFile|null Returns null on failure
1349 public function getLocalCopy( $virtualUrl ) {
1350 $path = $this->resolveToStoragePath( $virtualUrl );
1351 return $this->backend
->getLocalCopy( array( 'src' => $path ) );
1355 * Get a local FS file with a given virtual URL/storage path.
1356 * The file is either an original or a copy. It should not be changed.
1357 * Temporary files may be purged when the file object falls out of scope.
1359 * @param $virtualUrl string
1360 * @return FSFile|null Returns null on failure.
1362 public function getLocalReference( $virtualUrl ) {
1363 $path = $this->resolveToStoragePath( $virtualUrl );
1364 return $this->backend
->getLocalReference( array( 'src' => $path ) );
1368 * Get properties of a file with a given virtual URL/storage path.
1369 * Properties should ultimately be obtained via FSFile::getProps().
1371 * @param $virtualUrl string
1374 public function getFileProps( $virtualUrl ) {
1375 $path = $this->resolveToStoragePath( $virtualUrl );
1376 return $this->backend
->getFileProps( array( 'src' => $path ) );
1380 * Get the timestamp of a file with a given virtual URL/storage path
1382 * @param $virtualUrl string
1383 * @return string|bool False on failure
1385 public function getFileTimestamp( $virtualUrl ) {
1386 $path = $this->resolveToStoragePath( $virtualUrl );
1387 return $this->backend
->getFileTimestamp( array( 'src' => $path ) );
1391 * Get the sha1 of a file with a given virtual URL/storage path
1393 * @param $virtualUrl string
1394 * @return string|bool
1396 public function getFileSha1( $virtualUrl ) {
1397 $path = $this->resolveToStoragePath( $virtualUrl );
1398 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $path ) );
1402 return $tmpFile->getSha1Base36();
1406 * Attempt to stream a file with the given virtual URL/storage path
1408 * @param $virtualUrl string
1409 * @param $headers Array Additional HTTP headers to send on success
1410 * @return bool Success
1412 public function streamFile( $virtualUrl, $headers = array() ) {
1413 $path = $this->resolveToStoragePath( $virtualUrl );
1414 $params = array( 'src' => $path, 'headers' => $headers );
1415 return $this->backend
->streamFile( $params )->isOK();
1419 * Call a callback function for every public regular file in the repository.
1420 * This only acts on the current version of files, not any old versions.
1421 * May use either the database or the filesystem.
1423 * @param $callback Array|string
1426 public function enumFiles( $callback ) {
1427 $this->enumFilesInStorage( $callback );
1431 * Call a callback function for every public file in the repository.
1432 * May use either the database or the filesystem.
1434 * @param $callback Array|string
1437 protected function enumFilesInStorage( $callback ) {
1438 $publicRoot = $this->getZonePath( 'public' );
1439 $numDirs = 1 << ( $this->hashLevels
* 4 );
1440 // Use a priori assumptions about directory structure
1441 // to reduce the tree height of the scanning process.
1442 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++
) {
1443 $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
1444 $path = $publicRoot;
1445 for ( $hexPos = 0; $hexPos < $this->hashLevels
; $hexPos++
) {
1446 $path .= '/' . substr( $hexString, 0, $hexPos +
1 );
1448 $iterator = $this->backend
->getFileList( array( 'dir' => $path ) );
1449 foreach ( $iterator as $name ) {
1450 // Each item returned is a public file
1451 call_user_func( $callback, "{$path}/{$name}" );
1457 * Determine if a relative path is valid, i.e. not blank or involving directory traveral
1459 * @param $filename string
1462 public function validateFilename( $filename ) {
1463 if ( strval( $filename ) == '' ) {
1466 return FileBackend
::isPathTraversalFree( $filename );
1470 * Get a callback function to use for cleaning error message parameters
1474 function getErrorCleanupFunction() {
1475 switch ( $this->pathDisclosureProtection
) {
1477 case 'simple': // b/c
1478 $callback = array( $this, 'passThrough' );
1480 default: // 'paranoid'
1481 $callback = array( $this, 'paranoidClean' );
1487 * Path disclosure protection function
1489 * @param $param string
1492 function paranoidClean( $param ) {
1497 * Path disclosure protection function
1499 * @param $param string
1502 function passThrough( $param ) {
1507 * Create a new fatal error
1509 * @return FileRepoStatus
1511 public function newFatal( $message /*, parameters...*/ ) {
1512 $params = func_get_args();
1513 array_unshift( $params, $this );
1514 return MWInit
::callStaticMethod( 'FileRepoStatus', 'newFatal', $params );
1518 * Create a new good result
1520 * @param $value null|string
1521 * @return FileRepoStatus
1523 public function newGood( $value = null ) {
1524 return FileRepoStatus
::newGood( $this, $value );
1528 * Checks if there is a redirect named as $title. If there is, return the
1529 * title object. If not, return false.
1532 * @param $title Title of image
1535 public function checkRedirect( Title
$title ) {
1540 * Invalidates image redirect cache related to that image
1541 * Doesn't do anything for repositories that don't support image redirects.
1544 * @param $title Title of image
1546 public function invalidateImageRedirect( Title
$title ) {}
1549 * Get the human-readable name of the repo
1553 public function getDisplayName() {
1554 // We don't name our own repo, return nothing
1555 if ( $this->isLocal() ) {
1558 // 'shared-repo-name-wikimediacommons' is used when $wgUseInstantCommons = true
1559 return wfMessageFallback( 'shared-repo-name-' . $this->name
, 'shared-repo' )->text();
1563 * Returns true if this the local file repository.
1567 public function isLocal() {
1568 return $this->getName() == 'local';
1572 * Get a key on the primary cache for this repository.
1573 * Returns false if the repository's cache is not accessible at this site.
1574 * The parameters are the parts of the key, as for wfMemcKey().
1579 public function getSharedCacheKey( /*...*/ ) {
1584 * Get a key for this repo in the local cache domain. These cache keys are
1585 * not shared with remote instances of the repo.
1586 * The parameters are the parts of the key, as for wfMemcKey().
1590 public function getLocalCacheKey( /*...*/ ) {
1591 $args = func_get_args();
1592 array_unshift( $args, 'filerepo', $this->getName() );
1593 return call_user_func_array( 'wfMemcKey', $args );
1597 * Get an temporary FileRepo associated with this repo.
1598 * Files will be created in the temp zone of this repo and
1599 * thumbnails in a /temp subdirectory in thumb zone of this repo.
1600 * It will have the same backend as this repo.
1602 * @return TempFileRepo
1604 public function getTempRepo() {
1605 return new TempFileRepo( array(
1606 'name' => "{$this->name}-temp",
1607 'backend' => $this->backend
,
1610 'container' => $this->zones
['temp']['container'],
1611 'directory' => $this->zones
['temp']['directory']
1614 'container' => $this->zones
['thumb']['container'],
1615 'directory' => ( $this->zones
['thumb']['directory'] == '' )
1617 : $this->zones
['thumb']['directory'] . '/temp'
1620 'url' => $this->getZoneUrl( 'temp' ),
1621 'thumbUrl' => $this->getZoneUrl( 'thumb' ) . '/temp',
1622 'hashLevels' => $this->hashLevels
// performance
1627 * Get an UploadStash associated with this repo.
1629 * @return UploadStash
1631 public function getUploadStash() {
1632 return new UploadStash( $this );
1636 * Throw an exception if this repo is read-only by design.
1637 * This does not and should not check getReadOnlyReason().
1640 * @throws MWException
1642 protected function assertWritableRepo() {}
1646 * FileRepo for temporary files created via FileRepo::getTempRepo()
1648 class TempFileRepo
extends FileRepo
{
1649 public function getTempRepo() {
1650 throw new MWException( "Cannot get a temp repo from a temp repo." );