2017-04-21 03:26:24 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\FullTextSearch\Search\Updaters;
|
2017-04-26 13:13:26 +02:00
|
|
|
|
2017-04-21 03:26:24 +02:00
|
|
|
use SilverStripe\ORM\DataExtension;
|
2017-04-26 13:13:26 +02:00
|
|
|
use SilverStripe\FullTextSearch\Search\Variants\SearchVariant;
|
2017-05-01 10:18:59 +02:00
|
|
|
use SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater;
|
2017-04-26 13:13:26 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2017-05-01 10:18:59 +02:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
2017-04-21 03:26:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete operations do not use database manipulations.
|
|
|
|
*
|
|
|
|
* If a delete has been requested, force a write on objects that should be
|
|
|
|
* indexed. This causes the object to be marked for deletion from the index.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class SearchUpdater_ObjectHandler extends DataExtension
|
|
|
|
{
|
|
|
|
public function onAfterDelete()
|
|
|
|
{
|
|
|
|
// Calling delete() on empty objects does nothing
|
|
|
|
if (!$this->owner->ID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force SearchUpdater to mark this record as dirty
|
2017-05-01 10:18:59 +02:00
|
|
|
// Note: Some extensions require entire hierarchy passed to augmentWrite()
|
|
|
|
$manipulation = array();
|
|
|
|
foreach (ClassInfo::ancestry($this->owner) as $class) {
|
|
|
|
if (!is_subclass_of($class, DataObject::class)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tableName = DataObject::getSchema()->tableName($class);
|
|
|
|
$manipulation[$tableName] = array(
|
2017-04-21 03:26:24 +02:00
|
|
|
'fields' => array(),
|
|
|
|
'id' => $this->owner->ID,
|
2017-05-01 10:18:59 +02:00
|
|
|
'class' => $class,
|
|
|
|
// Note: 'delete' command not actually handled by manipulations,
|
|
|
|
// but added so that SearchUpdater can detect the deletion
|
|
|
|
'command' => 'delete'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-21 03:26:24 +02:00
|
|
|
$this->owner->extend('augmentWrite', $manipulation);
|
2017-05-01 10:18:59 +02:00
|
|
|
|
2017-04-21 03:26:24 +02:00
|
|
|
SearchUpdater::handle_manipulation($manipulation);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forces this object to trigger a re-index in the current state
|
|
|
|
*/
|
|
|
|
public function triggerReindex()
|
|
|
|
{
|
|
|
|
if (!$this->owner->ID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = $this->owner->ID;
|
|
|
|
$class = $this->owner->ClassName;
|
|
|
|
$state = SearchVariant::current_state($class);
|
2017-04-26 13:13:26 +02:00
|
|
|
$base = DataObject::getSchema()->baseDataClass($class);
|
2017-04-21 03:26:24 +02:00
|
|
|
$key = "$id:$base:".serialize($state);
|
|
|
|
|
|
|
|
$statefulids = array(array(
|
|
|
|
'id' => $id,
|
|
|
|
'state' => $state
|
|
|
|
));
|
|
|
|
|
|
|
|
$writes = array(
|
|
|
|
$key => array(
|
|
|
|
'base' => $base,
|
|
|
|
'class' => $class,
|
|
|
|
'id' => $id,
|
|
|
|
'statefulids' => $statefulids,
|
|
|
|
'fields' => array()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
SearchUpdater::process_writes($writes);
|
|
|
|
}
|
2017-04-26 13:13:26 +02:00
|
|
|
}
|