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.
48 function getRootDirectory() {
49 return $this->directory
;
53 * Get the public root URL of the repository
55 function getRootUrl() {
60 * Returns true if the repository uses a multi-level directory structure
63 return (bool)$this->hashLevels
;
67 * Get the local directory corresponding to one of the three basic zones
69 function getZonePath( $zone ) {
72 return $this->directory
;
74 return "{$this->directory}/temp";
76 return $this->deletedDir
;
78 return $this->thumbDir
;
85 * @see FileRepo::getZoneUrl()
87 function getZoneUrl( $zone ) {
92 return "{$this->url}/temp";
94 return parent
::getZoneUrl( $zone ); // no public URL
96 return $this->thumbUrl
;
98 return parent
::getZoneUrl( $zone );
103 * Get a URL referring to this repository, with the private mwrepo protocol.
104 * The suffix, if supplied, is considered to be unencoded, and will be
105 * URL-encoded before being returned.
107 function getVirtualUrl( $suffix = false ) {
108 $path = 'mwrepo://' . $this->name
;
109 if ( $suffix !== false ) {
110 $path .= '/' . rawurlencode( $suffix );
116 * Get the local path corresponding to a virtual URL
118 function resolveVirtualUrl( $url ) {
119 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
120 throw new MWException( __METHOD__
.': unknown protoocl' );
123 $bits = explode( '/', substr( $url, 9 ), 3 );
124 if ( count( $bits ) != 3 ) {
125 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
127 list( $repo, $zone, $rel ) = $bits;
128 if ( $repo !== $this->name
) {
129 throw new MWException( __METHOD__
.": fetching from a foreign repo is not supported" );
131 $base = $this->getZonePath( $zone );
133 throw new MWException( __METHOD__
.": invalid zone: $zone" );
135 return $base . '/' . rawurldecode( $rel );
139 * Store a batch of files
141 * @param $triplets Array: (src,zone,dest) triplets as per store()
142 * @param $flags Integer: bitwise combination of the following flags:
143 * self::DELETE_SOURCE Delete the source file after upload
144 * self::OVERWRITE Overwrite an existing destination file instead of failing
145 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
146 * same contents as the source
148 function storeBatch( $triplets, $flags = 0 ) {
149 wfDebug( __METHOD__
. ': Storing ' . count( $triplets ) .
150 " triplets; flags: {$flags}\n" );
152 // Try creating directories
153 if ( !wfMkdirParents( $this->directory
) ) {
154 return $this->newFatal( 'upload_directory_missing', $this->directory
);
156 if ( !is_writable( $this->directory
) ) {
157 return $this->newFatal( 'upload_directory_read_only', $this->directory
);
160 // Validate each triplet
161 $status = $this->newGood();
162 foreach ( $triplets as $i => $triplet ) {
163 list( $srcPath, $dstZone, $dstRel ) = $triplet;
165 // Resolve destination path
166 $root = $this->getZonePath( $dstZone );
168 throw new MWException( "Invalid zone: $dstZone" );
170 if ( !$this->validateFilename( $dstRel ) ) {
171 throw new MWException( 'Validation error in $dstRel' );
173 $dstPath = "$root/$dstRel";
174 $dstDir = dirname( $dstPath );
176 // Create destination directories for this triplet
177 if ( !is_dir( $dstDir ) ) {
178 if ( !wfMkdirParents( $dstDir ) ) {
179 return $this->newFatal( 'directorycreateerror', $dstDir );
181 if ( $dstZone == 'deleted' ) {
182 $this->initDeletedDir( $dstDir );
187 if ( self
::isVirtualUrl( $srcPath ) ) {
188 $srcPath = $triplets[$i][0] = $this->resolveVirtualUrl( $srcPath );
190 if ( !is_file( $srcPath ) ) {
191 // Make a list of files that don't exist for return to the caller
192 $status->fatal( 'filenotfound', $srcPath );
197 if ( !( $flags & self
::OVERWRITE
) && file_exists( $dstPath ) ) {
198 if ( $flags & self
::OVERWRITE_SAME
) {
199 $hashSource = sha1_file( $srcPath );
200 $hashDest = sha1_file( $dstPath );
201 if ( $hashSource != $hashDest ) {
202 $status->fatal( 'fileexistserror', $dstPath );
205 $status->fatal( 'fileexistserror', $dstPath );
210 // Windows does not support moving over existing files, so explicitly delete them
211 $deleteDest = wfIsWindows() && ( $flags & self
::OVERWRITE
);
213 // Abort now on failure
214 if ( !$status->ok
) {
218 // Execute the store operation for each triplet
219 foreach ( $triplets as $i => $triplet ) {
220 list( $srcPath, $dstZone, $dstRel ) = $triplet;
221 $root = $this->getZonePath( $dstZone );
222 $dstPath = "$root/$dstRel";
225 if ( $flags & self
::DELETE_SOURCE
) {
229 if ( !rename( $srcPath, $dstPath ) ) {
230 $status->error( 'filerenameerror', $srcPath, $dstPath );
234 if ( !copy( $srcPath, $dstPath ) ) {
235 $status->error( 'filecopyerror', $srcPath, $dstPath );
238 if ( !( $flags & self
::SKIP_VALIDATION
) ) {
239 wfSuppressWarnings();
240 $hashSource = sha1_file( $srcPath );
241 $hashDest = sha1_file( $dstPath );
244 if ( $hashDest === false ||
$hashSource !== $hashDest ) {
245 wfDebug( __METHOD__
. ': File copy validation failed: ' .
246 "$srcPath ($hashSource) to $dstPath ($hashDest)\n" );
248 $status->error( 'filecopyerror', $srcPath, $dstPath );
254 $this->chmod( $dstPath );
255 $status->successCount++
;
257 $status->failCount++
;
259 $status->success
[$i] = $good;
265 * Deletes a batch of files. Each file can be a (zone, rel) pairs, a
266 * virtual url or a real path. It will try to delete each file, but
267 * ignores any errors that may occur
269 * @param $pairs array List of files to delete
271 function cleanupBatch( $files ) {
272 foreach ( $files as $file ) {
273 if ( is_array( $file ) ) {
274 // This is a pair, extract it
275 list( $zone, $rel ) = $file;
276 $root = $this->getZonePath( $zone );
277 $path = "$root/$rel";
279 if ( self
::isVirtualUrl( $file ) ) {
280 // This is a virtual url, resolve it
281 $path = $this->resolveVirtualUrl( $file );
283 // This is a full file name
288 wfSuppressWarnings();
294 function append( $srcPath, $toAppendPath, $flags = 0 ) {
295 $status = $this->newGood();
297 // Resolve the virtual URL
298 if ( self
::isVirtualUrl( $srcPath ) ) {
299 $srcPath = $this->resolveVirtualUrl( $srcPath );
301 // Make sure the files are there
302 if ( !is_file( $srcPath ) )
303 $status->fatal( 'filenotfound', $srcPath );
305 if ( !is_file( $toAppendPath ) )
306 $status->fatal( 'filenotfound', $toAppendPath );
308 if ( !$status->isOk() ) return $status;
311 $chunk = file_get_contents( $toAppendPath );
312 if( $chunk === false ) {
313 $status->fatal( 'fileappenderrorread', $toAppendPath );
316 if( $status->isOk() ) {
317 if ( file_put_contents( $srcPath, $chunk, FILE_APPEND
) ) {
318 $status->value
= $srcPath;
320 $status->fatal( 'fileappenderror', $toAppendPath, $srcPath);
324 if ( $flags & self
::DELETE_SOURCE
) {
325 unlink( $toAppendPath );
332 * Checks existence of specified array of files.
334 * @param $files Array: URLs of files to check
335 * @param $flags Integer: bitwise combination of the following flags:
336 * self::FILES_ONLY Mark file as existing only if it is a file (not directory)
337 * @return Either array of files and existence flags, or false
339 function fileExistsBatch( $files, $flags = 0 ) {
340 if ( !file_exists( $this->directory
) ||
!is_readable( $this->directory
) ) {
344 foreach ( $files as $key => $file ) {
345 if ( self
::isVirtualUrl( $file ) ) {
346 $file = $this->resolveVirtualUrl( $file );
348 if( $flags & self
::FILES_ONLY
) {
349 $result[$key] = is_file( $file );
351 $result[$key] = file_exists( $file );
359 * Take all available measures to prevent web accessibility of new deleted
360 * directories, in case the user has not configured offline storage
362 protected function initDeletedDir( $dir ) {
363 // Add a .htaccess file to the root of the deleted zone
364 $root = $this->getZonePath( 'deleted' );
365 if ( !file_exists( "$root/.htaccess" ) ) {
366 file_put_contents( "$root/.htaccess", "Deny from all\n" );
368 // Seed new directories with a blank index.html, to prevent crawling
369 file_put_contents( "$dir/index.html", '' );
373 * Pick a random name in the temp zone and store a file to it.
374 * @param $originalName String: the base name of the file as specified
375 * by the user. The file extension will be maintained.
376 * @param $srcPath String: the current location of the file.
377 * @return FileRepoStatus object with the URL in the value.
379 function storeTemp( $originalName, $srcPath ) {
380 $date = gmdate( "YmdHis" );
381 $hashPath = $this->getHashPath( $originalName );
382 $dstRel = "$hashPath$date!$originalName";
383 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
385 $result = $this->store( $srcPath, 'temp', $dstRel );
386 $result->value
= $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
391 * Remove a temporary file or mark it for garbage collection
392 * @param $virtualUrl String: the virtual URL returned by storeTemp
393 * @return Boolean: true on success, false on failure
395 function freeTemp( $virtualUrl ) {
396 $temp = "mwrepo://{$this->name}/temp";
397 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
398 wfDebug( __METHOD__
.": Invalid virtual URL\n" );
401 $path = $this->resolveVirtualUrl( $virtualUrl );
402 wfSuppressWarnings();
403 $success = unlink( $path );
409 * Publish a batch of files
410 * @param $triplets Array: (source,dest,archive) triplets as per publish()
411 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
412 * that the source files should be deleted if possible
414 function publishBatch( $triplets, $flags = 0 ) {
415 // Perform initial checks
416 if ( !wfMkdirParents( $this->directory
) ) {
417 return $this->newFatal( 'upload_directory_missing', $this->directory
);
419 if ( !is_writable( $this->directory
) ) {
420 return $this->newFatal( 'upload_directory_read_only', $this->directory
);
422 $status = $this->newGood( array() );
423 foreach ( $triplets as $i => $triplet ) {
424 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
426 if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) {
427 $triplets[$i][0] = $srcPath = $this->resolveVirtualUrl( $srcPath );
429 if ( !$this->validateFilename( $dstRel ) ) {
430 throw new MWException( 'Validation error in $dstRel' );
432 if ( !$this->validateFilename( $archiveRel ) ) {
433 throw new MWException( 'Validation error in $archiveRel' );
435 $dstPath = "{$this->directory}/$dstRel";
436 $archivePath = "{$this->directory}/$archiveRel";
438 $dstDir = dirname( $dstPath );
439 $archiveDir = dirname( $archivePath );
440 // Abort immediately on directory creation errors since they're likely to be repetitive
441 if ( !is_dir( $dstDir ) && !wfMkdirParents( $dstDir ) ) {
442 return $this->newFatal( 'directorycreateerror', $dstDir );
444 if ( !is_dir( $archiveDir ) && !wfMkdirParents( $archiveDir ) ) {
445 return $this->newFatal( 'directorycreateerror', $archiveDir );
447 if ( !is_file( $srcPath ) ) {
448 // Make a list of files that don't exist for return to the caller
449 $status->fatal( 'filenotfound', $srcPath );
453 if ( !$status->ok
) {
457 foreach ( $triplets as $i => $triplet ) {
458 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
459 $dstPath = "{$this->directory}/$dstRel";
460 $archivePath = "{$this->directory}/$archiveRel";
462 // Archive destination file if it exists
463 if( is_file( $dstPath ) ) {
464 // Check if the archive file exists
465 // This is a sanity check to avoid data loss. In UNIX, the rename primitive
466 // unlinks the destination file if it exists. DB-based synchronisation in
467 // publishBatch's caller should prevent races. In Windows there's no
468 // problem because the rename primitive fails if the destination exists.
469 if ( is_file( $archivePath ) ) {
472 wfSuppressWarnings();
473 $success = rename( $dstPath, $archivePath );
478 $status->error( 'filerenameerror',$dstPath, $archivePath );
479 $status->failCount++
;
482 wfDebug(__METHOD__
.": moved file $dstPath to $archivePath\n");
484 $status->value
[$i] = 'archived';
486 $status->value
[$i] = 'new';
490 wfSuppressWarnings();
491 if ( $flags & self
::DELETE_SOURCE
) {
492 if ( !rename( $srcPath, $dstPath ) ) {
493 $status->error( 'filerenameerror', $srcPath, $dstPath );
497 if ( !copy( $srcPath, $dstPath ) ) {
498 $status->error( 'filecopyerror', $srcPath, $dstPath );
505 $status->successCount++
;
506 wfDebug(__METHOD__
.": wrote tempfile $srcPath to $dstPath\n");
507 // Thread-safe override for umask
508 $this->chmod( $dstPath );
510 $status->failCount++
;
517 * Move a group of files to the deletion archive.
518 * If no valid deletion archive is configured, this may either delete the
519 * file or throw an exception, depending on the preference of the repository.
521 * @param $sourceDestPairs Array of source/destination pairs. Each element
522 * is a two-element array containing the source file path relative to the
523 * public root in the first element, and the archive file path relative
524 * to the deleted zone root in the second element.
525 * @return FileRepoStatus
527 function deleteBatch( $sourceDestPairs ) {
528 $status = $this->newGood();
529 if ( !$this->deletedDir
) {
530 throw new MWException( __METHOD__
.': no valid deletion archive directory' );
534 * Validate filenames and create archive directories
536 foreach ( $sourceDestPairs as $pair ) {
537 list( $srcRel, $archiveRel ) = $pair;
538 if ( !$this->validateFilename( $srcRel ) ) {
539 throw new MWException( __METHOD__
.':Validation error in $srcRel' );
541 if ( !$this->validateFilename( $archiveRel ) ) {
542 throw new MWException( __METHOD__
.':Validation error in $archiveRel' );
544 $archivePath = "{$this->deletedDir}/$archiveRel";
545 $archiveDir = dirname( $archivePath );
546 if ( !is_dir( $archiveDir ) ) {
547 if ( !wfMkdirParents( $archiveDir ) ) {
548 $status->fatal( 'directorycreateerror', $archiveDir );
551 $this->initDeletedDir( $archiveDir );
553 // Check if the archive directory is writable
554 // This doesn't appear to work on NTFS
555 if ( !is_writable( $archiveDir ) ) {
556 $status->fatal( 'filedelete-archive-read-only', $archiveDir );
559 if ( !$status->ok
) {
566 * We're now committed to returning an OK result, which will lead to
567 * the files being moved in the DB also.
569 foreach ( $sourceDestPairs as $pair ) {
570 list( $srcRel, $archiveRel ) = $pair;
571 $srcPath = "{$this->directory}/$srcRel";
572 $archivePath = "{$this->deletedDir}/$archiveRel";
574 if ( file_exists( $archivePath ) ) {
575 # A file with this content hash is already archived
576 if ( !@unlink
( $srcPath ) ) {
577 $status->error( 'filedeleteerror', $srcPath );
581 if ( !@rename
( $srcPath, $archivePath ) ) {
582 $status->error( 'filerenameerror', $srcPath, $archivePath );
585 $this->chmod( $archivePath );
589 $status->successCount++
;
591 $status->failCount++
;
598 * Get a relative path for a deletion archive key,
599 * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
601 function getDeletedHashPath( $key ) {
603 for ( $i = 0; $i < $this->deletedHashLevels
; $i++
) {
604 $path .= $key[$i] . '/';
610 * Call a callback function for every file in the repository.
611 * Uses the filesystem even in child classes.
613 function enumFilesInFS( $callback ) {
614 $numDirs = 1 << ( $this->hashLevels
* 4 );
615 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++
) {
616 $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
617 $path = $this->directory
;
618 for ( $hexPos = 0; $hexPos < $this->hashLevels
; $hexPos++
) {
619 $path .= '/' . substr( $hexString, 0, $hexPos +
1 );
621 if ( !file_exists( $path ) ||
!is_dir( $path ) ) {
624 $dir = opendir( $path );
625 while ( false !== ( $name = readdir( $dir ) ) ) {
626 call_user_func( $callback, $path . '/' . $name );
632 * Call a callback function for every file in the repository
633 * May use either the database or the filesystem
635 function enumFiles( $callback ) {
636 $this->enumFilesInFS( $callback );
640 * Get properties of a file with a given virtual URL
641 * The virtual URL must refer to this repo
643 function getFileProps( $virtualUrl ) {
644 $path = $this->resolveVirtualUrl( $virtualUrl );
645 return File
::getPropsFromPath( $path );
649 * Path disclosure protection functions
651 * Get a callback function to use for cleaning error message parameters
653 function getErrorCleanupFunction() {
654 switch ( $this->pathDisclosureProtection
) {
656 $callback = array( $this, 'simpleClean' );
659 $callback = parent
::getErrorCleanupFunction();
664 function simpleClean( $param ) {
665 if ( !isset( $this->simpleCleanPairs
) ) {
667 $this->simpleCleanPairs
= array(
668 $this->directory
=> 'public',
669 "{$this->directory}/temp" => 'temp',
671 dirname( __FILE__
) => '$IP/extensions/WebStore',
673 if ( $this->deletedDir
) {
674 $this->simpleCleanPairs
[$this->deletedDir
] = 'deleted';
677 return strtr( $param, $this->simpleCleanPairs
);
681 * Chmod a file, supressing the warnings.
682 * @param $path String: the path to change
684 protected function chmod( $path ) {
685 wfSuppressWarnings();
686 chmod( $path, $this->fileMode
);