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 if ( !wfMkdirParents( $this->directory
) ) {
150 return $this->newFatal( 'upload_directory_missing', $this->directory
);
152 if ( !is_writable( $this->directory
) ) {
153 return $this->newFatal( 'upload_directory_read_only', $this->directory
);
155 $status = $this->newGood();
156 foreach ( $triplets as $i => $triplet ) {
157 list( $srcPath, $dstZone, $dstRel ) = $triplet;
159 $root = $this->getZonePath( $dstZone );
161 throw new MWException( "Invalid zone: $dstZone" );
163 if ( !$this->validateFilename( $dstRel ) ) {
164 throw new MWException( 'Validation error in $dstRel' );
166 $dstPath = "$root/$dstRel";
167 $dstDir = dirname( $dstPath );
169 if ( !is_dir( $dstDir ) ) {
170 if ( !wfMkdirParents( $dstDir ) ) {
171 return $this->newFatal( 'directorycreateerror', $dstDir );
173 if ( $dstZone == 'deleted' ) {
174 $this->initDeletedDir( $dstDir );
178 if ( self
::isVirtualUrl( $srcPath ) ) {
179 $srcPath = $triplets[$i][0] = $this->resolveVirtualUrl( $srcPath );
181 if ( !is_file( $srcPath ) ) {
182 // Make a list of files that don't exist for return to the caller
183 $status->fatal( 'filenotfound', $srcPath );
186 if ( !( $flags & self
::OVERWRITE
) && file_exists( $dstPath ) ) {
187 if ( $flags & self
::OVERWRITE_SAME
) {
188 $hashSource = sha1_file( $srcPath );
189 $hashDest = sha1_file( $dstPath );
190 if ( $hashSource != $hashDest ) {
191 $status->fatal( 'fileexistserror', $dstPath );
194 $status->fatal( 'fileexistserror', $dstPath );
199 $deleteDest = wfIsWindows() && ( $flags & self
::OVERWRITE
);
201 // Abort now on failure
202 if ( !$status->ok
) {
206 foreach ( $triplets as $triplet ) {
207 list( $srcPath, $dstZone, $dstRel ) = $triplet;
208 $root = $this->getZonePath( $dstZone );
209 $dstPath = "$root/$dstRel";
212 if ( $flags & self
::DELETE_SOURCE
) {
216 if ( !rename( $srcPath, $dstPath ) ) {
217 $status->error( 'filerenameerror', $srcPath, $dstPath );
221 if ( !copy( $srcPath, $dstPath ) ) {
222 $status->error( 'filecopyerror', $srcPath, $dstPath );
227 $this->chmod( $dstPath );
228 $status->successCount++
;
230 $status->failCount++
;
236 function append( $srcPath, $toAppendPath, $flags = 0 ) {
237 $status = $this->newGood();
239 // Resolve the virtual URL
240 if ( self
::isVirtualUrl( $srcPath ) ) {
241 $srcPath = $this->resolveVirtualUrl( $srcPath );
243 // Make sure the files are there
244 if ( !is_file( $srcPath ) )
245 $status->fatal( 'filenotfound', $srcPath );
247 if ( !is_file( $toAppendPath ) )
248 $status->fatal( 'filenotfound', $toAppendPath );
250 if ( !$status->isOk() ) return $status;
253 $chunk = file_get_contents( $toAppendPath );
254 if( $chunk === false ) {
255 $status->fatal( 'fileappenderrorread', $toAppendPath );
258 if( $status->isOk() ) {
259 if ( file_put_contents( $srcPath, $chunk, FILE_APPEND
) ) {
260 $status->value
= $srcPath;
262 $status->fatal( 'fileappenderror', $toAppendPath, $srcPath);
266 if ( $flags & self
::DELETE_SOURCE
) {
267 unlink( $toAppendPath );
274 * Checks existence of specified array of files.
276 * @param $files Array: URLs of files to check
277 * @param $flags Integer: bitwise combination of the following flags:
278 * self::FILES_ONLY Mark file as existing only if it is a file (not directory)
279 * @return Either array of files and existence flags, or false
281 function fileExistsBatch( $files, $flags = 0 ) {
282 if ( !file_exists( $this->directory
) ||
!is_readable( $this->directory
) ) {
286 foreach ( $files as $key => $file ) {
287 if ( self
::isVirtualUrl( $file ) ) {
288 $file = $this->resolveVirtualUrl( $file );
290 if( $flags & self
::FILES_ONLY
) {
291 $result[$key] = is_file( $file );
293 $result[$key] = file_exists( $file );
301 * Take all available measures to prevent web accessibility of new deleted
302 * directories, in case the user has not configured offline storage
304 protected function initDeletedDir( $dir ) {
305 // Add a .htaccess file to the root of the deleted zone
306 $root = $this->getZonePath( 'deleted' );
307 if ( !file_exists( "$root/.htaccess" ) ) {
308 file_put_contents( "$root/.htaccess", "Deny from all\n" );
310 // Seed new directories with a blank index.html, to prevent crawling
311 file_put_contents( "$dir/index.html", '' );
315 * Pick a random name in the temp zone and store a file to it.
316 * @param $originalName String: the base name of the file as specified
317 * by the user. The file extension will be maintained.
318 * @param $srcPath String: the current location of the file.
319 * @return FileRepoStatus object with the URL in the value.
321 function storeTemp( $originalName, $srcPath ) {
322 $date = gmdate( "YmdHis" );
323 $hashPath = $this->getHashPath( $originalName );
324 $dstRel = "$hashPath$date!$originalName";
325 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
327 $result = $this->store( $srcPath, 'temp', $dstRel );
328 $result->value
= $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
333 * Remove a temporary file or mark it for garbage collection
334 * @param $virtualUrl String: the virtual URL returned by storeTemp
335 * @return Boolean: true on success, false on failure
337 function freeTemp( $virtualUrl ) {
338 $temp = "mwrepo://{$this->name}/temp";
339 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
340 wfDebug( __METHOD__
.": Invalid virtual URL\n" );
343 $path = $this->resolveVirtualUrl( $virtualUrl );
344 wfSuppressWarnings();
345 $success = unlink( $path );
351 * Publish a batch of files
352 * @param $triplets Array: (source,dest,archive) triplets as per publish()
353 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
354 * that the source files should be deleted if possible
356 function publishBatch( $triplets, $flags = 0 ) {
357 // Perform initial checks
358 if ( !wfMkdirParents( $this->directory
) ) {
359 return $this->newFatal( 'upload_directory_missing', $this->directory
);
361 if ( !is_writable( $this->directory
) ) {
362 return $this->newFatal( 'upload_directory_read_only', $this->directory
);
364 $status = $this->newGood( array() );
365 foreach ( $triplets as $i => $triplet ) {
366 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
368 if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) {
369 $triplets[$i][0] = $srcPath = $this->resolveVirtualUrl( $srcPath );
371 if ( !$this->validateFilename( $dstRel ) ) {
372 throw new MWException( 'Validation error in $dstRel' );
374 if ( !$this->validateFilename( $archiveRel ) ) {
375 throw new MWException( 'Validation error in $archiveRel' );
377 $dstPath = "{$this->directory}/$dstRel";
378 $archivePath = "{$this->directory}/$archiveRel";
380 $dstDir = dirname( $dstPath );
381 $archiveDir = dirname( $archivePath );
382 // Abort immediately on directory creation errors since they're likely to be repetitive
383 if ( !is_dir( $dstDir ) && !wfMkdirParents( $dstDir ) ) {
384 return $this->newFatal( 'directorycreateerror', $dstDir );
386 if ( !is_dir( $archiveDir ) && !wfMkdirParents( $archiveDir ) ) {
387 return $this->newFatal( 'directorycreateerror', $archiveDir );
389 if ( !is_file( $srcPath ) ) {
390 // Make a list of files that don't exist for return to the caller
391 $status->fatal( 'filenotfound', $srcPath );
395 if ( !$status->ok
) {
399 foreach ( $triplets as $i => $triplet ) {
400 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
401 $dstPath = "{$this->directory}/$dstRel";
402 $archivePath = "{$this->directory}/$archiveRel";
404 // Archive destination file if it exists
405 if( is_file( $dstPath ) ) {
406 // Check if the archive file exists
407 // This is a sanity check to avoid data loss. In UNIX, the rename primitive
408 // unlinks the destination file if it exists. DB-based synchronisation in
409 // publishBatch's caller should prevent races. In Windows there's no
410 // problem because the rename primitive fails if the destination exists.
411 if ( is_file( $archivePath ) ) {
414 wfSuppressWarnings();
415 $success = rename( $dstPath, $archivePath );
420 $status->error( 'filerenameerror',$dstPath, $archivePath );
421 $status->failCount++
;
424 wfDebug(__METHOD__
.": moved file $dstPath to $archivePath\n");
426 $status->value
[$i] = 'archived';
428 $status->value
[$i] = 'new';
432 wfSuppressWarnings();
433 if ( $flags & self
::DELETE_SOURCE
) {
434 if ( !rename( $srcPath, $dstPath ) ) {
435 $status->error( 'filerenameerror', $srcPath, $dstPath );
439 if ( !copy( $srcPath, $dstPath ) ) {
440 $status->error( 'filecopyerror', $srcPath, $dstPath );
447 $status->successCount++
;
448 wfDebug(__METHOD__
.": wrote tempfile $srcPath to $dstPath\n");
449 // Thread-safe override for umask
450 $this->chmod( $dstPath );
452 $status->failCount++
;
459 * Move a group of files to the deletion archive.
460 * If no valid deletion archive is configured, this may either delete the
461 * file or throw an exception, depending on the preference of the repository.
463 * @param $sourceDestPairs Array of source/destination pairs. Each element
464 * is a two-element array containing the source file path relative to the
465 * public root in the first element, and the archive file path relative
466 * to the deleted zone root in the second element.
467 * @return FileRepoStatus
469 function deleteBatch( $sourceDestPairs ) {
470 $status = $this->newGood();
471 if ( !$this->deletedDir
) {
472 throw new MWException( __METHOD__
.': no valid deletion archive directory' );
476 * Validate filenames and create archive directories
478 foreach ( $sourceDestPairs as $pair ) {
479 list( $srcRel, $archiveRel ) = $pair;
480 if ( !$this->validateFilename( $srcRel ) ) {
481 throw new MWException( __METHOD__
.':Validation error in $srcRel' );
483 if ( !$this->validateFilename( $archiveRel ) ) {
484 throw new MWException( __METHOD__
.':Validation error in $archiveRel' );
486 $archivePath = "{$this->deletedDir}/$archiveRel";
487 $archiveDir = dirname( $archivePath );
488 if ( !is_dir( $archiveDir ) ) {
489 if ( !wfMkdirParents( $archiveDir ) ) {
490 $status->fatal( 'directorycreateerror', $archiveDir );
493 $this->initDeletedDir( $archiveDir );
495 // Check if the archive directory is writable
496 // This doesn't appear to work on NTFS
497 if ( !is_writable( $archiveDir ) ) {
498 $status->fatal( 'filedelete-archive-read-only', $archiveDir );
501 if ( !$status->ok
) {
508 * We're now committed to returning an OK result, which will lead to
509 * the files being moved in the DB also.
511 foreach ( $sourceDestPairs as $pair ) {
512 list( $srcRel, $archiveRel ) = $pair;
513 $srcPath = "{$this->directory}/$srcRel";
514 $archivePath = "{$this->deletedDir}/$archiveRel";
516 if ( file_exists( $archivePath ) ) {
517 # A file with this content hash is already archived
518 if ( !@unlink
( $srcPath ) ) {
519 $status->error( 'filedeleteerror', $srcPath );
523 if ( !@rename
( $srcPath, $archivePath ) ) {
524 $status->error( 'filerenameerror', $srcPath, $archivePath );
527 $this->chmod( $archivePath );
531 $status->successCount++
;
533 $status->failCount++
;
540 * Get a relative path for a deletion archive key,
541 * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
543 function getDeletedHashPath( $key ) {
545 for ( $i = 0; $i < $this->deletedHashLevels
; $i++
) {
546 $path .= $key[$i] . '/';
552 * Call a callback function for every file in the repository.
553 * Uses the filesystem even in child classes.
555 function enumFilesInFS( $callback ) {
556 $numDirs = 1 << ( $this->hashLevels
* 4 );
557 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++
) {
558 $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
559 $path = $this->directory
;
560 for ( $hexPos = 0; $hexPos < $this->hashLevels
; $hexPos++
) {
561 $path .= '/' . substr( $hexString, 0, $hexPos +
1 );
563 if ( !file_exists( $path ) ||
!is_dir( $path ) ) {
566 $dir = opendir( $path );
567 while ( false !== ( $name = readdir( $dir ) ) ) {
568 call_user_func( $callback, $path . '/' . $name );
574 * Call a callback function for every file in the repository
575 * May use either the database or the filesystem
577 function enumFiles( $callback ) {
578 $this->enumFilesInFS( $callback );
582 * Get properties of a file with a given virtual URL
583 * The virtual URL must refer to this repo
585 function getFileProps( $virtualUrl ) {
586 $path = $this->resolveVirtualUrl( $virtualUrl );
587 return File
::getPropsFromPath( $path );
591 * Path disclosure protection functions
593 * Get a callback function to use for cleaning error message parameters
595 function getErrorCleanupFunction() {
596 switch ( $this->pathDisclosureProtection
) {
598 $callback = array( $this, 'simpleClean' );
601 $callback = parent
::getErrorCleanupFunction();
606 function simpleClean( $param ) {
607 if ( !isset( $this->simpleCleanPairs
) ) {
609 $this->simpleCleanPairs
= array(
610 $this->directory
=> 'public',
611 "{$this->directory}/temp" => 'temp',
613 dirname( __FILE__
) => '$IP/extensions/WebStore',
615 if ( $this->deletedDir
) {
616 $this->simpleCleanPairs
[$this->deletedDir
] = 'deleted';
619 return strtr( $param, $this->simpleCleanPairs
);
623 * Chmod a file, supressing the warnings.
624 * @param $path String: the path to change
626 protected function chmod( $path ) {
627 wfSuppressWarnings();
628 chmod( $path, $this->fileMode
);