Skip to content

Commit

Permalink
Added volume support to console commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel van Hintum committed Apr 3, 2023
1 parent 6685bff commit 9a4c81e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 3.1.0 - 2023-04-03
### Added
- Added volume support to console commands.

## 3.0.0 - 2022-07-13
### Changed
- Merged fixes from Craft 3 plugin
Expand Down
52 changes: 33 additions & 19 deletions src/console/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use craft\db\Query;
use craft\db\Table;
use yii\console\Controller;
use yii\helpers\Console;
use yii\console\ExitCode;

/**
* Controls
Expand All @@ -15,57 +15,71 @@ class DefaultController extends Controller
{
/**
* Lists all unused assets.
* @param string|null $volume The handle of the asset's volume.
*/
public function actionListUnused()
public function actionListUnused(?string $volume = null)
{
echo "Listing all unused asset ids:\n";
$this->stdout('Listing all unused asset ids:' . PHP_EOL);

$results = $this->getUnusedAssets();
$results = $this->getUnusedAssets($volume);
foreach ($results as $result) {
echo $result['id'] . ' : ' . $result['filename'] . "\n";
$this->stdout($result['id'] . ' : ' . $result['filename'] . PHP_EOL);
}

return "Done.";
return ExitCode::OK;
}

/**
* Deletes all unused assets.
* @param string|null $volume The handle of the asset's volume.
*/
public function actionDeleteUnused()
public function actionDeleteUnused(?string $volume = null)
{
echo "Deleting all unused asset ids:\n";
$this->stdout('Deleting all unused asset ids:' . PHP_EOL);

$assets = Craft::$app->getAssets();

$results = $this->getUnusedAssets();
$results = $this->getUnusedAssets($volume);
$assetCount = count($results);

if ($this->confirm("Delete $assetCount assets?")) {
$assets = Craft::$app->getAssets();

foreach ($results as $result) {
echo 'Deleting ' . $result['id'] . ' : ' . $result['filename'] . "\n";
$this->stdout('Deleting ' . $result['id'] . ' : ' . $result['filename'] . PHP_EOL);

$asset = $assets->getAssetById($result['id']);
if ($asset) {
Craft::$app->getElements()->deleteElement($asset);
}
}

return "Done.";
$this->stdout('Deleted all unused asset ids.' . PHP_EOL);
}

return ExitCode::OK;
}

private function getUnusedAssets()
private function getUnusedAssets(?string $volume = null)
{
if ($volume) {
/** @var craft\models\Volume */
$volumeModel = Craft::$app->getVolumes()->getVolumeByHandle($volume);
}

$subQuery = (new Query())
->select('id')
->from(Table::RELATIONS . ' relations')
->where('relations.targetId=assets.id')
->orWhere('relations.sourceId=assets.id');

return (new Query())
->select(['id', 'filename'])
->from(Table::ASSETS . ' assets')
->where(['not exists', $subQuery])
->all();
$query = (new Query())
->select(['id', 'filename'])
->from(Table::ASSETS . ' assets')
->where(['not exists', $subQuery]);

if (isset($volumeModel)) {
$query->where(['volumeId' => $volumeModel->id]);
}

return $query->all();
}
}

0 comments on commit 9a4c81e

Please sign in to comment.