-
Notifications
You must be signed in to change notification settings - Fork 646
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14154 from craftcms/feature/cms-1236-propagate-el…
…ement-site-deletions-to-nested-elements Feature/cms 1236 propagate element site deletions to nested elements
- Loading branch information
Showing
8 changed files
with
191 additions
and
1 deletion.
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
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
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
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
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
79 changes: 79 additions & 0 deletions
79
src/console/controllers/utils/PruneOrphanedMatrixBlocksController.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,79 @@ | ||
<?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 craft\elements\MatrixBlock; | ||
use craft\helpers\Console; | ||
use yii\console\ExitCode; | ||
|
||
/** | ||
* Prunes orphaned matrix blocks for each site. | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 4.7.0 | ||
*/ | ||
class PruneOrphanedMatrixBlocksController extends Controller | ||
{ | ||
/** | ||
* Prunes orphaned matrix blocks for each site. | ||
* | ||
* @return int | ||
*/ | ||
public function actionIndex(): int | ||
{ | ||
if (!Craft::$app->getIsMultiSite()) { | ||
$this->stdout("This command should only be run for multi-site installs.\n", Console::FG_YELLOW); | ||
return ExitCode::OK; | ||
} | ||
|
||
$elementsService = Craft::$app->getElements(); | ||
|
||
// get all sites | ||
$sites = Craft::$app->getSites()->getAllSites(); | ||
|
||
// for each site get all matrix blocks with owner that doesn't exist for this site | ||
foreach ($sites as $site) { | ||
$this->stdout(sprintf('Finding orphaned matrix blocks for site "%s" ... ', $site->getName())); | ||
|
||
$esSubQuery = (new Query()) | ||
->from(['es' => Table::ELEMENTS_SITES]) | ||
->where([ | ||
'and', | ||
'[[es.elementId]] = [[matrixblocks.primaryOwnerId]]', | ||
['es.siteId' => $site->id], | ||
]); | ||
|
||
$matrixBlocks = MatrixBlock::find() | ||
->status(null) | ||
->siteId($site->id) | ||
->where(['not exists', $esSubQuery]) | ||
->all(); | ||
|
||
if (empty($matrixBlocks)) { | ||
$this->stdout("none found\n", Console::FG_GREEN); | ||
continue; | ||
} | ||
|
||
$this->stdout(sprintf("%s found\n", count($matrixBlocks)), Console::FG_RED); | ||
|
||
// delete the ones we found | ||
foreach ($matrixBlocks as $block) { | ||
$this->do(sprintf('Deleting block %s in %s', $block->id, $site->getName()), function() use ($block, $elementsService) { | ||
$elementsService->deleteElementForSite($block); | ||
}); | ||
} | ||
} | ||
|
||
$this->stdout("\nFinished pruning orphaned Matrix blocks.\n", Console::FG_GREEN); | ||
return ExitCode::OK; | ||
} | ||
} |
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
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