|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\FullTextSearch\Search\Extensions; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Core\ClassInfo; |
|
6
|
|
|
use SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater; |
|
7
|
|
|
use SilverStripe\FullTextSearch\Search\Variants\SearchVariant; |
|
8
|
|
|
use SilverStripe\ORM\DataExtension; |
|
9
|
|
|
use SilverStripe\ORM\DataObject; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Delete operations do not use database manipulations. |
|
13
|
|
|
* |
|
14
|
|
|
* If a delete has been requested, force a write on objects that should be |
|
15
|
|
|
* indexed. This causes the object to be marked for deletion from the index. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
class SearchUpdater_ObjectHandler extends DataExtension |
|
19
|
|
|
{ |
|
20
|
|
|
public function onAfterDelete() |
|
21
|
|
|
{ |
|
22
|
|
|
// Calling delete() on empty objects does nothing |
|
23
|
|
|
if (!$this->owner->ID) { |
|
24
|
|
|
return; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
// Force SearchUpdater to mark this record as dirty |
|
28
|
|
|
// Note: Some extensions require entire hierarchy passed to augmentWrite() |
|
29
|
|
|
$manipulation = array(); |
|
30
|
|
|
foreach (ClassInfo::ancestry($this->owner) as $class) { |
|
31
|
|
|
if (!is_subclass_of($class, DataObject::class)) { |
|
|
|
|
|
|
32
|
|
|
continue; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$tableName = DataObject::getSchema()->tableName($class); |
|
36
|
|
|
$manipulation[$tableName] = array( |
|
37
|
|
|
'fields' => array(), |
|
38
|
|
|
'id' => $this->owner->ID, |
|
39
|
|
|
'class' => $class, |
|
40
|
|
|
// Note: 'delete' command not actually handled by manipulations, |
|
41
|
|
|
// but added so that SearchUpdater can detect the deletion |
|
42
|
|
|
'command' => 'delete' |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->owner->extend('augmentWrite', $manipulation); |
|
47
|
|
|
|
|
48
|
|
|
SearchUpdater::handle_manipulation($manipulation); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Forces this object to trigger a re-index in the current state |
|
53
|
|
|
*/ |
|
54
|
|
|
public function triggerReindex() |
|
55
|
|
|
{ |
|
56
|
|
|
if (!$this->owner->ID) { |
|
57
|
|
|
return; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$id = $this->owner->ID; |
|
61
|
|
|
$class = $this->owner->ClassName; |
|
62
|
|
|
$state = SearchVariant::current_state($class); |
|
63
|
|
|
$base = DataObject::getSchema()->baseDataClass($class); |
|
64
|
|
|
$key = "$id:$base:".serialize($state); |
|
65
|
|
|
|
|
66
|
|
|
$statefulids = array(array( |
|
67
|
|
|
'id' => $id, |
|
68
|
|
|
'state' => $state |
|
69
|
|
|
)); |
|
70
|
|
|
|
|
71
|
|
|
$writes = array( |
|
72
|
|
|
$key => array( |
|
73
|
|
|
'base' => $base, |
|
74
|
|
|
'class' => $class, |
|
75
|
|
|
'id' => $id, |
|
76
|
|
|
'statefulids' => $statefulids, |
|
77
|
|
|
'fields' => array() |
|
78
|
|
|
) |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
SearchUpdater::process_writes($writes); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|