Skip to content

Commit

Permalink
Silverstripe 5 compatibility (#35)
Browse files Browse the repository at this point in the history
* Updated composer dependencies
* Before adding/updating an index content check
Empty content is checked for before adding/updating an index. If it is empty the index is not added/updated
  • Loading branch information
aletail authored Apr 29, 2024
1 parent af6a8b2 commit a9aefad
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 76 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
"license": "MIT",
"homepage": "https://github.com/werkbot/silverstripe-module-search",
"require": {
"php": "^7.3 || ^8.0",
"silverstripe/cms": "~4.8",
"silverstripe/framework": "~4.8",
"teamtnt/tntsearch": "v2.8.0",
"php": "^8.1",
"silverstripe/cms": "^5",
"silverstripe/framework": "^5",
"teamtnt/tntsearch": "v4.2.1",
"ext-sqlite3": "*",
"symbiote/silverstripe-queuedjobs": "*"
"symbiote/silverstripe-queuedjobs": "^5.1"
},
"autoload": {
"psr-4": {
Expand Down
151 changes: 80 additions & 71 deletions src/SearchableExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,54 +38,54 @@ class SearchableExtension extends DataExtension
SiteTree.ID = Page.ID
WHERE
SiteTree.ShowInSearch = '1'";
**/
**/
public function getIndexQuery()
{
return false;
return false;
}
/**
* getSearchableTitle
* Returns the title, to be used in search results
* Override if Title uses a different variable name
*
* @return string
**/
**/
public function getSearchableTitle()
{
if ($this->owner->SearchableExtension_Title_ColumnName) {
return $this->owner->{$this->owner->SearchableExtension_Title_ColumnName};
} else {
return $this->owner->Title;
}
if ($this->owner->SearchableExtension_Title_ColumnName) {
return $this->owner->{$this->owner->SearchableExtension_Title_ColumnName};
} else {
return $this->owner->Title;
}
}
/**
* getSearchableTitleColumnName
* Returns the name of the Title Column, "Title" is returned if the
* SearchableExtension_Title_ColumnName is not overridden
* @return string
**/
**/
public function getSearchableTitleColumnName()
{
if ($this->owner->SearchableExtension_Title_ColumnName) {
return $this->owner->SearchableExtension_Title_ColumnName;
} else {
return "Title";
}
if ($this->owner->SearchableExtension_Title_ColumnName) {
return $this->owner->SearchableExtension_Title_ColumnName;
} else {
return "Title";
}
}
/**
* getSearchableSummary
* Returns the content to be used in search results
* Override if Content uses a different variable name
*
* @return string
**/
**/
public function getSearchableSummary()
{
if ($this->owner->SearchableExtension_Summary_ColumnName) {
return $this->owner->{$this->owner->SearchableExtension_Summary_ColumnName};
} else {
return $this->owner->Content;
}
if ($this->owner->SearchableExtension_Summary_ColumnName) {
return $this->owner->{$this->owner->SearchableExtension_Summary_ColumnName};
} else {
return $this->owner->Content;
}
}
/**
* getSearchableContent
Expand All @@ -96,7 +96,7 @@ public function getSearchableSummary()
* return $this->Content . implode(',', $this->Children()->column('SearchTerm'));
*
* @return string
**/
**/
public function getSearchableContent()
{
return $this->getSearchableSummary();
Expand All @@ -106,121 +106,130 @@ public function getSearchableContent()
* Returns the name of the Summary Column, "Content" is returned if the
* SearchableExtension_Summary_ColumnName is not overridden
* @return string
**/
**/
public function getSearchableSummaryColumnName()
{
if ($this->owner->SearchableExtension_Summary_ColumnName) {
return $this->owner->SearchableExtension_Summary_ColumnName;
} else {
return "Content";
}
if ($this->owner->SearchableExtension_Summary_ColumnName) {
return $this->owner->SearchableExtension_Summary_ColumnName;
} else {
return "Content";
}
}
/**
* insertIndex
*
* @return void
**/
**/
public function insertIndex()
{
$index = TNTSearchHelper::Instance()->getTNTSearchIndex();
$index->insert([
'ID' => ClassInfo::shortName($this->owner->ClassName)."_".$this->owner->ID,
'ClassName' => $this->owner->ClassName,
'Title' => $this->owner->getSearchableTitle(),
'Content' => $this->owner->getSearchableContent(),
]);
$content = $this->owner->getSearchableContent();
if (!$content) {
return;
}

$index = TNTSearchHelper::Instance()->getTNTSearchIndex();
$index->insert([
'ID' => ClassInfo::shortName($this->owner->ClassName) . "_" . $this->owner->ID,
'ClassName' => $this->owner->ClassName,
'Title' => $this->owner->getSearchableTitle(),
'Content' => $content,
]);
}
/**
* updateIndex
*
* @return void
**/
**/
public function updateIndex()
{
$index = TNTSearchHelper::Instance()->getTNTSearchIndex();
$index->update(
ClassInfo::shortName($this->owner->ClassName)."_".$this->owner->ID,
[
'ID' => ClassInfo::shortName($this->owner->ClassName)."_".$this->owner->ID,
'ClassName' => $this->owner->ClassName,
'Title' => $this->owner->getSearchableTitle(),
'Content' => $this->owner->getSearchableContent(),
]
);
$content = $this->owner->getSearchableContent();
if (!$content) {
return;
}

$index = TNTSearchHelper::Instance()->getTNTSearchIndex();
$index->update(
ClassInfo::shortName($this->owner->ClassName) . "_" . $this->owner->ID,
[
'ID' => ClassInfo::shortName($this->owner->ClassName) . "_" . $this->owner->ID,
'ClassName' => $this->owner->ClassName,
'Title' => $this->owner->getSearchableTitle(),
'Content' => $content,
]
);
}
/**
* deleteIndex
*
* @return void
**/
**/
public function deleteIndex()
{
$index = TNTSearchHelper::Instance()->getTNTSearchIndex();
$index->delete(ClassInfo::shortName($this->owner->ClassName)."_".$this->owner->ID);
$index = TNTSearchHelper::Instance()->getTNTSearchIndex();
$index->delete(ClassInfo::shortName($this->owner->ClassName) . "_" . $this->owner->ID);
}
/**
* onBeforeWrite
*
* @return void
**/
**/
public function onBeforeWrite()
{
if ($this->owner->isInDB() && !$this->owner->hasExtension(Versioned::class)) {
if ($this->owner->isChanged($this->owner->SearchableExtension_Title_ColumnName) || $this->owner->isChanged($this->owner->SearchableExtension_Summary_ColumnName)) {
$this->owner->updateIndex();
}
if ($this->owner->isInDB() && !$this->owner->hasExtension(Versioned::class)) {
if ($this->owner->isChanged($this->owner->SearchableExtension_Title_ColumnName) || $this->owner->isChanged($this->owner->SearchableExtension_Summary_ColumnName)) {
$this->owner->updateIndex();
}
parent::onBeforeWrite();
}
parent::onBeforeWrite();
}
/**
* onAfterWrite
*
* @return void
**/
**/
public function onAfterWrite()
{
if ($this->owner->isChanged('ID') && !$this->owner->hasExtension(Versioned::class)) {
$this->owner->insertIndex();
}
parent::onAfterWrite();
if ($this->owner->isChanged('ID') && !$this->owner->hasExtension(Versioned::class)) {
$this->owner->insertIndex();
}
parent::onAfterWrite();
}
/**
* onBeforePublish
*
* @return void
**/
**/
public function onBeforePublish()
{
if (!$this->owner->isPublished()) {
$this->owner->insertIndex();
}
if (!$this->owner->isPublished()) {
$this->owner->insertIndex();
}
}
/**
* onAfterPublish
*
* @return void
**/
**/
public function onAfterPublish()
{
$this->owner->updateIndex();
$this->owner->updateIndex();
}
/**
* onAfterUnpublish
*
* @return void
**/
**/
public function onAfterUnpublish()
{
$this->owner->deleteIndex();
$this->owner->deleteIndex();
}
/**
* onAfterDelete
*
* @return void
**/
**/
public function onAfterDelete()
{
$this->owner->deleteIndex();
$this->owner->deleteIndex();
}
}

0 comments on commit a9aefad

Please sign in to comment.