-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils/delete-empty-volume-folders command
Resolves #16388
- Loading branch information
1 parent
feb3f22
commit f3708d2
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/console/controllers/utils/DeleteEmptyVolumeFoldersController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\console\controllers\utils; | ||
|
||
use Craft; | ||
use craft\console\Controller; | ||
use craft\db\Query; | ||
use craft\db\Table; | ||
use yii\console\ExitCode; | ||
|
||
/** | ||
* Deletes empty volume folders. | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 4.14.0 | ||
*/ | ||
class DeleteEmptyVolumeFoldersController extends Controller | ||
{ | ||
/** | ||
* @var string|null The volume handle(s) to delete folders from. Can be set to multiple comma-separated volumes. | ||
*/ | ||
public ?string $volume = null; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function options($actionID): array | ||
{ | ||
return [ | ||
...parent::options($actionID), | ||
'volume', | ||
]; | ||
} | ||
|
||
/** | ||
* Deletes empty volume folders. | ||
* | ||
* @return int | ||
*/ | ||
public function actionIndex(): int | ||
{ | ||
$query = (new Query()) | ||
->select(['folders.id']) | ||
->from(['folders' => Table::VOLUMEFOLDERS]) | ||
->leftJoin(['assets' => Table::ASSETS], '[[assets.folderId]] = [[folders.id]]') | ||
->where(['assets.id' => null]) | ||
->andWhere(['not', ['folders.parentId' => null]]) | ||
->andWhere(['not', ['folders.path' => null]]); | ||
|
||
if ($this->volume) { | ||
$volumeHandles = explode(',', $this->volume); | ||
$volumeIds = []; | ||
$volumesService = Craft::$app->getVolumes(); | ||
foreach ($volumeHandles as $handle) { | ||
$volume = $volumesService->getVolumeByHandle($handle); | ||
if (!$volume) { | ||
$this->stderr("Invalid volume handle: $handle\n"); | ||
return ExitCode::UNSPECIFIED_ERROR; | ||
} | ||
$volumeIds[] = $volume->id; | ||
} | ||
|
||
$query->andWhere(['folders.volumeId' => $volumeIds]); | ||
} | ||
|
||
$emptyFolderIds = $query->column(); | ||
|
||
if (empty($emptyFolderIds)) { | ||
$this->stdout("No empty folders found.\n"); | ||
return ExitCode::OK; | ||
} | ||
|
||
$message = sprintf( | ||
'Deleting %s empty %s', | ||
count($emptyFolderIds), | ||
count($emptyFolderIds) === 1 ? 'folder' : 'folders', | ||
); | ||
|
||
$this->do($message, function() use ($emptyFolderIds) { | ||
Craft::$app->getAssets()->deleteFoldersByIds($emptyFolderIds); | ||
}); | ||
|
||
return ExitCode::OK; | ||
} | ||
} |