| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class for handling the finalization of the sync |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace WPSynchro\Finalize; |
| 8 |
|
| 9 |
use WPSynchro\Files\SyncList; |
| 10 |
use WPSynchro\Database\DatabaseFinalize; |
| 11 |
use WPSynchro\Files\FinalizeFiles; |
| 12 |
use WPSynchro\Migration\MigrationController; |
| 13 |
use WPSynchro\Transport\Destination; |
| 14 |
use WPSynchro\Transport\RemoteTransport; |
| 15 |
use WPSynchro\Utilities\SyncTimerList; |
| 16 |
|
| 17 |
class FinalizeSync |
| 18 |
{ |
| 19 |
// Base data |
| 20 |
private $job = null; |
| 21 |
private $migration = null; |
| 22 |
// Dependencies |
| 23 |
private $logger; |
| 24 |
public $timer = null; |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor |
| 28 |
*/ |
| 29 |
public function __construct() |
| 30 |
{ |
| 31 |
$this->logger = MigrationController::getInstance()->getLogger(); |
| 32 |
$this->timer = SyncTimerList::getInstance(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Run finalize method |
| 37 |
*/ |
| 38 |
public function runFinalize(&$migration, &$job) |
| 39 |
{ |
| 40 |
$this->migration = &$migration; |
| 41 |
$this->job = &$job; |
| 42 |
|
| 43 |
$this->logger->log("INFO", "Starting finalize - Remaining time: " . $this->timer->getRemainingSyncTime()); |
| 44 |
|
| 45 |
$this->job->finalize_progress = 10; |
| 46 |
|
| 47 |
/** |
| 48 |
* Check what we need to do |
| 49 |
*/ |
| 50 |
if (!$this->migration->sync_files) { |
| 51 |
$this->job->finalize_files_completed = true; |
| 52 |
$this->job->finalize_progress += 45; |
| 53 |
} |
| 54 |
if (!$this->migration->sync_database) { |
| 55 |
$this->job->finalize_db_completed = true; |
| 56 |
$this->job->finalize_progress += 45; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* If we have errors, return |
| 61 |
*/ |
| 62 |
if (count($this->job->errors) > 0) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Files finalize |
| 68 |
*/ |
| 69 |
if (!$this->job->finalize_files_completed) { |
| 70 |
$this->finalizefiles(); |
| 71 |
if ($this->job->finalize_files_completed) { |
| 72 |
$this->job->finalize_progress += 45; |
| 73 |
} |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* DB finalize |
| 79 |
*/ |
| 80 |
if (!$this->job->finalize_db_completed) { |
| 81 |
$destination = new Destination(Destination::TARGET); |
| 82 |
$databasefinalize = new DatabaseFinalize(); |
| 83 |
$databasefinalize->finalize(); |
| 84 |
$this->job->finalize_progress += floor(45 * $databasefinalize->getPercentCompletedForDatabaseFinalize()); |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
$this->job->finalize_progress = 100; |
| 89 |
|
| 90 |
$this->logger->log("INFO", "Completed finalize - remaining time: " . $this->timer->getRemainingSyncTime()); |
| 91 |
|
| 92 |
if ($this->job->finalize_files_completed && $this->job->finalize_db_completed) { |
| 93 |
// Execute last actions on target |
| 94 |
$this->logger->log("INFO", "Execute last actions on target - remaining time: " . $this->timer->getRemainingSyncTime()); |
| 95 |
$this->finalizeActionsOnTarget(); |
| 96 |
$this->logger->log("INFO", "Completed last actions on target - remaining time: " . $this->timer->getRemainingSyncTime()); |
| 97 |
|
| 98 |
// Update progress |
| 99 |
$this->job->finalize_progress = 100; |
| 100 |
$this->job->finalize_completed = true; |
| 101 |
$this->job->finalize_progress_description = ""; |
| 102 |
|
| 103 |
// Update option with counted success times |
| 104 |
$success_count = get_site_option("wpsynchro_success_count", 0); |
| 105 |
$success_count++; |
| 106 |
update_site_option("wpsynchro_success_count", $success_count); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
/** |
| 112 |
* Finalize files |
| 113 |
*/ |
| 114 |
private function finalizefiles() |
| 115 |
{ |
| 116 |
$sync_list = new SyncList(); |
| 117 |
$sync_list->init($this->migration, $this->job); |
| 118 |
|
| 119 |
$finalize_files_handler = new FinalizeFiles(); |
| 120 |
$finalize_files_handler->init($sync_list, $this->migration, $this->job); |
| 121 |
|
| 122 |
$finalize_files_handler->finalizeFiles(); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Finalize actions on target |
| 127 |
*/ |
| 128 |
private function finalizeActionsOnTarget() |
| 129 |
{ |
| 130 |
|
| 131 |
$actions_to_run = ["cleartransfertoken"]; |
| 132 |
if ($this->migration->clear_cache_on_success === true) { |
| 133 |
array_unshift($actions_to_run, "clearcaches"); |
| 134 |
} |
| 135 |
|
| 136 |
$destination = new Destination(Destination::TARGET); |
| 137 |
|
| 138 |
$url = $destination->getFullURL() . '?action=wpsynchro_execute_action'; |
| 139 |
|
| 140 |
// Get remote transfer object |
| 141 |
$remotetransport = new RemoteTransport(); |
| 142 |
$remotetransport->setDestination($destination); |
| 143 |
$remotetransport->init(); |
| 144 |
$remotetransport->setUrl($url); |
| 145 |
$remotetransport->setDataObject($actions_to_run); |
| 146 |
$action_result = $remotetransport->remotePOST(); |
| 147 |
|
| 148 |
if (!$action_result->isSuccess()) { |
| 149 |
$this->job->warnings[] = __("Some finalize actions failed to run - This can be cache clearing error or other error from target site. This normally does not impact the migration that much and therefore just a warning.", "wpsynchro"); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|