Skip to content

Commit

Permalink
Merge branch 'release/3.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Feb 11, 2020
2 parents 431fd82 + 297e221 commit bd7fb20
Show file tree
Hide file tree
Showing 30 changed files with 557 additions and 157 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# v3.1.0
## 02/11/2020

1. [](#new)
* Require Grav v1.6.21
* Upgraded to TNTSearch version 2.2 (PHP 7.4 fixes)
1. [](#improved)
* Code cleanup
1. [](#bugfix)
* Fixed Grav initialization in CLI
* Work around inconsistencies in page content if page template uses `grav.page` instead of `page`

# v3.0.1
## 02/03/2020

Expand Down
4 changes: 2 additions & 2 deletions blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: TNT Search
version: 3.0.1
version: 3.1.0
testing: false
description: Powerful indexed-based full text search engine powered by TNTSearch
icon: binoculars
Expand All @@ -13,7 +13,7 @@ docs: https://github.com/trilbymedia/grav-plugin-tntsearch/blob/develop/README.m
license: MIT

dependencies:
- { name: grav, version: '>=1.6.3' }
- { name: grav, version: '>=1.6.21' }

form:
validation: strict
Expand Down
15 changes: 9 additions & 6 deletions classes/GravConnector.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
namespace Grav\Plugin\TNTSearch;

use Grav\Common\Config\Config;
use Grav\Common\Grav;
use Grav\Common\Yaml;
use Grav\Common\Page\Page;
use Grav\Plugin\TNTSearchPlugin;

class GravConnector extends \PDO
{
Expand All @@ -12,7 +14,7 @@ public function __construct()

}

public function getAttribute($attribute)
public function getAttribute($attribute): bool
{
return false;
}
Expand All @@ -22,10 +24,11 @@ public function query($query)
$counter = 0;
$results = [];

/** @var Config $config */
$config = Grav::instance()['config'];
$filter = $config->get('plugins.tntsearch.filter');
$default_process = $config->get('plugins.tntsearch.index_page_by_default');
$gtnt = \Grav\Plugin\TNTSearchPlugin::getSearchObjectType();
$gtnt = TNTSearchPlugin::getSearchObjectType();


if ($filter && array_key_exists('items', $filter)) {
Expand Down Expand Up @@ -53,17 +56,17 @@ public function query($query)

// Only process what's configured
if (!$process) {
echo("Skipped $counter $route\n");
echo("Skipped {$counter} {$route}\n");
continue;
}

try {
$fields = $gtnt->indexPageData($page);
$results[] = (array) $fields;
$display_route = isset($fields->display_route) ? $fields->display_route : $route;
echo("Added $counter $display_route\n");
$display_route = $fields->display_route ?? $route;
echo("Added {$counter} {$display_route}\n");
} catch (\Exception $e) {
echo("Skipped $counter $route - {$e->getMessage()}\n");
echo("Skipped {$counter} {$route} - {$e->getMessage()}\n");
continue;
}
}
Expand Down
146 changes: 103 additions & 43 deletions classes/GravTNTSearch.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<?php
namespace Grav\Plugin\TNTSearch;

use Grav\Common\Config\Config;
use Grav\Common\Grav;
use Grav\Common\Language\Language;
use Grav\Common\Page\Interfaces\PageInterface;
use Grav\Common\Page\Pages;
use Grav\Common\Twig\Twig;
use Grav\Common\Uri;
use Grav\Common\Yaml;
use Grav\Common\Page\Collection;
use Grav\Common\Page\Page;
use RocketTheme\Toolbox\Event\Event;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
use TeamTNT\TNTSearch\Exceptions\IndexNotFoundException;
use TeamTNT\TNTSearch\TNTSearch;

Expand All @@ -18,21 +24,31 @@ class GravTNTSearch
protected $index = 'grav.index';
protected $language;

/**
* GravTNTSearch constructor.
* @param array $options
*/
public function __construct($options = [])
{
$search_type = Grav::instance()['config']->get('plugins.tntsearch.search_type', 'auto');
$stemmer = Grav::instance()['config']->get('plugins.tntsearch.stemmer', 'default');
$limit = Grav::instance()['config']->get('plugins.tntsearch.limit', 20);
$snippet = Grav::instance()['config']->get('plugins.tntsearch.snippet', 300);
$data_path = Grav::instance()['locator']->findResource('user://data', true) . '/tntsearch';
/** @var Config $config */
$config = Grav::instance()['config'];

/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];

$search_type = $config->get('plugins.tntsearch.search_type', 'auto');
$stemmer = $config->get('plugins.tntsearch.stemmer', 'default');
$limit = $config->get('plugins.tntsearch.limit', 20);
$snippet = $config->get('plugins.tntsearch.snippet', 300);
$data_path = $locator->findResource('user://data', true) . '/tntsearch';

/** @var Language $language */
$language = Grav::instance()['language'];

if ($language->enabled()) {
$active = $language->getActive();
$default = $language->getDefault();
$this->language = $active ? $active : $default;
$this->language = $active ?: $default;
$this->index = $this->language . '.index';
}

Expand All @@ -50,16 +66,25 @@ public function __construct($options = [])
'phrases' => true,
];

$this->options = array_merge($defaults, $options);
$this->options = array_replace($defaults, $options);
$this->tnt = new TNTSearch();
$this->tnt->loadConfig([
"storage" => $data_path,
"driver" => 'sqlite',
'charset' => 'utf8'
]);
$this->tnt->loadConfig(
[
'storage' => $data_path,
'driver' => 'sqlite',
'charset' => 'utf8'
]
);
}

public function search($query) {
/**
* @param $query
* @return object|string
* @throws IndexNotFoundException
*/
public function search($query)
{
/** @var Uri $uri */
$uri = Grav::instance()['uri'];
$type = $uri->query('search_type');
$this->tnt->selectIndex($this->index);
Expand All @@ -69,14 +94,14 @@ public function search($query) {
$this->tnt->fuzziness = true;
}

$limit = intval($this->options['limit']);
$type = isset($type) ? $type : $this->options['search_type'];
$limit = (int)$this->options['limit'];
$type = $type ?? $this->options['search_type'];

$multiword = null;
if (isset($this->options['phrases']) && $this->options['phrases']) {
if (strlen($query) > 2) {
if ($query[0] === "\"" && $query[strlen($query) - 1] === "\"") {
$multiword = substr($query, 1, strlen($query) - 2);
if ($query[0] === '"' && $query[strlen($query) - 1] === '"') {
$multiword = substr($query, 1, -1);
$type = 'basic';
$query = $multiword;
}
Expand All @@ -102,43 +127,70 @@ public function search($query) {
}
}

$results = $this->tnt->$guess($query, $limit);
$results = $this->tnt->{$guess}($query, $limit);
}

return $this->processResults($results, $query);
}

/**
* @param array $res
* @param string $query
* @return object|string
*/
protected function processResults($res, $query)
{
$counter = 0;
$data = new \stdClass();
$data->number_of_hits = isset($res['hits']) ? $res['hits'] : 0;
$data->number_of_hits = $res['hits'] ?? 0;
$data->execution_time = $res['execution_time'];

/** @var Pages $pages */
$pages = Grav::instance()['pages'];

$counter = 0;
foreach ($res['ids'] as $path) {

if ($counter++ > $this->options['limit']) {
break;
}

$page = $pages->dispatch($path);
$page = $pages->find($path);

if ($page) {
Grav::instance()->fireEvent('onTNTSearchQuery', new Event(['page' => $page, 'query' => $query, 'options' => $this->options, 'fields' => $data, 'gtnt' => $this]));
$event = new Event(
[
'page' => $page,
'query' => $query,
'options' => $this->options,
'fields' => $data,
'gtnt' => $this
]
);
Grav::instance()->fireEvent('onTNTSearchQuery', $event);
}
}

if ($this->options['json']) {
return json_encode($data, JSON_PRETTY_PRINT);
} else {
return $data;
}

return $data;
}

/**
* @param PageInterface $page
* @return string
*/
public static function getCleanContent($page)
{
$twig = Grav::instance()['twig'];
$grav = Grav::instance();
$activePage = $grav['page'];

// Set active page in grav to the one we are currently processing.
unset($grav['page']);
$grav['page'] = $page;

/** @var Twig $twig */
$twig = $grav['twig'];
$header = $page->header();

if (isset($header->tntsearch['template'])) {
Expand All @@ -150,6 +202,10 @@ public static function getCleanContent($page)

$content = preg_replace('/[ \t]+/', ' ', preg_replace('/\s*$^\s*/m', "\n", strip_tags($content)));

// Restore active page in Grav.
unset($grav['page']);
$grav['page'] = $activePage;

return $content;
}

Expand All @@ -159,7 +215,7 @@ public function createIndex()
$indexer = $this->tnt->createIndex($this->index);

// Set the stemmer language if set
if ($this->options['stemmer'] != 'default') {
if ($this->options['stemmer'] !== 'default') {
$indexer->setLanguage($this->options['stemmer']);
}

Expand All @@ -171,16 +227,16 @@ public function selectIndex()
$this->tnt->selectIndex($this->index);
}

public function deleteIndex($obj)
/**
* @param object $object
*/
public function deleteIndex($object)
{
if ($obj instanceof Page) {
$page = $obj;
} else {
if (!$object instanceof Page) {
return;
}

$this->tnt->setDatabaseHandle(new GravConnector);

try {
$this->tnt->selectIndex($this->index);
} catch (IndexNotFoundException $e) {
Expand All @@ -190,14 +246,15 @@ public function deleteIndex($obj)
$indexer = $this->tnt->getIndex();

// Delete existing if it exists
$indexer->delete($page->route());
$indexer->delete($object->route());
}

public function updateIndex($obj)
/**
* @param object $object
*/
public function updateIndex($object)
{
if ($obj instanceof Page) {
$page = $obj;
} else {
if (!$object instanceof Page) {
return;
}

Expand All @@ -212,9 +269,9 @@ public function updateIndex($obj)
$indexer = $this->tnt->getIndex();

// Delete existing if it exists
$indexer->delete($page->route());
$indexer->delete($object->route());

$filter = $config = Grav::instance()['config']->get('plugins.tntsearch.filter');
$filter = Grav::instance()['config']->get('plugins.tntsearch.filter');
if ($filter && array_key_exists('items', $filter)) {

if (is_string($filter['items'])) {
Expand All @@ -225,8 +282,8 @@ public function updateIndex($obj)
/** @var Collection $collection */
$collection = $apage->collection($filter, false);

if (array_key_exists($page->path(), $collection->toArray())) {
$fields = GravTNTSearch::indexPageData($page);
if (array_key_exists($object->path(), $collection->toArray())) {
$fields = $this->indexPageData($object);
$document = (array) $fields;

// Insert document
Expand All @@ -235,6 +292,10 @@ public function updateIndex($obj)
}
}

/**
* @param PageInterface $page
* @return object
*/
public function indexPageData($page)
{
$header = (array) $page->header();
Expand All @@ -249,7 +310,7 @@ public function indexPageData($page)
$fields = new \stdClass();
$fields->id = $route;
$fields->name = $page->title();
$fields->content = $this->getCleanContent($page);
$fields->content = static::getCleanContent($page);

if ($this->language) {
$fields->display_route = '/' . $this->language . $route;
Expand All @@ -259,5 +320,4 @@ public function indexPageData($page)

return $fields;
}

}
Loading

0 comments on commit bd7fb20

Please sign in to comment.