-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
MetaSearchService.php
148 lines (126 loc) · 3.27 KB
/
MetaSearchService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\services;
use humhub\interfaces\MetaSearchProviderInterface;
use humhub\interfaces\MetaSearchResultInterface;
use Yii;
use yii\helpers\Url;
/**
* Meta Search Service
*
* @author luke
* @since 1.16
*/
class MetaSearchService
{
public int $pageSize = 4;
public int $cacheTimeout = 180;
protected ?int $totalCount = null;
protected ?MetaSearchProviderInterface $provider = null;
public function __construct(MetaSearchProviderInterface $provider)
{
$this->provider = $provider;
}
/**
* @var MetaSearchResultInterface[]|null $results
*/
protected ?array $results = null;
/**
* @var string|null $route Route to the searching page with all filters
*/
protected ?string $route = null;
/**
* Run search process and cache results
*
* @return void
*/
public function search(): void
{
if ($this->provider->getKeyword() === null) {
return;
}
$cacheKey = get_class($this->provider) .
Yii::$app->user->id .
sha1($this->provider->getKeyword() . json_encode($this->provider->getRoute()));
$data = Yii::$app->cache->getOrSet($cacheKey, function () {
return $this->provider->getResults($this->pageSize);
}, $this->cacheTimeout);
$this->totalCount = $data['totalCount'] ?? 0;
$this->results = $data['results'] ?? [];
}
/**
* Get URL to all results filtered with keyword
*
* @return string
*/
public function getUrl(): string
{
$route = $this->provider->getRoute();
$params = is_string($route) ? [$route] : $route;
if ($this->provider->getKeyword() !== null && $this->hasResults()) {
$params['keyword'] = $this->provider->getKeyword();
}
return Url::to($params);
}
/**
* Get a link target depending on URL,
* external URLs should be opened in a new window
*
* @param string|null $url
* @return string|null
*/
public function getLinkTarget(?string $url = null): ?string
{
if ($url === null) {
$url = $this->getUrl();
}
if (!is_string($url) || Url::isRelative($url)) {
return null;
}
$url = parse_url($url);
if (!isset($url['host']) || $url['host'] !== Yii::$app->request->hostName) {
return '_blank';
}
return null;
}
/**
* Check if a searching has been done
*
* @return bool
*/
public function isSearched(): bool
{
return $this->results !== null;
}
/**
* Get number of results
*
* @return int
*/
public function getTotal(): int
{
return isset($this->totalCount) ? (int) $this->totalCount : 0;
}
/**
* Has at least one searched result
*
* @return bool
*/
public function hasResults(): bool
{
return !empty($this->results);
}
/**
* Get searched results
*
* @return MetaSearchResultInterface[]
*/
public function getResults(): array
{
return $this->results ?? [];
}
}