r104405 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r104404‎ | r104405 | r104406 >
Date:06:59, 28 November 2011
Author:aaron
Status:deferred
Tags:
Comment:
* Fixed S&R mistake from r104382 in doOperation()
* Various fixes after some basic testing
Modified paths:
  • /branches/FileBackend/phase3/includes/AutoLoader.php (modified) (history)
  • /branches/FileBackend/phase3/includes/filerepo/FileRepo.php (modified) (history)
  • /branches/FileBackend/phase3/includes/filerepo/ForeignAPIRepo.php (modified) (history)
  • /branches/FileBackend/phase3/includes/filerepo/backend/FSFileBackend.php (modified) (history)
  • /branches/FileBackend/phase3/includes/filerepo/backend/FileBackend.php (modified) (history)
  • /branches/FileBackend/phase3/includes/filerepo/backend/FileOp.php (modified) (history)
  • /branches/FileBackend/phase3/includes/filerepo/file/FSFile.php (modified) (history)

Diff [purge]

Index: branches/FileBackend/phase3/includes/filerepo/ForeignAPIRepo.php
@@ -40,12 +40,14 @@
4141 protected $mFileExists = array();
4242
4343 function __construct( $info ) {
 44+ global $wgLocalFileRepo, $wgUploadDirectory;
 45+ if ( !isset( $info['directory'] ) ) { // b/c
 46+ $info['directory'] = $wgUploadDirectory;
 47+ }
4448 parent::__construct( $info );
45 - global $wgUploadDirectory;
4649
4750 // http://commons.wikimedia.org/w/api.php
4851 $this->mApiBase = isset( $info['apibase'] ) ? $info['apibase'] : null;
49 - $this->directory = isset( $info['directory'] ) ? $info['directory'] : $wgUploadDirectory;
5052
5153 if( isset( $info['apiThumbCacheExpiry'] ) ) {
5254 $this->apiThumbCacheExpiry = $info['apiThumbCacheExpiry'];
@@ -59,17 +61,11 @@
6062 }
6163 // If we can cache thumbs we can guess sane defaults for these
6264 if( $this->canCacheThumbs() && !$this->url ) {
63 - global $wgLocalFileRepo;
6465 $this->url = $wgLocalFileRepo['url'];
6566 }
6667 if( $this->canCacheThumbs() && !$this->thumbUrl ) {
6768 $this->thumbUrl = $this->url . '/thumb';
6869 }
69 - if ( isset( $info['thumbDir'] ) ) {
70 - $this->thumbDir = $info['thumbDir'];
71 - } else {
72 - $this->thumbDir = "{$this->directory}/thumb";
73 - }
7470 }
7571
7672 /**
@@ -280,9 +276,9 @@
281277 $localFilename = $localPath . "/" . $fileName;
282278 $localUrl = $this->getZoneUrl( 'thumb' ) . "/" . $this->getHashPath( $name ) . rawurlencode( $name ) . "/" . rawurlencode( $fileName );
283279
284 - if( $this->repo->fileExists( $localFilename ) && isset( $metadata['timestamp'] ) ) {
 280+ if( $this->fileExists( $localFilename ) && isset( $metadata['timestamp'] ) ) {
285281 wfDebug( __METHOD__ . " Thumbnail was already downloaded before\n" );
286 - $modified = $this->repo->getFileTimestamp( $localFilename );
 282+ $modified = $this->getFileTimestamp( $localFilename );
287283 $remoteModified = strtotime( $metadata['timestamp'] );
288284 $current = time();
289285 $diff = abs( $modified - $current );
@@ -302,7 +298,7 @@
303299
304300 # @todo FIXME: Delete old thumbs that aren't being used. Maintenance script?
305301 wfSuppressWarnings();
306 - $backend = $this->repo->getBackend();
 302+ $backend = $this->getBackend();
307303 $op = array( 'op' => 'create', 'dest' => $localFilename, 'content' => $thumb );
308304 if( !$backend->doOperation( $op )->isOK() ) {
309305 wfRestoreWarnings();
@@ -331,17 +327,14 @@
332328 }
333329
334330 /**
335 - * Get the local directory corresponding to one of the three basic zones
 331+ * Get the local directory corresponding to one of the basic zones
336332 */
337333 function getZonePath( $zone ) {
338 - switch ( $zone ) {
339 - case 'public':
340 - return $this->directory;
341 - case 'thumb':
342 - return $this->thumbDir;
343 - default:
344 - return false;
 334+ $supported = array( 'public', 'thumb' );
 335+ if ( in_array( $zone, $supported ) ) {
 336+ return parent::getZonePath( $zone );
345337 }
 338+ return false;
346339 }
347340
348341 /**
Index: branches/FileBackend/phase3/includes/filerepo/file/FSFile.php
@@ -38,7 +38,7 @@
3939 * @return bool
4040 */
4141 public function exists() {
42 - return ( file_exists( $this->path ) && !is_dir( $this->path ) );
 42+ return is_file( $this->path );
4343 }
4444
4545 /**
@@ -47,7 +47,9 @@
4848 * @return string|false TS_MW timestamp or false on failure
4949 */
5050 public function getTimestamp() {
 51+ wfSuppressWarnings();
5152 $timestamp = filemtime( $this->path );
 53+ wfRestoreWarnings();
5254 if ( $timestamp !== false ) {
5355 $timestamp = wfTimestamp( TS_MW, $timestamp );
5456 }
Index: branches/FileBackend/phase3/includes/filerepo/backend/FileOp.php
@@ -14,7 +14,7 @@
1515 abstract class FileOp {
1616 /** $var Array */
1717 protected $params;
18 - /** $var FileBackend */
 18+ /** $var FileBackendBase */
1919 protected $backend;
2020
2121 protected $state;
@@ -32,7 +32,7 @@
3333 * @params $backend FileBackend
3434 * @params $params Array
3535 */
36 - final public function __construct( FileBackend $backend, array $params ) {
 36+ final public function __construct( FileBackendBase $backend, array $params ) {
3737 $this->backend = $backend;
3838 $this->params = $params;
3939 $this->state = self::STATE_NEW;
@@ -157,7 +157,8 @@
158158 protected function checkAndBackupDest() {
159159 $status = Status::newGood();
160160 // Check if a file already exists at the destination
161 - if ( !$this->backend->fileExists( $this->params['dest'] ) ) {
 161+ $params = array( 'source' => $this->params['dest'] );
 162+ if ( !$this->backend->fileExists( $params ) ) {
162163 return $status; // nothing to do
163164 }
164165
Index: branches/FileBackend/phase3/includes/filerepo/backend/FSFileBackend.php
@@ -375,7 +375,7 @@
376376 if ( $source === null ) {
377377 return false; // invalid storage path
378378 }
379 - return file_exists( $source ) && is_file( $source );
 379+ return is_file( $source );
380380 }
381381
382382 function getHashType() {
@@ -519,14 +519,18 @@
520520 * @param $directory string
521521 */
522522 public function __construct( $directory ) {
523 - $this->directory = (string)$directory;
 523+ // Removing any trailing slash
 524+ if ( substr( $this->directory, -1 ) === '/' ) {
 525+ $this->directory = substr( $this->directory, 0, -1 );
 526+ }
 527+ $this->directory = realpath( $directory );
524528 }
525529
526530 private function load() {
527531 if ( !$this->loaded ) {
528532 $this->loaded = true;
529533 // If we get an invalid directory then the result is an empty list
530 - if ( file_exists( $this->directory ) && is_dir( $this->directory ) ) {
 534+ if ( is_dir( $this->directory ) ) {
531535 $handle = opendir( $this->directory );
532536 if ( $handle ) {
533537 $this->pushDirectory( $this->directory, $handle );
@@ -565,36 +569,49 @@
566570 }
567571
568572 function nextFile() {
569 - $set = $this->currentDirectory();
570 - if ( !$set ) {
 573+ if ( !$this->currentDirectory() ) {
571574 return false; // nothing else to scan
572575 }
573 - list( $dir, $handle ) = $set;
 576+ # Next file under the current directory (and subdirectories).
 577+ # This may advance the current directory down to a descendent.
 578+ # The current directory is set to the parent if nothing is found.
 579+ $nextFile = $this->nextFileBelowCurrent();
 580+ if ( $nextFile !== false ) {
 581+ return $nextFile;
 582+ } else {
 583+ # Scan the higher directories
 584+ return $this->nextFile();
 585+ }
 586+ }
 587+
 588+ function nextFileBelowCurrent() {
 589+ list( $dir, $handle ) = $this->currentDirectory();
574590 while ( false !== ( $file = readdir( $handle ) ) ) {
575591 // Exclude '.' and '..' as well .svn or .lock type files
576592 if ( $file[0] !== '.' ) {
 593+ $path = "{$dir}/{$file}";
577594 // If the first thing we find is a file, then return it.
578595 // If the first thing we find is a directory, then return
579596 // the first file that it contains (via recursion).
580597 // We exclude symlink dirs in order to avoid cycles.
581 - if ( is_dir( "{$dir}/{$file}" ) && !is_link( "{$dir}/{$file}" ) ) {
582 - $subHandle = opendir( "$dir/$file" );
 598+ if ( is_dir( $path ) && !is_link( $path ) ) {
 599+ $subHandle = opendir( $path );
583600 if ( $subHandle ) {
584 - $this->pushDirectory( "{$dir}/{$file}", $subHandle );
585 - $nextFile = $this->nextFile();
 601+ $this->pushDirectory( $path, $subHandle );
 602+ $nextFile = $this->nextFileBelowCurrent();
586603 if ( $nextFile !== false ) {
587604 return $nextFile; // found the next one!
588605 }
589606 }
590 - } elseif ( is_file( "{$dir}/{$file}" ) ) {
591 - return "{$dir}/{$file}"; // found the next one!
 607+ } elseif ( is_file( $path ) ) {
 608+ return $path; // found the next one!
592609 }
593610 }
594611 }
595 - # If we didn't find anything in this directory,
596 - # then back out and scan the other higher directories
 612+ # If we didn't find anything else in this directory,
 613+ # then back out so we scan the other higher directories
597614 $this->popDirectory();
598 - return $this->nextFile();
 615+ return false;
599616 }
600617
601618 private function currentDirectory() {
Index: branches/FileBackend/phase3/includes/filerepo/backend/FileBackend.php
@@ -84,7 +84,7 @@
8585 * @return Status
8686 */
8787 final public function doOperation( $op ) {
88 - return $this->doOperation( $op );
 88+ return $this->doOperations( array( $op ) );
8989 }
9090
9191 /**
@@ -400,7 +400,7 @@
401401 unset( $params['op'] ); // don't need this
402402 unset( $params['ignoreErrors'] ); // don't need this
403403 // Append the FileOp class
404 - $performOps[] = new $class( $params );
 404+ $performOps[] = new $class( $this, $params );
405405 } else {
406406 throw new MWException( "Operation `$opName` is not supported." );
407407 }
Index: branches/FileBackend/phase3/includes/filerepo/FileRepo.php
@@ -43,7 +43,9 @@
4444 function __construct( $info ) {
4545 // Required settings
4646 $this->name = $info['name'];
47 - $this->url = $info['url'];
 47+ $this->url = isset( $info['url'] )
 48+ ? $info['url']
 49+ : false; // a subclass will need to set the URL (e.g. ForeignAPIRepo)
4850
4951 // Optional settings that can have no value
5052 $optionalSettings = array(
@@ -306,7 +308,10 @@
307309 return null;
308310 }
309311 $backendName = $this->backend->getName();
310 - return "mwstore://$backendName/{$container}/{$base}";
 312+ if ( $base != '' ) { // may not be set
 313+ $base = "/{$base}";
 314+ }
 315+ return "mwstore://$backendName/{$container}{$base}";
311316 }
312317
313318 /**
Index: branches/FileBackend/phase3/includes/AutoLoader.php
@@ -489,6 +489,7 @@
490490 'FileBackend' => 'includes/filerepo/backend/FileBackend.php',
491491 'FileBackendMultiWrite' => 'includes/filerepo/backend/FileBackendMultiWrite.php',
492492 'FSFileBackend' => 'includes/filerepo/backend/FSFileBackend.php',
 493+ 'FileIterator' => 'includes/filerepo/backend/FSFileBackend.php',
493494 'FileLockManager' => 'includes/filerepo/backend/FileLockManager.php',
494495 'FSFileLockManager' => 'includes/filerepo/backend/FileLockManager.php',
495496 'DBFileLockManager' => 'includes/filerepo/backend/FileLockManager.php',

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r104382* Removed raw FS access in ForeignAPIFile and added FileBackend::clean() func...aaron00:29, 28 November 2011

Status & tagging log