eZPublish  4.5
eZDBFileHandler Class Reference

Public Member Functions

 __construct ($filePath=false)
 Constructor. More...
 
 __get ($propertyName)
 Magic getter. More...
 
 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 ($rename=true)
 Ends the cache generation started by startCacheGeneration(). More...
 
 exists ()
 Check if given file/dir exists. More...
 
 fetch ($noLocalCache=false)
 Fetches file from db and saves it in FS under the same name. More...
 
 fetchContents ()
 Returns file contents. More...
 
 fetchExpiredBinaryItems ($limit=array(0, 100))
 Fetches the first $limit expired binary items from the DB. More...
 
 fetchExpiredItems ($scopes, $limit=array(0, 100), $expiry=false)
 Fetches the first $limit expired files from the DB. More...
 
 fileStore ($filePath, $scope=false, $delete=false, $datatype=false)
 Stores a local file to the cluster. More...
 
 fileStoreContents ($filePath, $contents, $scope=false, $datatype=false)
 Store file contents. More...
 
 getFileList ($scopes=false, $excludeScopes=false)
 Get list of files stored in database. More...
 
 isDBFileExpired ($expiry, $curtime, $ttl)
 Calculates if the DB file is expired or not. More...
 
 isExpired ($expiry, $curtime, $ttl)
 Calculates if the current file data is expired or not. More...
 
 isLocalFileExpired ($expiry, $curtime, $ttl)
 Calculates if the local file is expired or not. More...
 
 loadMetaData ($force=false)
 Load file meta information from the database. More...
 
 move ($dstPath)
 Move file. More...
 
 mtime ()
 Returns file modification time. More...
 
 name ()
 Returns file name. More...
 
 passthrough ()
 Outputs file contents prepending them with appropriate HTTP headers. More...
 
 processCache ($retrieveCallback, $generateCallback=null, $ttl=null, $expiry=null, $extraData=null)
 Creates a single transaction out of the typical file operations for accessing caches. More...
 
 processFile ($callback, $expiry=false, $extraData=null)
 
 purge ($printCallback=false, $microsleep=false, $max=false, $expiry=false)
 
 requiresBinaryPurge ()
 eZDB does require binary purge. More...
 
 requiresClusterizing ()
 Since eZDB uses the database, running clusterize.php is required. More...
 
 requiresPurge ()
 eZDB does require binary purge. More...
 
 size ()
 Returns file size. More...
 
 startCacheGeneration ()
 Starts cache generation for the current file. More...
 
 stat ()
 Returns file metadata. More...
 
 storeCache ($fileData)
 Stores the data in $fileData to the remote and local file and commits the transaction. More...
 
 storeContents ($contents, $scope=false, $datatype=false, $storeLocally=false)
 Store file contents. More...
 

Static Public Member Functions

static cleanPath ($path)
 Returns a clean version of input $path. More...
 
 delete ()
 Deletes specified file/directory. More...
 
 fetchUnique ()
 Fetches file from db and saves it in FS under unique name. More...
 
 fileCopy ($srcPath, $dstPath)
 Copy file. More...
 
 fileDelete ($path, $fnamePart=false)
 Deletes specified file/directory. More...
 
 fileDeleteByDirList ($dirList, $commonPath, $commonSuffix)
 
 fileDeleteByRegex ($dir, $fileRegex)
 
 fileDeleteByWildcard ($wildcard)
 
 fileDeleteLocal ($path)
 Deletes a file that has been fetched before. More...
 
 fileExists ($path)
 Check if given file/dir exists. More...
 
 fileFetch ($filePath)
 Fetches file from db and saves it in FS under the same name. More...
 
 fileFetchContents ($filePath)
 Returns file contents. More...
 
 fileLinkCopy ($srcPath, $dstPath, $symLink)
 Create symbolic or hard link to file. More...
 
 fileMove ($srcPath, $dstPath)
 Move file. More...
 
static isFileExpired ($fname, $mtime, $expiry, $curtime, $ttl)
 Calculates if the file data is expired or not. More...
 

Public Attributes

 $_metaData = null
 holds the file's metaData loaded from database The variable's type indicates the exact status: More...
 
 $backend
 
 $backendVerify
 
 $filePath
 
 $realFilePath = null
 holds the real file path. More...
 
const INFOCACHE_MAX = 200
 
