https://www.mediawiki.org/wiki/Special:Code/MediaWiki/106399

Revision: 106399
Author:   aaron
Date:     2011-12-16 00:10:05 +0000 (Fri, 16 Dec 2011)
Log Message:
-----------
* Made getFileList() return null on some failures.
* Revived FSFileIterator but as a RecursiveDirectoryIterator wrapper. This can 
catch errors that occur to avoid annoying exceptions.
* Other random cleanups.

Modified Paths:
--------------
    branches/FileBackend/phase3/includes/filerepo/backend/FSFileBackend.php
    branches/FileBackend/phase3/includes/filerepo/backend/FileBackend.php
    branches/FileBackend/phase3/includes/filerepo/backend/FileOp.php
    
branches/FileBackend/phase3/tests/phpunit/includes/filerepo/FileBackendTest.php

Modified: 
branches/FileBackend/phase3/includes/filerepo/backend/FSFileBackend.php
===================================================================
--- branches/FileBackend/phase3/includes/filerepo/backend/FSFileBackend.php     
2011-12-16 00:02:22 UTC (rev 106398)
+++ branches/FileBackend/phase3/includes/filerepo/backend/FSFileBackend.php     
2011-12-16 00:10:05 UTC (rev 106399)
@@ -438,14 +438,21 @@
        function getFileList( array $params ) {
                list( $c, $dir ) = $this->resolveStoragePath( $params['dir'] );
                if ( $dir === null ) { // invalid storage path
-                       return array(); // empty result
+                       return null;
                }
-               try {
-                       $iter = new RecursiveIteratorIterator( new 
RecursiveDirectoryIterator( $dir ) );
-               } catch ( UnexpectedValueException $e ) {
-                       $iter = array(); // dir does not exist?
+               wfSuppressWarnings();
+               $exists = is_dir( $dir );
+               wfRestoreWarnings();
+               if ( !$exists ) {
+                       return array(); // nothing under this dir
                }
-               return $iter;
+               wfSuppressWarnings();
+               $readable = is_readable( $dir );
+               wfRestoreWarnings();
+               if ( !$readable ) {
+                       return null; // bad permissions?
+               }
+               return new FSFileIterator( $dir );
        }
 
        function getLocalReference( array $params ) {
@@ -499,3 +506,53 @@
                return $ok;
        }
 }
+
+/**
+ * Wrapper around RecursiveDirectoryIterator that catches
+ * exception or does any custom behavoir that we may want.
+ */
+class FSFileIterator implements Iterator {
+       /** @var RecursiveIteratorIterator */
+       protected $iter;
+
+       /**
+        * Get an FSFileIterator from a file system directory
+        * 
+        * @param $dir string
+        */
+       public function __construct( $dir ) {
+               try {
+                       $this->iter = new RecursiveIteratorIterator( new 
RecursiveDirectoryIterator( $dir ) );
+               } catch ( UnexpectedValueException $e ) {
+                       $this->iter = null; // bad permissions? deleted?
+               }
+       }
+
+       public function current() {
+               return $this->iter->current();
+       }
+
+       public function key() {
+               return $this->iter->key();
+       }
+
+       public function next() {
+               try {
+                       $this->iter->next();
+               } catch ( UnexpectedValueException $e ) {
+                       $this->iter = null;
+               }
+       }
+
+       public function rewind() {
+               try {
+                       $this->iter->rewind();
+               } catch ( UnexpectedValueException $e ) {
+                       $this->iter = null;
+               }
+       }
+
+       public function valid() {
+               return $this->iter && $this->iter->valid();
+       }
+}

Modified: branches/FileBackend/phase3/includes/filerepo/backend/FileBackend.php
===================================================================
--- branches/FileBackend/phase3/includes/filerepo/backend/FileBackend.php       
2011-12-16 00:02:22 UTC (rev 106398)
+++ branches/FileBackend/phase3/includes/filerepo/backend/FileBackend.php       
2011-12-16 00:10:05 UTC (rev 106399)
@@ -263,7 +263,7 @@
         * $params include:
         *     dir : storage path directory
         *
-        * @return Iterator|Array
+        * @return Traversable|Array|null Returns null on failure
         */
        abstract public function getFileList( array $params );
 

Modified: branches/FileBackend/phase3/includes/filerepo/backend/FileOp.php
===================================================================
--- branches/FileBackend/phase3/includes/filerepo/backend/FileOp.php    
2011-12-16 00:02:22 UTC (rev 106398)
+++ branches/FileBackend/phase3/includes/filerepo/backend/FileOp.php    
2011-12-16 00:10:05 UTC (rev 106399)
@@ -59,7 +59,7 @@
         * @param $performOps Array List of FileOp operations
         * @return Status 
         */
-       public static function attemptBatch( array $performOps ) {
+       final public static function attemptBatch( array $performOps ) {
                $status = Status::newGood();
 
                $predicates = FileOp::newPredicates(); // account for previous 
op in prechecks

Modified: 
branches/FileBackend/phase3/tests/phpunit/includes/filerepo/FileBackendTest.php
===================================================================
--- 
branches/FileBackend/phase3/tests/phpunit/includes/filerepo/FileBackendTest.php 
    2011-12-16 00:02:22 UTC (rev 106398)
+++ 
branches/FileBackend/phase3/tests/phpunit/includes/filerepo/FileBackendTest.php 
    2011-12-16 00:10:05 UTC (rev 106399)
@@ -352,6 +352,7 @@
                        "$base/cont1/subdir2/subdir/test4.txt",
                        "$base/cont1/subdir2/subdir/test5.txt",
                        "$base/cont1/subdir2/subdir/sub/test0.txt",
+                       "$base/cont1/subdir2/subdir/sub/120-px-file.txt",
                );
                $this->pathsToPrune = array_merge( $this->pathsToPrune, $files 
);
 
@@ -378,6 +379,7 @@
                        "subdir2/subdir/test4.txt",
                        "subdir2/subdir/test5.txt",
                        "subdir2/subdir/sub/test0.txt",
+                       "subdir2/subdir/sub/120-px-file.txt",
                );
                $expected = sort( $expected );
 
@@ -398,6 +400,13 @@
                }
 
                $this->assertEquals( $expected, sort( $list ), "Correct file 
listing." );
+
+               foreach ( $files as $file ) {
+                       $this->backend->delete( array( 'src' => "$base/$files" 
) );
+               }
+
+               $iter = $this->backend->getFileList( array( 'dir' => 
"$base/cont1/not/exists" ) );
+               foreach ( $iter as $iter ) {} // no errors
        }
 
        function tearDown() {


_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to