| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <[email protected]> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
class MWP_Action_IncrementalBackup_StreamTables extends MWP_Action_IncrementalBackup_AbstractTablesAction |
| 12 |
{ |
| 13 |
/** |
| 14 |
* {@inheritdoc} |
| 15 |
*/ |
| 16 |
protected function executeAction(MWP_IncrementalBackup_Database_DumperInterface $dumper, array $tables = array(), array $params = array()) |
| 17 |
{ |
| 18 |
$this->assertTablesExist($tables); |
| 19 |
|
| 20 |
try { |
| 21 |
$createSeparateStreams = isset($params['options']['create_separate_streams']) ? $params['options']['create_separate_streams'] : null; |
| 22 |
$encoding = isset($params['options']['encoding']) ? $params['options']['encoding'] : 'binary'; |
| 23 |
|
| 24 |
if ($createSeparateStreams === true) { |
| 25 |
$result = $this->dumpToMultipleStreams($dumper, $tables); |
| 26 |
} else { |
| 27 |
$result = $this->dumpToSingleStream($dumper, $tables); |
| 28 |
} |
| 29 |
|
| 30 |
foreach ($result->getFiles() as $file) { |
| 31 |
$file->setEncoding($encoding); |
| 32 |
} |
| 33 |
|
| 34 |
return $result; |
| 35 |
} catch (Exception $e) { |
| 36 |
if ($e instanceof MWP_Worker_Exception) { |
| 37 |
throw $e; |
| 38 |
} |
| 39 |
|
| 40 |
// Convert any other exception to MWP_Worker_Exception |
| 41 |
throw new MWP_Worker_Exception(MWP_Worker_Exception::BACKUP_DATABASE_FAILED, $e->getMessage(), array( |
| 42 |
'code' => $e->getCode(), |
| 43 |
'message' => $e->getMessage(), |
| 44 |
'type' => get_class($e), |
| 45 |
)); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @param MWP_IncrementalBackup_Database_DumperInterface $dumper |
| 51 |
* @param array $tables |
| 52 |
* |
| 53 |
* @return MWP_IncrementalBackup_Model_FetchFilesResult |
| 54 |
*/ |
| 55 |
protected function dumpToMultipleStreams(MWP_IncrementalBackup_Database_DumperInterface $dumper, array $tables) |
| 56 |
{ |
| 57 |
$result = new MWP_IncrementalBackup_Model_FetchFilesResult(); |
| 58 |
$result->setServerStatistics(MWP_IncrementalBackup_Model_ServerStatistics::factory()); |
| 59 |
|
| 60 |
foreach ($tables as $table) { |
| 61 |
$stream = $dumper->createStream(array($table)); |
| 62 |
|
| 63 |
$file = new MWP_IncrementalBackup_Model_File(); |
| 64 |
$file->setPathname($table); |
| 65 |
$file->setStream($stream); |
| 66 |
$result->addFile($file); |
| 67 |
} |
| 68 |
|
| 69 |
return $result; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* @param MWP_IncrementalBackup_Database_DumperInterface $dumper |
| 74 |
* @param array $tables |
| 75 |
* |
| 76 |
* @return MWP_IncrementalBackup_Model_FetchFilesResult |
| 77 |
*/ |
| 78 |
protected function dumpToSingleStream(MWP_IncrementalBackup_Database_DumperInterface $dumper, array $tables) |
| 79 |
{ |
| 80 |
$stream = $dumper->createStream($tables); |
| 81 |
|
| 82 |
$file = new MWP_IncrementalBackup_Model_File(); |
| 83 |
$file->setPathname('tables.sql'); |
| 84 |
$file->setStream($stream); |
| 85 |
|
| 86 |
$result = new MWP_IncrementalBackup_Model_FetchFilesResult(); |
| 87 |
$result->setServerStatistics(MWP_IncrementalBackup_Model_ServerStatistics::factory()); |
| 88 |
$result->setFiles(array($file)); |
| 89 |
|
| 90 |
return $result; |
| 91 |
} |
| 92 |
} |
| 93 |
|