eZPublishCommunityProject(LegacyStack)  2013.9
eZFSFileHandler Class Reference

Public Member Functions

 abortCacheGeneration ()
 Aborts the current cache generation process. More...
 
 checkCacheGenerationTimeout ()
 Checks if the .generating file was changed, which would mean that generation timed out. More...
 
 deleteLocal ()
 Deletes a file that has been fetched before. More...
 
 endCacheGeneration ()
 Ends the cache generation started by startCacheGeneration(). More...
 
 exists ()
 Check if given file/dir exists. More...
 
 eZFSFileHandler ($filePath=false)
 Constructor. More...
 
 fetch ($noLocalCache=false)
 Fetches file from db and saves it in FS under the same name. More...
 
 fetchContents ()
 Returns file contents. More...
 
 fetchUnique ()
 Fetches file from db and saves it in FS under unique name. More...
 
 fileDeleteLocal ($path)
 Deletes a file that has been fetched before. More...
 
 fileFetch ($filePath)
 Fetches file from db and saves it in FS under the same name. More...
 
 fileFetchContents ($filePath)
 Returns file contents. More...
 
 fileStore ($filePath, $scope=false, $delete=false, $datatype=false)
 Store file. More...
 
 hasStaleCacheSupport ()
 
 isExpired ($expiry, $curtime, $ttl)
 Calculates if the current file data is expired or not. More...
 
 loadMetaData ($force=false)
 
 move ($dstPath)
 Move file. More...
 
 mtime ()
 Returns file modification time. More...
 
 name ()
 Returns file name. More...
 
 passthrough ($offset=0, $length=false)
 Outputs file contents to the browser Note: does not handle headers. More...
 
 processCache ($retrieveCallback, $generateCallback=null, $ttl=null, $expiry=null, $extraData=null)
 
 processFile ($callback, $expiry=false, $extraData=null)
 
 purge ($printCallback=false, $microsleep=false, $max=false, $expiry=false)
 
 requiresClusterizing ()
 eZFS only stores data to FS and doesn't require/support clusterizing More...
 
 requiresPurge ()
 eZFS does not require binary purge. More...
 
 size ()
 Returns file size. More...
 
 startCacheGeneration ()
 Starts cache generation for the current file. More...
 
 stat ()
 Returns file metadata. More...
 
 storeContents ($contents, $scope=false, $datatype=false, $storeLocally=false)
 Store file contents to disk. More...
 

Static Public Member Functions

 delete ()
 Deletes specified file/directory. More...
 
 fileCopy ($srcPath, $dstPath)
 Copy file. More...
 
 fileDelete ($path, $fnamePart=false)
 
 fileDeleteByDirList ($dirList, $commonPath, $commonSuffix)
 Delete files located in a directories from dirList, with common prefix specified by commonPath, and common suffix with added wildcard at the end. More...
 
 fileDeleteByWildcard ($wildcard)
 Delete files matching given wildcard. More...
 
 fileExists ($path)
 Check if given file/dir exists. More...
 
 fileLinkCopy ($srcPath, $dstPath, $symLink)
 Create symbolic or hard link to file. More...
 
 fileMove ($srcPath, $dstPath)
 Move file. More...
 
 fileStoreContents ($filePath, $contents, $scope=false, $datatype=false)
 Store file contents. More...
 
static isFileExpired ($fname, $mtime, $expiry, $curtime, $ttl)
 Calculates if the file data is expired or not. More...
 

Public Attributes

 $filePath
 
 $metaData = null
 
const EXPIRY_TIMESTAMP = 233366400
 

Private Member Functions

 _exclusiveLock ($fname=false)
 
 _freeExclusiveLock ($fname=false)
 
_mutex ()
 
 storeCache ($fileData, $storeCache=true)
 

Member Function Documentation

eZFSFileHandler::_exclusiveLock (   $fname = false)
private

Acquires an exclusive lock to the current file by using eZMutex.

If a lock is already present it will sleep 0.5 seconds and try again until the lock lifetime is exceeded and the lock is stolen.

Note: Lock stealing might be removed.

Parameters
$fnameName of the calling code (usually function name).

Referenced by processCache().

eZFSFileHandler::_freeExclusiveLock (   $fname = false)
private

Frees the current exclusive lock in use.

Parameters
$fnameName of the calling code (usually function name).

Referenced by processCache(), and storeCache().

& eZFSFileHandler::_mutex ( )
private

Returns the mutex object for the current file.

Referenced by _exclusiveLock(), and _freeExclusiveLock().

eZFSFileHandler::abortCacheGeneration ( )

Aborts the current cache generation process.

Does so by rolling back the current transaction, which should be the .generating file lock

