Cleaners
5 years ago
Exceptions
5 years ago
Cancel.php
2 years ago
CancelUpdate.php
2 years ago
Cloning.php
2 years ago
CloningProcess.php
2 years ago
Data.php
2 years ago
Database.php
2 years ago
Delete.php
2 years ago
Directories.php
2 years ago
Files.php
2 years ago
Finish.php
2 years ago
Job.php
2 years ago
JobExecutable.php
2 years ago
Logs.php
3 years ago
PreserveDataFirstStep.php
3 years ago
PreserveDataSecondStep.php
3 years ago
ProcessLock.php
2 years ago
Scan.php
2 years ago
SearchReplace.php
2 years ago
TotalStepsAreNumberOfTables.php
5 years ago
Updating.php
2 years ago
CloningProcess.php
96 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backend\Modules\Jobs; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | |
| 7 | abstract class CloningProcess extends JobExecutable |
| 8 | { |
| 9 | /** |
| 10 | * Can be local or external \wpdb object |
| 11 | * @var \wpdb |
| 12 | */ |
| 13 | protected $stagingDb; |
| 14 | |
| 15 | /** |
| 16 | * Always be the local \wpdb object |
| 17 | * @var \wpdb |
| 18 | */ |
| 19 | protected $productionDb; |
| 20 | |
| 21 | /** |
| 22 | * @return void |
| 23 | */ |
| 24 | protected function setupMemoryExhaustFile() |
| 25 | { |
| 26 | $this->memoryExhaustErrorTmpFile = $this->getMemoryExhaustErrorTmpFile(Cloning::WPSTG_REQUEST); |
| 27 | } |
| 28 | |
| 29 | protected function initializeDbObjects() |
| 30 | { |
| 31 | $this->productionDb = WPStaging::getInstance()->get("wpdb"); |
| 32 | |
| 33 | if ($this->isExternalDatabase()) { |
| 34 | $this->setExternalDatabase(); |
| 35 | } else { |
| 36 | $this->setLocalDatabase(); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | protected function setLocalDatabase() |
| 41 | { |
| 42 | $this->stagingDb = WPStaging::getInstance()->get("wpdb"); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @return bool |
| 47 | */ |
| 48 | protected function setExternalDatabase() |
| 49 | { |
| 50 | if ($this->options->databaseSsl && !defined('MYSQL_CLIENT_FLAGS')) { |
| 51 | // phpcs:disable PHPCompatibility.Constants.NewConstants.mysqli_client_ssl_dont_verify_server_certFound |
| 52 | define('MYSQL_CLIENT_FLAGS', MYSQLI_CLIENT_SSL | MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT); |
| 53 | } |
| 54 | |
| 55 | $this->stagingDb = new \wpdb($this->options->databaseUser, str_replace("\\\\", "\\", $this->options->databasePassword), $this->options->databaseDatabase, $this->options->databaseServer); |
| 56 | |
| 57 | // Check if there were any error when connecting |
| 58 | if ( |
| 59 | property_exists($this->stagingDb, 'error') && |
| 60 | $this->stagingDb->error instanceof \WP_Error |
| 61 | ) { |
| 62 | /** @var \WP_Error $wp_error */ |
| 63 | $wp_error = $this->stagingDb->error; |
| 64 | if ($wp_error->get_error_code() === 'db_connect_fail') { |
| 65 | $this->returnException(sprintf('Can not connect to external database %s. Reason: %s', $this->options->databaseDatabase, $wp_error->get_error_message())); |
| 66 | return false; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | $this->stagingDb->select($this->options->databaseDatabase); |
| 71 | if (!$this->stagingDb->ready) { |
| 72 | if ( |
| 73 | property_exists($this->stagingDb, 'error') && |
| 74 | $this->stagingDb->error instanceof \WP_Error |
| 75 | ) { |
| 76 | /** @var \WP_Error $wp_error */ |
| 77 | $wp_error = $this->stagingDb->error; |
| 78 | if ($wp_error->get_error_code() === 'db_select_fail') { |
| 79 | $this->returnException($wp_error->get_error_message()); |
| 80 | exit; |
| 81 | } |
| 82 | |
| 83 | // Generic error |
| 84 | $this->returnException(sprintf('Error: Can\'t select database %s. Either it does not exist or you don\'t have privileges to access it.', $this->options->databaseDatabase)); |
| 85 | exit; |
| 86 | } |
| 87 | |
| 88 | // Generic error |
| 89 | $this->returnException(sprintf('Error: Can\'t select database %s. Either it does not exist or you don\'t have privileges to access it.', $this->options->databaseDatabase)); |
| 90 | exit; |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | } |
| 96 |