1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Extensions; |
5
|
|
|
|
6
|
|
|
use Exception; |
7
|
|
|
use Firesphere\SolrSearch\Models\DirtyClass; |
8
|
|
|
use Firesphere\SolrSearch\Services\SolrCoreService; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use SilverStripe\Assets\File; |
11
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
12
|
|
|
use SilverStripe\Control\Controller; |
13
|
|
|
use SilverStripe\Core\Injector\Injector; |
14
|
|
|
use SilverStripe\ORM\ArrayList; |
15
|
|
|
use SilverStripe\ORM\DataExtension; |
16
|
|
|
use SilverStripe\ORM\DataList; |
17
|
|
|
use SilverStripe\ORM\DataObject; |
18
|
|
|
use SilverStripe\ORM\ValidationException; |
19
|
|
|
use SilverStripe\Security\Group; |
20
|
|
|
use SilverStripe\Security\Member; |
21
|
|
|
use SilverStripe\Security\Security; |
22
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
23
|
|
|
use SilverStripe\Versioned\ChangeSet; |
24
|
|
|
use SilverStripe\Versioned\ChangeSetItem; |
25
|
|
|
use SilverStripe\Versioned\Versioned; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class \Firesphere\SolrSearch\Compat\DataObjectExtension |
29
|
|
|
* |
30
|
|
|
* @property CarouselItem|BlocksPage|Item|File|Image|SiteConfig|ChangeSetItem|SecurityAlert|Package|ElementalArea|ElementForm|Blog|SiteTree|Group|Member|EditableCustomRule|EditableFormField|UserDefinedForm|EditableOption|DataObjectExtension $owner |
31
|
|
|
*/ |
32
|
|
|
class DataObjectExtension extends DataExtension |
33
|
|
|
{ |
34
|
|
|
public const WRITE = 'write'; |
35
|
|
|
public const DELETE = 'delete'; |
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
public static $canViewClasses = []; |
40
|
|
|
/** |
41
|
|
|
* @var DataList |
42
|
|
|
*/ |
43
|
|
|
protected static $members; |
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected static $excludedClasses = [ |
48
|
|
|
DirtyClass::class, |
49
|
|
|
ChangeSet::class, |
50
|
|
|
ChangeSetItem::class, |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @throws ValidationException |
55
|
|
|
*/ |
56
|
63 |
|
public function onAfterWrite() |
57
|
|
|
{ |
58
|
|
|
/** @var DataObject $owner */ |
59
|
63 |
|
$owner = $this->owner; |
60
|
63 |
|
if (in_array($owner->ClassName, static::$excludedClasses, true) || |
61
|
63 |
|
(Controller::curr()->getRequest()->getURL() && |
62
|
63 |
|
strpos('dev/build', Controller::curr()->getRequest()->getURL()) !== false) |
63
|
|
|
) { |
64
|
62 |
|
return; |
65
|
|
|
} |
66
|
63 |
|
if (!$owner->hasExtension(Versioned::class)) { |
67
|
62 |
|
$this->pushToSolr($owner); |
68
|
|
|
} |
69
|
63 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @throws ValidationException |
73
|
|
|
*/ |
74
|
2 |
|
public function onAfterPublish() |
75
|
|
|
{ |
76
|
2 |
|
$this->pushToSolr($this->owner); |
|
|
|
|
77
|
2 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param DataObject $owner |
81
|
|
|
* @param string $type |
82
|
|
|
* @return DirtyClass |
83
|
|
|
* @throws ValidationException |
84
|
|
|
*/ |
85
|
63 |
|
protected function getDirtyClass(DataObject $owner, $type) |
86
|
|
|
{ |
87
|
|
|
// Get the DirtyClass object for this item |
88
|
|
|
/** @var null|DirtyClass $record */ |
89
|
63 |
|
$record = DirtyClass::get()->filter(['Class' => $owner->ClassName, 'Type' => $type])->first(); |
90
|
63 |
|
if (!$record || !$record->exists()) { |
|
|
|
|
91
|
62 |
|
$record = DirtyClass::create([ |
92
|
62 |
|
'Class' => $owner->ClassName, |
93
|
62 |
|
'Type' => $type |
94
|
|
|
]); |
95
|
62 |
|
$record->write(); |
96
|
|
|
} |
97
|
|
|
|
98
|
63 |
|
return $record; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array $ids |
103
|
|
|
* @param $record |
104
|
|
|
* @param Exception $e |
105
|
|
|
*/ |
106
|
|
|
protected function registerException(array $ids, $record, Exception $error): void |
107
|
|
|
{ |
108
|
|
|
/** @var DataObject $owner */ |
109
|
|
|
$owner = $this->owner; |
110
|
|
|
$ids[] = $owner->ID; |
111
|
|
|
// If we don't get an exception, mark the item as clean |
112
|
|
|
$record->IDs = json_encode($ids); |
113
|
|
|
$record->write(); |
114
|
|
|
$logger = Injector::inst()->get(LoggerInterface::class); |
115
|
|
|
$logger->warn( |
116
|
|
|
sprintf( |
117
|
|
|
'Unable to alter %s with ID %s', |
118
|
|
|
$owner->ClassName, |
119
|
|
|
$owner->ID |
120
|
|
|
) |
121
|
|
|
); |
122
|
|
|
$logger->error($error->getMessage()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @throws ValidationException |
127
|
|
|
*/ |
128
|
2 |
|
public function onAfterDelete(): void |
129
|
|
|
{ |
130
|
|
|
/** @var DataObject $owner */ |
131
|
2 |
|
$owner = $this->owner; |
132
|
|
|
/** @var DirtyClass $record */ |
133
|
2 |
|
$record = $this->getDirtyClass($owner, self::DELETE); |
134
|
|
|
|
135
|
2 |
|
$ids = json_decode($record->IDs, 1) ?: []; |
136
|
2 |
|
parent::onAfterDelete(); |
137
|
|
|
try { |
138
|
2 |
|
(new SolrCoreService())->updateItems(ArrayList::create([$owner]), SolrCoreService::DELETE_TYPE); |
139
|
|
|
// If successful, remove it from the array |
140
|
|
|
// Added bonus, array_flip removes duplicates |
141
|
2 |
|
$values = array_flip($ids); |
142
|
2 |
|
unset($values[$owner->ID]); |
143
|
|
|
|
144
|
2 |
|
$record->IDs = json_encode(array_keys($values)); |
145
|
2 |
|
$record->write(); |
146
|
|
|
} catch (Exception $error) { |
147
|
|
|
$this->registerException($ids, $record, $error); |
148
|
|
|
} |
149
|
2 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the view status for each member in this object |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
5 |
|
public function getViewStatus(): array |
156
|
|
|
{ |
157
|
|
|
// Return empty if it's not allowed to show in search |
158
|
|
|
// The setting needs to be explicitly false, to avoid any possible collision |
159
|
|
|
// with objects not having the setting, thus being `null` |
160
|
|
|
/** @var DataObject|SiteTree $owner */ |
161
|
5 |
|
$owner = $this->owner; |
162
|
|
|
// Return immediately if the owner has ShowInSearch not being `null` |
163
|
5 |
|
if ($owner->ShowInSearch === false || $owner->ShowInSearch === 0) { |
164
|
1 |
|
return []; |
165
|
|
|
} |
166
|
|
|
|
167
|
5 |
|
return self::$canViewClasses[$owner->ClassName] ?? $this->getMemberPermissions($owner); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param DataObject|SiteTree $owner |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
5 |
|
protected function getMemberPermissions($owner): array |
175
|
|
|
{ |
176
|
|
|
// Log out the current user to avoid collisions in permissions |
177
|
5 |
|
$currMember = Security::getCurrentUser(); |
178
|
5 |
|
Security::setCurrentUser(null); |
179
|
|
|
|
180
|
5 |
|
$return = []; |
181
|
|
|
|
182
|
5 |
|
if ($owner->canView(null)) { |
183
|
3 |
|
$return[] = '1-null'; |
184
|
|
|
} else { |
185
|
|
|
// Return a default '0-0' to basically say "noboday can view" |
186
|
3 |
|
$return[] = '0-0'; |
187
|
3 |
|
if (!self::$members) { |
188
|
1 |
|
self::$members = Member::get(); |
189
|
|
|
} |
190
|
3 |
|
foreach (self::$members as $member) { |
191
|
3 |
|
$return[] = sprintf('%s-%s', (int)$owner->canView($member), (int)$member->ID); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|
196
|
5 |
|
if (!$owner->hasField('ShowInSearch')) { |
197
|
1 |
|
self::$canViewClasses[$owner->ClassName] = $return; |
198
|
|
|
} |
199
|
|
|
|
200
|
5 |
|
Security::setCurrentUser($currMember); |
201
|
|
|
|
202
|
5 |
|
return $return; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param DataObject $owner |
207
|
|
|
* @throws ValidationException |
208
|
|
|
*/ |
209
|
62 |
|
protected function pushToSolr(DataObject $owner): void |
210
|
|
|
{ |
211
|
|
|
/** @var DataObject $owner */ |
212
|
62 |
|
$record = $this->getDirtyClass($owner, self::WRITE); |
213
|
|
|
|
214
|
62 |
|
$ids = json_decode($record->IDs, 1) ?: []; |
215
|
62 |
|
$mode = Versioned::get_reading_mode(); |
216
|
|
|
try { |
217
|
62 |
|
Versioned::set_reading_mode(Versioned::DEFAULT_MODE); |
218
|
62 |
|
$service = new SolrCoreService(); |
219
|
62 |
|
$service->setInDebugMode(false); |
220
|
62 |
|
$service->updateItems(ArrayList::create([$owner]), SolrCoreService::UPDATE_TYPE); |
221
|
|
|
// If we don't get an exception, mark the item as clean |
222
|
|
|
// Added bonus, array_flip removes duplicates |
223
|
62 |
|
$values = array_flip($ids); |
224
|
62 |
|
unset($values[$owner->ID]); |
225
|
|
|
|
226
|
62 |
|
$record->IDs = json_encode(array_keys($values)); |
227
|
62 |
|
$record->write(); |
228
|
62 |
|
Versioned::set_reading_mode($mode); |
229
|
|
|
} catch (Exception $error) { |
230
|
|
|
Versioned::set_reading_mode($mode); |
231
|
|
|
$this->registerException($ids, $record, $error); |
232
|
|
|
} |
233
|
62 |
|
} |
234
|
|
|
} |
235
|
|
|
|