3 * A repository for files accessible via the local filesystem.
10 * A repository for files accessible via the local filesystem. Does not support
11 * database access or registration.
14 class FSRepo
extends FileRepo
{
15 var $directory, $deletedDir, $deletedHashLevels, $fileMode;
16 var $fileFactory = array( 'UnregisteredLocalFile', 'newFromTitle' );
17 var $oldFileFactory = false;
18 var $pathDisclosureProtection = 'simple';
20 function __construct( $info ) {
21 parent
::__construct( $info );
24 $this->directory
= $info['directory'];
25 $this->url
= $info['url'];
28 $this->hashLevels
= isset( $info['hashLevels'] ) ?
$info['hashLevels'] : 2;
29 $this->deletedHashLevels
= isset( $info['deletedHashLevels'] ) ?
30 $info['deletedHashLevels'] : $this->hashLevels
;
31 $this->deletedDir
= isset( $info['deletedDir'] ) ?
$info['deletedDir'] : false;
32 $this->fileMode
= isset( $info['fileMode'] ) ?
$info['fileMode'] : 0644;
33 if ( isset( $info['thumbDir'] ) ) {
34 $this->thumbDir
= $info['thumbDir'];
36 $this->thumbDir
= "{$this->directory}/thumb";
38 if ( isset( $info['thumbUrl'] ) ) {
39 $this->thumbUrl
= $info['thumbUrl'];
41 $this->thumbUrl
= "{$this->url}/thumb";
46 * Get the public root directory of the repository.
49 function getRootDirectory() {
50 return $this->directory
;
54 * Get the public root URL of the repository
57 function getRootUrl() {
62 * Returns true if the repository uses a multi-level directory structure
66 return (bool)$this->hashLevels
;
70 * Get the local directory corresponding to one of the three basic zones
76 function getZonePath( $zone ) {
79 return $this->directory
;
81 return "{$this->directory}/temp";
83 return $this->deletedDir
;
85 return $this->thumbDir
;
92 * @see FileRepo::getZoneUrl()
98 function getZoneUrl( $zone ) {
103 return "{$this->url}/temp";
105 return parent
::getZoneUrl( $zone ); // no public URL
107 return $this->thumbUrl
;
109 return parent
::getZoneUrl( $zone );
114 * Get a URL referring to this repository, with the private mwrepo protocol.
115 * The suffix, if supplied, is considered to be unencoded, and will be
116 * URL-encoded before being returned.
118 * @param $suffix string
122 function getVirtualUrl( $suffix = false ) {
123 $path = 'mwrepo://' . $this->name
;
124 if ( $suffix !== false ) {
125 $path .= '/' . rawurlencode( $suffix );
131 * Get the local path corresponding to a virtual URL
137 function resolveVirtualUrl( $url ) {
138 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
139 throw new MWException( __METHOD__
.': unknown protocol' );
142 $bits = explode( '/', substr( $url, 9 ), 3 );
143 if ( count( $bits ) != 3 ) {
144 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
146 list( $repo, $zone, $rel ) = $bits;
147 if ( $repo !== $this->name
) {
148 throw new MWException( __METHOD__
.": fetching from a foreign repo is not supported" );
150 $base = $this->getZonePath( $zone );
152 throw new MWException( __METHOD__
.": invalid zone: $zone" );
154 return $base . '/' . rawurldecode( $rel );
158 * Store a batch of files
160 * @param $triplets Array: (src,zone,dest) triplets as per store()
161 * @param $flags Integer: bitwise combination of the following flags:
162 * self::DELETE_SOURCE Delete the source file after upload
163 * self::OVERWRITE Overwrite an existing destination file instead of failing
164 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
165 * same contents as the source
168 function storeBatch( $triplets, $flags = 0 ) {
169 wfDebug( __METHOD__
. ': Storing ' . count( $triplets ) .
170 " triplets; flags: {$flags}\n" );
172 // Try creating directories
173 if ( !wfMkdirParents( $this->directory
, null, __METHOD__
) ) {
174 return $this->newFatal( 'upload_directory_missing', $this->directory
);
176 if ( !is_writable( $this->directory
) ) {
177 return $this->newFatal( 'upload_directory_read_only', $this->directory
);
180 // Validate each triplet
181 $status = $this->newGood();
182 foreach ( $triplets as $i => $triplet ) {
183 list( $srcPath, $dstZone, $dstRel ) = $triplet;
185 // Resolve destination path
186 $root = $this->getZonePath( $dstZone );
188 throw new MWException( "Invalid zone: $dstZone" );
190 if ( !$this->validateFilename( $dstRel ) ) {
191 throw new MWException( 'Validation error in $dstRel' );
193 $dstPath = "$root/$dstRel";
194 $dstDir = dirname( $dstPath );
196 // Create destination directories for this triplet
197 if ( !is_dir( $dstDir ) ) {
198 if ( !wfMkdirParents( $dstDir, null, __METHOD__
) ) {
199 return $this->newFatal( 'directorycreateerror', $dstDir );
201 if ( $dstZone == 'deleted' ) {
202 $this->initDeletedDir( $dstDir );
207 if ( self
::isVirtualUrl( $srcPath ) ) {
208 $srcPath = $triplets[$i][0] = $this->resolveVirtualUrl( $srcPath );
210 if ( !is_file( $srcPath ) ) {
211 // Make a list of files that don't exist for return to the caller
212 $status->fatal( 'filenotfound', $srcPath );
217 if ( !( $flags & self
::OVERWRITE
) && file_exists( $dstPath ) ) {
218 if ( $flags & self
::OVERWRITE_SAME
) {
219 $hashSource = sha1_file( $srcPath );
220 $hashDest = sha1_file( $dstPath );
221 if ( $hashSource != $hashDest ) {
222 $status->fatal( 'fileexistserror', $dstPath );
223 $status->failCount++
;
226 $status->fatal( 'fileexistserror', $dstPath );
227 $status->failCount++
;
232 // Windows does not support moving over existing files, so explicitly delete them
233 $deleteDest = wfIsWindows() && ( $flags & self
::OVERWRITE
);
235 // Abort now on failure
236 if ( !$status->ok
) {
240 // Execute the store operation for each triplet
241 foreach ( $triplets as $i => $triplet ) {
242 list( $srcPath, $dstZone, $dstRel ) = $triplet;
243 $root = $this->getZonePath( $dstZone );
244 $dstPath = "$root/$dstRel";
247 if ( $flags & self
::DELETE_SOURCE
) {
251 if ( !rename( $srcPath, $dstPath ) ) {
252 $status->error( 'filerenameerror', $srcPath, $dstPath );
256 if ( !copy( $srcPath, $dstPath ) ) {
257 $status->error( 'filecopyerror', $srcPath, $dstPath );
260 if ( !( $flags & self
::SKIP_VALIDATION
) ) {
261 wfSuppressWarnings();
262 $hashSource = sha1_file( $srcPath );
263 $hashDest = sha1_file( $dstPath );
266 if ( $hashDest === false ||
$hashSource !== $hashDest ) {
267 wfDebug( __METHOD__
. ': File copy validation failed: ' .
268 "$srcPath ($hashSource) to $dstPath ($hashDest)\n" );
270 $status->error( 'filecopyerror', $srcPath, $dstPath );
276 $this->chmod( $dstPath );
277 $status->successCount++
;
279 $status->failCount++
;
281 $status->success
[$i] = $good;
287 * Deletes a batch of files. Each file can be a (zone, rel) pairs, a
288 * virtual url or a real path. It will try to delete each file, but
289 * ignores any errors that may occur
291 * @param $pairs array List of files to delete
294 function cleanupBatch( $files ) {
295 foreach ( $files as $file ) {
296 if ( is_array( $file ) ) {
297 // This is a pair, extract it
298 list( $zone, $rel ) = $file;
299 $root = $this->getZonePath( $zone );
300 $path = "$root/$rel";
302 if ( self
::isVirtualUrl( $file ) ) {
303 // This is a virtual url, resolve it
304 $path = $this->resolveVirtualUrl( $file );
306 // This is a full file name
311 wfSuppressWarnings();
320 function append( $srcPath, $toAppendPath, $flags = 0 ) {
321 $status = $this->newGood();
323 // Resolve the virtual URL
324 if ( self
::isVirtualUrl( $toAppendPath ) ) {
325 $toAppendPath = $this->resolveVirtualUrl( $toAppendPath );
327 // Make sure the files are there
328 if ( !is_file( $toAppendPath ) )
329 $status->fatal( 'filenotfound', $toAppendPath );
331 if ( !is_file( $srcPath ) )
332 $status->fatal( 'filenotfound', $srcPath );
334 if ( !$status->isOk() ) return $status;
337 $chunk = file_get_contents( $srcPath );
338 if( $chunk === false ) {
339 $status->fatal( 'fileappenderrorread', $srcPath );
342 if( $status->isOk() ) {
343 if ( file_put_contents( $toAppendPath, $chunk, FILE_APPEND
) ) {
344 $status->value
= $toAppendPath;
346 $status->fatal( 'fileappenderror', $srcPath, $toAppendPath);
350 if ( $flags & self
::DELETE_SOURCE
) {
357 /* We can actually append to the files, so no-op needed here. */
358 function appendFinish( $toAppendPath ) {}
361 * Checks existence of specified array of files.
363 * @param $files Array: URLs of files to check
364 * @param $flags Integer: bitwise combination of the following flags:
365 * self::FILES_ONLY Mark file as existing only if it is a file (not directory)
366 * @return Either array of files and existence flags, or false
368 function fileExistsBatch( $files, $flags = 0 ) {
369 if ( !file_exists( $this->directory
) ||
!is_readable( $this->directory
) ) {
373 foreach ( $files as $key => $file ) {
374 if ( self
::isVirtualUrl( $file ) ) {
375 $file = $this->resolveVirtualUrl( $file );
377 if( $flags & self
::FILES_ONLY
) {
378 $result[$key] = is_file( $file );
380 $result[$key] = file_exists( $file );
388 * Take all available measures to prevent web accessibility of new deleted
389 * directories, in case the user has not configured offline storage
392 protected function initDeletedDir( $dir ) {
393 // Add a .htaccess file to the root of the deleted zone
394 $root = $this->getZonePath( 'deleted' );
395 if ( !file_exists( "$root/.htaccess" ) ) {
396 file_put_contents( "$root/.htaccess", "Deny from all\n" );
398 // Seed new directories with a blank index.html, to prevent crawling
399 file_put_contents( "$dir/index.html", '' );
403 * Pick a random name in the temp zone and store a file to it.
404 * @param $originalName String: the base name of the file as specified
405 * by the user. The file extension will be maintained.
406 * @param $srcPath String: the current location of the file.
407 * @return FileRepoStatus object with the URL in the value.
409 function storeTemp( $originalName, $srcPath ) {
410 $date = gmdate( "YmdHis" );
411 $hashPath = $this->getHashPath( $originalName );
412 $dstRel = "$hashPath$date!$originalName";
413 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
415 $result = $this->store( $srcPath, 'temp', $dstRel );
416 $result->value
= $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
421 * Remove a temporary file or mark it for garbage collection
422 * @param $virtualUrl String: the virtual URL returned by storeTemp
423 * @return Boolean: true on success, false on failure
425 function freeTemp( $virtualUrl ) {
426 $temp = "mwrepo://{$this->name}/temp";
427 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
428 wfDebug( __METHOD__
.": Invalid virtual URL\n" );
431 $path = $this->resolveVirtualUrl( $virtualUrl );
432 wfSuppressWarnings();
433 $success = unlink( $path );
439 * Publish a batch of files
440 * @param $triplets Array: (source,dest,archive) triplets as per publish()
441 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
442 * that the source files should be deleted if possible
445 function publishBatch( $triplets, $flags = 0 ) {
446 // Perform initial checks
447 if ( !wfMkdirParents( $this->directory
, null, __METHOD__
) ) {
448 return $this->newFatal( 'upload_directory_missing', $this->directory
);
450 if ( !is_writable( $this->directory
) ) {
451 return $this->newFatal( 'upload_directory_read_only', $this->directory
);
453 $status = $this->newGood( array() );
454 foreach ( $triplets as $i => $triplet ) {
455 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
457 if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) {
458 $triplets[$i][0] = $srcPath = $this->resolveVirtualUrl( $srcPath );
460 if ( !$this->validateFilename( $dstRel ) ) {
461 throw new MWException( 'Validation error in $dstRel' );
463 if ( !$this->validateFilename( $archiveRel ) ) {
464 throw new MWException( 'Validation error in $archiveRel' );
466 $dstPath = "{$this->directory}/$dstRel";
467 $archivePath = "{$this->directory}/$archiveRel";
469 $dstDir = dirname( $dstPath );
470 $archiveDir = dirname( $archivePath );
471 // Abort immediately on directory creation errors since they're likely to be repetitive
472 if ( !is_dir( $dstDir ) && !wfMkdirParents( $dstDir, null, __METHOD__
) ) {
473 return $this->newFatal( 'directorycreateerror', $dstDir );
475 if ( !is_dir( $archiveDir ) && !wfMkdirParents( $archiveDir, null, __METHOD__
) ) {
476 return $this->newFatal( 'directorycreateerror', $archiveDir );
478 if ( !is_file( $srcPath ) ) {
479 // Make a list of files that don't exist for return to the caller
480 $status->fatal( 'filenotfound', $srcPath );
484 if ( !$status->ok
) {
488 foreach ( $triplets as $i => $triplet ) {
489 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
490 $dstPath = "{$this->directory}/$dstRel";
491 $archivePath = "{$this->directory}/$archiveRel";
493 // Archive destination file if it exists
494 if( is_file( $dstPath ) ) {
495 // Check if the archive file exists
496 // This is a sanity check to avoid data loss. In UNIX, the rename primitive
497 // unlinks the destination file if it exists. DB-based synchronisation in
498 // publishBatch's caller should prevent races. In Windows there's no
499 // problem because the rename primitive fails if the destination exists.
500 if ( is_file( $archivePath ) ) {
503 wfSuppressWarnings();
504 $success = rename( $dstPath, $archivePath );
509 $status->error( 'filerenameerror',$dstPath, $archivePath );
510 $status->failCount++
;
513 wfDebug(__METHOD__
.": moved file $dstPath to $archivePath\n");
515 $status->value
[$i] = 'archived';
517 $status->value
[$i] = 'new';
521 wfSuppressWarnings();
522 if ( $flags & self
::DELETE_SOURCE
) {
523 if ( !rename( $srcPath, $dstPath ) ) {
524 $status->error( 'filerenameerror', $srcPath, $dstPath );
528 if ( !copy( $srcPath, $dstPath ) ) {
529 $status->error( 'filecopyerror', $srcPath, $dstPath );
536 $status->successCount++
;
537 wfDebug(__METHOD__
.": wrote tempfile $srcPath to $dstPath\n");
538 // Thread-safe override for umask
539 $this->chmod( $dstPath );
541 $status->failCount++
;
548 * Move a group of files to the deletion archive.
549 * If no valid deletion archive is configured, this may either delete the
550 * file or throw an exception, depending on the preference of the repository.
552 * @param $sourceDestPairs Array of source/destination pairs. Each element
553 * is a two-element array containing the source file path relative to the
554 * public root in the first element, and the archive file path relative
555 * to the deleted zone root in the second element.
556 * @return FileRepoStatus
558 function deleteBatch( $sourceDestPairs ) {
559 $status = $this->newGood();
560 if ( !$this->deletedDir
) {
561 throw new MWException( __METHOD__
.': no valid deletion archive directory' );
565 * Validate filenames and create archive directories
567 foreach ( $sourceDestPairs as $pair ) {
568 list( $srcRel, $archiveRel ) = $pair;
569 if ( !$this->validateFilename( $srcRel ) ) {
570 throw new MWException( __METHOD__
.':Validation error in $srcRel' );
572 if ( !$this->validateFilename( $archiveRel ) ) {
573 throw new MWException( __METHOD__
.':Validation error in $archiveRel' );
575 $archivePath = "{$this->deletedDir}/$archiveRel";
576 $archiveDir = dirname( $archivePath );
577 if ( !is_dir( $archiveDir ) ) {
578 if ( !wfMkdirParents( $archiveDir, null, __METHOD__
) ) {
579 $status->fatal( 'directorycreateerror', $archiveDir );
582 $this->initDeletedDir( $archiveDir );
584 // Check if the archive directory is writable
585 // This doesn't appear to work on NTFS
586 if ( !is_writable( $archiveDir ) ) {
587 $status->fatal( 'filedelete-archive-read-only', $archiveDir );
590 if ( !$status->ok
) {
597 * We're now committed to returning an OK result, which will lead to
598 * the files being moved in the DB also.
600 foreach ( $sourceDestPairs as $pair ) {
601 list( $srcRel, $archiveRel ) = $pair;
602 $srcPath = "{$this->directory}/$srcRel";
603 $archivePath = "{$this->deletedDir}/$archiveRel";
604 if ( file_exists( $archivePath ) ) {
605 # A file with this content hash is already archived
606 wfSuppressWarnings();
607 $good = unlink( $srcPath );
610 $status->error( 'filedeleteerror', $srcPath );
613 wfSuppressWarnings();
614 $good = rename( $srcPath, $archivePath );
617 $status->error( 'filerenameerror', $srcPath, $archivePath );
619 $this->chmod( $archivePath );
623 $status->successCount++
;
625 $status->failCount++
;
632 * Get a relative path for a deletion archive key,
633 * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
636 function getDeletedHashPath( $key ) {
638 for ( $i = 0; $i < $this->deletedHashLevels
; $i++
) {
639 $path .= $key[$i] . '/';
645 * Call a callback function for every file in the repository.
646 * Uses the filesystem even in child classes.
649 function enumFilesInFS( $callback ) {
650 $numDirs = 1 << ( $this->hashLevels
* 4 );
651 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++
) {
652 $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
653 $path = $this->directory
;
654 for ( $hexPos = 0; $hexPos < $this->hashLevels
; $hexPos++
) {
655 $path .= '/' . substr( $hexString, 0, $hexPos +
1 );
657 if ( !file_exists( $path ) ||
!is_dir( $path ) ) {
660 $dir = opendir( $path );
662 while ( false !== ( $name = readdir( $dir ) ) ) {
663 call_user_func( $callback, $path . '/' . $name );
671 * Call a callback function for every file in the repository
672 * May use either the database or the filesystem
675 function enumFiles( $callback ) {
676 $this->enumFilesInFS( $callback );
680 * Get properties of a file with a given virtual URL
681 * The virtual URL must refer to this repo
684 function getFileProps( $virtualUrl ) {
685 $path = $this->resolveVirtualUrl( $virtualUrl );
686 return File
::getPropsFromPath( $path );
690 * Path disclosure protection functions
692 * Get a callback function to use for cleaning error message parameters
694 function getErrorCleanupFunction() {
695 switch ( $this->pathDisclosureProtection
) {
697 $callback = array( $this, 'simpleClean' );
700 $callback = parent
::getErrorCleanupFunction();
705 function simpleClean( $param ) {
706 if ( !isset( $this->simpleCleanPairs
) ) {
708 $this->simpleCleanPairs
= array(
709 $this->directory
=> 'public',
710 "{$this->directory}/temp" => 'temp',
712 dirname( __FILE__
) => '$IP/extensions/WebStore',
714 if ( $this->deletedDir
) {
715 $this->simpleCleanPairs
[$this->deletedDir
] = 'deleted';
718 return strtr( $param, $this->simpleCleanPairs
);
722 * Chmod a file, supressing the warnings.
723 * @param $path String: the path to change
726 protected function chmod( $path ) {
727 wfSuppressWarnings();
728 chmod( $path, $this->fileMode
);