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\FieldType\DBDatetime; |
19
|
|
|
use SilverStripe\ORM\ValidationException; |
20
|
|
|
use SilverStripe\Security\Group; |
21
|
|
|
use SilverStripe\Security\Member; |
22
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
23
|
|
|
use SilverStripe\Versioned\ChangeSet; |
24
|
|
|
use SilverStripe\Versioned\ChangeSetItem; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class \Firesphere\SolrSearch\Compat\DataObjectExtension |
28
|
|
|
* |
29
|
|
|
* @property File|SiteConfig|SiteTree|Group|Member|DataObjectExtension $owner |
30
|
|
|
*/ |
31
|
|
|
class DataObjectExtension extends DataExtension |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var DataList |
35
|
|
|
*/ |
36
|
|
|
protected static $members; |
37
|
|
|
protected static $excludedClasses = [ |
38
|
|
|
DirtyClass::class, |
39
|
|
|
ChangeSet::class, |
40
|
|
|
ChangeSetItem::class, |
41
|
|
|
]; |
42
|
|
|
protected $canViewClasses = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @throws ValidationException |
46
|
|
|
*/ |
47
|
|
|
public function onAfterWrite() |
48
|
|
|
{ |
49
|
|
|
parent::onAfterWrite(); |
50
|
|
|
if (Controller::curr()->getRequest()->getURL() && |
51
|
|
|
strpos('dev/build', Controller::curr()->getRequest()->getURL()) !== false |
52
|
|
|
) { |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
/** @var DataObject $owner */ |
56
|
|
|
$owner = $this->owner; |
57
|
|
|
if (!in_array($owner->ClassName, static::$excludedClasses, true)) { |
58
|
|
|
$record = $this->getDirtyClass($owner); |
59
|
|
|
|
60
|
|
|
$ids = json_decode($record->IDs, 1) ?: []; |
61
|
|
|
try { |
62
|
|
|
$service = new SolrCoreService(); |
63
|
|
|
$service->setInDebugMode(false); |
64
|
|
|
$service->updateItems(ArrayList::create([$owner]), SolrCoreService::UPDATE_TYPE); |
65
|
|
|
// If we don't get an exception, mark the item as clean |
66
|
|
|
$record->Clean = DBDatetime::now()->Format(DBDatetime::ISO_DATETIME); |
67
|
|
|
$record->IDs = json_encode($ids); |
68
|
|
|
} catch (Exception $e) { |
69
|
|
|
$this->registerException($ids, $record, $e); |
70
|
|
|
} |
71
|
|
|
$record->write(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param DataObject $owner |
77
|
|
|
* @return DirtyClass |
78
|
|
|
* @throws ValidationException |
79
|
|
|
*/ |
80
|
|
|
protected function getDirtyClass(DataObject $owner) |
81
|
|
|
{ |
82
|
|
|
// Get the DirtyClass object for this item |
83
|
|
|
/** @var null|DirtyClass $record */ |
84
|
|
|
$record = DirtyClass::get()->filter(['Class' => $owner->ClassName])->first(); |
85
|
|
|
if (!$record || !$record->exists()) { |
|
|
|
|
86
|
|
|
$record = DirtyClass::create([ |
87
|
|
|
'Class' => $owner->ClassName, |
88
|
|
|
'Dirty' => DBDatetime::now()->Format(DBDatetime::ISO_DATETIME), |
89
|
|
|
]); |
90
|
|
|
$record->write(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $record; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param array $ids |
98
|
|
|
* @param $record |
99
|
|
|
* @param Exception $e |
100
|
|
|
*/ |
101
|
|
|
protected function registerException(array $ids, $record, Exception $e): void |
102
|
|
|
{ |
103
|
|
|
/** @var DataObject $owner */ |
104
|
|
|
$owner = $this->owner; |
105
|
|
|
$ids[] = $owner->ID; |
106
|
|
|
// If we don't get an exception, mark the item as clean |
107
|
|
|
$record->Dirty = DBDatetime::now()->Format(DBDatetime::ISO_DATETIME); |
108
|
|
|
$record->IDs = json_encode($ids); |
109
|
|
|
$logger = Injector::inst()->get(LoggerInterface::class); |
110
|
|
|
$logger->warn( |
111
|
|
|
sprintf( |
112
|
|
|
'Unable to alter %s with ID %s', |
113
|
|
|
$owner->ClassName, |
114
|
|
|
$owner->ID |
115
|
|
|
) |
116
|
|
|
); |
117
|
|
|
$logger->error($e->getMessage()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @throws ValidationException |
122
|
|
|
*/ |
123
|
|
|
public function onAfterDelete(): void |
124
|
|
|
{ |
125
|
|
|
/** @var DataObject $owner */ |
126
|
|
|
$owner = $this->owner; |
127
|
|
|
/** @var DirtyClass $record */ |
128
|
|
|
$record = $this->getDirtyClass($owner); |
129
|
|
|
|
130
|
|
|
$ids = json_decode($record->IDs, 1) ?: []; |
131
|
|
|
parent::onAfterDelete(); |
132
|
|
|
try { |
133
|
|
|
(new SolrCoreService())->updateItems(ArrayList::create([$owner]), SolrCoreService::DELETE_TYPE); |
134
|
|
|
$record->Clean = DBDatetime::now()->Format(DBDatetime::ISO_DATETIME); |
135
|
|
|
$record->IDs = json_encode($ids); |
136
|
|
|
} catch (Exception $e) { |
137
|
|
|
$this->registerException($ids, $record, $e); |
138
|
|
|
} |
139
|
|
|
$record->write(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get the view status for each member in this object |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
public function getViewStatus(): array |
147
|
|
|
{ |
148
|
|
|
/** @var DataObject $owner */ |
149
|
|
|
$owner = $this->owner; |
150
|
|
|
$return = []; |
151
|
|
|
// Add null users if it's publicly viewable |
152
|
|
|
if ($owner->canView()) { |
153
|
|
|
return ['1-null']; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (!self::$members) { |
157
|
|
|
self::$members = Member::get(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
foreach (self::$members as $member) { |
161
|
|
|
$return[] = sprintf('%s-%s', $owner->canView($member), $member->ID); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $return; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|