Referenced by storeCache().

eZFSFileHandler::checkCacheGenerationTimeout ( )

Checks if the .generating file was changed, which would mean that generation timed out.

If not timed out, refreshes the timestamp so that storage won't be stolen

eZFSFileHandler::delete ( )
static

Deletes specified file/directory.

If a directory specified it is deleted recursively.

eZFSFileHandler::deleteLocal ( )

Deletes a file that has been fetched before.

In case of fetching from filesystem does nothing.

eZFSFileHandler::endCacheGeneration ( )

Ends the cache generation started by startCacheGeneration().

eZFSFileHandler::exists ( )

Check if given file/dir exists.

NOTE: this function does not interact with filesystem. Instead, it just returns existance status determined in the constructor.

eZFSFileHandler::eZFSFileHandler (   $filePath = false)

Constructor.

$filePath File path. If specified, file metadata is fetched in the constructor.

eZFSFileHandler::fetch (   $noLocalCache = false)

Fetches file from db and saves it in FS under the same name.

In case of fetching from filesystem does nothing.

eZFSFileHandler::fetchContents ( )

Returns file contents.

Returns
contents string, or false in case of an error.
eZFSFileHandler::fetchUnique ( )

Fetches file from db and saves it in FS under unique name.

In case of fetching from filesystem, does nothing

Returns
string The unique file path. on FS, the file path.
eZFSFileHandler::fileCopy (   $srcPath,
  $dstPath 
)
static

Copy file.

eZFSFileHandler::fileDelete (   $path,
  $fnamePart = false 
)
static
eZFSFileHandler::fileDeleteByDirList (   $dirList,
  $commonPath,
  $commonSuffix 
)
static

Delete files located in a directories from dirList, with common prefix specified by commonPath, and common suffix with added wildcard at the end.

eZFSFileHandler::fileDeleteByWildcard (   $wildcard)
static

Delete files matching given wildcard.

eZFSFileHandler::fileDeleteLocal (   $path)

Deletes a file that has been fetched before.

See Also
fetchUnique

In case of fetching from filesystem does nothing.

eZFSFileHandler::fileExists (   $path)
static

Check if given file/dir exists.

eZFSFileHandler::fileFetch (   $filePath)

Fetches file from db and saves it in FS under the same name.

In case of fetching from filesystem does nothing.

eZFSFileHandler::fileFetchContents (   $filePath)

Returns file contents.

Returns
string|false contents string, or false in case of an error.
eZFSFileHandler::fileLinkCopy (   $srcPath,
  $dstPath,
  $symLink 
)
static

Create symbolic or hard link to file.

eZFSFileHandler::fileMove (   $srcPath,
  $dstPath 
)
static

Move file.

eZFSFileHandler::fileStore (   $filePath,
  $scope = false,
  $delete = false,
  $datatype = false 
)

Store file.

In case of storing to filesystem does nothing.

Parameters
string$filePathPath to the file being stored.
string$scopeMeans something like "file category". May be used to clean caches of a certain type.
string$deletetrue if the file should be deleted after storing.
eZFSFileHandler::fileStoreContents (   $filePath,
  $contents,
  $scope = false,
  $datatype = false 
)
static

Store file contents.

eZFSFileHandler::hasStaleCacheSupport ( )
eZFSFileHandler::isExpired (   $expiry,
  $curtime,
  $ttl 
)

Calculates if the current file data is expired or not.

Parameters
int$expiryTime when file is to be expired, a value of -1 will disable this check.
int$curtimeThe current time to check against.
int$ttlNumber of seconds the data can live, set to null to disable TTL.
Returns
bool

Referenced by processCache().

static eZFSFileHandler::isFileExpired (   $fname,
  $mtime,
  $expiry,
  $curtime,
  $ttl 
)
static

Calculates if the file data is expired or not.

Parameters
string$fnameName of file, available for easy debugging.
int$mtimeModification time of file, can be set to false if file does not exist.
int$expiryTime when file is to be expired, a value of -1 will disable this check.
int$curtimeThe current time to check against.
int$ttlNumber of seconds the data can live, set to null to disable TTL.
Returns
bool
eZFSFileHandler::loadMetaData (   $force = false)

Load file meta information.

Parameters
$forceIf true, file stats will be refreshed

Referenced by delete(), and eZFSFileHandler().

eZFSFileHandler::move (   $dstPath)

Move file.

eZFSFileHandler::mtime ( )

Returns file modification time.

eZFSFileHandler::name ( )

Returns file name.

eZFSFileHandler::passthrough (   $offset = 0,
  $length = false 
)

Outputs file contents to the browser Note: does not handle headers.

eZFile::downloadHeaders() can be used for this

