UpdateRequirementsCheckTask.php
245 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Staging\Tasks\StagingSiteUpdate; |
| 4 | |
| 5 | use RuntimeException; |
| 6 | use WPStaging\Backend\Modules\SystemInfo; |
| 7 | use WPStaging\Framework\Adapter\Database; |
| 8 | use WPStaging\Framework\Adapter\Directory; |
| 9 | use WPStaging\Framework\Analytics\Actions\AnalyticsStagingUpdate; |
| 10 | use WPStaging\Framework\Filesystem\DiskWriteCheck; |
| 11 | use WPStaging\Framework\Filesystem\Filesystem; |
| 12 | use WPStaging\Framework\Queue\SeekableQueueInterface; |
| 13 | use WPStaging\Framework\Utils\Cache\Cache; |
| 14 | use WPStaging\Framework\Job\Dto\StepsDto; |
| 15 | use WPStaging\Framework\Job\Dto\TaskResponseDto; |
| 16 | use WPStaging\Framework\Job\Exception\DiskNotWritableException; |
| 17 | use WPStaging\Staging\Dto\Job\StagingSiteJobsDataDto; |
| 18 | use WPStaging\Staging\Service\StagingEngine; |
| 19 | use WPStaging\Staging\Sites; |
| 20 | use WPStaging\Staging\Tasks\StagingTask; |
| 21 | use WPStaging\Staging\Traits\WithStagingEnginePreference; |
| 22 | use WPStaging\Staging\Traits\WithStagingRequirementLogs; |
| 23 | use WPStaging\Vendor\Psr\Log\LoggerInterface; |
| 24 | |
| 25 | /** |
| 26 | * Validates prerequisites before a staging site update job starts. |
| 27 | */ |
| 28 | class UpdateRequirementsCheckTask extends StagingTask |
| 29 | { |
| 30 | use WithStagingEnginePreference; |
| 31 | use WithStagingRequirementLogs; |
| 32 | |
| 33 | /** @var Directory */ |
| 34 | protected $directory; |
| 35 | |
| 36 | /** @var Database */ |
| 37 | protected $database; |
| 38 | |
| 39 | /** @var Filesystem */ |
| 40 | protected $filesystem; |
| 41 | |
| 42 | /** @var DiskWriteCheck */ |
| 43 | protected $diskWriteCheck; |
| 44 | |
| 45 | /** @var AnalyticsStagingUpdate */ |
| 46 | protected $analyticsStagingUpdate; |
| 47 | |
| 48 | /** @var SystemInfo */ |
| 49 | protected $systemInfo; |
| 50 | |
| 51 | /** @var StagingSiteJobsDataDto $jobDataDto */ |
| 52 | protected $jobDataDto; |
| 53 | |
| 54 | /** @var Sites */ |
| 55 | protected $sites; |
| 56 | |
| 57 | public function __construct( |
| 58 | Directory $directory, |
| 59 | Database $database, |
| 60 | Filesystem $filesystem, |
| 61 | LoggerInterface $logger, |
| 62 | Cache $cache, |
| 63 | StepsDto $stepsDto, |
| 64 | SeekableQueueInterface $taskQueue, |
| 65 | DiskWriteCheck $diskWriteCheck, |
| 66 | AnalyticsStagingUpdate $analyticsStagingUpdate, |
| 67 | StagingEngine $stagingEngine, |
| 68 | SystemInfo $systemInfo, |
| 69 | Sites $sites |
| 70 | ) { |
| 71 | parent::__construct($logger, $cache, $stepsDto, $taskQueue); |
| 72 | $this->directory = $directory; |
| 73 | $this->filesystem = $filesystem; |
| 74 | $this->diskWriteCheck = $diskWriteCheck; |
| 75 | $this->analyticsStagingUpdate = $analyticsStagingUpdate; |
| 76 | $this->stagingEngine = $stagingEngine; |
| 77 | $this->systemInfo = $systemInfo; |
| 78 | $this->database = $database; |
| 79 | $this->sites = $sites; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @return string |
| 84 | */ |
| 85 | public static function getTaskName() |
| 86 | { |
| 87 | return 'staging_site_update_requirements_check'; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @return string |
| 92 | */ |
| 93 | public static function getTaskTitle() |
| 94 | { |
| 95 | return 'Requirements Check'; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @return TaskResponseDto |
| 100 | */ |
| 101 | public function execute() |
| 102 | { |
| 103 | if (!$this->stepsDto->getTotal()) { |
| 104 | $this->stepsDto->setTotal(1); |
| 105 | } |
| 106 | |
| 107 | $this->persistStagingEnginePreference(); |
| 108 | $this->enqueueStartEvent(); |
| 109 | |
| 110 | try { |
| 111 | $this->logStartHeader(); |
| 112 | $this->logger->writeLogHeader(); |
| 113 | $this->logger->writeInstalledPluginsAndThemes(); |
| 114 | $this->writeStagingSettingsLogs(); |
| 115 | $this->writeAdvancedSettingsToLogs(); |
| 116 | $this->cannotUpdateStagingSiteOnMultisite(); |
| 117 | $this->cannotUpdateIfCantWriteToDisk(); |
| 118 | $this->cannotUpdateIfStagingSiteNoExists(); |
| 119 | $this->cannotUpdateIfUsingExternalDatabase(); |
| 120 | $this->cannotUpdateIfStagingPrefixSameAsProductionSite(); |
| 121 | } catch (RuntimeException $e) { |
| 122 | $this->enqueueRequirementFailEvent(); |
| 123 | $this->logger->critical($e->getMessage()); |
| 124 | |
| 125 | return $this->generateResponse(false); |
| 126 | } |
| 127 | |
| 128 | $this->saveStagingSite(); |
| 129 | $this->logRequirementsCheckPassed(); |
| 130 | |
| 131 | return $this->generateResponse(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @return void |
| 136 | */ |
| 137 | protected function logStartHeader() |
| 138 | { |
| 139 | $this->logger->info('#################### Start Staging Site Update Job ####################'); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @return void |
| 144 | */ |
| 145 | protected function logRequirementsCheckPassed() |
| 146 | { |
| 147 | $this->logger->info('Staging Site update requirements passed...'); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @return void |
| 152 | */ |
| 153 | protected function enqueueStartEvent() |
| 154 | { |
| 155 | $this->analyticsStagingUpdate->enqueueStartEvent($this->jobDataDto->getId(), $this->jobDataDto); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @return void |
| 160 | */ |
| 161 | protected function enqueueRequirementFailEvent() |
| 162 | { |
| 163 | $this->analyticsStagingUpdate->enqueueFinishEvent($this->jobDataDto->getId(), $this->jobDataDto); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * @return void |
| 168 | */ |
| 169 | protected function saveStagingSite() |
| 170 | { |
| 171 | $stagingSites = $this->sites->tryGettingStagingSites(); |
| 172 | $stagingSites[$this->jobDataDto->getCloneId()] = $this->jobDataDto->getStagingSite()->toArray(); |
| 173 | $this->sites->updateStagingSites($stagingSites); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * @return void |
| 178 | * @throws RuntimeException |
| 179 | */ |
| 180 | protected function cannotUpdateStagingSiteOnMultisite() |
| 181 | { |
| 182 | if (is_multisite()) { |
| 183 | throw new RuntimeException(__('Basic version doesn\'t support staging feature on multisite.', 'wp-staging')); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * @return void |
| 189 | * @throws RuntimeException |
| 190 | */ |
| 191 | protected function cannotUpdateIfCantWriteToDisk() |
| 192 | { |
| 193 | try { |
| 194 | $this->diskWriteCheck->testDiskIsWriteable(); |
| 195 | } catch (DiskNotWritableException $e) { |
| 196 | throw new RuntimeException($e->getMessage()); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * @return void |
| 202 | * @throws RuntimeException |
| 203 | */ |
| 204 | protected function cannotUpdateIfStagingSiteNoExists() |
| 205 | { |
| 206 | $stagingSitePath = $this->jobDataDto->getStagingSitePath(); |
| 207 | if (!is_dir($stagingSitePath)) { |
| 208 | throw new RuntimeException(esc_html__('Cannot update staging site. Staging site directory does not exist!', 'wp-staging')); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @return void |
| 214 | * @throws RuntimeException |
| 215 | */ |
| 216 | protected function cannotUpdateIfUsingExternalDatabase() |
| 217 | { |
| 218 | if ($this->jobDataDto->getIsExternalDatabase()) { |
| 219 | throw new RuntimeException(esc_html__('Staging site update with external database is not supported in the basic version.', 'wp-staging')); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | protected function cannotUpdateIfStagingPrefixSameAsProductionSite() |
| 224 | { |
| 225 | $isSamePrefix = $this->database->getBasePrefix() === $this->jobDataDto->getDatabasePrefix(); |
| 226 | if ($isSamePrefix) { |
| 227 | throw new RuntimeException(esc_html__('Staging site prefix is same as production site prefix. Use different prefix for staging site.', 'wp-staging')); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * @return void |
| 233 | */ |
| 234 | protected function writeStagingSettingsLogs() |
| 235 | { |
| 236 | $this->logger->info('Staging Settings:'); |
| 237 | $this->logger->info('Staging Site Path: ' . $this->jobDataDto->getStagingSitePath()); |
| 238 | $this->logger->info('Staging Site URL: ' . $this->jobDataDto->getStagingSiteUrl()); |
| 239 | $this->logger->info('Database Prefix: ' . $this->jobDataDto->getDatabasePrefix()); |
| 240 | $this->logger->info('Clone ID: ' . $this->jobDataDto->getCloneId()); |
| 241 | $this->logger->info('Clone Name: ' . $this->jobDataDto->getName()); |
| 242 | $this->logger->info('Is External Database: ' . ($this->jobDataDto->getIsExternalDatabase() ? 'Yes' : 'No')); |
| 243 | } |
| 244 | } |
| 245 |