diff --git a/protected/humhub/docs/CHANGELOG.md b/protected/humhub/docs/CHANGELOG.md index bebf6603e5..6647ee772f 100644 --- a/protected/humhub/docs/CHANGELOG.md +++ b/protected/humhub/docs/CHANGELOG.md @@ -7,6 +7,8 @@ HumHub Change Log - Fix #3241: Profile header space count invalid - Fix: Disabled Notification E-Mails for installation sample contents - Fix: No e-mail summary immediately after installation +- Enh: Added queuing for search updates of commments + 1.3.1 (August 7, 2018) ----------------------- diff --git a/protected/humhub/modules/comment/models/Comment.php b/protected/humhub/modules/comment/models/Comment.php index 186c57f741..46740d4ec8 100644 --- a/protected/humhub/modules/comment/models/Comment.php +++ b/protected/humhub/modules/comment/models/Comment.php @@ -12,11 +12,13 @@ use humhub\components\behaviors\PolymorphicRelation; use humhub\modules\comment\activities\NewComment; use humhub\modules\comment\live\NewComment as NewCommentLive; use humhub\modules\comment\notifications\NewComment as NewCommentNotification; +use humhub\modules\content\components\ContentActiveRecord; use humhub\modules\content\components\ContentAddonActiveRecord; use humhub\modules\content\interfaces\ContentOwner; use humhub\modules\content\widgets\richtext\RichText; use humhub\modules\post\models\Post; use humhub\modules\search\interfaces\Searchable; +use humhub\modules\search\libs\SearchHelper; use humhub\modules\space\models\Space; use humhub\modules\user\models\User; use Yii; @@ -168,8 +170,10 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner */ protected function updateContentSearch() { - if ($this->getCommentedRecord() instanceof Searchable) { - Yii::$app->search->update($this->getCommentedRecord()); + /** @var ContentActiveRecord $content */ + $contentRecord = $this->getCommentedRecord(); + if ($contentRecord !== null) { + SearchHelper::queueUpdate($contentRecord); } } diff --git a/protected/humhub/modules/content/Events.php b/protected/humhub/modules/content/Events.php index 89f9401791..6eb9e0cd4a 100644 --- a/protected/humhub/modules/content/Events.php +++ b/protected/humhub/modules/content/Events.php @@ -8,11 +8,11 @@ namespace humhub\modules\content; +use humhub\modules\content\components\ContentActiveRecord; use humhub\modules\content\models\Content; -use humhub\modules\search\jobs\DeleteDocument; -use humhub\modules\search\jobs\UpdateDocument; -use humhub\modules\user\events\UserEvent; use humhub\modules\search\interfaces\Searchable; +use humhub\modules\search\libs\SearchHelper; +use humhub\modules\user\events\UserEvent; use Yii; use yii\base\BaseObject; @@ -98,7 +98,7 @@ class Events extends BaseObject 'object' => $event->sender->object, 'seperator' => ' · ', 'template' => '
{content}
', - ], ['sortOrder' => 10] + ], ['sortOrder' => 10] ); } @@ -124,12 +124,9 @@ class Events extends BaseObject */ public static function onContentActiveRecordSave($event) { - if ($event->sender instanceof Searchable) { - Yii::$app->queue->push(new UpdateDocument([ - 'activeRecordClass' => get_class($event->sender), - 'primaryKey' => $event->sender->id - ])); - } + /** @var ContentActiveRecord $record */ + $record = $event->sender; + SearchHelper::queueUpdate($record); } /** @@ -139,12 +136,9 @@ class Events extends BaseObject */ public static function onContentActiveRecordDelete($event) { - if ($event->sender instanceof Searchable) { - Yii::$app->queue->push(new DeleteDocument([ - 'activeRecordClass' => get_class($event->sender), - 'primaryKey' => $event->sender->id - ])); - } + /** @var ContentActiveRecord $record */ + $record = $event->sender; + SearchHelper::queueDelete($record); } } diff --git a/protected/humhub/modules/search/Events.php b/protected/humhub/modules/search/Events.php index 718c2ddd85..5e682a2d83 100644 --- a/protected/humhub/modules/search/Events.php +++ b/protected/humhub/modules/search/Events.php @@ -10,10 +10,11 @@ namespace humhub\modules\search; use Yii; use yii\base\BaseObject; +use yii\console\Controller; use yii\helpers\Console; /** - * Description of SearchModuleEvents + * Search module event callbacks * * @author luke */ @@ -25,18 +26,11 @@ class Events extends BaseObject $event->sender->addWidget(widgets\SearchMenu::class); } - public static function onAfterSaveComment($event) - { - $comment = $event->sender; - - if ($comment->content->getPolymorphicRelation() instanceof ISearchable) { - Yii::app()->search->update($comment->content->getPolymorphicRelation()); - } - } - public static function onHourlyCron($event) { + /** @var Controller $controller */ $controller = $event->sender; + $controller->stdout('Optimizing search index...'); Yii::$app->search->optimize(); $controller->stdout('done.' . PHP_EOL, Console::FG_GREEN); diff --git a/protected/humhub/modules/search/libs/SearchHelper.php b/protected/humhub/modules/search/libs/SearchHelper.php index 33305ca577..4ccc7116e5 100644 --- a/protected/humhub/modules/search/libs/SearchHelper.php +++ b/protected/humhub/modules/search/libs/SearchHelper.php @@ -8,7 +8,12 @@ namespace humhub\modules\search\libs; +use humhub\modules\search\interfaces\Searchable; +use humhub\modules\search\jobs\DeleteDocument; +use humhub\modules\search\jobs\UpdateDocument; +use Yii; use yii\base\BaseObject; +use yii\db\ActiveRecord; /** * SearchHelper @@ -21,9 +26,10 @@ class SearchHelper extends BaseObject /** * Checks if given text matches a search query. - * + * * @param string $query * @param string $text + * @return boolean */ public static function matchQuery($query, $text) { @@ -36,4 +42,47 @@ class SearchHelper extends BaseObject return false; } + /** + * Queues search index update of an active record + * + * @param ActiveRecord $record + * @return bool + */ + public static function queueUpdate(ActiveRecord $record) + { + if ($record instanceof Searchable) { + $pk = $record->getPrimaryKey(); + if (!empty($pk) && !is_array($pk)) { + Yii::$app->queue->push(new UpdateDocument([ + 'activeRecordClass' => get_class($record), + 'primaryKey' => $pk + ])); + return true; + } + } + return false; + } + + /** + * Queues search index delete of an active record + * + * @param ActiveRecord $record + * @return bool + */ + public static function queueDelete(ActiveRecord $record) + { + if ($record instanceof Searchable) { + $pk = $record->getPrimaryKey(); + if (!empty($pk) && !is_array($pk)) { + Yii::$app->queue->push(new DeleteDocument([ + 'activeRecordClass' => get_class($record), + 'primaryKey' => $pk + ])); + return true; + } + } + return false; + } + + }