CreateRequirementsCheckTask.php
197 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Staging\Tasks\StagingSiteCreate; |
| 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\AnalyticsStagingCreate; |
| 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\Exception\DiskNotWritableException; |
| 16 | use WPStaging\Staging\Dto\Job\StagingSiteJobsDataDto; |
| 17 | use WPStaging\Staging\Service\StagingEngine; |
| 18 | use WPStaging\Staging\Sites; |
| 19 | use WPStaging\Staging\Tasks\StagingTask; |
| 20 | use WPStaging\Staging\Traits\WithStagingEnginePreference; |
| 21 | use WPStaging\Staging\Traits\WithStagingRequirementLogs; |
| 22 | use WPStaging\Vendor\Psr\Log\LoggerInterface; |
| 23 | |
| 24 | /** |
| 25 | * Validates prerequisites before a staging site create job starts. |
| 26 | */ |
| 27 | class CreateRequirementsCheckTask extends StagingTask |
| 28 | { |
| 29 | use WithStagingEnginePreference; |
| 30 | use WithStagingRequirementLogs; |
| 31 | |
| 32 | /** @var Directory */ |
| 33 | protected $directory; |
| 34 | |
| 35 | /** @var Database */ |
| 36 | protected $database; |
| 37 | |
| 38 | /** @var Filesystem */ |
| 39 | protected $filesystem; |
| 40 | |
| 41 | /** @var DiskWriteCheck */ |
| 42 | protected $diskWriteCheck; |
| 43 | |
| 44 | /** @var AnalyticsStagingCreate */ |
| 45 | protected $analyticsStagingCreate; |
| 46 | |
| 47 | /** @var SystemInfo */ |
| 48 | protected $systemInfo; |
| 49 | |
| 50 | /** @var StagingSiteJobsDataDto $jobDataDto */ |
| 51 | protected $jobDataDto; |
| 52 | |
| 53 | /** @var Sites */ |
| 54 | protected $sites; |
| 55 | |
| 56 | public function __construct( |
| 57 | Directory $directory, |
| 58 | Database $database, |
| 59 | Filesystem $filesystem, |
| 60 | LoggerInterface $logger, |
| 61 | Cache $cache, |
| 62 | StepsDto $stepsDto, |
| 63 | SeekableQueueInterface $taskQueue, |
| 64 | DiskWriteCheck $diskWriteCheck, |
| 65 | AnalyticsStagingCreate $analyticsStagingCreate, |
| 66 | StagingEngine $stagingEngine, |
| 67 | SystemInfo $systemInfo, |
| 68 | Sites $sites |
| 69 | ) { |
| 70 | parent::__construct($logger, $cache, $stepsDto, $taskQueue); |
| 71 | $this->directory = $directory; |
| 72 | $this->filesystem = $filesystem; |
| 73 | $this->diskWriteCheck = $diskWriteCheck; |
| 74 | $this->analyticsStagingCreate = $analyticsStagingCreate; |
| 75 | $this->stagingEngine = $stagingEngine; |
| 76 | $this->systemInfo = $systemInfo; |
| 77 | $this->database = $database; |
| 78 | $this->sites = $sites; |
| 79 | } |
| 80 | |
| 81 | public static function getTaskName() |
| 82 | { |
| 83 | return 'staging_site_create_requirements_check'; |
| 84 | } |
| 85 | |
| 86 | public static function getTaskTitle() |
| 87 | { |
| 88 | return 'Requirements Check'; |
| 89 | } |
| 90 | |
| 91 | public function execute() |
| 92 | { |
| 93 | if (!$this->stepsDto->getTotal()) { |
| 94 | $this->stepsDto->setTotal(1); |
| 95 | } |
| 96 | |
| 97 | $this->persistStagingEnginePreference(); |
| 98 | $this->analyticsStagingCreate->enqueueStartEvent($this->jobDataDto->getId(), $this->jobDataDto); |
| 99 | |
| 100 | try { |
| 101 | $this->logger->info('#################### Start Staging Site Create Job ####################'); |
| 102 | $this->logger->writeLogHeader(); |
| 103 | $this->logger->writeInstalledPluginsAndThemes(); |
| 104 | $this->writeStagingSettingsLogs(); |
| 105 | $this->writeAdvancedSettingsToLogs(); |
| 106 | $this->cannotCreateStagingSiteOnMultisite(); |
| 107 | $this->cannotCreateIfCantWriteToDisk(); |
| 108 | $this->cannotCreateStagingDirectory(); |
| 109 | $this->cannotCreateIfPrefixContainsInvalidCharacter(); |
| 110 | $this->cannotCreateIfUsingExternalDatabase(); |
| 111 | $this->cannotCreateIfStagingPrefixSameAsProductionSite(); |
| 112 | } catch (RuntimeException $e) { |
| 113 | $this->analyticsStagingCreate->enqueueFinishEvent($this->jobDataDto->getId(), $this->jobDataDto); |
| 114 | $this->logger->critical($e->getMessage()); |
| 115 | |
| 116 | return $this->generateResponse(false); |
| 117 | } |
| 118 | |
| 119 | $this->saveStagingSite(); |
| 120 | $this->logger->info('Staging Site creation requirements passed...'); |
| 121 | |
| 122 | return $this->generateResponse(); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @return void |
| 127 | */ |
| 128 | protected function saveStagingSite() |
| 129 | { |
| 130 | $stagingSites = $this->sites->tryGettingStagingSites(); |
| 131 | $stagingSites[$this->jobDataDto->getCloneId()] = $this->jobDataDto->getStagingSite()->toArray(); |
| 132 | $this->sites->updateStagingSites($stagingSites); |
| 133 | } |
| 134 | |
| 135 | protected function cannotCreateStagingSiteOnMultisite() |
| 136 | { |
| 137 | if (is_multisite()) { |
| 138 | throw new RuntimeException(__('Basic version doesn\'t support staging feature on multisite.', 'wp-staging')); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | protected function cannotCreateIfCantWriteToDisk() |
| 143 | { |
| 144 | try { |
| 145 | $this->diskWriteCheck->testDiskIsWriteable(); |
| 146 | } catch (DiskNotWritableException $e) { |
| 147 | throw new RuntimeException($e->getMessage()); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | protected function cannotCreateStagingDirectory() |
| 152 | { |
| 153 | $stagingSitePath = $this->jobDataDto->getStagingSitePath(); |
| 154 | if (!is_dir($stagingSitePath) && !$this->filesystem->mkdir($stagingSitePath)) { |
| 155 | throw new RuntimeException(esc_html__('Cannot create staging site. Staging site directory does not exist and cannot be created!', 'wp-staging')); |
| 156 | } |
| 157 | |
| 158 | if (!$this->filesystem->isEmptyDir($stagingSitePath)) { |
| 159 | throw new RuntimeException(esc_html__('Cannot create staging site. Staging site directory is not empty!', 'wp-staging')); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | protected function cannotCreateIfPrefixContainsInvalidCharacter() |
| 164 | { |
| 165 | $stagingSitePrefix = $this->jobDataDto->getDatabasePrefix(); |
| 166 | if (preg_match('|[^a-z0-9_]|i', $stagingSitePrefix)) { |
| 167 | throw new RuntimeException(esc_html__("Staging site prefix contains invalid character(s). Use different prefix with valid characters.", 'wp-staging')); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | protected function cannotCreateIfUsingExternalDatabase() |
| 172 | { |
| 173 | if ($this->jobDataDto->getIsExternalDatabase()) { |
| 174 | throw new RuntimeException(esc_html__('Staging site creation with external database is not supported in the basic version.', 'wp-staging')); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | protected function cannotCreateIfStagingPrefixSameAsProductionSite() |
| 179 | { |
| 180 | $isSamePrefix = $this->database->getBasePrefix() === $this->jobDataDto->getDatabasePrefix(); |
| 181 | if ($isSamePrefix) { |
| 182 | throw new RuntimeException(esc_html__('Staging site prefix is same as production site prefix. Use different prefix for staging site.', 'wp-staging')); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | protected function writeStagingSettingsLogs() |
| 187 | { |
| 188 | $this->logger->info('Staging Settings:'); |
| 189 | $this->logger->info('Staging Site Path: ' . $this->jobDataDto->getStagingSitePath()); |
| 190 | $this->logger->info('Staging Site URL: ' . $this->jobDataDto->getStagingSiteUrl()); |
| 191 | $this->logger->info('Database Prefix: ' . $this->jobDataDto->getDatabasePrefix()); |
| 192 | $this->logger->info('Clone ID: ' . $this->jobDataDto->getCloneId()); |
| 193 | $this->logger->info('Clone Name: ' . $this->jobDataDto->getName()); |
| 194 | $this->logger->info('Is External Database: ' . ($this->jobDataDto->getIsExternalDatabase() ? 'Yes' : 'No')); |
| 195 | } |
| 196 | } |
| 197 |