const LOCAL_CACHE = 1
 

Protected Member Functions

 _cacheType ()
 Determines the cache type based on the path. More...
 

Protected Attributes

 $generationStartTimestamp = false
 
 $nonExistantStaleCacheHandling
 Holds the preferences used when stale cache is activated and no expired file is available. More...
 
 $remainingCacheGenerationTime = false
 
 $useStaleCache = false
 

Constructor & Destructor Documentation

eZDBFileHandler::__construct (   $filePath = false)

Constructor.

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

Member Function Documentation

eZDBFileHandler::__get (   $propertyName)

Magic getter.

eZDBFileHandler::_cacheType ( )
protected

Determines the cache type based on the path.

Returns
string viewcache, cacheblock or misc

Referenced by __get().

eZDBFileHandler::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().

eZDBFileHandler::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

Referenced by storeCache().

eZDBFileHandler::delete ( )
static

Deletes specified file/directory.

If a directory specified it is deleted recursively.

eZDBFileHandler::deleteLocal ( )

Deletes a file that has been fetched before.

eZDBFileHandler::endCacheGeneration (   $rename = true)

Ends the cache generation started by startCacheGeneration().

Referenced by storeCache().

eZDBFileHandler::exists ( )

Check if given file/dir exists.

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

eZDBFileHandler::fetch (   $noLocalCache = false)

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

Referenced by processCache().

eZDBFileHandler::fetchContents ( )

Returns file contents.

Returns
contents string, or false in case of an error.
eZDBFileHandler::fetchExpiredBinaryItems (   $limit = array( 0, 100 ))

Fetches the first $limit expired binary items from the DB.

Parameters
array$limitA 2 items array( offset, limit )
Returns
array(eZClusterFileHandlerInterace)
Since
4.3.0
Deprecated:
Deprecated as of 4.5, use eZDBFileHandler::fetchExpiredItems() instead.
Todo:
handle output using $cli or something
eZDBFileHandler::fetchExpiredItems (   $scopes,
  $limit = array( 0 , 100 ),
  $expiry = false 
)

Fetches the first $limit expired files from the DB.

Parameters
array$scopesArray of scopes to fetch from
array$limitA 2 items array( offset, limit )
int$expiryNumber of seconds, only items older than this will be returned
Returns
array(filepath)
Since
4.5.0
eZDBFileHandler::fetchUnique ( )
static

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

Returns
filename with path of a saved file. You can use this filename to get contents of file from filesystem.

Referenced by processCache().

eZDBFileHandler::fileCopy (   $srcPath,
  $dstPath 
)
static

Copy file.

eZDBFileHandler::fileDelete (   $path,
  $fnamePart = false 
)
static

Deletes specified file/directory.

If a directory specified it is deleted recursively.

eZDBFileHandler::fileDeleteByDirList (   $dirList,
  $commonPath,
  $commonSuffix 
)
static
eZDBFileHandler::fileDeleteByRegex (   $dir,
  $fileRegex 
)
static
eZDBFileHandler::fileDeleteByWildcard (   $wildcard)
static
eZDBFileHandler::fileDeleteLocal (   $path)
static

Deletes a file that has been fetched before.

Referenced by processCache().

eZDBFileHandler::fileExists (   $path)
static

Check if given file/dir exists.

eZDBFileHandler::fileFetch (   $filePath)
static

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

eZDBFileHandler::fileFetchContents (   $filePath)
static

Returns file contents.

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

Create symbolic or hard link to file.

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

Move file.

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

Stores a local file to the cluster.

Parameters
string$filePathPath to the file being stored.
string$scopeFile scope. Used to group similar files together. Examples: image, template-block...
string$deletetrue if the file should be deleted after storing.
string$datatypeFile mime type
Returns
void
eZDBFileHandler::fileStoreContents (   $filePath,
  $contents,
  $scope = false,
  $datatype = false 
)

Store file contents.

Parameters
string$filePath
mixed$contents
string$scope
string$datatype
Returns
void
eZDBFileHandler::getFileList (   $scopes = false,
  $excludeScopes = false 
)

Get list of files stored in database.

Used in bin/php/clusterize.php.

Parameters
array$scopesreturn only files that belong to any of these scopes
boolean$excludeScopesif true, then reverse the meaning of $scopes, which is return only files that do not belong to any of the scopes listed in $scopes
eZDBFileHandler::isDBFileExpired (   $expiry,
  $curtime,
  $ttl 
)

