MediaWiki  REL1_24
MemoryFileBackend.php
Go to the documentation of this file.
00001 <?php
00034 class MemoryFileBackend extends FileBackendStore {
00036     protected $files = array();
00037 
00038     public function isPathUsableInternal( $storagePath ) {
00039         return true;
00040     }
00041 
00042     protected function doCreateInternal( array $params ) {
00043         $status = Status::newGood();
00044 
00045         $dst = $this->resolveHashKey( $params['dst'] );
00046         if ( $dst === null ) {
00047             $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
00048 
00049             return $status;
00050         }
00051 
00052         $this->files[$dst] = array(
00053             'data' => $params['content'],
00054             'mtime' => wfTimestamp( TS_MW, time() )
00055         );
00056 
00057         return $status;
00058     }
00059 
00060     protected function doStoreInternal( array $params ) {
00061         $status = Status::newGood();
00062 
00063         $dst = $this->resolveHashKey( $params['dst'] );
00064         if ( $dst === null ) {
00065             $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
00066 
00067             return $status;
00068         }
00069 
00070         wfSuppressWarnings();
00071         $data = file_get_contents( $params['src'] );
00072         wfRestoreWarnings();
00073         if ( $data === false ) { // source doesn't exist?
00074             $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
00075 
00076             return $status;
00077         }
00078 
00079         $this->files[$dst] = array(
00080             'data' => $data,
00081             'mtime' => wfTimestamp( TS_MW, time() )
00082         );
00083 
00084         return $status;
00085     }
00086 
00087     protected function doCopyInternal( array $params ) {
00088         $status = Status::newGood();
00089 
00090         $src = $this->resolveHashKey( $params['src'] );
00091         if ( $src === null ) {
00092             $status->fatal( 'backend-fail-invalidpath', $params['src'] );
00093 
00094             return $status;
00095         }
00096 
00097         $dst = $this->resolveHashKey( $params['dst'] );
00098         if ( $dst === null ) {
00099             $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
00100 
00101             return $status;
00102         }
00103 
00104         if ( !isset( $this->files[$src] ) ) {
00105             if ( empty( $params['ignoreMissingSource'] ) ) {
00106                 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
00107             }
00108 
00109             return $status;
00110         }
00111 
00112         $this->files[$dst] = array(
00113             'data' => $this->files[$src]['data'],
00114             'mtime' => wfTimestamp( TS_MW, time() )
00115         );
00116 
00117         return $status;
00118     }
00119 
00120     protected function doDeleteInternal( array $params ) {
00121         $status = Status::newGood();
00122 
00123         $src = $this->resolveHashKey( $params['src'] );
00124         if ( $src === null ) {
00125             $status->fatal( 'backend-fail-invalidpath', $params['src'] );
00126 
00127             return $status;
00128         }
00129 
00130         if ( !isset( $this->files[$src] ) ) {
00131             if ( empty( $params['ignoreMissingSource'] ) ) {
00132                 $status->fatal( 'backend-fail-delete', $params['src'] );
00133             }
00134 
00135             return $status;
00136         }
00137 
00138         unset( $this->files[$src] );
00139 
00140         return $status;
00141     }
00142 
00143     protected function doGetFileStat( array $params ) {
00144         $src = $this->resolveHashKey( $params['src'] );
00145         if ( $src === null ) {
00146             return null;
00147         }
00148 
00149         if ( isset( $this->files[$src] ) ) {
00150             return array(
00151                 'mtime' => $this->files[$src]['mtime'],
00152                 'size' => strlen( $this->files[$src]['data'] ),
00153             );
00154         }
00155 
00156         return false;
00157     }
00158 
00159     protected function doGetLocalCopyMulti( array $params ) {
00160         $tmpFiles = array(); // (path => TempFSFile)
00161         foreach ( $params['srcs'] as $srcPath ) {
00162             $src = $this->resolveHashKey( $srcPath );
00163             if ( $src === null || !isset( $this->files[$src] ) ) {
00164                 $fsFile = null;
00165             } else {
00166                 // Create a new temporary file with the same extension...
00167                 $ext = FileBackend::extensionFromPath( $src );
00168                 $fsFile = TempFSFile::factory( 'localcopy_', $ext );
00169                 if ( $fsFile ) {
00170                     $bytes = file_put_contents( $fsFile->getPath(), $this->files[$src]['data'] );
00171                     if ( $bytes !== strlen( $this->files[$src]['data'] ) ) {
00172                         $fsFile = null;
00173                     }
00174                 }
00175             }
00176             $tmpFiles[$srcPath] = $fsFile;
00177         }
00178 
00179         return $tmpFiles;
00180     }
00181 
00182     protected function doStreamFile( array $params ) {
00183         $status = Status::newGood();
00184 
00185         $src = $this->resolveHashKey( $params['src'] );
00186         if ( $src === null || !isset( $this->files[$src] ) ) {
00187             $status->fatal( 'backend-fail-stream', $params['src'] );
00188 
00189             return $status;
00190         }
00191 
00192         print $this->files[$src]['data'];
00193 
00194         return $status;
00195     }
00196 
00197     protected function doDirectoryExists( $container, $dir, array $params ) {
00198         $prefix = rtrim( "$container/$dir", '/' ) . '/';
00199         foreach ( $this->files as $path => $data ) {
00200             if ( strpos( $path, $prefix ) === 0 ) {
00201                 return true;
00202             }
00203         }
00204 
00205         return false;
00206     }
00207 
00208     public function getDirectoryListInternal( $container, $dir, array $params ) {
00209         $dirs = array();
00210         $prefix = rtrim( "$container/$dir", '/' ) . '/';
00211         $prefixLen = strlen( $prefix );
00212         foreach ( $this->files as $path => $data ) {
00213             if ( strpos( $path, $prefix ) === 0 ) {
00214                 $relPath = substr( $path, $prefixLen );
00215                 if ( $relPath === false ) {
00216                     continue;
00217                 } elseif ( strpos( $relPath, '/' ) === false ) {
00218                     continue; // just a file
00219                 }
00220                 $parts = array_slice( explode( '/', $relPath ), 0, -1 ); // last part is file name
00221                 if ( !empty( $params['topOnly'] ) ) {
00222                     $dirs[$parts[0]] = 1; // top directory
00223                 } else {
00224                     $current = '';
00225                     foreach ( $parts as $part ) { // all directories
00226                         $dir = ( $current === '' ) ? $part : "$current/$part";
00227                         $dirs[$dir] = 1;
00228                         $current = $dir;
00229                     }
00230                 }
00231             }
00232         }
00233 
00234         return array_keys( $dirs );
00235     }
00236 
00237     public function getFileListInternal( $container, $dir, array $params ) {
00238         $files = array();
00239         $prefix = rtrim( "$container/$dir", '/' ) . '/';
00240         $prefixLen = strlen( $prefix );
00241         foreach ( $this->files as $path => $data ) {
00242             if ( strpos( $path, $prefix ) === 0 ) {
00243                 $relPath = substr( $path, $prefixLen );
00244                 if ( $relPath === false ) {
00245                     continue;
00246                 } elseif ( !empty( $params['topOnly'] ) && strpos( $relPath, '/' ) !== false ) {
00247                     continue;
00248                 }
00249                 $files[] = $relPath;
00250             }
00251         }
00252 
00253         return $files;
00254     }
00255 
00256     protected function directoriesAreVirtual() {
00257         return true;
00258     }
00259 
00266     protected function resolveHashKey( $storagePath ) {
00267         list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
00268         if ( $relPath === null ) {
00269             return null; // invalid
00270         }
00271 
00272         return ( $relPath !== '' ) ? "$fullCont/$relPath" : $fullCont;
00273     }
00274 }