2013-07-25 04:27:09 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
abstract class SearchUpdateProcessor {
|
2014-05-16 07:42:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List of dirty records to process in format
|
|
|
|
*
|
|
|
|
* array(
|
|
|
|
* '$BaseClass' => array(
|
|
|
|
* '$State Key' => array(
|
|
|
|
* 'state' => array(
|
|
|
|
* 'key1' => 'value',
|
|
|
|
* 'key2' => 'value'
|
|
|
|
* ),
|
|
|
|
* 'ids' => array(
|
|
|
|
* '*id*' => array(
|
|
|
|
* '*Index Name 1*',
|
|
|
|
* '*Index Name 2*'
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $dirty;
|
|
|
|
|
|
|
|
public function __construct() {
|
2013-07-25 04:27:09 +02:00
|
|
|
$this->dirty = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addDirtyIDs($class, $statefulids, $index) {
|
|
|
|
$base = ClassInfo::baseDataClass($class);
|
|
|
|
$forclass = isset($this->dirty[$base]) ? $this->dirty[$base] : array();
|
|
|
|
|
|
|
|
foreach ($statefulids as $statefulid) {
|
|
|
|
$id = $statefulid['id'];
|
|
|
|
$state = $statefulid['state']; $statekey = serialize($state);
|
|
|
|
|
|
|
|
if (!isset($forclass[$statekey])) {
|
|
|
|
$forclass[$statekey] = array('state' => $state, 'ids' => array($id => array($index)));
|
|
|
|
}
|
|
|
|
else if (!isset($forclass[$statekey]['ids'][$id])) {
|
|
|
|
$forclass[$statekey]['ids'][$id] = array($index);
|
|
|
|
}
|
|
|
|
else if (array_search($index, $forclass[$statekey]['ids'][$id]) === false) {
|
|
|
|
$forclass[$statekey]['ids'][$id][] = $index;
|
|
|
|
// dirty count stays the same
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->dirty[$base] = $forclass;
|
|
|
|
}
|
2014-05-16 07:42:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the list of indexes to process for the dirty items
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function prepareIndexes() {
|
2013-07-25 04:27:09 +02:00
|
|
|
$originalState = SearchVariant::current_state();
|
2014-05-16 07:42:08 +02:00
|
|
|
$dirtyIndexes = array();
|
|
|
|
$dirty = $this->getSource();
|
|
|
|
$indexes = FullTextSearch::get_indexes();
|
|
|
|
foreach ($dirty as $base => $statefulids) {
|
2013-07-25 04:27:09 +02:00
|
|
|
if (!$statefulids) continue;
|
|
|
|
|
|
|
|
foreach ($statefulids as $statefulid) {
|
|
|
|
$state = $statefulid['state'];
|
|
|
|
$ids = $statefulid['ids'];
|
|
|
|
|
|
|
|
SearchVariant::activate_state($state);
|
|
|
|
|
2014-05-16 07:42:08 +02:00
|
|
|
// Ensure that indexes for all new / updated objects are included
|
|
|
|
$objs = DataObject::get($base)->byIDs(array_keys($ids));
|
|
|
|
foreach ($objs as $obj) {
|
2013-07-25 04:27:09 +02:00
|
|
|
foreach ($ids[$obj->ID] as $index) {
|
|
|
|
if (!$indexes[$index]->variantStateExcluded($state)) {
|
|
|
|
$indexes[$index]->add($obj);
|
2014-05-16 07:42:08 +02:00
|
|
|
$dirtyIndexes[$index] = $indexes[$index];
|
2013-07-25 04:27:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($ids[$obj->ID]);
|
|
|
|
}
|
|
|
|
|
2014-05-16 07:42:08 +02:00
|
|
|
// Generate list of records that do not exist and should be removed
|
2013-07-25 04:27:09 +02:00
|
|
|
foreach ($ids as $id => $fromindexes) {
|
|
|
|
foreach ($fromindexes as $index) {
|
|
|
|
if (!$indexes[$index]->variantStateExcluded($state)) {
|
|
|
|
$indexes[$index]->delete($base, $id, $state);
|
2014-05-16 07:42:08 +02:00
|
|
|
$dirtyIndexes[$index] = $indexes[$index];
|
2013-07-25 04:27:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-16 07:42:08 +02:00
|
|
|
|
2013-07-25 04:27:09 +02:00
|
|
|
SearchVariant::activate_state($originalState);
|
2014-05-16 07:42:08 +02:00
|
|
|
return $dirtyIndexes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commits the specified index to the Solr service
|
|
|
|
*
|
|
|
|
* @param SolrIndex $index Index object
|
|
|
|
* @return bool Flag indicating success
|
|
|
|
*/
|
|
|
|
protected function commitIndex($index) {
|
|
|
|
return $index->commit() !== false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the record data source to process
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getSource() {
|
|
|
|
return $this->dirty;
|
|
|
|
}
|
2013-07-25 04:27:09 +02:00
|
|
|
|
2014-05-16 07:42:08 +02:00
|
|
|
/**
|
|
|
|
* Process all indexes, returning true if successful
|
|
|
|
*
|
|
|
|
* @return bool Flag indicating success
|
|
|
|
*/
|
|
|
|
public function process() {
|
|
|
|
// Generate and commit all instances
|
|
|
|
$indexes = $this->prepareIndexes();
|
|
|
|
foreach ($indexes as $index) {
|
|
|
|
if(!$this->commitIndex($index)) return false;
|
2013-07-25 04:27:09 +02:00
|
|
|
}
|
2014-05-16 07:42:08 +02:00
|
|
|
return true;
|
2013-07-25 04:27:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
abstract public function triggerProcessing();
|
|
|
|
}
|