| 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 |
abstract class MWP_Action_IncrementalBackup_AbstractTablesAction extends MWP_Action_IncrementalBackup_Abstract |
| 12 |
{ |
| 13 |
const METHOD_MYSQLDUMP = 'mysqldump'; |
| 14 |
const METHOD_PHPDUMPER = 'phpdumper'; |
| 15 |
|
| 16 |
/** |
| 17 |
* @param MWP_IncrementalBackup_Database_DumperInterface $dumper |
| 18 |
* @param array $params |
| 19 |
* @param array $tables |
| 20 |
* |
| 21 |
* @return mixed |
| 22 |
*/ |
| 23 |
abstract protected function executeAction(MWP_IncrementalBackup_Database_DumperInterface $dumper, array $tables = array(), array $params = array()); |
| 24 |
|
| 25 |
public function execute(array $params = array()) |
| 26 |
{ |
| 27 |
$method = $params['method']; |
| 28 |
$tables = $params['tables']; |
| 29 |
|
| 30 |
if (isset($params['options']) && is_array($params['options'])) { |
| 31 |
$options = $params['options']; |
| 32 |
} else { |
| 33 |
$options = array(); |
| 34 |
} |
| 35 |
|
| 36 |
$dumper = $this->createDumper($method, $options); |
| 37 |
|
| 38 |
return $this->executeAction($dumper, $tables, $params); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* @param array $tables |
| 43 |
* |
| 44 |
* @throws MWP_Worker_Exception |
| 45 |
*/ |
| 46 |
protected function assertTablesExist(array $tables) |
| 47 |
{ |
| 48 |
$rows = mwp_context()->getDb()->get_results("SHOW TABLES", ARRAY_N); |
| 49 |
foreach ($rows as $row) { |
| 50 |
// $row is always an array with only a single member |
| 51 |
$table = $row[0]; |
| 52 |
|
| 53 |
$index = array_search($table, $tables); |
| 54 |
if ($index !== false) { |
| 55 |
// Remove from $tables - don't worry, $tables has not been passed by reference :) |
| 56 |
array_splice($tables, $index, 1); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
// Tables which have not been found are located in $tables |
| 61 |
if (count($tables)) { |
| 62 |
throw new MWP_Worker_Exception(MWP_Worker_Exception::BACKUP_DATABASE_MISSING_TABLES, "Some tables are missing", array( |
| 63 |
// Return an array of missing tables |
| 64 |
'tables' => $tables, |
| 65 |
)); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @param string $method |
| 71 |
* |
| 72 |
* @param array $options |
| 73 |
* |
| 74 |
* @throws MWP_Worker_Exception |
| 75 |
* @return MWP_IncrementalBackup_Database_DumperInterface |
| 76 |
*/ |
| 77 |
protected function createDumper($method, array $options = array()) |
| 78 |
{ |
| 79 |
$dumperOptions = MWP_IncrementalBackup_Database_DumpOptions::createFromArray($options); |
| 80 |
|
| 81 |
switch ($method) { |
| 82 |
case self::METHOD_MYSQLDUMP: |
| 83 |
$configuration = MWP_IncrementalBackup_Database_Configuration::createFromWordPressContext(mwp_context()); |
| 84 |
$dumper = new MWP_IncrementalBackup_Database_MysqlDumpDumper($configuration, $dumperOptions); |
| 85 |
|
| 86 |
return $dumper; |
| 87 |
case self::METHOD_PHPDUMPER: |
| 88 |
$configuration = MWP_IncrementalBackup_Database_Configuration::createFromWordPressContext(mwp_context()); |
| 89 |
$dumper = new MWP_IncrementalBackup_Database_PhpDumper($configuration, mwp_container()->getSystemEnvironment(), $dumperOptions); |
| 90 |
|
| 91 |
return $dumper; |
| 92 |
default: |
| 93 |
throw new MWP_Worker_Exception(MWP_Worker_Exception::BACKUP_DATABASE_METHOD_NOT_AVAILABLE); |
| 94 |
break; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|