diff --git a/CHANGELOG-v3.md b/CHANGELOG-v3.md index 5d471f5646b..fd4bd2f28cb 100644 --- a/CHANGELOG-v3.md +++ b/CHANGELOG-v3.md @@ -3,11 +3,11 @@ ## Unreleased ### Added +- Added the `index-assets ` and `index-assets/all` console commands. ([#3595](https://github.com/craftcms/cms/pull/3595)) - Added `craft\base\FieldTrait::$oldSettings`. - Added `craft\helpers\Install`. - Added `craft\services\Fields::prepFieldForSave()`. - Added `craft\services\Path::getProjectConfigFilePath()`. -- Added the `craft asset-indexing/all` and `craft asset-indexing/one` console commands which can be used to index asset volumes. ### Changed - The installer now checks `project.yaml` when determining the default site name, handle, base URL, and language values. ([#3661](https://github.com/craftcms/cms/issues/3661)) diff --git a/src/console/controllers/AssetIndexingController.php b/src/console/controllers/AssetIndexingController.php deleted file mode 100644 index 7e1a763c2a7..00000000000 --- a/src/console/controllers/AssetIndexingController.php +++ /dev/null @@ -1,106 +0,0 @@ -getAssetIndexer()->getIndexingSessionId(); - if ($volumes = Craft::$app->getVolumes()->getAllVolumes()) { - foreach ($volumes as $volume) { - foreach (Craft::$app->getAssetIndexer()->getIndexListOnVolume($volume) as $item) { - Craft::$app->getAssetIndexer()->indexFile( - $volume, - $item['path'], - $session, - $cacheImages - ); - } - } - } else { - return false; - } - return true; - } catch (\Throwable $e) { - echo $e->getMessage(); - return false; - } - } - - /** - * Indexes all assets on one specific volume. - * - * ``` - * craft asset-indexing/one EXAMPLE_HANDLE - * // Index volume without caching images: - * craft asset-indexing/one EXAMPLE_HANDLE 0 - * ``` - * - * @param string $name The name of the volume to index. This should only contain - * letters, digits, and underscores. - * @param boolean $cacheImages Whether or not to cache images. - * @return boolean. - * @throws \Throwable $e. - */ - - public function actionOne($name, $cacheImages = 1) - { - try { - $session = Craft::$app->getAssetIndexer()->getIndexingSessionId(); - if ($volume = Craft::$app->getVolumes()->getVolumeByHandle($name)) { - foreach (Craft::$app->getAssetIndexer()->getIndexListOnVolume($volume) as $item) { - Craft::$app->getAssetIndexer()->indexFile( - $volume, - $item['path'], - $session, - $cacheImages - ); - } - } else { - return false; - } - return true; - } catch (\Throwable $e) { - echo $e->getMessage(); - return false; - } - } -} diff --git a/src/console/controllers/IndexAssetsController.php b/src/console/controllers/IndexAssetsController.php new file mode 100644 index 00000000000..4aad0fd93dc --- /dev/null +++ b/src/console/controllers/IndexAssetsController.php @@ -0,0 +1,120 @@ +getVolumes()->getAllVolumes(); + + if (empty($volumes)) { + $this->stdout('No volumes exist.' . PHP_EOL, Console::FG_YELLOW); + return ExitCode::OK; + } + + return $this->_indexAssets($volumes); + } + + /** + * Re-indexes assets from the given volume handle. + * + * @param string $handle The handle of the volume to index + * @return int + */ + public function actionOne($handle) + { + $volume = Craft::$app->getVolumes()->getVolumeByHandle($handle); + + if (!$volume) { + $this->stdout('No volume exists with the handle “' . $handle . '”.', Console::FG_RED); + return ExitCode::UNSPECIFIED_ERROR; + } + + return $this->_indexAssets([$volume]); + } + + /** + * Indexes the assets in the given volumes. + * + * @param VolumeInterface[] $volumes + * @return int + */ + private function _indexAssets(array $volumes): int + { + $assetIndexer = Craft::$app->getAssetIndexer(); + $session = $assetIndexer->getIndexingSessionId(); + + $this->stdout(PHP_EOL); + + foreach ($volumes as $volume) { + /** @var Volume $volume */ + $this->stdout('Indexing assets in ', Console::FG_YELLOW); + $this->stdout($volume->name, Console::FG_CYAN); + $this->stdout(' ...' . PHP_EOL, Console::FG_YELLOW); + foreach ($assetIndexer->getIndexListOnVolume($volume) as $item) { + $this->stdout(' > '); + $this->stdout($item['path'], Console::FG_CYAN); + $this->stdout(' ... '); + try { + $assetIndexer->indexFile($volume, $item['path'], $session, $this->cacheRemoteImages); + } catch (\Throwable $e) { + $this->stdout('error: ' . $e->getMessage() . PHP_EOL . PHP_EOL, Console::FG_RED); + Craft::$app->getErrorHandler()->logException($e); + return ExitCode::UNSPECIFIED_ERROR; + } + + $this->stdout('done' . PHP_EOL, Console::FG_GREEN); + } + + $this->stdout('Done indexing assets in ', Console::FG_GREEN); + $this->stdout($volume->name, Console::FG_CYAN); + $this->stdout('.' . PHP_EOL . PHP_EOL, Console::FG_GREEN); + } + + return ExitCode::OK; + } +}