|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @link https://craftcms.com/ |
| 4 | + * @copyright Copyright (c) Pixel & Tonic, Inc. |
| 5 | + * @license https://craftcms.github.io/license/ |
| 6 | + */ |
| 7 | + |
| 8 | +namespace craft\console\controllers; |
| 9 | + |
| 10 | +use Craft; |
| 11 | +use craft\console\Controller; |
| 12 | +use craft\elements\Entry; |
| 13 | +use craft\events\MultiElementActionEvent; |
| 14 | +use craft\helpers\Console; |
| 15 | +use craft\helpers\DateTimeHelper; |
| 16 | +use craft\helpers\Db; |
| 17 | +use craft\services\Elements; |
| 18 | +use yii\console\ExitCode; |
| 19 | + |
| 20 | +/** |
| 21 | + * Updates statically-stored entry statuses. |
| 22 | + * |
| 23 | + * @author Pixel & Tonic, Inc. <support@pixelandtonic.com> |
| 24 | + * @since 5.7.0 |
| 25 | + */ |
| 26 | +class UpdateStatusesController extends Controller |
| 27 | +{ |
| 28 | + /** |
| 29 | + * Updates statically-stored entry statuses. |
| 30 | + * |
| 31 | + * @return int |
| 32 | + */ |
| 33 | + public function actionIndex(): int |
| 34 | + { |
| 35 | + $now = Db::prepareDateForDb(DateTimeHelper::now()); |
| 36 | + $elementsService = Craft::$app->getElements(); |
| 37 | + |
| 38 | + $conditions = [ |
| 39 | + Entry::STATUS_LIVE => [ |
| 40 | + 'and', |
| 41 | + ['<=', 'entries.postDate', $now], |
| 42 | + [ |
| 43 | + 'or', |
| 44 | + ['entries.expiryDate' => null], |
| 45 | + ['>', 'entries.expiryDate', $now], |
| 46 | + ], |
| 47 | + ], |
| 48 | + Entry::STATUS_PENDING => ['>', 'entries.postDate', $now], |
| 49 | + Entry::STATUS_EXPIRED => [ |
| 50 | + 'and', |
| 51 | + ['not', ['entries.expiryDate' => null]], |
| 52 | + ['<=', 'entries.expiryDate', $now], |
| 53 | + ], |
| 54 | + ]; |
| 55 | + |
| 56 | + foreach ($conditions as $status => $condition) { |
| 57 | + $query = Entry::find() |
| 58 | + ->site('*') |
| 59 | + ->unique() |
| 60 | + ->status(null) |
| 61 | + ->andWhere(['not', ['status' => $status]]) |
| 62 | + ->andWhere($condition); |
| 63 | + |
| 64 | + $this->do("Updating $status entries", function() use ($elementsService, $query) { |
| 65 | + $count = (int)$query->count(); |
| 66 | + |
| 67 | + $beforeCallback = function(MultiElementActionEvent $e) use ($query, $count) { |
| 68 | + if ($e->query === $query) { |
| 69 | + $this->stdout(Console::indentStr() . " - [$e->position/$count] Updating entry ({$e->element->id}) ... "); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + $afterCallback = function(MultiElementActionEvent $e) use ($query, &$fail) { |
| 74 | + if ($e->query === $query) { |
| 75 | + if ($e->exception) { |
| 76 | + $this->stdout('error: ' . $e->exception->getMessage() . PHP_EOL, Console::FG_RED); |
| 77 | + $fail = true; |
| 78 | + } elseif ($e->element->hasErrors()) { |
| 79 | + $this->stdout('failed: ' . implode(', ', $e->element->getErrorSummary(true)) . PHP_EOL, Console::FG_RED); |
| 80 | + $fail = true; |
| 81 | + } else { |
| 82 | + $this->stdout('done' . PHP_EOL, Console::FG_GREEN); |
| 83 | + } |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + $elementsService->on(Elements::EVENT_BEFORE_RESAVE_ELEMENT, $beforeCallback); |
| 88 | + $elementsService->on(Elements::EVENT_AFTER_RESAVE_ELEMENT, $afterCallback); |
| 89 | + $elementsService->resaveElements($query, true, updateSearchIndex: false); |
| 90 | + $elementsService->off(Elements::EVENT_BEFORE_RESAVE_ELEMENT, $beforeCallback); |
| 91 | + $elementsService->off(Elements::EVENT_AFTER_RESAVE_ELEMENT, $afterCallback); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + return ExitCode::OK; |
| 96 | + } |
| 97 | +} |
0 commit comments