2011-05-02 06:33:05 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class is responsible for capturing changes to DataObjects and triggering index updates of the resulting dirty index
|
|
|
|
* items.
|
|
|
|
*
|
|
|
|
* Attached automatically by _config calling SearchUpdater#bind_manipulation_capture. Overloads the current database connector's
|
|
|
|
* manipulate method - basically we need to capture a manipulation _after_ all the augmentManipulation code (for instance Version's)
|
|
|
|
* is run
|
|
|
|
*
|
|
|
|
* Pretty closely tied to the field structure of SearchIndex.
|
|
|
|
*
|
2013-07-25 04:27:09 +02:00
|
|
|
* TODO: The way we bind in is awful hacky.
|
2011-05-02 06:33:05 +02:00
|
|
|
*/
|
|
|
|
class SearchUpdater extends Object {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace the database object with a subclass that captures all manipulations and passes them to us
|
|
|
|
*/
|
|
|
|
static function bind_manipulation_capture() {
|
|
|
|
global $databaseConfig;
|
|
|
|
|
2012-07-19 04:14:12 +02:00
|
|
|
$current = DB::getConn();
|
|
|
|
if (!$current || @$current->isManipulationCapture) return; // If not yet set, or its already captured, just return
|
2012-07-19 02:09:15 +02:00
|
|
|
|
2013-04-12 06:31:10 +02:00
|
|
|
$type = get_class($current);
|
2011-05-02 06:33:05 +02:00
|
|
|
$file = TEMP_FOLDER."/.cache.SMC.$type";
|
|
|
|
|
|
|
|
if (!is_file($file)) {
|
|
|
|
file_put_contents($file, "<?php
|
|
|
|
class SearchManipulateCapture_$type extends $type {
|
2012-07-19 02:09:15 +02:00
|
|
|
public \$isManipulationCapture = true;
|
|
|
|
|
2011-05-02 06:33:05 +02:00
|
|
|
function manipulate(\$manipulation) {
|
|
|
|
\$res = parent::manipulate(\$manipulation);
|
|
|
|
SearchUpdater::handle_manipulation(\$manipulation);
|
|
|
|
return \$res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
");
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once($file);
|
2012-07-19 02:09:15 +02:00
|
|
|
$dbClass = 'SearchManipulateCapture_'.$type;
|
|
|
|
|
2015-08-27 22:43:03 +02:00
|
|
|
/** @var SS_Database $captured */
|
2012-07-19 04:14:12 +02:00
|
|
|
$captured = new $dbClass($databaseConfig);
|
2015-08-27 22:43:03 +02:00
|
|
|
|
|
|
|
// Framework 3.2+ ORM needs some dependencies set
|
|
|
|
if (method_exists($captured, "setConnector")) {
|
|
|
|
$captured->setConnector($current->getConnector());
|
|
|
|
$captured->setQueryBuilder($current->getQueryBuilder());
|
|
|
|
$captured->setSchemaManager($current->getSchemaManager());
|
|
|
|
}
|
|
|
|
|
2012-07-19 04:14:12 +02:00
|
|
|
// The connection might have had it's name changed (like if we're currently in a test)
|
|
|
|
$captured->selectDatabase($current->currentDatabase());
|
|
|
|
DB::setConn($captured);
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static $registered = false;
|
2013-07-25 04:27:09 +02:00
|
|
|
/** @var SearchUpdateProcessor */
|
|
|
|
static $processor = null;
|
|
|
|
|
2011-05-02 06:33:05 +02:00
|
|
|
/**
|
|
|
|
* Called by the SearchManiplateCapture database adapter with every manipulation made against the database.
|
|
|
|
*
|
|
|
|
* Check every index to see what objects need re-inserting into what indexes to keep the index fresh,
|
|
|
|
* but doesn't actually do it yet.
|
|
|
|
*
|
|
|
|
* TODO: This is pretty sensitive to the format of manipulation that DataObject::write produces. Specifically,
|
|
|
|
* it expects the actual class of the object to be present as a table, regardless of if any fields changed in that table
|
|
|
|
* (so a class => array( 'fields' => array() ) item), in order to find the actual class for a set of table manipulations
|
|
|
|
*/
|
|
|
|
static function handle_manipulation($manipulation) {
|
|
|
|
// First, extract any state that is in the manipulation itself
|
|
|
|
foreach ($manipulation as $table => $details) {
|
|
|
|
$manipulation[$table]['class'] = $table;
|
|
|
|
$manipulation[$table]['state'] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchVariant::call('extractManipulationState', $manipulation);
|
|
|
|
|
|
|
|
// Then combine the manipulation back into object field sets
|
2015-05-06 07:01:41 +02:00
|
|
|
|
2011-05-02 06:33:05 +02:00
|
|
|
$writes = array();
|
|
|
|
|
|
|
|
foreach ($manipulation as $table => $details) {
|
|
|
|
if (!isset($details['id']) || !isset($details['fields'])) continue;
|
|
|
|
|
|
|
|
$id = $details['id'];
|
|
|
|
$state = $details['state'];
|
|
|
|
$class = $details['class'];
|
|
|
|
$fields = $details['fields'];
|
|
|
|
|
|
|
|
$base = ClassInfo::baseDataClass($class);
|
|
|
|
$key = "$id:$base:".serialize($state);
|
|
|
|
|
|
|
|
$statefulids = array(array('id' => $id, 'state' => $state));
|
2015-05-06 07:01:41 +02:00
|
|
|
|
2011-05-02 06:33:05 +02:00
|
|
|
// Is this the first table for this particular object? Then add an item to $writes
|
2015-05-06 07:01:41 +02:00
|
|
|
if (!isset($writes[$key])) {
|
|
|
|
$writes[$key] = array(
|
|
|
|
'base' => $base,
|
|
|
|
'class' => $class,
|
|
|
|
'id' => $id,
|
|
|
|
'statefulids' => $statefulids,
|
|
|
|
'fields' => array()
|
|
|
|
);
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
// Otherwise update the class label if it's more specific than the currently recorded one
|
2015-05-06 07:01:41 +02:00
|
|
|
else if (is_subclass_of($class, $writes[$key]['class'])) {
|
|
|
|
$writes[$key]['class'] = $class;
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
|
|
|
|
// Update the fields
|
|
|
|
foreach ($fields as $field => $value) {
|
|
|
|
$writes[$key]['fields']["$class:$field"] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then extract any state that is needed for the writes
|
|
|
|
|
|
|
|
SearchVariant::call('extractManipulationWriteState', $writes);
|
|
|
|
|
2015-05-06 07:01:41 +02:00
|
|
|
// Submit all of these writes to the search processor
|
|
|
|
|
|
|
|
static::process_writes($writes);
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-05-06 07:01:41 +02:00
|
|
|
/**
|
|
|
|
* Send updates to the current search processor for execution
|
|
|
|
*
|
|
|
|
* @param array $writes
|
|
|
|
*/
|
|
|
|
public static function process_writes($writes) {
|
2011-05-02 06:33:05 +02:00
|
|
|
foreach ($writes as $write) {
|
|
|
|
// For every index
|
|
|
|
foreach (FullTextSearch::get_indexes() as $index => $instance) {
|
|
|
|
// If that index as a field from this class
|
|
|
|
if (SearchIntrospection::is_subclass_of($write['class'], $instance->dependancyList)) {
|
|
|
|
// Get the dirty IDs
|
|
|
|
$dirtyids = $instance->getDirtyIDs($write['class'], $write['id'], $write['statefulids'], $write['fields']);
|
|
|
|
|
|
|
|
// Then add then then to the global list to deal with later
|
|
|
|
foreach ($dirtyids as $dirtyclass => $ids) {
|
2013-07-25 04:27:09 +02:00
|
|
|
if ($ids) {
|
2015-05-06 07:01:41 +02:00
|
|
|
if (!self::$processor) {
|
|
|
|
self::$processor = Injector::inst()->create('SearchUpdateProcessor');
|
|
|
|
}
|
2013-07-25 04:27:09 +02:00
|
|
|
self::$processor->addDirtyIDs($dirtyclass, $ids, $index);
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 07:01:41 +02:00
|
|
|
// If we do have some work to do register the shutdown function to actually do the work
|
2011-05-02 06:33:05 +02:00
|
|
|
|
|
|
|
// Don't do it if we're testing - there's no database connection outside the test methods, so we'd
|
|
|
|
// just get errors
|
2015-05-06 07:01:41 +02:00
|
|
|
$runningTests = class_exists('SapphireTest', false) && SapphireTest::is_running_test();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2013-07-25 04:27:09 +02:00
|
|
|
if (self::$processor && !self::$registered && !$runningTests) {
|
2011-05-02 06:33:05 +02:00
|
|
|
register_shutdown_function(array("SearchUpdater", "flush_dirty_indexes"));
|
|
|
|
self::$registered = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throw away the recorded dirty IDs without doing anything with them.
|
|
|
|
*/
|
|
|
|
static function clear_dirty_indexes() {
|
2013-07-25 04:27:09 +02:00
|
|
|
self::$processor = null;
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do something with the recorded dirty IDs, where that "something" depends on the value of self::$update_method,
|
|
|
|
* either immediately update the indexes, queue a messsage to update the indexes at some point in the future, or
|
|
|
|
* just throw the dirty IDs away.
|
|
|
|
*/
|
|
|
|
static function flush_dirty_indexes() {
|
2013-07-25 04:27:09 +02:00
|
|
|
if (!self::$processor) return;
|
|
|
|
self::$processor->triggerProcessing();
|
|
|
|
self::$processor = null;
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|
|
|
|
}
|
2012-07-19 02:09:15 +02:00
|
|
|
|
|
|
|
class SearchUpdater_BindManipulationCaptureFilter implements RequestFilter {
|
|
|
|
public function preRequest(SS_HTTPRequest $request, Session $session, DataModel $model) {
|
|
|
|
SearchUpdater::bind_manipulation_capture();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function postRequest(SS_HTTPRequest $request, SS_HTTPResponse $response, DataModel $model) {
|
|
|
|
/* NOP */
|
|
|
|
}
|
|
|
|
}
|
2014-03-24 21:55:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete operations do not use database manipulations.
|
2015-05-06 07:01:41 +02:00
|
|
|
*
|
2014-03-24 21:55:13 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-05-06 07:01:41 +02:00
|
|
|
class SearchUpdater_ObjectHandler extends DataExtension {
|
2014-03-24 21:55:13 +01:00
|
|
|
|
|
|
|
public function onAfterDelete() {
|
|
|
|
// Calling delete() on empty objects does nothing
|
|
|
|
if (!$this->owner->ID) return;
|
2015-05-06 07:01:41 +02:00
|
|
|
|
2014-03-24 21:55:13 +01:00
|
|
|
// Force SearchUpdater to mark this record as dirty
|
|
|
|
$manipulation = array(
|
|
|
|
$this->owner->ClassName => array(
|
|
|
|
'fields' => array(),
|
|
|
|
'id' => $this->owner->ID,
|
|
|
|
'command' => 'update'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->owner->extend('augmentWrite', $manipulation);
|
|
|
|
SearchUpdater::handle_manipulation($manipulation);
|
|
|
|
}
|
|
|
|
|
2015-05-06 07:01:41 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
2015-05-07 11:57:35 +02:00
|
|
|
$state = SearchVariant::current_state($class);
|
2015-05-06 07:01:41 +02:00
|
|
|
$base = ClassInfo::baseDataClass($class);
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2014-03-24 21:55:13 +01:00
|
|
|
}
|