| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPSynchro\Status; |
| 4 |
|
| 5 |
use WPSynchro\Migration\Job; |
| 6 |
use WPSynchro\Migration\MigrationFactory; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class for serving migration status to the frontend |
| 10 |
* Called from status service |
| 11 |
* |
| 12 |
*/ |
| 13 |
class MigrateStatus |
| 14 |
{ |
| 15 |
// Data id's |
| 16 |
public $migration_id; |
| 17 |
public $job_id; |
| 18 |
// Objects |
| 19 |
public $job = null; |
| 20 |
public $migration = null; |
| 21 |
// What are we doing |
| 22 |
public $db_backup = false; |
| 23 |
public $database_sync = false; |
| 24 |
public $files_sync = false; |
| 25 |
// Stages |
| 26 |
public $stages = null; |
| 27 |
|
| 28 |
/** |
| 29 |
* Setup the data needed for migration status thread |
| 30 |
* @codeCoverageIgnore |
| 31 |
*/ |
| 32 |
public function setup($migration_id, $job_id) |
| 33 |
{ |
| 34 |
$this->migration_id = $migration_id; |
| 35 |
$this->job_id = $job_id; |
| 36 |
|
| 37 |
// Get job data |
| 38 |
$this->job = new Job(); |
| 39 |
$this->job->load($this->migration_id, $this->job_id); |
| 40 |
|
| 41 |
// Get migration |
| 42 |
$migrationFactory = MigrationFactory::getInstance(); |
| 43 |
$this->migration = $migrationFactory->retrieveMigration($this->migration_id); |
| 44 |
|
| 45 |
$this->setupStages(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get default sync stages |
| 50 |
*/ |
| 51 |
public function setupStages() |
| 52 |
{ |
| 53 |
/** |
| 54 |
* Figure out what we actually doing |
| 55 |
*/ |
| 56 |
if (isset($this->migration->sync_database) && $this->migration->sync_database && isset($this->migration->db_make_backup) && $this->migration->db_make_backup) { |
| 57 |
$this->db_backup = true; |
| 58 |
} |
| 59 |
if (isset($this->migration->sync_database) && $this->migration->sync_database) { |
| 60 |
$this->database_sync = true; |
| 61 |
} |
| 62 |
if (isset($this->migration->sync_files) && $this->migration->sync_files) { |
| 63 |
$this->files_sync = true; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Stages |
| 68 |
*/ |
| 69 |
$this->stages = []; |
| 70 |
|
| 71 |
// Initiate |
| 72 |
$this->stages[] = $this->createMigrationStage("initialize", __("Initialize", "wpsynchro"), __("Initiating migration on source and target and sets up security tokens on both ends", "wpsynchro")); |
| 73 |
|
| 74 |
// Masterdata |
| 75 |
$this->stages[] = $this->createMigrationStage("masterdata", __("Masterdata", "wpsynchro"), __("Fetching masterdata on both source and target and check that we are ready to migrate", "wpsynchro")); |
| 76 |
|
| 77 |
// Database backup |
| 78 |
if ($this->db_backup) { |
| 79 |
$this->stages[] = $this->createMigrationStage("databasebackup", __("Database backup", "wpsynchro"), __("Backup of database tables that will be changed by database sync. Backup location can be found in the log file, which can be found in the 'Logs' menu", "wpsynchro")); |
| 80 |
} |
| 81 |
|
| 82 |
// Database sync |
| 83 |
if ($this->database_sync) { |
| 84 |
$this->stages[] = $this->createMigrationStage("databasesync", __('Migrate database', 'wpsynchro'), __("Migrate the database, moving the database table rows to the target", "wpsynchro")); |
| 85 |
} |
| 86 |
|
| 87 |
// Files sync |
| 88 |
if ($this->files_sync) { |
| 89 |
$this->stages[] = $this->createMigrationStage("filessync", __('Migrate files', 'wpsynchro'), __("Migrate the files, by comparing and transferring the missing files", "wpsynchro")); |
| 90 |
} |
| 91 |
|
| 92 |
// Finalize |
| 93 |
$this->stages[] = $this->createMigrationStage("finalize", __('Finalizing', 'wpsynchro'), __("Completes the migration by doing the last few steps and cleaning up", "wpsynchro")); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Set status for stage id |
| 98 |
*/ |
| 99 |
public function setStatus($id, $percent_complete, $status_text) |
| 100 |
{ |
| 101 |
foreach ($this->stages as $stage) { |
| 102 |
if ($stage->id == $id) { |
| 103 |
$stage->percent_complete = $percent_complete; |
| 104 |
$stage->status_text = $status_text; |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get stages |
| 112 |
*/ |
| 113 |
public function getStages() |
| 114 |
{ |
| 115 |
return $this->stages; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Create migration stage object |
| 120 |
*/ |
| 121 |
public function createMigrationStage($id, $title, $help_text = "") |
| 122 |
{ |
| 123 |
$temp = new \stdClass(); |
| 124 |
$temp->id = $id; |
| 125 |
$temp->title = $title; |
| 126 |
$temp->help_text = $help_text; |
| 127 |
$temp->percent_complete = 0; |
| 128 |
$temp->status_text = ""; |
| 129 |
return $temp; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Get migration status |
| 134 |
*/ |
| 135 |
public function getMigrationStatus() |
| 136 |
{ |
| 137 |
|
| 138 |
// Initialize |
| 139 |
$this->setStatus("initialize", ($this->job->initiation_completed ? 100 : 10), ""); |
| 140 |
// Metadata |
| 141 |
$this->setStatus("masterdata", $this->job->masterdata_progress, ""); |
| 142 |
// Database backup |
| 143 |
$this->setStatus("databasebackup", $this->job->database_backup_progress, $this->job->database_backup_progress_description); |
| 144 |
// Database |
| 145 |
$this->setStatus("databasesync", $this->job->database_progress, $this->job->database_progress_description); |
| 146 |
// Files |
| 147 |
$this->setStatus("filessync", $this->job->files_progress, $this->job->files_progress_description); |
| 148 |
// Finalize |
| 149 |
$this->setStatus("finalize", $this->job->finalize_progress, $this->job->finalize_progress_description); |
| 150 |
|
| 151 |
// User confirms - stuff we want confirmed by user |
| 152 |
$user_confirms = []; |
| 153 |
if ($this->job->files_ready_for_user_confirm && !$this->job->files_user_confirmed_actions) { |
| 154 |
$user_confirms['confirmFileActions'] = true; |
| 155 |
} |
| 156 |
|
| 157 |
// Check if PHP process may have stopped, due to errors, but only if we are not waiting for any confirms |
| 158 |
if (count($user_confirms) == 0) { |
| 159 |
if ($this->job->run_lock_problem_time > 0 && $this->job->run_lock_problem_time < time()) { |
| 160 |
$this->job->errors[] = __("The migration process seem to have problems - It may be PHP errors - please check the PHP logs", "wpsynchro"); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
// Set results |
| 165 |
$result = new \stdClass(); |
| 166 |
$result->is_completed = $this->job->is_completed; |
| 167 |
$result->stages = $this->stages; |
| 168 |
$result->errors = $this->job->errors; |
| 169 |
$result->warnings = $this->job->warnings; |
| 170 |
$result->userConfirms = $user_confirms; |
| 171 |
|
| 172 |
return $result; |
| 173 |
} |
| 174 |
} |
| 175 |
|