eZ Publish  [trunk]
ezbinaryfile.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZBinaryFile class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 /*!
00012   \class eZBinaryFile ezbinaryfile.php
00013   \ingroup eZDatatype
00014   \brief The class eZBinaryFile handles registered binaryfiles
00015 
00016 */
00017 
00018 class eZBinaryFile extends eZPersistentObject
00019 {
00020     function eZBinaryFile( $row )
00021     {
00022         $this->eZPersistentObject( $row );
00023     }
00024 
00025     static function definition()
00026     {
00027         static $definition = array( 'fields' => array( 'contentobject_attribute_id' => array( 'name' => 'ContentObjectAttributeID',
00028                                                                                 'datatype' => 'integer',
00029                                                                                 'default' => 0,
00030                                                                                 'required' => true,
00031                                                                                 'foreign_class' => 'eZContentObjectAttribute',
00032                                                                                 'foreign_attribute' => 'id',
00033                                                                                 'multiplicity' => '1..*' ),
00034                                          'version' => array( 'name' => 'Version',
00035                                                              'datatype' => 'integer',
00036                                                              'default' => 0,
00037                                                              'required' => true ),
00038                                          'filename' =>  array( 'name' => 'Filename',
00039                                                                'datatype' => 'string',
00040                                                                'default' => '',
00041                                                                'required' => true ),
00042                                          'original_filename' =>  array( 'name' => 'OriginalFilename',
00043                                                                         'datatype' => 'string',
00044                                                                         'default' => '',
00045                                                                         'required' => true ),
00046                                          'mime_type' => array( 'name' => 'MimeType',
00047                                                                'datatype' => 'string',
00048                                                                'default' => '',
00049                                                                'required' => true ),
00050                                          'download_count' => array( 'name' => 'DownloadCount',
00051                                                                     'datatype' => 'integer',
00052                                                                     'default' => 0,
00053                                                                     'required' => true ) ),
00054                       'keys' => array( 'contentobject_attribute_id', 'version' ),
00055                       'relations' => array( 'contentobject_attribute_id' => array( 'class' => 'ezcontentobjectattribute',
00056                                                                                    'field' => 'id' ) ),
00057                       "function_attributes" => array( 'filesize' => 'fileSize',
00058                                                       'filepath' => 'filePath',
00059                                                       'mime_type_category' => 'mimeTypeCategory',
00060                                                       'mime_type_part' => 'mimeTypePart' ),
00061                       'class_name' => 'eZBinaryFile',
00062                       'name' => 'ezbinaryfile' );
00063         return $definition;
00064     }
00065 
00066 
00067     function fileSize()
00068     {
00069         $fileInfo = $this->storedFileInfo();
00070 
00071         $file = eZClusterFileHandler::instance( $fileInfo['filepath'] );
00072         if ( $file->exists() )
00073         {
00074             $stat = $file->stat();
00075             return $stat['size'];
00076         }
00077 
00078         return 0;
00079     }
00080 
00081     function filePath()
00082     {
00083         $fileInfo = $this->storedFileInfo();
00084         return $fileInfo['filepath'];
00085     }
00086 
00087     function mimeTypeCategory()
00088     {
00089         $types = explode( '/', eZPersistentObject::attribute( 'mime_type' ) );
00090         return $types[0];
00091     }
00092 
00093     function mimeTypePart()
00094     {
00095         $types = explode( '/', eZPersistentObject::attribute( 'mime_type' ) );
00096         return $types[1];
00097     }
00098 
00099     static function create( $contentObjectAttributeID, $version )
00100     {
00101         $row = array( 'contentobject_attribute_id' => $contentObjectAttributeID,
00102                       'version' => $version,
00103                       'filename' => '',
00104                       'original_filename' => '',
00105                       'mime_type' => ''
00106                       );
00107         return new eZBinaryFile( $row );
00108     }
00109 
00110     static function fetch( $id, $version = null, $asObject = true )
00111     {
00112         if ( $version == null )
00113         {
00114             return eZPersistentObject::fetchObjectList( eZBinaryFile::definition(),
00115                                                         null,
00116                                                         array( 'contentobject_attribute_id' => $id ),
00117                                                         null,
00118                                                         null,
00119                                                         $asObject );
00120         }
00121         else
00122         {
00123             return eZPersistentObject::fetchObject( eZBinaryFile::definition(),
00124                                                     null,
00125                                                     array( 'contentobject_attribute_id' => $id,
00126                                                            'version' => $version ),
00127                                                     $asObject );
00128         }
00129     }
00130 
00131     static function fetchByFileName( $filename, $version = null, $asObject = true )
00132     {
00133         if ( $version == null )
00134         {
00135             return eZPersistentObject::fetchObjectList( eZBinaryFile::definition(),
00136                                                         null,
00137                                                         array( 'filename' => $filename ),
00138                                                         null,
00139                                                         null,
00140                                                         $asObject );
00141         }
00142         else
00143         {
00144             return eZPersistentObject::fetchObject( eZBinaryFile::definition(),
00145                                                     null,
00146                                                     array( 'filename' => $filename,
00147                                                            'version' => $version ),
00148                                                     $asObject );
00149         }
00150     }
00151 
00152     static function removeByID( $id, $version )
00153     {
00154         if ( $version == null )
00155         {
00156             eZPersistentObject::removeObject( eZBinaryFile::definition(),
00157                                               array( 'contentobject_attribute_id' => $id ) );
00158         }
00159         else
00160         {
00161             eZPersistentObject::removeObject( eZBinaryFile::definition(),
00162                                               array( 'contentobject_attribute_id' => $id,
00163                                                      'version' => $version ) );
00164         }
00165     }
00166 
00167 
00168     /*!
00169      \return the medatata from the binary file, if extraction is supported
00170       for the current mimetype.
00171     */
00172     function metaData()
00173     {
00174         $metaData = "";
00175         $binaryINI = eZINI::instance( 'binaryfile.ini' );
00176 
00177         $handlerSettings = $binaryINI->variable( 'HandlerSettings', 'MetaDataExtractor' );
00178 
00179         if ( isset( $handlerSettings[$this->MimeType] ) )
00180         {
00181             // Check if plugin exists
00182             if ( eZExtension::findExtensionType( array( 'ini-name' => 'binaryfile.ini',
00183                                                     'repository-group' => 'HandlerSettings',
00184                                                     'repository-variable' => 'Repositories',
00185                                                     'extension-group' => 'HandlerSettings',
00186                                                     'extension-variable' => 'ExtensionRepositories',
00187                                                     'type-directory' => false,
00188                                                     'type' => $handlerSettings[$this->MimeType],
00189                                                     'subdir' => 'plugins',
00190                                                     'extension-subdir' => 'plugins',
00191                                                     'suffix-name' => 'parser.php' ),
00192                                              $out ) )
00193             {
00194                 $filePath = $out['found-file-path'];
00195                 include_once( $filePath );
00196                 $class = $handlerSettings[$this->MimeType] . 'Parser';
00197 
00198                 $parserObject = new $class( );
00199                 $fileInfo = $this->storedFileInfo();
00200 
00201                 $file = eZClusterFileHandler::instance( $fileInfo['filepath'] );
00202                 if ( $file->exists() )
00203                 {
00204                     $fetchedFilePath = $file->fetchUnique();
00205                     $metaData = $parserObject->parseFile( $fetchedFilePath );
00206                     $file->fileDeleteLocal( $fetchedFilePath );
00207                 }
00208             }
00209             else
00210             {
00211                 eZDebug::writeWarning( "Plugin for $this->MimeType was not found", 'eZBinaryFile' );
00212             }
00213         }
00214         else
00215         {
00216             eZDebug::writeWarning( "Mimetype $this->MimeType not supported for indexing", 'eZBinaryFile' );
00217         }
00218 
00219         return $metaData;
00220     }
00221 
00222     function storedFileInfo()
00223     {
00224         $fileName = $this->attribute( 'filename' );
00225         $mimeType = $this->attribute( 'mime_type' );
00226         $originalFileName = $this->attribute( 'original_filename' );
00227         $storageDir = eZSys::storageDirectory();
00228         list( $group, $type ) = explode( '/', $mimeType );
00229         $filePath = $storageDir . '/original/' . $group . '/' . $fileName;
00230         return array( 'filename' => $fileName,
00231                       'original_filename' => $originalFileName,
00232                       'filepath' => $filePath,
00233                       'mime_type' => $mimeType );
00234     }
00235 
00236     public $ContentObjectAttributeID;
00237     public $Filename;
00238     public $OriginalFilename;
00239     public $MimeType;
00240 }
00241 
00242 ?>