Skip to content
Snippets Groups Projects
Commit 7737f090 authored by Jared Hancock's avatar Jared Hancock
Browse files

search: Plumb up deletes to clear the search index

parent 4d074562
No related branches found
No related tags found
No related merge requests found
...@@ -82,16 +82,19 @@ class SearchInterface { ...@@ -82,16 +82,19 @@ class SearchInterface {
} }
function update($model, $id, $content, $new=false, $attrs=array()) { function update($model, $id, $content, $new=false, $attrs=array()) {
if (!$this->backend) if ($this->backend)
return; $this->backend->update($model, $id, $content, $new, $attrs);
$this->backend->update($model, $id, $content, $new, $attrs);
} }
function createModel($model) { function createModel($model) {
return $this->updateModel($model, true); return $this->updateModel($model, true);
} }
function deleteModel($model) {
if ($this->backend)
$this->backend->delete($model);
}
function updateModel($model, $new=false) { function updateModel($model, $new=false) {
// The MySQL backend does not need to index attributes of the // The MySQL backend does not need to index attributes of the
// various models, because those other attributes are available in // various models, because those other attributes are available in
...@@ -104,11 +107,10 @@ class SearchInterface { ...@@ -104,11 +107,10 @@ class SearchInterface {
break; break;
$this->update($model, $model->getId(), $this->update($model, $model->getId(),
$model->getBody()->getSearchable(), $new, $model->getBody()->getSearchable(),
$new === true,
array( array(
'title' => $model->getTitle(), 'title' => $model->getTitle(),
//TODO: send attribute of object_id
'ticket_id' => $model->getThread()->getObjectId(),
'created' => $model->getCreateDate(), 'created' => $model->getCreateDate(),
) )
); );
...@@ -121,7 +123,7 @@ class SearchInterface { ...@@ -121,7 +123,7 @@ class SearchInterface {
$cdata[] = $v; $cdata[] = $v;
$this->update($model, $model->getId(), $this->update($model, $model->getId(),
trim(implode("\n", $cdata)), trim(implode("\n", $cdata)),
$new, $new === true,
array( array(
'title'=> Format::searchable($model->getSubject()), 'title'=> Format::searchable($model->getSubject()),
'number'=> $model->getNumber(), 'number'=> $model->getNumber(),
...@@ -148,7 +150,7 @@ class SearchInterface { ...@@ -148,7 +150,7 @@ class SearchInterface {
$cdata[] = $v; $cdata[] = $v;
$this->update($model, $model->getId(), $this->update($model, $model->getId(),
trim(implode("\n", $cdata)), trim(implode("\n", $cdata)),
$new, $new === true,
array( array(
'title'=> Format::searchable($model->getFullName()), 'title'=> Format::searchable($model->getFullName()),
'emails'=> $model->emails->asArray(), 'emails'=> $model->emails->asArray(),
...@@ -166,7 +168,7 @@ class SearchInterface { ...@@ -166,7 +168,7 @@ class SearchInterface {
$cdata[] = $v; $cdata[] = $v;
$this->update($model, $model->getId(), $this->update($model, $model->getId(),
trim(implode("\n", $cdata)), trim(implode("\n", $cdata)),
$new, $new === true,
array( array(
'title'=> Format::searchable($model->getName()), 'title'=> Format::searchable($model->getName()),
'created'=> $model->getCreateDate(), 'created'=> $model->getCreateDate(),
...@@ -177,7 +179,7 @@ class SearchInterface { ...@@ -177,7 +179,7 @@ class SearchInterface {
case $model instanceof FAQ: case $model instanceof FAQ:
$this->update($model, $model->getId(), $this->update($model, $model->getId(),
$model->getSearchableAnswer(), $model->getSearchableAnswer(),
$new, $new === true,
array( array(
'title'=> Format::searchable($model->getQuestion()), 'title'=> Format::searchable($model->getQuestion()),
'keywords'=> $model->getKeywords(), 'keywords'=> $model->getKeywords(),
...@@ -219,7 +221,7 @@ class SearchInterface { ...@@ -219,7 +221,7 @@ class SearchInterface {
Signal::connect('model.created', array($this, 'createModel'), 'FAQ'); Signal::connect('model.created', array($this, 'createModel'), 'FAQ');
Signal::connect('model.updated', array($this, 'updateModel')); Signal::connect('model.updated', array($this, 'updateModel'));
#Signal::connect('model.deleted', array($this, 'deleteModel')); Signal::connect('model.deleted', array($this, 'deleteModel'));
} }
} }
...@@ -239,6 +241,7 @@ class MysqlSearchBackend extends SearchBackend { ...@@ -239,6 +241,7 @@ class MysqlSearchBackend extends SearchBackend {
// Only index 20 batches per cron run // Only index 20 batches per cron run
var $max_batches = 60; var $max_batches = 60;
var $_reindexed = 0; var $_reindexed = 0;
var $SEARCH_TABLE;
function __construct() { function __construct() {
$this->SEARCH_TABLE = TABLE_PREFIX . '_search'; $this->SEARCH_TABLE = TABLE_PREFIX . '_search';
...@@ -257,8 +260,6 @@ class MysqlSearchBackend extends SearchBackend { ...@@ -257,8 +260,6 @@ class MysqlSearchBackend extends SearchBackend {
} }
function update($model, $id, $content, $new=false, $attrs=array()) { function update($model, $id, $content, $new=false, $attrs=array()) {
if (!($type=ObjectModel::getType($model))) if (!($type=ObjectModel::getType($model)))
return; return;
...@@ -271,6 +272,8 @@ class MysqlSearchBackend extends SearchBackend { ...@@ -271,6 +272,8 @@ class MysqlSearchBackend extends SearchBackend {
if (!$content && !$title) if (!$content && !$title)
return; return;
if (!$id)
return;
$sql = 'REPLACE INTO '.$this->SEARCH_TABLE $sql = 'REPLACE INTO '.$this->SEARCH_TABLE
. ' SET object_type='.db_input($type) . ' SET object_type='.db_input($type)
...@@ -280,6 +283,26 @@ class MysqlSearchBackend extends SearchBackend { ...@@ -280,6 +283,26 @@ class MysqlSearchBackend extends SearchBackend {
return db_query($sql); return db_query($sql);
} }
function delete($model) {
switch (true) {
case $model instanceof Thread:
$sql = 'DELETE s.* FROM '.$this->SEARCH_TABLE
. " s JOIN ".THREAD_ENTRY_TABLE." h ON (h.id = s.object_id) "
. " WHERE s.object_type='H'"
. ' AND h.thread_id='.db_input($model->getId());
return db_query($sql);
default:
if (!($type = ObjectModel::getType($model)))
return;
$sql = 'DELETE FROM '.$this->SEARCH_TABLE
. ' WHERE object_type='.db_input($type)
. ' AND object_id='.db_input($model->getId());
return db_query($sql);
}
}
// Quote things like email addresses // Quote things like email addresses
function quote($query) { function quote($query) {
$parts = array(); $parts = array();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment