Revert r106545 and pass a null variable by ref (also updated the documentation) so...
[mediawiki.git] / includes / filerepo / FSRepo.php
blobcec4c23f7e26e091d8e4c1ce3189d70675ceed00
1 <?php
2 /**
3 * A repository for files accessible via the local filesystem.
5 * @file
6 * @ingroup FileRepo
7 */
9 /**
10 * A repository for files accessible via the local filesystem. Does not support
11 * database access or registration.
12 * @ingroup FileRepo
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 );
23 // Required settings
24 $this->directory = $info['directory'];
25 $this->url = $info['url'];
27 // Optional settings
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'];
35 } else {
36 $this->thumbDir = "{$this->directory}/thumb";
38 if ( isset( $info['thumbUrl'] ) ) {
39 $this->thumbUrl = $info['thumbUrl'];
40 } else {
41 $this->thumbUrl = "{$this->url}/thumb";
45 /**
46 * Get the public root directory of the repository.
47 * @return string
49 function getRootDirectory() {
50 return $this->directory;
53 /**
54 * Get the public root URL of the repository
55 * @return string
57 function getRootUrl() {
58 return $this->url;
61 /**
62 * Returns true if the repository uses a multi-level directory structure
63 * @return string
65 function isHashed() {
66 return (bool)$this->hashLevels;
69 /**
70 * Get the local directory corresponding to one of the three basic zones
72 * @param $zone string
74 * @return string
76 function getZonePath( $zone ) {
77 switch ( $zone ) {
78 case 'public':
79 return $this->directory;
80 case 'temp':
81 return "{$this->directory}/temp";
82 case 'deleted':
83 return $this->deletedDir;
84 case 'thumb':
85 return $this->thumbDir;
86 default:
87 return false;
91 /**
92 * @see FileRepo::getZoneUrl()
94 * @param $zone string
96 * @return string url
98 function getZoneUrl( $zone ) {
99 switch ( $zone ) {
100 case 'public':
101 return $this->url;
102 case 'temp':
103 return "{$this->url}/temp";
104 case 'deleted':
105 return parent::getZoneUrl( $zone ); // no public URL
106 case 'thumb':
107 return $this->thumbUrl;
108 default:
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
120 * @return string
122 function getVirtualUrl( $suffix = false ) {
123 $path = 'mwrepo://' . $this->name;
124 if ( $suffix !== false ) {
125 $path .= '/' . rawurlencode( $suffix );
127 return $path;
131 * Get the local path corresponding to a virtual URL
133 * @param $url string
135 * @return string
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 );
151 if ( !$base ) {
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
166 * @return Status
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 );
187 if ( !$root ) {
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 );
206 // Resolve source
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 );
213 continue;
216 // Check overwriting
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++;
225 } else {
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 ) {
237 return $status;
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";
245 $good = true;
247 if ( $flags & self::DELETE_SOURCE ) {
248 if ( $deleteDest ) {
249 unlink( $dstPath );
251 if ( !rename( $srcPath, $dstPath ) ) {
252 $status->error( 'filerenameerror', $srcPath, $dstPath );
253 $good = false;
255 } else {
256 if ( !copy( $srcPath, $dstPath ) ) {
257 $status->error( 'filecopyerror', $srcPath, $dstPath );
258 $good = false;
260 if ( !( $flags & self::SKIP_VALIDATION ) ) {
261 wfSuppressWarnings();
262 $hashSource = sha1_file( $srcPath );
263 $hashDest = sha1_file( $dstPath );
264 wfRestoreWarnings();
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 );
271 $good = false;
275 if ( $good ) {
276 $this->chmod( $dstPath );
277 $status->successCount++;
278 } else {
279 $status->failCount++;
281 $status->success[$i] = $good;
283 return $status;
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
292 * @return void
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";
301 } else {
302 if ( self::isVirtualUrl( $file ) ) {
303 // This is a virtual url, resolve it
304 $path = $this->resolveVirtualUrl( $file );
305 } else {
306 // This is a full file name
307 $path = $file;
311 wfSuppressWarnings();
312 unlink( $path );
313 wfRestoreWarnings();
317 * Concatenate a list of files into a target file location.
319 * @param $fileList array of files
320 * @param $targetFile String target path
321 * @param $flags Integer: bitwise combination of the following flags:
322 * self::FILES_ONLY Mark file as existing only if it is a file (not directory)
324 function concatenate( $fileList, $targetPath, $flags = 0 ){
325 $status = $this->newGood();
326 // Resolve the virtual URL for taget:
327 if ( self::isVirtualUrl( $targetPath ) ) {
328 $targetPath = $this->resolveVirtualUrl( $targetPath );
329 // empty out the target file:
330 if ( is_file( $targetPath ) ){
331 unlink( $targetPath );
334 foreach( $fileList as $sourcePath ){
335 // Resolve the virtual URL for source:
336 if ( self::isVirtualUrl( $sourcePath ) ) {
337 $sourcePath = $this->resolveVirtualUrl( $sourcePath );
339 if ( !is_file( $sourcePath ) )
340 $status->fatal( 'filenotfound', $sourcePath );
342 if ( !$status->isOk() ){
343 return $status;
346 // Do the append
347 $chunk = file_get_contents( $sourcePath );
348 if( $chunk === false ) {
349 $status->fatal( 'fileconcatenateerrorread', $sourcePath );
350 return $status;
352 if( $status->isOk() ) {
353 if ( file_put_contents( $targetPath, $chunk, FILE_APPEND ) ) {
354 $status->value = $targetPath;
355 } else {
356 $status->fatal( 'fileconcatenateerror', $sourcePath, $targetPath);
359 if ( $flags & self::DELETE_SOURCE ) {
360 unlink( $sourcePath );
363 return $status;
366 * @deprecated 1.19
368 * @return Status
370 function append( $srcPath, $toAppendPath, $flags = 0 ) {
371 wfDeprecated( __METHOD__, '1.19' );
373 $status = $this->newGood();
375 // Resolve the virtual URL
376 if ( self::isVirtualUrl( $toAppendPath ) ) {
377 $toAppendPath = $this->resolveVirtualUrl( $toAppendPath );
379 // Make sure the files are there
380 if ( !is_file( $toAppendPath ) )
381 $status->fatal( 'filenotfound', $toAppendPath );
383 if ( !is_file( $srcPath ) )
384 $status->fatal( 'filenotfound', $srcPath );
386 if ( !$status->isOk() ) return $status;
388 // Do the append
389 $chunk = file_get_contents( $srcPath );
390 if( $chunk === false ) {
391 $status->fatal( 'fileappenderrorread', $srcPath );
394 if( $status->isOk() ) {
395 if ( file_put_contents( $toAppendPath, $chunk, FILE_APPEND ) ) {
396 $status->value = $toAppendPath;
397 } else {
398 $status->fatal( 'fileappenderror', $srcPath, $toAppendPath);
402 if ( $flags & self::DELETE_SOURCE ) {
403 unlink( $srcPath );
406 return $status;
409 /* We can actually append to the files, so no-op needed here. */
410 function appendFinish( $toAppendPath ) {}
413 * Checks existence of specified array of files.
415 * @param $files Array: URLs of files to check
416 * @param $flags Integer: bitwise combination of the following flags:
417 * self::FILES_ONLY Mark file as existing only if it is a file (not directory)
418 * @return Either array of files and existence flags, or false
420 function fileExistsBatch( $files, $flags = 0 ) {
421 if ( !file_exists( $this->directory ) || !is_readable( $this->directory ) ) {
422 return false;
424 $result = array();
425 foreach ( $files as $key => $file ) {
426 if ( self::isVirtualUrl( $file ) ) {
427 $file = $this->resolveVirtualUrl( $file );
429 if( $flags & self::FILES_ONLY ) {
430 $result[$key] = is_file( $file );
431 } else {
432 $result[$key] = file_exists( $file );
436 return $result;
440 * Take all available measures to prevent web accessibility of new deleted
441 * directories, in case the user has not configured offline storage
442 * @return void
444 protected function initDeletedDir( $dir ) {
445 // Add a .htaccess file to the root of the deleted zone
446 $root = $this->getZonePath( 'deleted' );
447 if ( !file_exists( "$root/.htaccess" ) ) {
448 file_put_contents( "$root/.htaccess", "Deny from all\n" );
450 // Seed new directories with a blank index.html, to prevent crawling
451 file_put_contents( "$dir/index.html", '' );
455 * Pick a random name in the temp zone and store a file to it.
456 * @param $originalName String: the base name of the file as specified
457 * by the user. The file extension will be maintained.
458 * @param $srcPath String: the current location of the file.
459 * @return FileRepoStatus object with the URL in the value.
461 function storeTemp( $originalName, $srcPath ) {
462 $date = gmdate( "YmdHis" );
463 $hashPath = $this->getHashPath( $originalName );
464 $dstRel = "$hashPath$date!$originalName";
465 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
467 $result = $this->store( $srcPath, 'temp', $dstRel );
468 $result->value = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
469 return $result;
473 * Remove a temporary file or mark it for garbage collection
474 * @param $virtualUrl String: the virtual URL returned by storeTemp
475 * @return Boolean: true on success, false on failure
477 function freeTemp( $virtualUrl ) {
478 $temp = "mwrepo://{$this->name}/temp";
479 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
480 wfDebug( __METHOD__.": Invalid virtual URL\n" );
481 return false;
483 $path = $this->resolveVirtualUrl( $virtualUrl );
484 wfSuppressWarnings();
485 $success = unlink( $path );
486 wfRestoreWarnings();
487 return $success;
491 * Publish a batch of files
492 * @param $triplets Array: (source,dest,archive) triplets as per publish()
493 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
494 * that the source files should be deleted if possible
495 * @return Status
497 function publishBatch( $triplets, $flags = 0 ) {
498 // Perform initial checks
499 if ( !wfMkdirParents( $this->directory, null, __METHOD__ ) ) {
500 return $this->newFatal( 'upload_directory_missing', $this->directory );
502 if ( !is_writable( $this->directory ) ) {
503 return $this->newFatal( 'upload_directory_read_only', $this->directory );
505 $status = $this->newGood( array() );
506 foreach ( $triplets as $i => $triplet ) {
507 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
509 if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) {
510 $triplets[$i][0] = $srcPath = $this->resolveVirtualUrl( $srcPath );
512 if ( !$this->validateFilename( $dstRel ) ) {
513 throw new MWException( 'Validation error in $dstRel' );
515 if ( !$this->validateFilename( $archiveRel ) ) {
516 throw new MWException( 'Validation error in $archiveRel' );
518 $dstPath = "{$this->directory}/$dstRel";
519 $archivePath = "{$this->directory}/$archiveRel";
521 $dstDir = dirname( $dstPath );
522 $archiveDir = dirname( $archivePath );
523 // Abort immediately on directory creation errors since they're likely to be repetitive
524 if ( !is_dir( $dstDir ) && !wfMkdirParents( $dstDir, null, __METHOD__ ) ) {
525 return $this->newFatal( 'directorycreateerror', $dstDir );
527 if ( !is_dir( $archiveDir ) && !wfMkdirParents( $archiveDir, null, __METHOD__ ) ) {
528 return $this->newFatal( 'directorycreateerror', $archiveDir );
530 if ( !is_file( $srcPath ) ) {
531 // Make a list of files that don't exist for return to the caller
532 $status->fatal( 'filenotfound', $srcPath );
536 if ( !$status->ok ) {
537 return $status;
540 foreach ( $triplets as $i => $triplet ) {
541 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
542 $dstPath = "{$this->directory}/$dstRel";
543 $archivePath = "{$this->directory}/$archiveRel";
545 // Archive destination file if it exists
546 if( is_file( $dstPath ) ) {
547 // Check if the archive file exists
548 // This is a sanity check to avoid data loss. In UNIX, the rename primitive
549 // unlinks the destination file if it exists. DB-based synchronisation in
550 // publishBatch's caller should prevent races. In Windows there's no
551 // problem because the rename primitive fails if the destination exists.
552 if ( is_file( $archivePath ) ) {
553 $success = false;
554 } else {
555 wfSuppressWarnings();
556 $success = rename( $dstPath, $archivePath );
557 wfRestoreWarnings();
560 if( !$success ) {
561 $status->error( 'filerenameerror',$dstPath, $archivePath );
562 $status->failCount++;
563 continue;
564 } else {
565 wfDebug(__METHOD__.": moved file $dstPath to $archivePath\n");
567 $status->value[$i] = 'archived';
568 } else {
569 $status->value[$i] = 'new';
572 $good = true;
573 wfSuppressWarnings();
574 if ( $flags & self::DELETE_SOURCE ) {
575 if ( !rename( $srcPath, $dstPath ) ) {
576 $status->error( 'filerenameerror', $srcPath, $dstPath );
577 $good = false;
579 } else {
580 if ( !copy( $srcPath, $dstPath ) ) {
581 $status->error( 'filecopyerror', $srcPath, $dstPath );
582 $good = false;
585 wfRestoreWarnings();
587 if ( $good ) {
588 $status->successCount++;
589 wfDebug(__METHOD__.": wrote tempfile $srcPath to $dstPath\n");
590 // Thread-safe override for umask
591 $this->chmod( $dstPath );
592 } else {
593 $status->failCount++;
596 return $status;
600 * Move a group of files to the deletion archive.
601 * If no valid deletion archive is configured, this may either delete the
602 * file or throw an exception, depending on the preference of the repository.
604 * @param $sourceDestPairs Array of source/destination pairs. Each element
605 * is a two-element array containing the source file path relative to the
606 * public root in the first element, and the archive file path relative
607 * to the deleted zone root in the second element.
608 * @return FileRepoStatus
610 function deleteBatch( $sourceDestPairs ) {
611 $status = $this->newGood();
612 if ( !$this->deletedDir ) {
613 throw new MWException( __METHOD__.': no valid deletion archive directory' );
617 * Validate filenames and create archive directories
619 foreach ( $sourceDestPairs as $pair ) {
620 list( $srcRel, $archiveRel ) = $pair;
621 if ( !$this->validateFilename( $srcRel ) ) {
622 throw new MWException( __METHOD__.':Validation error in $srcRel' );
624 if ( !$this->validateFilename( $archiveRel ) ) {
625 throw new MWException( __METHOD__.':Validation error in $archiveRel' );
627 $archivePath = "{$this->deletedDir}/$archiveRel";
628 $archiveDir = dirname( $archivePath );
629 if ( !is_dir( $archiveDir ) ) {
630 if ( !wfMkdirParents( $archiveDir, null, __METHOD__ ) ) {
631 $status->fatal( 'directorycreateerror', $archiveDir );
632 continue;
634 $this->initDeletedDir( $archiveDir );
636 // Check if the archive directory is writable
637 // This doesn't appear to work on NTFS
638 if ( !is_writable( $archiveDir ) ) {
639 $status->fatal( 'filedelete-archive-read-only', $archiveDir );
642 if ( !$status->ok ) {
643 // Abort early
644 return $status;
648 * Move the files
649 * We're now committed to returning an OK result, which will lead to
650 * the files being moved in the DB also.
652 foreach ( $sourceDestPairs as $pair ) {
653 list( $srcRel, $archiveRel ) = $pair;
654 $srcPath = "{$this->directory}/$srcRel";
655 $archivePath = "{$this->deletedDir}/$archiveRel";
656 if ( file_exists( $archivePath ) ) {
657 # A file with this content hash is already archived
658 wfSuppressWarnings();
659 $good = unlink( $srcPath );
660 wfRestoreWarnings();
661 if ( !$good ) {
662 $status->error( 'filedeleteerror', $srcPath );
664 } else{
665 wfSuppressWarnings();
666 $good = rename( $srcPath, $archivePath );
667 wfRestoreWarnings();
668 if ( !$good ) {
669 $status->error( 'filerenameerror', $srcPath, $archivePath );
670 } else {
671 $this->chmod( $archivePath );
674 if ( $good ) {
675 $status->successCount++;
676 } else {
677 $status->failCount++;
680 return $status;
684 * Get a relative path for a deletion archive key,
685 * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
686 * @return string
688 function getDeletedHashPath( $key ) {
689 $path = '';
690 for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) {
691 $path .= $key[$i] . '/';
693 return $path;
697 * Call a callback function for every file in the repository.
698 * Uses the filesystem even in child classes.
699 * @return void
701 function enumFilesInFS( $callback ) {
702 $numDirs = 1 << ( $this->hashLevels * 4 );
703 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++ ) {
704 $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
705 $path = $this->directory;
706 for ( $hexPos = 0; $hexPos < $this->hashLevels; $hexPos++ ) {
707 $path .= '/' . substr( $hexString, 0, $hexPos + 1 );
709 if ( !file_exists( $path ) || !is_dir( $path ) ) {
710 continue;
712 $dir = opendir( $path );
713 if ($dir) {
714 while ( false !== ( $name = readdir( $dir ) ) ) {
715 call_user_func( $callback, $path . '/' . $name );
717 closedir( $dir );
723 * Call a callback function for every file in the repository
724 * May use either the database or the filesystem
725 * @return void
727 function enumFiles( $callback ) {
728 $this->enumFilesInFS( $callback );
732 * Get properties of a file with a given virtual URL
733 * The virtual URL must refer to this repo
734 * @return array
736 function getFileProps( $virtualUrl ) {
737 $path = $this->resolveVirtualUrl( $virtualUrl );
738 return File::getPropsFromPath( $path );
742 * Path disclosure protection functions
744 * Get a callback function to use for cleaning error message parameters
746 function getErrorCleanupFunction() {
747 switch ( $this->pathDisclosureProtection ) {
748 case 'simple':
749 $callback = array( $this, 'simpleClean' );
750 break;
751 default:
752 $callback = parent::getErrorCleanupFunction();
754 return $callback;
757 function simpleClean( $param ) {
758 if ( !isset( $this->simpleCleanPairs ) ) {
759 global $IP;
760 $this->simpleCleanPairs = array(
761 $this->directory => 'public',
762 "{$this->directory}/temp" => 'temp',
763 $IP => '$IP',
764 dirname( __FILE__ ) => '$IP/extensions/WebStore',
766 if ( $this->deletedDir ) {
767 $this->simpleCleanPairs[$this->deletedDir] = 'deleted';
770 return strtr( $param, $this->simpleCleanPairs );
774 * Chmod a file, supressing the warnings.
775 * @param $path String: the path to change
776 * @return void
778 protected function chmod( $path ) {
779 wfSuppressWarnings();
780 chmod( $path, $this->fileMode );
781 wfRestoreWarnings();