|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Extensions; |
|
5
|
|
|
|
|
6
|
|
|
use Exception; |
|
7
|
|
|
use Firesphere\SolrSearch\Helpers\SolrLogger; |
|
8
|
|
|
use Firesphere\SolrSearch\Models\DirtyClass; |
|
9
|
|
|
use Firesphere\SolrSearch\Services\SolrCoreService; |
|
10
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
11
|
|
|
use Psr\Log\LoggerInterface; |
|
12
|
|
|
use ReflectionException; |
|
13
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
|
14
|
|
|
use SilverStripe\Control\Controller; |
|
15
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
16
|
|
|
use SilverStripe\ORM\ArrayList; |
|
17
|
|
|
use SilverStripe\ORM\DataExtension; |
|
18
|
|
|
use SilverStripe\ORM\DataList; |
|
19
|
|
|
use SilverStripe\ORM\DataObject; |
|
20
|
|
|
use SilverStripe\ORM\ValidationException; |
|
21
|
|
|
use SilverStripe\Security\InheritedPermissionsExtension; |
|
22
|
|
|
use SilverStripe\Security\Member; |
|
23
|
|
|
use SilverStripe\Security\Security; |
|
24
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
|
25
|
|
|
use SilverStripe\Versioned\Versioned; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class \Firesphere\SolrSearch\Compat\DataObjectExtension |
|
29
|
|
|
* |
|
30
|
|
|
* Extend every DataObject with the option to update the index. |
|
31
|
|
|
* |
|
32
|
|
|
* @package Firesphere\SolrSearch\Extensions |
|
33
|
|
|
* @property DataObject|DataObjectExtension $owner |
|
34
|
|
|
*/ |
|
35
|
|
|
class DataObjectExtension extends DataExtension |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var array Cached permission list |
|
39
|
|
|
*/ |
|
40
|
|
|
public static $cachedClasses; |
|
41
|
|
|
/** |
|
42
|
|
|
* @var SiteConfig Current siteconfig |
|
43
|
|
|
*/ |
|
44
|
|
|
protected static $siteConfig; |
|
45
|
|
|
/** |
|
46
|
|
|
* @var ArrayList|Member[] List of all the members in the system |
|
47
|
|
|
*/ |
|
48
|
|
|
protected static $memberList; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
79 |
|
* Push the item to solr if it is not versioned |
|
52
|
|
|
* Update the index after write. |
|
53
|
|
|
* |
|
54
|
79 |
|
* @throws ValidationException |
|
55
|
|
|
* @throws GuzzleException |
|
56
|
79 |
|
* @throws ReflectionException |
|
57
|
79 |
|
*/ |
|
58
|
|
|
public function onAfterWrite() |
|
59
|
79 |
|
{ |
|
60
|
|
|
/** @var DataObject $owner */ |
|
61
|
|
|
$owner = $this->owner; |
|
62
|
|
|
|
|
63
|
|
|
if ($this->shouldPush() && !$owner->hasExtension(Versioned::class)) { |
|
64
|
|
|
$this->pushToSolr($owner); |
|
65
|
79 |
|
} |
|
66
|
|
|
} |
|
67
|
79 |
|
|
|
68
|
79 |
|
/** |
|
69
|
|
|
* Should this write be pushed to Solr |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function shouldPush() |
|
73
|
|
|
{ |
|
74
|
|
|
return !(Controller::curr()->getRequest()->getURL() && |
|
75
|
|
|
strpos('dev/build', Controller::curr()->getRequest()->getURL()) !== false); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
79 |
|
* Try to push the newly updated item to Solr |
|
80
|
|
|
* |
|
81
|
79 |
|
* @param DataObject $owner |
|
82
|
79 |
|
* @throws ValidationException |
|
83
|
79 |
|
* @throws GuzzleException |
|
84
|
|
|
* @throws ReflectionException |
|
85
|
|
|
*/ |
|
86
|
2 |
|
protected function pushToSolr(DataObject $owner) |
|
87
|
|
|
{ |
|
88
|
2 |
|
$service = new SolrCoreService(); |
|
89
|
2 |
|
if (!$service->isValidClass($owner->ClassName)) { |
|
90
|
|
|
return; |
|
91
|
2 |
|
} |
|
92
|
2 |
|
/** @var DataObject $owner */ |
|
93
|
2 |
|
$record = $this->getDirtyClass($owner, SolrCoreService::UPDATE_TYPE); |
|
94
|
|
|
|
|
95
|
2 |
|
$ids = json_decode($record->IDs, 1) ?: []; |
|
96
|
|
|
$mode = Versioned::get_reading_mode(); |
|
97
|
|
|
try { |
|
98
|
2 |
|
Versioned::set_reading_mode(Versioned::DEFAULT_MODE); |
|
99
|
|
|
$service->setInDebugMode(false); |
|
100
|
|
|
$type = SolrCoreService::UPDATE_TYPE; |
|
101
|
2 |
|
// If the object should not show in search, remove it |
|
102
|
|
|
if ($owner->ShowInSearch === 0) { |
|
103
|
|
|
$type = SolrCoreService::DELETE_TYPE; |
|
104
|
|
|
} |
|
105
|
|
|
$service->updateItems(ArrayList::create([$owner]), $type); |
|
106
|
2 |
|
// If we don't get an exception, mark the item as clean |
|
107
|
2 |
|
// Added bonus, array_flip removes duplicates |
|
108
|
|
|
$this->clearIDs($owner, $ids, $record); |
|
109
|
|
|
} catch (Exception $error) { |
|
110
|
|
|
Versioned::set_reading_mode($mode); |
|
111
|
|
|
$this->registerException($ids, $record, $error); |
|
112
|
|
|
} |
|
113
|
|
|
Versioned::set_reading_mode($mode); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
6 |
|
* Find or create a new DirtyClass for recording dirty IDs |
|
118
|
|
|
* |
|
119
|
|
|
* @param DataObject $owner |
|
120
|
|
|
* @param string $type |
|
121
|
6 |
|
* @return DirtyClass |
|
122
|
6 |
|
* @throws ValidationException |
|
123
|
5 |
|
*/ |
|
124
|
5 |
|
protected function getDirtyClass(DataObject $owner, $type) |
|
125
|
5 |
|
{ |
|
126
|
|
|
// Get the DirtyClass object for this item |
|
127
|
5 |
|
/** @var null|DirtyClass $record */ |
|
128
|
|
|
$record = DirtyClass::get()->filter(['Class' => $owner->ClassName, 'Type' => $type])->first(); |
|
129
|
|
|
if (!$record || !$record->exists()) { |
|
130
|
6 |
|
$record = DirtyClass::create([ |
|
131
|
|
|
'Class' => $owner->ClassName, |
|
132
|
|
|
'Type' => $type, |
|
133
|
|
|
]); |
|
134
|
|
|
$record->write(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $record; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
6 |
|
* Remove the owner ID from the dirty ID set |
|
142
|
|
|
* |
|
143
|
6 |
|
* @param DataObject $owner |
|
144
|
6 |
|
* @param array $ids |
|
145
|
|
|
* @param DirtyClass $record |
|
146
|
6 |
|
* @throws ValidationException |
|
147
|
6 |
|
*/ |
|
148
|
6 |
|
protected function clearIDs(DataObject $owner, array $ids, DirtyClass $record): void |
|
149
|
|
|
{ |
|
150
|
|
|
$values = array_flip($ids); |
|
151
|
|
|
unset($values[$owner->ID]); |
|
152
|
|
|
|
|
153
|
|
|
$record->IDs = json_encode(array_keys($values)); |
|
154
|
|
|
$record->write(); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Register the exception of the attempted index for later clean-up use |
|
159
|
|
|
* |
|
160
|
|
|
* @param array $ids |
|
161
|
|
|
* @param $record |
|
162
|
|
|
* @param Exception $error |
|
163
|
|
|
* @throws ValidationException |
|
164
|
|
|
* @throws GuzzleException |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function registerException(array $ids, $record, Exception $error): void |
|
167
|
|
|
{ |
|
168
|
|
|
/** @var DataObject $owner */ |
|
169
|
|
|
$owner = $this->owner; |
|
170
|
|
|
$ids[] = $owner->ID; |
|
171
|
|
|
// If we don't get an exception, mark the item as clean |
|
172
|
|
|
$record->IDs = json_encode($ids); |
|
173
|
|
|
$record->write(); |
|
174
|
|
|
$logger = Injector::inst()->get(LoggerInterface::class); |
|
175
|
|
|
$logger->warn( |
|
176
|
|
|
sprintf( |
|
177
|
|
|
'Unable to alter %s with ID %s', |
|
178
|
|
|
$owner->ClassName, |
|
179
|
|
|
$owner->ID |
|
180
|
|
|
) |
|
181
|
|
|
); |
|
182
|
|
|
$solrLogger = new SolrLogger(); |
|
183
|
|
|
$solrLogger->saveSolrLog('Index'); |
|
184
|
|
|
|
|
185
|
|
|
$logger->error($error->getMessage()); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
2 |
|
/** |
|
189
|
|
|
* Push the item to Solr after publishing |
|
190
|
2 |
|
* |
|
191
|
|
|
* @throws ValidationException |
|
192
|
2 |
|
* @throws GuzzleException |
|
193
|
2 |
|
* @throws ReflectionException |
|
194
|
|
|
*/ |
|
195
|
2 |
|
public function onAfterPublish() |
|
196
|
|
|
{ |
|
197
|
|
|
if ($this->shouldPush()) { |
|
198
|
|
|
/** @var DataObject $owner */ |
|
199
|
|
|
$owner = $this->owner; |
|
200
|
|
|
$this->pushToSolr($owner); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
4 |
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Attempt to remove the item from Solr |
|
206
|
4 |
|
* |
|
207
|
|
|
* @throws ValidationException |
|
208
|
4 |
|
* @throws GuzzleException |
|
209
|
|
|
*/ |
|
210
|
4 |
|
public function onAfterDelete(): void |
|
211
|
|
|
{ |
|
212
|
|
|
/** @var DataObject $owner */ |
|
213
|
4 |
|
$owner = $this->owner; |
|
214
|
|
|
/** @var DirtyClass $record */ |
|
215
|
|
|
$record = $this->getDirtyClass($owner, SolrCoreService::DELETE_TYPE); |
|
216
|
4 |
|
|
|
217
|
|
|
$ids = json_decode($record->IDs, 1) ?: []; |
|
218
|
|
|
|
|
219
|
|
|
try { |
|
220
|
4 |
|
(new SolrCoreService())->updateItems(ArrayList::create([$owner]), SolrCoreService::DELETE_TYPE); |
|
221
|
|
|
// If successful, remove it from the array |
|
222
|
|
|
// Added bonus, array_flip removes duplicates |
|
223
|
|
|
$this->clearIDs($owner, $ids, $record); |
|
224
|
|
|
} catch (Exception $error) { |
|
225
|
|
|
$this->registerException($ids, $record, $error); |
|
226
|
|
|
} |
|
227
|
7 |
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Get the view status for each member in this object |
|
231
|
7 |
|
* |
|
232
|
7 |
|
* @return array |
|
233
|
1 |
|
*/ |
|
234
|
|
|
public function getViewStatus(): array |
|
235
|
|
|
{ |
|
236
|
|
|
// return as early as possible |
|
237
|
7 |
|
/** @var DataObject|SiteTree $owner */ |
|
238
|
1 |
|
$owner = $this->owner; |
|
239
|
|
|
if (isset(static::$cachedClasses[$owner->ClassName])) { |
|
240
|
|
|
return static::$cachedClasses[$owner->ClassName]; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
// Make sure the siteconfig is loaded |
|
244
|
7 |
|
if (!static::$siteConfig) { |
|
245
|
1 |
|
static::$siteConfig = SiteConfig::current_site_config(); |
|
246
|
|
|
} |
|
247
|
|
|
// Return false if it's not allowed to show in search |
|
248
|
7 |
|
// The setting needs to be explicitly false, to avoid any possible collision |
|
249
|
|
|
// with objects not having the setting, thus being `null` |
|
250
|
7 |
|
// Return immediately if the owner has ShowInSearch not being `null` |
|
251
|
1 |
|
if ($owner->ShowInSearch === false || $owner->ShowInSearch === 0) { |
|
252
|
|
|
return ['false']; |
|
253
|
|
|
} |
|
254
|
7 |
|
|
|
255
|
|
|
$permissions = $this->getGroupViewPermissions($owner); |
|
256
|
|
|
|
|
257
|
|
|
if (!$owner->hasExtension(InheritedPermissionsExtension::class)) { |
|
258
|
|
|
static::$cachedClasses[$owner->ClassName] = $permissions; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
return $permissions; |
|
262
|
|
|
} |
|
263
|
7 |
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Determine the view permissions based on group settings |
|
266
|
7 |
|
* |
|
267
|
7 |
|
* @param DataObject|SiteTree|SiteConfig $owner |
|
268
|
1 |
|
* @return array |
|
269
|
1 |
|
*/ |
|
270
|
7 |
|
protected function getGroupViewPermissions($owner): array |
|
271
|
1 |
|
{ |
|
272
|
1 |
|
// Switches are not ideal, but it's a lot more readable this way! |
|
273
|
1 |
|
switch ($owner->CanViewType) { |
|
274
|
7 |
|
case 'LoggedInUsers': |
|
275
|
7 |
|
$return = ['false', 'LoggedIn']; |
|
276
|
7 |
|
break; |
|
277
|
7 |
|
case 'OnlyTheseUsers': |
|
278
|
7 |
|
$return = ['false']; |
|
279
|
7 |
|
$return = array_merge($return, $owner->ViewerGroups()->column('Code')); |
|
280
|
7 |
|
break; |
|
281
|
|
|
case 'Inherit': |
|
282
|
|
|
$parent = !$owner->ParentID ? static::$siteConfig : $owner->Parent(); |
|
283
|
1 |
|
$return = $this->getGroupViewPermissions($parent); |
|
284
|
|
|
break; |
|
285
|
|
|
case 'Anyone': // View is either not implemented, or it's "Anyone" |
|
286
|
7 |
|
$return = ['null']; |
|
287
|
|
|
break; |
|
288
|
|
|
default: |
|
289
|
|
|
// Default to "Anyone can view" |
|
290
|
|
|
$return = ['null']; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
return $return; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Get permissions for viewing per member |
|
298
|
|
|
* @param DataObject $owner |
|
299
|
|
|
* @return array |
|
300
|
|
|
*/ |
|
301
|
|
|
protected function getMemberViewPermissions(DataObject $owner) |
|
302
|
|
|
{ |
|
303
|
|
|
if (!static::$memberList) { |
|
304
|
|
|
static::$memberList = ArrayList::create(Member::get()->toArray()); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
$currentUser = Security::getCurrentUser(); |
|
308
|
|
|
Security::setCurrentUser(null); |
|
309
|
|
|
|
|
310
|
|
|
if ($owner->canView(null)) { |
|
311
|
|
|
Security::setCurrentUser($currentUser); |
|
312
|
|
|
return ['null']; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
$return = ['false']; |
|
316
|
|
|
foreach (static::$memberList as $member) { |
|
317
|
|
|
if ($owner->canView($member)) { |
|
318
|
|
|
$return[] = $member->ID; |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
if (!$owner->hasExtension(InheritedPermissionsExtension::class)) { |
|
323
|
|
|
static::$cachedClasses[$owner->ClassName] = $return; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
return $return; |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
|