Skip to content

Commit

Permalink
Merge pull request #3595 from OriginalFunko/feature/cli-asset-indexing
Browse files Browse the repository at this point in the history
Add asset indexing to Craft cli.
  • Loading branch information
andris-sevcenko authored Jan 18, 2019
2 parents 2f5ddd6 + 0be7cab commit d54bbd5
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added
- Added `craft\base\FieldTrait::$oldSettings`.
- Added `craft\services\Fields::prepFieldForSave()`.
- Added the `craft asset-indexing/all` and `craft asset-indexing/one` console commands which can be used to index asset volumes.

### Fixed
- Fixed a PHP notice that occurred when updating to Craft 3.1 if there were any plugins installed without settings.
Expand Down
106 changes: 106 additions & 0 deletions src/console/controllers/AssetIndexingController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\console\controllers;

use Craft;

use yii\helpers\Console;
use yii\console\Controller;

/**
* Manages Craft asset indexing.
* This command indexes assets on all volumes or specified volumes.
* ~~~
* # Indexes all assets on all volumes. Optional argument is whether or not to cache images.
* craft asset-indexing/all
* # Indexes all assets on a specified volume (EXAMPLE_HANDLE).
* Second (optional) argument is whether or not to cache images.
* craft asset-indexing/one EXAMPLE_HANDLE
* ~~~
*
* @since 3.1
*/
class AssetIndexingController extends Controller
{
/**
* Indexes all assets on all volumes.
*
* ```
* craft asset-indexing/all
* // Index all assets without caching images:
* craft asset-indexing/all 0
* ```
*
* @param boolean $cacheImages Whether or not to cache images.
* @return boolean.
* @throws \Throwable $e.
*/
public function actionAll($cacheImages = 1)
{
try {
$session = Craft::$app->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;
}
}
}

0 comments on commit d54bbd5

Please sign in to comment.