Mage_Backup_Model_Backup Class Reference

Inheritance diagram for Mage_Backup_Model_Backup:

Varien_Object

List of all members.

Public Member Functions

 load ($fileName, $filePath)
 exists ()
 getFileName ()
 setType ($value='db')
 getType ()
 setFile (&$content)
getFile ()
 deleteFile ()
 open ($write=false)
 read ($length)
 eof ()
 write ($string)
 close ()
 output ()
 getSize ()

Public Attributes

const BACKUP_DB = 'db'
const BACKUP_VIEW = 'view'
const BACKUP_MEDIA = 'media'
const BACKUP_EXTENSION = 'gz'
const COMPRESS_RATE = 9

Protected Attributes

 $_handler = null


Detailed Description

Definition at line 34 of file Backup.php.


Member Function Documentation

close (  ) 

Close open backup file

Returns:
Mage_Backup_Model_Backup

Definition at line 313 of file Backup.php.

00314     {
00315         @gzclose($this->_handler);
00316         $this->_handler = null;
00317 
00318         return $this;
00319     }

deleteFile (  ) 

Delete backup file

Exceptions:
Mage_Backup_Exception 

Definition at line 209 of file Backup.php.

00210     {
00211         if (!$this->exists()) {
00212             Mage::throwException(Mage::helper('backup')->__("Backup file doesn't exist"));
00213         }
00214 
00215         $ioProxy = new Varien_Io_File();
00216         $ioProxy->open(array('path'=>$this->getPath()));
00217         $ioProxy->rm($this->getFileName());
00218         return $this;
00219     }

eof (  ) 

Definition at line 277 of file Backup.php.

00278     {
00279         if (is_null($this->_handler)) {
00280             Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file handler don\'t specify'));
00281         }
00282 
00283         return gzeof($this->_handler);
00284     }

exists (  ) 

Checks backup file exists.

Returns:
boolean

Definition at line 84 of file Backup.php.

00085     {
00086         return is_file($this->getPath() . DS . $this->getFileName());
00087     }

& getFile (  ) 

Return content of backup file

Todo:
rewrite to Varien_IO, but there no possibility read part of files.
Returns:
string
Exceptions:
Mage_Backup_Exception 

Definition at line 168 of file Backup.php.

00169     {
00170 
00171         if (!$this->exists()) {
00172             Mage::throwException(Mage::helper('backup')->__("Backup file doesn't exist"));
00173         }
00174 
00175         $fResource = @fopen($this->getPath() . DS . $this->getFileName(), "rb");
00176         if (!$fResource) {
00177             Mage::throwException(Mage::helper('backup')->__("Cannot read backup file"));
00178         }
00179 
00180         $content = '';
00181         $compressed = 0;
00182 
00183         $info = unpack("lcompress/llength", fread($fResource, 8));
00184         if ($info['compress']) { // If file compressed by zlib
00185             $compressed = 1;
00186         }
00187 
00188         if ($compressed && !extension_loaded("zlib")) {
00189             fclose($fResource);
00190             Mage::throwException(Mage::helper('backup')->__('File compressed with Zlib, but this extension is not installed on server'));
00191         }
00192 
00193         if ($compressed) {
00194             $content = gzuncompress(fread($fResource, $info['length']));
00195         } else {
00196             $content = fread($fResource, $info['length']);
00197         }
00198 
00199         fclose($fResource);
00200 
00201         return $content;
00202     }

getFileName (  ) 

Return file name of backup file

Returns:
string

Definition at line 94 of file Backup.php.

00095     {
00096         return $this->getTime() . "_" . $this->getType()
00097                . "." . self::BACKUP_EXTENSION;
00098     }

getSize (  ) 

Definition at line 341 of file Backup.php.

00342     {
00343         if (!is_null($this->getData('size'))) {
00344             return $this->getData('size');
00345         }
00346 
00347         if ($this->exists()) {
00348             $this->setData('size', filesize($this->getPath() . DS . $this->getFileName()));
00349             return $this->getData('size');
00350         }
00351 
00352         return 0;
00353     }

getType (  ) 

Returns type of backup file

Returns:
string db|media|view

Definition at line 122 of file Backup.php.

00123     {
00124         return $this->_type;
00125     }

load ( fileName,
filePath 
)

Load backup file info

Parameters:
string fileName
string filePath
Returns:
Mage_Backup_Model_Backup

Definition at line 66 of file Backup.php.

00067     {
00068         list ($time, $type) = explode("_", substr($fileName, 0, strrpos($fileName, ".")));
00069         $this->addData(array(
00070             'id'   => $filePath . DS . $fileName,
00071             'time' => (int)$time,
00072             'path' => $filePath,
00073             'time_formated' => date('Y-m-d H:i:s', (int)$time))
00074         );
00075         $this->setType($type);
00076         return $this;
00077     }

open ( write = false  ) 

Open backup file (write or read mode)

Parameters:
bool $write
Returns:
Mage_Backup_Model_Backup

