4 * A repository for files accessible via the local filesystem. Does not support
5 * database access or registration.
8 class FSRepo
extends FileRepo
{
9 var $directory, $deletedDir, $url, $deletedHashLevels, $fileMode;
10 var $fileFactory = array( 'UnregisteredLocalFile', 'newFromTitle' );
11 var $oldFileFactory = false;
12 var $pathDisclosureProtection = 'simple';
14 function __construct( $info ) {
15 parent
::__construct( $info );
18 $this->directory
= $info['directory'];
19 $this->url
= $info['url'];
22 $this->hashLevels
= isset( $info['hashLevels'] ) ?
$info['hashLevels'] : 2;
23 $this->deletedHashLevels
= isset( $info['deletedHashLevels'] ) ?
24 $info['deletedHashLevels'] : $this->hashLevels
;
25 $this->deletedDir
= isset( $info['deletedDir'] ) ?
$info['deletedDir'] : false;
26 $this->fileMode
= isset( $info['fileMode'] ) ?
$info['fileMode'] : 0644;
30 * Get the public root directory of the repository.
32 function getRootDirectory() {
33 return $this->directory
;
37 * Get the public root URL of the repository
39 function getRootUrl() {
44 * Returns true if the repository uses a multi-level directory structure
47 return (bool)$this->hashLevels
;
51 * Get the local directory corresponding to one of the three basic zones
53 function getZonePath( $zone ) {
56 return $this->directory
;
58 return "{$this->directory}/temp";
60 return $this->deletedDir
;
67 * Get the URL corresponding to one of the three basic zones
69 function getZoneUrl( $zone ) {
74 return "{$this->url}/temp";
76 return false; // no public URL
83 * Get a URL referring to this repository, with the private mwrepo protocol.
84 * The suffix, if supplied, is considered to be unencoded, and will be
85 * URL-encoded before being returned.
87 function getVirtualUrl( $suffix = false ) {
88 $path = 'mwrepo://' . $this->name
;
89 if ( $suffix !== false ) {
90 $path .= '/' . rawurlencode( $suffix );
96 * Get the local path corresponding to a virtual URL
98 function resolveVirtualUrl( $url ) {
99 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
100 throw new MWException( __METHOD__
.': unknown protoocl' );
103 $bits = explode( '/', substr( $url, 9 ), 3 );
104 if ( count( $bits ) != 3 ) {
105 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
107 list( $repo, $zone, $rel ) = $bits;
108 if ( $repo !== $this->name
) {
109 throw new MWException( __METHOD__
.": fetching from a foreign repo is not supported" );
111 $base = $this->getZonePath( $zone );
113 throw new MWException( __METHOD__
.": invalid zone: $zone" );
115 return $base . '/' . rawurldecode( $rel );
119 * Store a batch of files
121 * @param array $triplets (src,zone,dest) triplets as per store()
122 * @param integer $flags Bitwise combination of the following flags:
123 * self::DELETE_SOURCE Delete the source file after upload
124 * self::OVERWRITE Overwrite an existing destination file instead of failing
125 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
126 * same contents as the source
128 function storeBatch( $triplets, $flags = 0 ) {
129 if ( !wfMkdirParents( $this->directory
) ) {
130 return $this->newFatal( 'upload_directory_missing', $this->directory
);
132 if ( !is_writable( $this->directory
) ) {
133 return $this->newFatal( 'upload_directory_read_only', $this->directory
);
135 $status = $this->newGood();
136 foreach ( $triplets as $i => $triplet ) {
137 list( $srcPath, $dstZone, $dstRel ) = $triplet;
139 $root = $this->getZonePath( $dstZone );
141 throw new MWException( "Invalid zone: $dstZone" );
143 if ( !$this->validateFilename( $dstRel ) ) {
144 throw new MWException( 'Validation error in $dstRel' );
146 $dstPath = "$root/$dstRel";
147 $dstDir = dirname( $dstPath );
149 if ( !is_dir( $dstDir ) ) {
150 if ( !wfMkdirParents( $dstDir ) ) {
151 return $this->newFatal( 'directorycreateerror', $dstDir );
153 if ( $dstZone == 'deleted' ) {
154 $this->initDeletedDir( $dstDir );
158 if ( self
::isVirtualUrl( $srcPath ) ) {
159 $srcPath = $triplets[$i][0] = $this->resolveVirtualUrl( $srcPath );
161 if ( !is_file( $srcPath ) ) {
162 // Make a list of files that don't exist for return to the caller
163 $status->fatal( 'filenotfound', $srcPath );
166 if ( !( $flags & self
::OVERWRITE
) && file_exists( $dstPath ) ) {
167 if ( $flags & self
::OVERWRITE_SAME
) {
168 $hashSource = sha1_file( $srcPath );
169 $hashDest = sha1_file( $dstPath );
170 if ( $hashSource != $hashDest ) {
171 $status->fatal( 'fileexistserror', $dstPath );
174 $status->fatal( 'fileexistserror', $dstPath );
179 $deleteDest = wfIsWindows() && ( $flags & self
::OVERWRITE
);
181 // Abort now on failure
182 if ( !$status->ok
) {
186 foreach ( $triplets as $triplet ) {
187 list( $srcPath, $dstZone, $dstRel ) = $triplet;
188 $root = $this->getZonePath( $dstZone );
189 $dstPath = "$root/$dstRel";
192 if ( $flags & self
::DELETE_SOURCE
) {
196 if ( !rename( $srcPath, $dstPath ) ) {
197 $status->error( 'filerenameerror', $srcPath, $dstPath );
201 if ( !copy( $srcPath, $dstPath ) ) {
202 $status->error( 'filecopyerror', $srcPath, $dstPath );
207 $this->chmod( $dstPath );
208 $status->successCount++
;
210 $status->failCount++
;
217 * Checks existence of specified array of files.
219 * @param array $files URLs of files to check
220 * @param integer $flags Bitwise combination of the following flags:
221 * self::FILES_ONLY Mark file as existing only if it is a file (not directory)
222 * @return Either array of files and existence flags, or false
224 function fileExistsBatch( $files, $flags = 0 ) {
225 if ( !file_exists( $this->directory
) ||
!is_readable( $this->directory
) ) {
229 foreach ( $files as $key => $file ) {
230 if ( self
::isVirtualUrl( $file ) ) {
231 $file = $this->resolveVirtualUrl( $file );
233 if( $flags & self
::FILES_ONLY
) {
234 $result[$key] = is_file( $file );
236 $result[$key] = file_exists( $file );
244 * Take all available measures to prevent web accessibility of new deleted
245 * directories, in case the user has not configured offline storage
247 protected function initDeletedDir( $dir ) {
248 // Add a .htaccess file to the root of the deleted zone
249 $root = $this->getZonePath( 'deleted' );
250 if ( !file_exists( "$root/.htaccess" ) ) {
251 file_put_contents( "$root/.htaccess", "Deny from all\n" );
253 // Seed new directories with a blank index.html, to prevent crawling
254 file_put_contents( "$dir/index.html", '' );
258 * Pick a random name in the temp zone and store a file to it.
259 * @param string $originalName The base name of the file as specified
260 * by the user. The file extension will be maintained.
261 * @param string $srcPath The current location of the file.
262 * @return FileRepoStatus object with the URL in the value.
264 function storeTemp( $originalName, $srcPath ) {
265 $date = gmdate( "YmdHis" );
266 $hashPath = $this->getHashPath( $originalName );
267 $dstRel = "$hashPath$date!$originalName";
268 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
270 $result = $this->store( $srcPath, 'temp', $dstRel );
271 $result->value
= $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
276 * Remove a temporary file or mark it for garbage collection
277 * @param string $virtualUrl The virtual URL returned by storeTemp
278 * @return boolean True on success, false on failure
280 function freeTemp( $virtualUrl ) {
281 $temp = "mwrepo://{$this->name}/temp";
282 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
283 wfDebug( __METHOD__
.": Invalid virtual URL\n" );
286 $path = $this->resolveVirtualUrl( $virtualUrl );
287 wfSuppressWarnings();
288 $success = unlink( $path );
294 * Publish a batch of files
295 * @param array $triplets (source,dest,archive) triplets as per publish()
296 * @param integer $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
297 * that the source files should be deleted if possible
299 function publishBatch( $triplets, $flags = 0 ) {
300 // Perform initial checks
301 if ( !wfMkdirParents( $this->directory
) ) {
302 return $this->newFatal( 'upload_directory_missing', $this->directory
);
304 if ( !is_writable( $this->directory
) ) {
305 return $this->newFatal( 'upload_directory_read_only', $this->directory
);
307 $status = $this->newGood( array() );
308 foreach ( $triplets as $i => $triplet ) {
309 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
311 if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) {
312 $triplets[$i][0] = $srcPath = $this->resolveVirtualUrl( $srcPath );
314 if ( !$this->validateFilename( $dstRel ) ) {
315 throw new MWException( 'Validation error in $dstRel' );
317 if ( !$this->validateFilename( $archiveRel ) ) {
318 throw new MWException( 'Validation error in $archiveRel' );
320 $dstPath = "{$this->directory}/$dstRel";
321 $archivePath = "{$this->directory}/$archiveRel";
323 $dstDir = dirname( $dstPath );
324 $archiveDir = dirname( $archivePath );
325 // Abort immediately on directory creation errors since they're likely to be repetitive
326 if ( !is_dir( $dstDir ) && !wfMkdirParents( $dstDir ) ) {
327 return $this->newFatal( 'directorycreateerror', $dstDir );
329 if ( !is_dir( $archiveDir ) && !wfMkdirParents( $archiveDir ) ) {
330 return $this->newFatal( 'directorycreateerror', $archiveDir );
332 if ( !is_file( $srcPath ) ) {
333 // Make a list of files that don't exist for return to the caller
334 $status->fatal( 'filenotfound', $srcPath );
338 if ( !$status->ok
) {
342 foreach ( $triplets as $i => $triplet ) {
343 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
344 $dstPath = "{$this->directory}/$dstRel";
345 $archivePath = "{$this->directory}/$archiveRel";
347 // Archive destination file if it exists
348 if( is_file( $dstPath ) ) {
349 // Check if the archive file exists
350 // This is a sanity check to avoid data loss. In UNIX, the rename primitive
351 // unlinks the destination file if it exists. DB-based synchronisation in
352 // publishBatch's caller should prevent races. In Windows there's no
353 // problem because the rename primitive fails if the destination exists.
354 if ( is_file( $archivePath ) ) {
357 wfSuppressWarnings();
358 $success = rename( $dstPath, $archivePath );
363 $status->error( 'filerenameerror',$dstPath, $archivePath );
364 $status->failCount++
;
367 wfDebug(__METHOD__
.": moved file $dstPath to $archivePath\n");
369 $status->value
[$i] = 'archived';
371 $status->value
[$i] = 'new';
375 wfSuppressWarnings();
376 if ( $flags & self
::DELETE_SOURCE
) {
377 if ( !rename( $srcPath, $dstPath ) ) {
378 $status->error( 'filerenameerror', $srcPath, $dstPath );
382 if ( !copy( $srcPath, $dstPath ) ) {
383 $status->error( 'filecopyerror', $srcPath, $dstPath );
390 $status->successCount++
;
391 wfDebug(__METHOD__
.": wrote tempfile $srcPath to $dstPath\n");
392 // Thread-safe override for umask
393 $this->chmod( $dstPath );
395 $status->failCount++
;
402 * Move a group of files to the deletion archive.
403 * If no valid deletion archive is configured, this may either delete the
404 * file or throw an exception, depending on the preference of the repository.
406 * @param array $sourceDestPairs Array of source/destination pairs. Each element
407 * is a two-element array containing the source file path relative to the
408 * public root in the first element, and the archive file path relative
409 * to the deleted zone root in the second element.
410 * @return FileRepoStatus
412 function deleteBatch( $sourceDestPairs ) {
413 $status = $this->newGood();
414 if ( !$this->deletedDir
) {
415 throw new MWException( __METHOD__
.': no valid deletion archive directory' );
419 * Validate filenames and create archive directories
421 foreach ( $sourceDestPairs as $pair ) {
422 list( $srcRel, $archiveRel ) = $pair;
423 if ( !$this->validateFilename( $srcRel ) ) {
424 throw new MWException( __METHOD__
.':Validation error in $srcRel' );
426 if ( !$this->validateFilename( $archiveRel ) ) {
427 throw new MWException( __METHOD__
.':Validation error in $archiveRel' );
429 $archivePath = "{$this->deletedDir}/$archiveRel";
430 $archiveDir = dirname( $archivePath );
431 if ( !is_dir( $archiveDir ) ) {
432 if ( !wfMkdirParents( $archiveDir ) ) {
433 $status->fatal( 'directorycreateerror', $archiveDir );
436 $this->initDeletedDir( $archiveDir );
438 // Check if the archive directory is writable
439 // This doesn't appear to work on NTFS
440 if ( !is_writable( $archiveDir ) ) {
441 $status->fatal( 'filedelete-archive-read-only', $archiveDir );
444 if ( !$status->ok
) {
451 * We're now committed to returning an OK result, which will lead to
452 * the files being moved in the DB also.
454 foreach ( $sourceDestPairs as $pair ) {
455 list( $srcRel, $archiveRel ) = $pair;
456 $srcPath = "{$this->directory}/$srcRel";
457 $archivePath = "{$this->deletedDir}/$archiveRel";
459 if ( file_exists( $archivePath ) ) {
460 # A file with this content hash is already archived
461 if ( !@unlink
( $srcPath ) ) {
462 $status->error( 'filedeleteerror', $srcPath );
466 if ( !@rename
( $srcPath, $archivePath ) ) {
467 $status->error( 'filerenameerror', $srcPath, $archivePath );
470 $this->chmod( $archivePath );
474 $status->successCount++
;
476 $status->failCount++
;
483 * Get a relative path for a deletion archive key,
484 * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
486 function getDeletedHashPath( $key ) {
488 for ( $i = 0; $i < $this->deletedHashLevels
; $i++
) {
489 $path .= $key[$i] . '/';
495 * Call a callback function for every file in the repository.
496 * Uses the filesystem even in child classes.
498 function enumFilesInFS( $callback ) {
499 $numDirs = 1 << ( $this->hashLevels
* 4 );
500 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++
) {
501 $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
502 $path = $this->directory
;
503 for ( $hexPos = 0; $hexPos < $this->hashLevels
; $hexPos++
) {
504 $path .= '/' . substr( $hexString, 0, $hexPos +
1 );
506 if ( !file_exists( $path ) ||
!is_dir( $path ) ) {
509 $dir = opendir( $path );
510 while ( false !== ( $name = readdir( $dir ) ) ) {
511 call_user_func( $callback, $path . '/' . $name );
517 * Call a callback function for every file in the repository
518 * May use either the database or the filesystem
520 function enumFiles( $callback ) {
521 $this->enumFilesInFS( $callback );
525 * Get properties of a file with a given virtual URL
526 * The virtual URL must refer to this repo
528 function getFileProps( $virtualUrl ) {
529 $path = $this->resolveVirtualUrl( $virtualUrl );
530 return File
::getPropsFromPath( $path );
534 * Path disclosure protection functions
536 * Get a callback function to use for cleaning error message parameters
538 function getErrorCleanupFunction() {
539 switch ( $this->pathDisclosureProtection
) {
541 $callback = array( $this, 'simpleClean' );
544 $callback = parent
::getErrorCleanupFunction();
549 function simpleClean( $param ) {
550 if ( !isset( $this->simpleCleanPairs
) ) {
552 $this->simpleCleanPairs
= array(
553 $this->directory
=> 'public',
554 "{$this->directory}/temp" => 'temp',
556 dirname( __FILE__
) => '$IP/extensions/WebStore',
558 if ( $this->deletedDir
) {
559 $this->simpleCleanPairs
[$this->deletedDir
] = 'deleted';
562 return strtr( $param, $this->simpleCleanPairs
);
566 * Chmod a file, supressing the warnings.
567 * @param String $path The path to change
569 protected function chmod( $path ) {
570 wfSuppressWarnings();
571 chmod( $path, $this->fileMode
);