diff --git a/protected/humhub/components/SearchProvider.php b/protected/humhub/components/SearchProvider.php index 0aa6c14d97..b7067c3069 100644 --- a/protected/humhub/components/SearchProvider.php +++ b/protected/humhub/components/SearchProvider.php @@ -44,11 +44,12 @@ abstract class SearchProvider abstract public function getName(): string; /** - * Search results + * Run search process, result may be cached * - * @return SearchRecordInterface[] + * @return array 'totalCount' - Number of total searched records, + * 'results' - Array of searched records SearchRecordInterface[] */ - abstract public function searchResults(): array; + abstract public function runSearch(): array; /** * Run search process and cache results @@ -61,10 +62,13 @@ abstract class SearchProvider return; } - $this->results = Yii::$app->cache->getOrSet( + $data = Yii::$app->cache->getOrSet( static::class . Yii::$app->user->id . ':search:' . $this->keyword, - [$this, 'searchResults'], + [$this, 'runSearch'], $this->cacheTimeout); + + $this->totalCount = $data['totalCount'] ?? 0; + $this->results = $data['results'] ?? []; } /** diff --git a/protected/humhub/modules/content/search/ContentSearchProvider.php b/protected/humhub/modules/content/search/ContentSearchProvider.php index 03f85d1608..bcd64656d3 100644 --- a/protected/humhub/modules/content/search/ContentSearchProvider.php +++ b/protected/humhub/modules/content/search/ContentSearchProvider.php @@ -42,7 +42,7 @@ class ContentSearchProvider extends SearchProvider /** * @inheritdoc */ - public function searchResults(): array + public function runSearch(): array { /* @var Module $module */ $module = Yii::$app->getModule('content'); @@ -52,13 +52,14 @@ class ContentSearchProvider extends SearchProvider 'pageSize' => $this->pageSize ])); - $this->totalCount = $resultSet->pagination->totalCount; - $results = []; foreach ($resultSet->results as $content) { $results[] = new SearchRecord($content); } - return $results; + return [ + 'totalCount' => $resultSet->pagination->totalCount, + 'results' => $results + ]; } } diff --git a/protected/humhub/modules/space/search/SpaceSearchProvider.php b/protected/humhub/modules/space/search/SpaceSearchProvider.php index 9b2a0d3cae..9168f8d0c6 100644 --- a/protected/humhub/modules/space/search/SpaceSearchProvider.php +++ b/protected/humhub/modules/space/search/SpaceSearchProvider.php @@ -43,20 +43,21 @@ class SpaceSearchProvider extends SearchProvider /** * @inheritdoc */ - public function searchResults(): array + public function runSearch(): array { $spaceDirectoryQuery = new SpaceDirectoryQuery([ 'defaultFilters' => ['keyword' => $this->keyword], 'pageSize' => $this->pageSize ]); - $this->totalCount = $spaceDirectoryQuery->pagination->totalCount; - $results = []; foreach ($spaceDirectoryQuery->all() as $space) { $results[] = new SearchRecord($space); } - return $results; + return [ + 'totalCount' => $spaceDirectoryQuery->pagination->totalCount, + 'results' => $results + ]; } } diff --git a/protected/humhub/modules/user/search/UserSearchProvider.php b/protected/humhub/modules/user/search/UserSearchProvider.php index afe4448b4e..d1c242f8e6 100644 --- a/protected/humhub/modules/user/search/UserSearchProvider.php +++ b/protected/humhub/modules/user/search/UserSearchProvider.php @@ -43,20 +43,21 @@ class UserSearchProvider extends SearchProvider /** * @inheritdoc */ - public function searchResults(): array + public function runSearch(): array { $peopleQuery = new PeopleQuery([ 'defaultFilters' => ['keyword' => $this->keyword], 'pageSize' => $this->pageSize ]); - $this->totalCount = $peopleQuery->pagination->totalCount; - $results = []; foreach ($peopleQuery->all() as $user) { $results[] = new SearchRecord($user); } - return $results; + return [ + 'totalCount' => $peopleQuery->pagination->totalCount, + 'results' => $results + ]; } }