Calculates if the DB file 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().

eZDBFileHandler::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
static eZDBFileHandler::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

Referenced by eZDBFileHandlerMysqlBackend\_verifyExclusiveLock(), and eZDBFileHandlerMysqliBackend\_verifyExclusiveLock().

eZDBFileHandler::isLocalFileExpired (   $expiry,
  $curtime,
  $ttl 
)

Calculates if the local file 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().

eZDBFileHandler::loadMetaData (   $force = false)

Load file meta information from the database.

Parameters
bool$forceFile stats will be refreshed if true

Referenced by __get(), isExpired(), mtime(), passthrough(), processCache(), size(), and stat().

eZDBFileHandler::move (   $dstPath)

Move file.

eZDBFileHandler::mtime ( )

Returns file modification time.

eZDBFileHandler::name ( )

Returns file name.

eZDBFileHandler::passthrough ( )

Outputs file contents prepending them with appropriate HTTP headers.

Deprecated:
This function should not be used since it cannot handle reading errors. For the PHP 5 port this should be removed.
eZDBFileHandler::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.

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 controlling how long a cache entry can be used the parameters $expiry and $ttl is used.

Parameters
mixed$retrieveCallbackThe $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. Set 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().
mixed$generateCallbackused 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. Set to false to disable generation callback. 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.
mixed$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.
mixed$expiry$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.
mixed$extraDataExtra parameters to be sent to $generateCallback and $retrieveCallback
Returns
array 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.

Referenced by processFile().

eZDBFileHandler::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.
eZDBFileHandler::purge (   $printCallback = false,
  $microsleep = false,
  $max = false,
  $expiry = false 
)

Purge local and remote file data for current file.

eZDBFileHandler::requiresBinaryPurge ( )

eZDB does require binary purge.

It does store files in DB and therefore doesn't remove files in real time

Since
4.3.0
Deprecated:
Deprecated as of 4.5, use eZDBFileHandler::requiresPurge() instead.
Returns
bool
eZDBFileHandler::requiresClusterizing ( )

Since eZDB uses the database, running clusterize.php is required.

Returns
bool
eZDBFileHandler::requiresPurge ( )

eZDB does require binary purge.

It does store files in DB and therefore doesn't remove files in real time

Since
4.5.0
Returns
bool
eZDBFileHandler::size ( )

Returns file size.

eZDBFileHandler::startCacheGeneration ( )

Starts cache generation for the current file.

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

Returns
bool false if the file is being generated, true if it is not

Referenced by processCache().

eZDBFileHandler::stat ( )

Returns file metadata.

eZDBFileHandler::storeCache (   $fileData)

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

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.

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

Referenced by processCache().

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

Store file contents.

Parameters
$storeLocallyIf true the file will also be stored on the local file system.

Referenced by storeCache().

Member Data Documentation

eZDBFileHandler::$_metaData = null

holds the file's metaData loaded from database The variable's type indicates the exact status:

  • null: means that metaData have not been loaded yet
  • false: means that metaData were loaded but the file was not found in DB
  • array: metaData have been loaded and file exists
    Todo:
    refactor to a magic property:

Referenced by __get().

eZDBFileHandler::$backend
eZDBFileHandler::$backendVerify
eZDBFileHandler::$generationStartTimestamp = false
protected
eZDBFileHandler::$nonExistantStaleCacheHandling
protected

Holds the preferences used when stale cache is activated and no expired file is available.

This is loaded from file.ini, ClusteringSettings.NonExistantStaleCacheHandling

eZDBFileHandler::$realFilePath = null

holds the real file path.

This is only used when we are generating a cache file, in which case $filePath holds the generating cache file name, and $realFilePath holds the real name

Referenced by abortCacheGeneration(), and endCacheGeneration().

eZDBFileHandler::$remainingCacheGenerationTime = false
protected
eZDBFileHandler::$useStaleCache = false
protected
const eZDBFileHandler::INFOCACHE_MAX = 200

Controls the maximum number of metdata entries to keep in memory for this request. If the limit is reached the least used entries are removed.

const eZDBFileHandler::LOCAL_CACHE = 1

Controls whether file data from database is cached on the local filesystem.

Note
This is primarily available for debugging purposes.

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