Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/en/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ The index is stored in the root directory of your project: `/search/site.index`

The index is initially created on a `dev/build`. Additional `dev/build`'s will update the index for all objects with the extension.

Alternatively, you can run `dev/tasks/Werkbot-Search-Tasks-SearchIndex` to build the index. Here, you can add `?debug` to the task URL to catch any potentially invalid index queries.

For all objects that have the extension applied, the index is updated on creation, edits/updates and delete/removals. So the index is always up to date.


Expand Down
27 changes: 24 additions & 3 deletions src/SearchableExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Werkbot\Search;

use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\FieldList;
Expand Down Expand Up @@ -256,8 +257,28 @@ public function getSearchableSummaryColumnName()
*/
public function getIndexQueryDeclaringClass()
{
$definesGetIndexQuery = new \ReflectionMethod($this->owner, 'getIndexQuery');
return $definesGetIndexQuery->getDeclaringClass()->getName();
try {
$definesGetIndexQuery = new \ReflectionMethod($this->owner, 'getIndexQuery');
return $definesGetIndexQuery->getDeclaringClass()->getName();
} catch (\ReflectionException $e) {
// If the base class does not define getIndexQuery, it may be defined in an Extension
return $this->getUpdateQueryDeclaringClass($this->owner->ClassName);
}
}

/**
* Recursively check extensions for the class with the extension that defines the updateIndexQuery method
* @param string $class - The DataOject class name to check for the method
* @return string|null - The class name with an extension that defines the updateIndexQuery method
*/
public function getUpdateQueryDeclaringClass($class)
{
if (!$class) return;
$extensions = Config::inst()->get($class, 'extensions', Config::UNINHERITED) ?: [];
foreach (array_reverse($extensions) as $extensionClass) {
if (method_exists($extensionClass, 'updateIndexQuery')) return $class;
}
return $this->getUpdateQueryDeclaringClass(get_parent_class($class));
}

/**
Expand All @@ -279,14 +300,14 @@ public function getIndexDocument()
{
$id = $this->getSearchableID();
$classQuery = rtrim($this->owner->getIndexQuery(), ';');
$classQuery = str_replace('"', "'", $classQuery);
$query = <<<SQL
SELECT * FROM (
$classQuery
) AS indexquery
WHERE
ID = '$id'
SQL;
$query = str_replace('"', "'", $query);
return DB::query($query)->record();
}

Expand Down
13 changes: 13 additions & 0 deletions src/Tasks/SearchIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public function run($request)
echo "Created search folder<br /><br />";
}

$debugMode = $request->getVar('debug') !== null;

$indexer = TNTSearchHelper::Instance()->getTNTSearchIndex(true);
$classes = ClassInfo::classesWithExtension(SearchableExtension::class);

Expand All @@ -30,6 +32,17 @@ public function run($request)
if ($classQuery = $searchableClass->getIndexQuery()) {
// Remove semi-colon if it exists
$classQuery = rtrim($classQuery, ';');
$classQuery = str_replace('"', "'", $classQuery);

if ($debugMode) {
// Ensure this query is valid by running it before adding it to the indexer
try {
DB::query($classQuery);
} catch (\SilverStripe\ORM\Connect\DatabaseException $e) {
echo "<br>Error in query for class $className: " . $e->getMessage() . '<br>';
return;
}
}

$query .= $classQuery . ' UNION ALL ';

Expand Down
Loading