Definition at line 227 of file Backup.php.

00228     {
00229         if (is_null($this->getPath())) {
00230             Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file path don\'t specify'));
00231         }
00232 
00233         $ioAdapter = new Varien_Io_File();
00234         try {
00235             $path = $ioAdapter->getCleanPath($this->getPath());
00236             $ioAdapter->checkAndCreateFolder($path);
00237             $filePath = $path . DS . $this->getFileName();
00238         }
00239         catch (Exception $e) {
00240             Mage::exception('Mage_Backup', $e->getMessage());
00241         }
00242 
00243         if ($write && $ioAdapter->fileExists($filePath)) {
00244             $ioAdapter->rm($filePath);
00245         }
00246         if (!$write && !$ioAdapter->fileExists($filePath)) {
00247             Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file "%s" doesn\'t exist', $this->getFileName()));
00248         }
00249 
00250         $mode = $write ? 'wb' . self::COMPRESS_RATE : 'rb';
00251 
00252         try {
00253             $this->_handler = gzopen($filePath, $mode);
00254         }
00255         catch (Exception $e) {
00256             Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file "%s" can\'t read or write', $this->getFileName()));
00257         }
00258 
00259         return $this;
00260     }

output (  ) 

Print output

Definition at line 325 of file Backup.php.

00326     {
00327         if (!$this->exists()) {
00328             return ;
00329         }
00330 
00331         $ioAdapter = new Varien_Io_File();
00332         $ioAdapter->open(array('path' => $this->getPath()));
00333 
00334         $ioAdapter->streamOpen($this->getFileName(), 'r');
00335         while ($buffer = $ioAdapter->streamRead()) {
00336             echo $buffer;
00337         }
00338         $ioAdapter->streamClose();
00339     }

read ( length  ) 

Read backup uncomressed data

Parameters:
int $length
Returns:
string

Definition at line 268 of file Backup.php.

00269     {
00270         if (is_null($this->_handler)) {
00271             Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file handler don\'t specify'));
00272         }
00273 
00274         return gzread($this->_handler, $length);
00275     }

setFile ( &$  content  ) 

Set the backup file content

Parameters:
string $content
Returns:
Mage_Backup_Model_Backup
Exceptions:
Mage_Backup_Exception 

Definition at line 134 of file Backup.php.

00135     {
00136         if (!$this->hasData('time') || !$this->hasData('type') || !$this->hasData('path')) {
00137             Mage::throwException(Mage::helper('backup')->__('Wrong order of creation for new backup'));
00138         }
00139 
00140         $ioProxy = new Varien_Io_File();
00141         $ioProxy->setAllowCreateFolders(true);
00142         $ioProxy->open(array('path'=>$this->getPath()));
00143 
00144         $compress = 0;
00145         if (extension_loaded("zlib")) {
00146             $compress = 1;
00147         }
00148 
00149         $rawContent = '';
00150         if ( $compress ) {
00151             $rawContent = gzcompress( $content, self::COMPRESS_RATE );
00152         } else {
00153             $rawContent = $content;
00154         }
00155 
00156         $fileHeaders = pack("ll", $compress, strlen($rawContent));
00157         $ioProxy->write($this->getFileName(), $fileHeaders . $rawContent);
00158         return $this;
00159     }

setType ( value = 'db'  ) 

Sets type of file

Parameters:
string $value db|media|view

Definition at line 105 of file Backup.php.

00106     {
00107         if(!in_array($value, array('db','media','view'))) {
00108             $value = 'db';
00109         }
00110 
00111         $this->_type = $value;
00112         $this->setData('type', $this->_type);
00113 
00114         return $this;
00115     }

write ( string  ) 

Write to backup file

Parameters:
string $string
Returns:
Mage_Backup_Model_Backup

Definition at line 292 of file Backup.php.

00293     {
00294         if (is_null($this->_handler)) {
00295             Mage::exception('Mage_Backup', Mage::helper('backup')->__('Backup file handler don\'t specify'));
00296         }
00297 
00298         try {
00299             gzwrite($this->_handler, $string);
00300         }
00301         catch (Exception $e) {
00302             Mage::exception('Mage_Backup', Mage::helper('backup')->__('Error write to Backup file "%s"', $this->getFileName()));
00303         }
00304 
00305         return $this;
00306     }


Member Data Documentation

$_handler = null [protected]

Definition at line 57 of file Backup.php.

const BACKUP_DB = 'db'

Definition at line 37 of file Backup.php.

const BACKUP_EXTENSION = 'gz'

Definition at line 42 of file Backup.php.

const BACKUP_MEDIA = 'media'

Definition at line 39 of file Backup.php.

const BACKUP_VIEW = 'view'

Definition at line 38 of file Backup.php.

const COMPRESS_RATE = 9

Definition at line 43 of file Backup.php.


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

Generated on Sat Jul 4 17:23:33 2009 for Magento by  doxygen 1.5.8