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;
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;
29 * Get the public root directory of the repository.
31 function getRootDirectory() {
32 return $this->directory
;
36 * Get the public root URL of the repository
38 function getRootUrl() {
43 * Returns true if the repository uses a multi-level directory structure
46 return (bool)$this->hashLevels
;
50 * Get the local directory corresponding to one of the three basic zones
52 function getZonePath( $zone ) {
55 return $this->directory
;
57 return "{$this->directory}/temp";
59 return $this->deletedDir
;
66 * Get the URL corresponding to one of the three basic zones
68 function getZoneUrl( $zone ) {
73 return "{$this->url}/temp";
75 return false; // no public URL
82 * Get a URL referring to this repository, with the private mwrepo protocol.
83 * The suffix, if supplied, is considered to be unencoded, and will be
84 * URL-encoded before being returned.
86 function getVirtualUrl( $suffix = false ) {
87 $path = 'mwrepo://' . $this->name
;
88 if ( $suffix !== false ) {
89 $path .= '/' . rawurlencode( $suffix );
95 * Get the local path corresponding to a virtual URL
97 function resolveVirtualUrl( $url ) {
98 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
99 throw new MWException( __METHOD__
.': unknown protoocl' );
102 $bits = explode( '/', substr( $url, 9 ), 3 );
103 if ( count( $bits ) != 3 ) {
104 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
106 list( $repo, $zone, $rel ) = $bits;
107 if ( $repo !== $this->name
) {
108 throw new MWException( __METHOD__
.": fetching from a foreign repo is not supported" );
110 $base = $this->getZonePath( $zone );
112 throw new MWException( __METHOD__
.": invalid zone: $zone" );
114 return $base . '/' . rawurldecode( $rel );
118 * Store a batch of files
120 * @param array $triplets (src,zone,dest) triplets as per store()
121 * @param integer $flags Bitwise combination of the following flags:
122 * self::DELETE_SOURCE Delete the source file after upload
123 * self::OVERWRITE Overwrite an existing destination file instead of failing
124 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
125 * same contents as the source
127 function storeBatch( $triplets, $flags = 0 ) {
128 if ( !wfMkdirParents( $this->directory
) ) {
129 return $this->newFatal( 'upload_directory_missing', $this->directory
);
131 if ( !is_writable( $this->directory
) ) {
132 return $this->newFatal( 'upload_directory_read_only', $this->directory
);
134 $status = $this->newGood();
135 foreach ( $triplets as $i => $triplet ) {
136 list( $srcPath, $dstZone, $dstRel ) = $triplet;
138 $root = $this->getZonePath( $dstZone );
140 throw new MWException( "Invalid zone: $dstZone" );
142 if ( !$this->validateFilename( $dstRel ) ) {
143 throw new MWException( 'Validation error in $dstRel' );
145 $dstPath = "$root/$dstRel";
146 $dstDir = dirname( $dstPath );
148 if ( !is_dir( $dstDir ) ) {
149 if ( !wfMkdirParents( $dstDir ) ) {
150 return $this->newFatal( 'directorycreateerror', $dstDir );
152 if ( $dstZone == 'deleted' ) {
153 $this->initDeletedDir( $dstDir );
157 if ( self
::isVirtualUrl( $srcPath ) ) {
158 $srcPath = $triplets[$i][0] = $this->resolveVirtualUrl( $srcPath );
160 if ( !is_file( $srcPath ) ) {
161 // Make a list of files that don't exist for return to the caller
162 $status->fatal( 'filenotfound', $srcPath );
165 if ( !( $flags & self
::OVERWRITE
) && file_exists( $dstPath ) ) {
166 if ( $flags & self
::OVERWRITE_SAME
) {
167 $hashSource = sha1_file( $srcPath );
168 $hashDest = sha1_file( $dstPath );
169 if ( $hashSource != $hashDest ) {
170 $status->fatal( 'fileexistserror', $dstPath );
173 $status->fatal( 'fileexistserror', $dstPath );
178 $deleteDest = wfIsWindows() && ( $flags & self
::OVERWRITE
);
180 // Abort now on failure
181 if ( !$status->ok
) {
185 foreach ( $triplets as $triplet ) {
186 list( $srcPath, $dstZone, $dstRel ) = $triplet;
187 $root = $this->getZonePath( $dstZone );
188 $dstPath = "$root/$dstRel";
191 if ( $flags & self
::DELETE_SOURCE
) {
195 if ( !rename( $srcPath, $dstPath ) ) {
196 $status->error( 'filerenameerror', $srcPath, $dstPath );
200 if ( !copy( $srcPath, $dstPath ) ) {
201 $status->error( 'filecopyerror', $srcPath, $dstPath );
206 chmod( $dstPath, 0644 );
207 $status->successCount++
;
209 $status->failCount++
;
216 * Take all available measures to prevent web accessibility of new deleted
217 * directories, in case the user has not configured offline storage
219 protected function initDeletedDir( $dir ) {
220 // Add a .htaccess file to the root of the deleted zone
221 $root = $this->getZonePath( 'deleted' );
222 if ( !file_exists( "$root/.htaccess" ) ) {
223 file_put_contents( "$root/.htaccess", "Deny from all\n" );
225 // Seed new directories with a blank index.html, to prevent crawling
226 file_put_contents( "$dir/index.html", '' );
230 * Pick a random name in the temp zone and store a file to it.
231 * @param string $originalName The base name of the file as specified
232 * by the user. The file extension will be maintained.
233 * @param string $srcPath The current location of the file.
234 * @return FileRepoStatus object with the URL in the value.
236 function storeTemp( $originalName, $srcPath ) {
237 $date = gmdate( "YmdHis" );
238 $hashPath = $this->getHashPath( $originalName );
239 $dstRel = "$hashPath$date!$originalName";
240 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
242 $result = $this->store( $srcPath, 'temp', $dstRel );
243 $result->value
= $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
248 * Remove a temporary file or mark it for garbage collection
249 * @param string $virtualUrl The virtual URL returned by storeTemp
250 * @return boolean True on success, false on failure
252 function freeTemp( $virtualUrl ) {
253 $temp = "mwrepo://{$this->name}/temp";
254 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
255 wfDebug( __METHOD__
.": Invalid virtual URL\n" );
258 $path = $this->resolveVirtualUrl( $virtualUrl );
259 wfSuppressWarnings();
260 $success = unlink( $path );
266 * Publish a batch of files
267 * @param array $triplets (source,dest,archive) triplets as per publish()
268 * @param integer $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
269 * that the source files should be deleted if possible
271 function publishBatch( $triplets, $flags = 0 ) {
272 // Perform initial checks
273 if ( !wfMkdirParents( $this->directory
) ) {
274 return $this->newFatal( 'upload_directory_missing', $this->directory
);
276 if ( !is_writable( $this->directory
) ) {
277 return $this->newFatal( 'upload_directory_read_only', $this->directory
);
279 $status = $this->newGood( array() );
280 foreach ( $triplets as $i => $triplet ) {
281 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
283 if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) {
284 $triplets[$i][0] = $srcPath = $this->resolveVirtualUrl( $srcPath );
286 if ( !$this->validateFilename( $dstRel ) ) {
287 throw new MWException( 'Validation error in $dstRel' );
289 if ( !$this->validateFilename( $archiveRel ) ) {
290 throw new MWException( 'Validation error in $archiveRel' );
292 $dstPath = "{$this->directory}/$dstRel";
293 $archivePath = "{$this->directory}/$archiveRel";
295 $dstDir = dirname( $dstPath );
296 $archiveDir = dirname( $archivePath );
297 // Abort immediately on directory creation errors since they're likely to be repetitive
298 if ( !is_dir( $dstDir ) && !wfMkdirParents( $dstDir ) ) {
299 return $this->newFatal( 'directorycreateerror', $dstDir );
301 if ( !is_dir( $archiveDir ) && !wfMkdirParents( $archiveDir ) ) {
302 return $this->newFatal( 'directorycreateerror', $archiveDir );
304 if ( !is_file( $srcPath ) ) {
305 // Make a list of files that don't exist for return to the caller
306 $status->fatal( 'filenotfound', $srcPath );
310 if ( !$status->ok
) {
314 foreach ( $triplets as $i => $triplet ) {
315 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
316 $dstPath = "{$this->directory}/$dstRel";
317 $archivePath = "{$this->directory}/$archiveRel";
319 // Archive destination file if it exists
320 if( is_file( $dstPath ) ) {
321 // Check if the archive file exists
322 // This is a sanity check to avoid data loss. In UNIX, the rename primitive
323 // unlinks the destination file if it exists. DB-based synchronisation in
324 // publishBatch's caller should prevent races. In Windows there's no
325 // problem because the rename primitive fails if the destination exists.
326 if ( is_file( $archivePath ) ) {
329 wfSuppressWarnings();
330 $success = rename( $dstPath, $archivePath );
335 $status->error( 'filerenameerror',$dstPath, $archivePath );
336 $status->failCount++
;
339 wfDebug(__METHOD__
.": moved file $dstPath to $archivePath\n");
341 $status->value
[$i] = 'archived';
343 $status->value
[$i] = 'new';
347 wfSuppressWarnings();
348 if ( $flags & self
::DELETE_SOURCE
) {
349 if ( !rename( $srcPath, $dstPath ) ) {
350 $status->error( 'filerenameerror', $srcPath, $dstPath );
354 if ( !copy( $srcPath, $dstPath ) ) {
355 $status->error( 'filecopyerror', $srcPath, $dstPath );
362 $status->successCount++
;
363 wfDebug(__METHOD__
.": wrote tempfile $srcPath to $dstPath\n");
364 // Thread-safe override for umask
365 chmod( $dstPath, 0644 );
367 $status->failCount++
;
374 * Move a group of files to the deletion archive.
375 * If no valid deletion archive is configured, this may either delete the
376 * file or throw an exception, depending on the preference of the repository.
378 * @param array $sourceDestPairs Array of source/destination pairs. Each element
379 * is a two-element array containing the source file path relative to the
380 * public root in the first element, and the archive file path relative
381 * to the deleted zone root in the second element.
382 * @return FileRepoStatus
384 function deleteBatch( $sourceDestPairs ) {
385 $status = $this->newGood();
386 if ( !$this->deletedDir
) {
387 throw new MWException( __METHOD__
.': no valid deletion archive directory' );
391 * Validate filenames and create archive directories
393 foreach ( $sourceDestPairs as $pair ) {
394 list( $srcRel, $archiveRel ) = $pair;
395 if ( !$this->validateFilename( $srcRel ) ) {
396 throw new MWException( __METHOD__
.':Validation error in $srcRel' );
398 if ( !$this->validateFilename( $archiveRel ) ) {
399 throw new MWException( __METHOD__
.':Validation error in $archiveRel' );
401 $archivePath = "{$this->deletedDir}/$archiveRel";
402 $archiveDir = dirname( $archivePath );
403 if ( !is_dir( $archiveDir ) ) {
404 if ( !wfMkdirParents( $archiveDir ) ) {
405 $status->fatal( 'directorycreateerror', $archiveDir );
408 $this->initDeletedDir( $archiveDir );
410 // Check if the archive directory is writable
411 // This doesn't appear to work on NTFS
412 if ( !is_writable( $archiveDir ) ) {
413 $status->fatal( 'filedelete-archive-read-only', $archiveDir );
416 if ( !$status->ok
) {
423 * We're now committed to returning an OK result, which will lead to
424 * the files being moved in the DB also.
426 foreach ( $sourceDestPairs as $pair ) {
427 list( $srcRel, $archiveRel ) = $pair;
428 $srcPath = "{$this->directory}/$srcRel";
429 $archivePath = "{$this->deletedDir}/$archiveRel";
431 if ( file_exists( $archivePath ) ) {
432 # A file with this content hash is already archived
433 if ( !@unlink
( $srcPath ) ) {
434 $status->error( 'filedeleteerror', $srcPath );
438 if ( !@rename
( $srcPath, $archivePath ) ) {
439 $status->error( 'filerenameerror', $srcPath, $archivePath );
442 @chmod
( $archivePath, 0644 );
446 $status->successCount++
;
448 $status->failCount++
;
455 * Get a relative path for a deletion archive key,
456 * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
458 function getDeletedHashPath( $key ) {
460 for ( $i = 0; $i < $this->deletedHashLevels
; $i++
) {
461 $path .= $key[$i] . '/';
467 * Call a callback function for every file in the repository.
468 * Uses the filesystem even in child classes.
470 function enumFilesInFS( $callback ) {
471 $numDirs = 1 << ( $this->hashLevels
* 4 );
472 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++
) {
473 $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
474 $path = $this->directory
;
475 for ( $hexPos = 0; $hexPos < $this->hashLevels
; $hexPos++
) {
476 $path .= '/' . substr( $hexString, 0, $hexPos +
1 );
478 if ( !file_exists( $path ) ||
!is_dir( $path ) ) {
481 $dir = opendir( $path );
482 while ( false !== ( $name = readdir( $dir ) ) ) {
483 call_user_func( $callback, $path . '/' . $name );
489 * Call a callback function for every file in the repository
490 * May use either the database or the filesystem
492 function enumFiles( $callback ) {
493 $this->enumFilesInFS( $callback );
497 * Get properties of a file with a given virtual URL
498 * The virtual URL must refer to this repo
500 function getFileProps( $virtualUrl ) {
501 $path = $this->resolveVirtualUrl( $virtualUrl );
502 return File
::getPropsFromPath( $path );
506 * Path disclosure protection functions
508 * Get a callback function to use for cleaning error message parameters
510 function getErrorCleanupFunction() {
511 switch ( $this->pathDisclosureProtection
) {
513 $callback = array( $this, 'simpleClean' );
516 $callback = parent
::getErrorCleanupFunction();
521 function simpleClean( $param ) {
522 if ( !isset( $this->simpleCleanPairs
) ) {
524 $this->simpleCleanPairs
= array(
525 $this->directory
=> 'public',
526 "{$this->directory}/temp" => 'temp',
528 dirname( __FILE__
) => '$IP/extensions/WebStore',
530 if ( $this->deletedDir
) {
531 $this->simpleCleanPairs
[$this->deletedDir
] = 'deleted';
534 return strtr( $param, $this->simpleCleanPairs
);