Parameters
int$offsetTransfer start offset
int$lengthTransfer length, in bytes
eZFSFileHandler::processCache (   $retrieveCallback,
  $generateCallback = null,
  $ttl = null,
  $expiry = null,
  $extraData = null 
)

Creates a single transaction out of the typical file operations for accessing caches. Caches are normally ready from the database or local file, if the entry does not exist or is expired then it generates the new cache data and stores it. This method takes care of these operations and handles the custom code by performing callbacks when needed.

The $retrieveCallback is used when the file contents can be used (ie. not re-generation) and is called when the file is ready locally. The function will be called with the file path as the first parameter, the mtime as the second and optionally $extraData as the third. The function must return the file contents or an instance of eZClusterFileFailure which can be used to tell the system that the retrieve data cannot be used after all. $retrieveCallback can be set to null which makes the system go directly to the generation.

The $generateCallback is used when the file content is expired or does not exist, in this case the content must be re-generated and stored. The function will be called with the file path as the first parameter and optionally $extraData as the second. The function must return an array with information on the contents, the array consists of:

  • scope - The current scope of the file, is optional.
  • datatype - The current datatype of the file, is optional.
  • content - The file content, this can be any type except null.
  • binarydata - The binary data which is written to the file.
  • store - Whether content or binarydata should be stored to the file, if false it will simply return the data. Optional, by default it is true. Note: Set $generateCallback to false to disable generation callback. Note: Set $generateCallback to null to tell the function to perform a write lock but not do any generation, the generation must done be done by the caller by calling storeCache().

Either content or binarydata must be supplied, if not an error is issued and it returns null. If content is set it will be used as the return value of this function, if not it will return the binary data. If binarydata is set it will be used as the binary data for the file, if not it will perform a var_export on content and use that as the binary data.

For convenience the $generateCallback function can return a string which will be considered as the binary data for the file and returned as the content.

For controlling how long a cache entry can be used the parameters $expiry and $ttl is used. $expiry can be set to a timestamp which controls the absolute max time for the cache, after this time/date the cache will never be used. If the value is set to a negative value or null there the expiration check is disabled.

$ttl (time to live) tells how many seconds the cache can live from the time it was stored. If the value is set to negative or null there is no limit for the lifetime of the cache. A value of 0 means that the cache will always expire and practically disables caching. For the cache to be used both the $expiry and $ttl check must hold.

Referenced by processFile().

eZFSFileHandler::processFile (   $callback,
  $expiry = false,
  $extraData = null 
)

Provides access to the file contents by downloading the file locally and calling $callback with the local filename. The callback can then process the contents and return the data in the same way as in processCache(). Downloading is only done once so the local copy is kept, while updates to the remote DB entry is synced with the local one.

The parameters $expiry and $extraData is the same as for processCache().

Note
Unlike processCache() this returns null if the file cannot be accessed.
eZFSFileHandler::purge (   $printCallback = false,
  $microsleep = false,
  $max = false,
  $expiry = false 
)

Purge local and remote file data for current file.

eZFSFileHandler::requiresClusterizing ( )

eZFS only stores data to FS and doesn't require/support clusterizing

Returns
bool false
eZFSFileHandler::requiresPurge ( )

eZFS does not require binary purge.

Files are stored on plain FS and removed using FS functions

Since
4.5.0
Returns
bool
eZFSFileHandler::size ( )

Returns file size.

eZFSFileHandler::startCacheGeneration ( )

Starts cache generation for the current file.

This is done by creating a file named by the original file name, prefixed with '.generating'.

Todo:
add timeout handling...
Returns
mixed true if generation lock was granted, an integer matching the time before the current generation times out
eZFSFileHandler::stat ( )

Returns file metadata.

Referenced by loadMetaData().

eZFSFileHandler::storeCache (   $fileData,
  $storeCache = true 
)
private

Stores the data in $fileData to the remote and local file and commits the transaction.

The parameter $fileData must contain the same as information as the $generateCallback returns as explained in processCache().

Note
This method is just a continuation of the code in processCache() and is not meant to be called alone since it relies on specific state in the database.

Referenced by processCache().

eZFSFileHandler::storeContents (   $contents,
  $scope = false,
  $datatype = false,
  $storeLocally = false 
)

Store file contents to disk.

Parameters
string$contentsBinary file data
string$datatypeNot used in the FS handler
string$scopeNot used in the FS handler
bool$storeLocallyNot used in the FS handler
Returns
void

Referenced by storeCache().

Member Data Documentation

eZFSFileHandler::$metaData = null

Referenced by stat().

const eZFSFileHandler::EXPIRY_TIMESTAMP = 233366400

The documentation for this class was generated from the following file: