Skip to content

Commit bd7fb20

Browse files
committed
Merge branch 'release/3.1.0'
2 parents 431fd82 + 297e221 commit bd7fb20

30 files changed

+557
-157
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# v3.1.0
2+
## 02/11/2020
3+
4+
1. [](#new)
5+
* Require Grav v1.6.21
6+
* Upgraded to TNTSearch version 2.2 (PHP 7.4 fixes)
7+
1. [](#improved)
8+
* Code cleanup
9+
1. [](#bugfix)
10+
* Fixed Grav initialization in CLI
11+
* Work around inconsistencies in page content if page template uses `grav.page` instead of `page`
12+
113
# v3.0.1
214
## 02/03/2020
315

blueprints.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: TNT Search
2-
version: 3.0.1
2+
version: 3.1.0
33
testing: false
44
description: Powerful indexed-based full text search engine powered by TNTSearch
55
icon: binoculars
@@ -13,7 +13,7 @@ docs: https://github.com/trilbymedia/grav-plugin-tntsearch/blob/develop/README.m
1313
license: MIT
1414

1515
dependencies:
16-
- { name: grav, version: '>=1.6.3' }
16+
- { name: grav, version: '>=1.6.21' }
1717

1818
form:
1919
validation: strict

classes/GravConnector.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
22
namespace Grav\Plugin\TNTSearch;
33

4+
use Grav\Common\Config\Config;
45
use Grav\Common\Grav;
56
use Grav\Common\Yaml;
67
use Grav\Common\Page\Page;
8+
use Grav\Plugin\TNTSearchPlugin;
79

810
class GravConnector extends \PDO
911
{
@@ -12,7 +14,7 @@ public function __construct()
1214

1315
}
1416

15-
public function getAttribute($attribute)
17+
public function getAttribute($attribute): bool
1618
{
1719
return false;
1820
}
@@ -22,10 +24,11 @@ public function query($query)
2224
$counter = 0;
2325
$results = [];
2426

27+
/** @var Config $config */
2528
$config = Grav::instance()['config'];
2629
$filter = $config->get('plugins.tntsearch.filter');
2730
$default_process = $config->get('plugins.tntsearch.index_page_by_default');
28-
$gtnt = \Grav\Plugin\TNTSearchPlugin::getSearchObjectType();
31+
$gtnt = TNTSearchPlugin::getSearchObjectType();
2932

3033

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

5457
// Only process what's configured
5558
if (!$process) {
56-
echo("Skipped $counter $route\n");
59+
echo("Skipped {$counter} {$route}\n");
5760
continue;
5861
}
5962

6063
try {
6164
$fields = $gtnt->indexPageData($page);
6265
$results[] = (array) $fields;
63-
$display_route = isset($fields->display_route) ? $fields->display_route : $route;
64-
echo("Added $counter $display_route\n");
66+
$display_route = $fields->display_route ?? $route;
67+
echo("Added {$counter} {$display_route}\n");
6568
} catch (\Exception $e) {
66-
echo("Skipped $counter $route - {$e->getMessage()}\n");
69+
echo("Skipped {$counter} {$route} - {$e->getMessage()}\n");
6770
continue;
6871
}
6972
}

classes/GravTNTSearch.php

Lines changed: 103 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
<?php
22
namespace Grav\Plugin\TNTSearch;
33

4+
use Grav\Common\Config\Config;
45
use Grav\Common\Grav;
56
use Grav\Common\Language\Language;
7+
use Grav\Common\Page\Interfaces\PageInterface;
8+
use Grav\Common\Page\Pages;
9+
use Grav\Common\Twig\Twig;
10+
use Grav\Common\Uri;
611
use Grav\Common\Yaml;
712
use Grav\Common\Page\Collection;
813
use Grav\Common\Page\Page;
914
use RocketTheme\Toolbox\Event\Event;
15+
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
1016
use TeamTNT\TNTSearch\Exceptions\IndexNotFoundException;
1117
use TeamTNT\TNTSearch\TNTSearch;
1218

@@ -18,21 +24,31 @@ class GravTNTSearch
1824
protected $index = 'grav.index';
1925
protected $language;
2026

27+
/**
28+
* GravTNTSearch constructor.
29+
* @param array $options
30+
*/
2131
public function __construct($options = [])
2232
{
23-
$search_type = Grav::instance()['config']->get('plugins.tntsearch.search_type', 'auto');
24-
$stemmer = Grav::instance()['config']->get('plugins.tntsearch.stemmer', 'default');
25-
$limit = Grav::instance()['config']->get('plugins.tntsearch.limit', 20);
26-
$snippet = Grav::instance()['config']->get('plugins.tntsearch.snippet', 300);
27-
$data_path = Grav::instance()['locator']->findResource('user://data', true) . '/tntsearch';
33+
/** @var Config $config */
34+
$config = Grav::instance()['config'];
35+
36+
/** @var UniformResourceLocator $locator */
37+
$locator = Grav::instance()['locator'];
38+
39+
$search_type = $config->get('plugins.tntsearch.search_type', 'auto');
40+
$stemmer = $config->get('plugins.tntsearch.stemmer', 'default');
41+
$limit = $config->get('plugins.tntsearch.limit', 20);
42+
$snippet = $config->get('plugins.tntsearch.snippet', 300);
43+
$data_path = $locator->findResource('user://data', true) . '/tntsearch';
2844

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

3248
if ($language->enabled()) {
3349
$active = $language->getActive();
3450
$default = $language->getDefault();
35-
$this->language = $active ? $active : $default;
51+
$this->language = $active ?: $default;
3652
$this->index = $this->language . '.index';
3753
}
3854

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

53-
$this->options = array_merge($defaults, $options);
69+
$this->options = array_replace($defaults, $options);
5470
$this->tnt = new TNTSearch();
55-
$this->tnt->loadConfig([
56-
"storage" => $data_path,
57-
"driver" => 'sqlite',
58-
'charset' => 'utf8'
59-
]);
71+
$this->tnt->loadConfig(
72+
[
73+
'storage' => $data_path,
74+
'driver' => 'sqlite',
75+
'charset' => 'utf8'
76+
]
77+
);
6078
}
6179

62-
public function search($query) {
80+
/**
81+
* @param $query
82+
* @return object|string
83+
* @throws IndexNotFoundException
84+
*/
85+
public function search($query)
86+
{
87+
/** @var Uri $uri */
6388
$uri = Grav::instance()['uri'];
6489
$type = $uri->query('search_type');
6590
$this->tnt->selectIndex($this->index);
@@ -69,14 +94,14 @@ public function search($query) {
6994
$this->tnt->fuzziness = true;
7095
}
7196

72-
$limit = intval($this->options['limit']);
73-
$type = isset($type) ? $type : $this->options['search_type'];
97+
$limit = (int)$this->options['limit'];
98+
$type = $type ?? $this->options['search_type'];
7499

75100
$multiword = null;
76101
if (isset($this->options['phrases']) && $this->options['phrases']) {
77102
if (strlen($query) > 2) {
78-
if ($query[0] === "\"" && $query[strlen($query) - 1] === "\"") {
79-
$multiword = substr($query, 1, strlen($query) - 2);
103+
if ($query[0] === '"' && $query[strlen($query) - 1] === '"') {
104+
$multiword = substr($query, 1, -1);
80105
$type = 'basic';
81106
$query = $multiword;
82107
}
@@ -102,43 +127,70 @@ public function search($query) {
102127
}
103128
}
104129

105-
$results = $this->tnt->$guess($query, $limit);
130+
$results = $this->tnt->{$guess}($query, $limit);
106131
}
107132

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

136+
/**
137+
* @param array $res
138+
* @param string $query
139+
* @return object|string
140+
*/
111141
protected function processResults($res, $query)
112142
{
113-
$counter = 0;
114143
$data = new \stdClass();
115-
$data->number_of_hits = isset($res['hits']) ? $res['hits'] : 0;
144+
$data->number_of_hits = $res['hits'] ?? 0;
116145
$data->execution_time = $res['execution_time'];
146+
147+
/** @var Pages $pages */
117148
$pages = Grav::instance()['pages'];
118149

150+
$counter = 0;
119151
foreach ($res['ids'] as $path) {
120-
121152
if ($counter++ > $this->options['limit']) {
122153
break;
123154
}
124155

125-
$page = $pages->dispatch($path);
156+
$page = $pages->find($path);
126157

127158
if ($page) {
128-
Grav::instance()->fireEvent('onTNTSearchQuery', new Event(['page' => $page, 'query' => $query, 'options' => $this->options, 'fields' => $data, 'gtnt' => $this]));
159+
$event = new Event(
160+
[
161+
'page' => $page,
162+
'query' => $query,
163+
'options' => $this->options,
164+
'fields' => $data,
165+
'gtnt' => $this
166+
]
167+
);
168+
Grav::instance()->fireEvent('onTNTSearchQuery', $event);
129169
}
130170
}
131171

132172
if ($this->options['json']) {
133173
return json_encode($data, JSON_PRETTY_PRINT);
134-
} else {
135-
return $data;
136174
}
175+
176+
return $data;
137177
}
138178

179+
/**
180+
* @param PageInterface $page
181+
* @return string
182+
*/
139183
public static function getCleanContent($page)
140184
{
141-
$twig = Grav::instance()['twig'];
185+
$grav = Grav::instance();
186+
$activePage = $grav['page'];
187+
188+
// Set active page in grav to the one we are currently processing.
189+
unset($grav['page']);
190+
$grav['page'] = $page;
191+
192+
/** @var Twig $twig */
193+
$twig = $grav['twig'];
142194
$header = $page->header();
143195

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

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

205+
// Restore active page in Grav.
206+
unset($grav['page']);
207+
$grav['page'] = $activePage;
208+
153209
return $content;
154210
}
155211

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

161217
// Set the stemmer language if set
162-
if ($this->options['stemmer'] != 'default') {
218+
if ($this->options['stemmer'] !== 'default') {
163219
$indexer->setLanguage($this->options['stemmer']);
164220
}
165221

@@ -171,16 +227,16 @@ public function selectIndex()
171227
$this->tnt->selectIndex($this->index);
172228
}
173229

174-
public function deleteIndex($obj)
230+
/**
231+
* @param object $object
232+
*/
233+
public function deleteIndex($object)
175234
{
176-
if ($obj instanceof Page) {
177-
$page = $obj;
178-
} else {
235+
if (!$object instanceof Page) {
179236
return;
180237
}
181238

182239
$this->tnt->setDatabaseHandle(new GravConnector);
183-
184240
try {
185241
$this->tnt->selectIndex($this->index);
186242
} catch (IndexNotFoundException $e) {
@@ -190,14 +246,15 @@ public function deleteIndex($obj)
190246
$indexer = $this->tnt->getIndex();
191247

192248
// Delete existing if it exists
193-
$indexer->delete($page->route());
249+
$indexer->delete($object->route());
194250
}
195251

196-
public function updateIndex($obj)
252+
/**
253+
* @param object $object
254+
*/
255+
public function updateIndex($object)
197256
{
198-
if ($obj instanceof Page) {
199-
$page = $obj;
200-
} else {
257+
if (!$object instanceof Page) {
201258
return;
202259
}
203260

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

214271
// Delete existing if it exists
215-
$indexer->delete($page->route());
272+
$indexer->delete($object->route());
216273

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

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

228-
if (array_key_exists($page->path(), $collection->toArray())) {
229-
$fields = GravTNTSearch::indexPageData($page);
285+
if (array_key_exists($object->path(), $collection->toArray())) {
286+
$fields = $this->indexPageData($object);
230287
$document = (array) $fields;
231288

232289
// Insert document
@@ -235,6 +292,10 @@ public function updateIndex($obj)
235292
}
236293
}
237294

295+
/**
296+
* @param PageInterface $page
297+
* @return object
298+
*/
238299
public function indexPageData($page)
239300
{
240301
$header = (array) $page->header();
@@ -249,7 +310,7 @@ public function indexPageData($page)
249310
$fields = new \stdClass();
250311
$fields->id = $route;
251312
$fields->name = $page->title();
252-
$fields->content = $this->getCleanContent($page);
313+
$fields->content = static::getCleanContent($page);
253314

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

260321
return $fields;
261322
}
262-
263323
}

0 commit comments

Comments
 (0)