-
Notifications
You must be signed in to change notification settings - Fork 23
/
tntsearch.php
523 lines (444 loc) · 15.7 KB
/
tntsearch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<?php
namespace Grav\Plugin;
use Composer\Autoload\ClassLoader;
use Grav\Common\Grav;
use Grav\Common\Language\Language;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;
use Grav\Common\Plugin;
use Grav\Common\Scheduler\Scheduler;
use Grav\Common\Uri;
use Grav\Plugin\TNTSearch\GravTNTSearch;
use RocketTheme\Toolbox\Event\Event;
use TeamTNT\TNTSearch\Exceptions\IndexNotFoundException;
/**
* Class TNTSearchPlugin
* @package Grav\Plugin
*/
class TNTSearchPlugin extends Plugin
{
/** @var array|object|string */
protected $results = [];
/** @var string */
protected $query;
/** @var bool */
protected $built_in_search_page;
/** @var string */
protected $query_route;
/** @var string */
protected $search_route;
/** @var string */
protected $current_route;
/** @var string */
protected $admin_route;
/**
* @return array
*
* The getSubscribedEvents() gives the core a list of events
* that the plugin wants to listen to. The key of each
* array section is the event that the plugin listens to
* and the value (in the form of an array) contains the
* callable (or function) as well as the priority. The
* higher the number the higher the priority.
*/
public static function getSubscribedEvents(): array
{
return [
'onPluginsInitialized' => [
['autoload', 100000],
['onPluginsInitialized', 0]
],
'onSchedulerInitialized' => ['onSchedulerInitialized', 0],
'onTwigLoader' => ['onTwigLoader', 0],
'onTNTSearchReIndex' => ['onTNTSearchReIndex', 0],
'onTNTSearchIndex' => ['onTNTSearchIndex', 0],
'onTNTSearchQuery' => ['onTNTSearchQuery', 0],
];
}
/**
* [onPluginsInitialized:100000] Composer autoload.
*is
* @return ClassLoader
*/
public function autoload(): ClassLoader
{
return require __DIR__ . '/vendor/autoload.php';
}
/**
* Initialize the plugin
*/
public function onPluginsInitialized(): void
{
if ($this->isAdmin()) {
$this->GravTNTSearch();
$route = $this->config->get('plugins.admin.route');
$base = '/' . trim($route, '/');
$this->admin_route = $this->grav['base_url'] . $base;
$this->enable([
'onAdminMenu' => ['onAdminMenu', 0],
'onAdminTaskExecute' => ['onAdminTaskExecute', 0],
'onTwigSiteVariables' => ['onTwigAdminVariables', 0],
'onTwigLoader' => ['addAdminTwigTemplates', 0],
]);
if ($this->config->get('plugins.tntsearch.enable_admin_page_events', true)) {
$this->enable([
'onAdminAfterSave' => ['onObjectSave', 0],
'onAdminAfterDelete' => ['onObjectDelete', 0],
'onFlexObjectSave' => ['onObjectSave', 0],
'onFlexObjectDelete' => ['onObjectDelete', 0],
]);
}
return;
}
$this->enable([
'onPagesInitialized' => ['onPagesInitialized', 1000],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
]);
}
/**
* Add index job to Grav Scheduler
* Requires Grav 1.6.0 - Scheduler
*/
public function onSchedulerInitialized(Event $e): void
{
if ($this->config->get('plugins.tntsearch.scheduled_index.enabled')) {
/** @var Scheduler $scheduler */
$scheduler = $e['scheduler'];
$at = $this->config->get('plugins.tntsearch.scheduled_index.at');
$logs = $this->config->get('plugins.tntsearch.scheduled_index.logs');
$job = $scheduler->addCommand('bin/plugin', ['tntsearch', 'index'], 'tntsearch-index');
$job->at($at);
$job->output($logs);
$job->backlink('/plugins/tntsearch');
}
}
/**
* Function to force a reindex from your own plugins
*/
public function onTNTSearchReIndex(): void
{
$this->GravTNTSearch()->createIndex();
}
/**
* A sample event to show how easy it is to extend the indexing fields
*
* @param Event $e
*/
public function onTNTSearchIndex(Event $e): void
{
$page = $e['page'];
$fields = $e['fields'];
if ($page && $page instanceof Page && isset($page->header()->author)) {
$author = $page->header()->author;
if (is_string($author)) {
$fields->author = $author;
}
}
}
public function onTNTSearchQuery(Event $e): void
{
$page = $e['page'];
$query = $e['query'];
$options = $e['options'];
$fields = $e['fields'];
$gtnt = $e['gtnt'];
$content = $gtnt->getCleanContent($page);
$title = $page->title();
$relevant = $gtnt->tnt->snippet($query, $content, $options['snippet']);
if (strlen($relevant) <= 6) {
$relevant = substr($content, 0, $options['snippet']);
}
$fields->hits[] = [
'link' => $page->route(),
'title' => $gtnt->tnt->highlight($title, $query, 'em', ['wholeWord' => false]),
'content' => $gtnt->tnt->highlight($relevant, $query, 'em', ['wholeWord' => false]),
];
}
/**
* Create pages and perform the search actions
*/
public function onPagesInitialized(): void
{
/** @var Uri $uri */
$uri = $this->grav['uri'];
$options = [];
$this->current_route = $uri->path();
$this->built_in_search_page = $this->config->get('plugins.tntsearch.built_in_search_page');
$this->search_route = $this->config->get('plugins.tntsearch.search_route');
$this->query_route = $this->config->get('plugins.tntsearch.query_route');
$pages = $this->grav['pages'];
$page = $pages->dispatch($this->current_route);
if (!$page) {
if ($this->query_route && $this->query_route === $this->current_route) {
$page = new Page;
$page->init(new \SplFileInfo(__DIR__ . '/pages/tntquery.md'));
$page->slug(basename($this->current_route));
if ($uri->param('ajax') || $uri->query('ajax')) {
$page->template('tntquery-ajax');
}
$pages->addPage($page, $this->current_route);
} elseif ($this->built_in_search_page && $this->search_route == $this->current_route) {
$page = new Page;
$page->init(new \SplFileInfo(__DIR__ . '/pages/search.md'));
$page->slug(basename($this->current_route));
$pages->addPage($page, $this->current_route);
}
}
$this->query = (string)($uri->param('q', null) ?: $uri->query('q') ?: '');
if ($this->query) {
$snippet = $this->getFormValue('sl');
$limit = $this->getFormValue('l');
if ($snippet) {
$options['snippet'] = $snippet;
}
if ($limit) {
$options['limit'] = $limit;
}
$this->grav['tntsearch'] = static::getSearchObjectType($options);
if ($page) {
$this->config->set('plugins.tntsearch', $this->mergeConfig($page));
}
try {
$this->results = $this->GravTNTSearch()->search($this->query);
} catch (IndexNotFoundException $e) {
$this->results = ['number_of_hits' => 0, 'hits' => [], 'execution_time' => 'missing index'];
}
}
}
/**
* Add the Twig template paths to the Twig laoder
*/
public function onTwigLoader(): void
{
$this->grav['twig']->addPath(__DIR__ . '/templates');
}
/**
* Add the current template paths to the admin Twig loader
*/
public function addAdminTwigTemplates(): void
{
$this->grav['twig']->addPath($this->grav['locator']->findResource('theme://templates'));
}
/**
* Add results and query to Twig as well as CSS/JS assets
*/
public function onTwigSiteVariables(): void
{
$twig = $this->grav['twig'];
if ($this->query) {
$twig->twig_vars['query'] = $this->query;
$twig->twig_vars['tntsearch_results'] = $this->results;
}
if ($this->config->get('plugins.tntsearch.built_in_css')) {
$this->grav['assets']->addCss('plugin://tntsearch/assets/tntsearch.css');
}
if ($this->config->get('plugins.tntsearch.built_in_js')) {
// $this->grav['assets']->addJs('plugin://tntsearch/assets/tntsearch.js');
$this->grav['assets']->addJs('plugin://tntsearch/assets/tntsearch.js');
}
}
/**
* Handle the Reindex task from the admin
*
* @param Event $e
*/
public function onAdminTaskExecute(Event $e): void
{
if ($e['method'] === 'taskReindexTNTSearch') {
$controller = $e['controller'];
header('Content-type: application/json');
if (!$controller->authorizeTask('reindexTNTSearch', ['admin.configuration', 'admin.super'])) {
$json_response = [
'status' => 'error',
'message' => '<i class="fa fa-warning"></i> Index not created',
'details' => 'Insufficient permissions to reindex the search engine database.'
];
echo json_encode($json_response);
exit;
}
// disable warnings
error_reporting(1);
// disable execution time
set_time_limit(0);
list($status, $msg, $output) = static::indexJob();
$json_response = [
'status' => $status ? 'success' : 'error',
'message' => $msg
];
echo json_encode($json_response);
exit;
}
}
/**
* Perform an 'add' or 'update' for index data as needed
*
* @param Event $event
* @return bool
*/
public function onObjectSave($event): bool
{
if (defined('CLI_DISABLE_TNTSEARCH')) {
return true;
}
$obj = $event['object'] ?: $event['page'];
if ($obj) {
$this->GravTNTSearch()->updateIndex($obj);
}
return true;
}
/**
* Perform a 'delete' for index data as needed
*
* @param Event $event
* @return bool
*/
public function onObjectDelete($event): bool
{
if (defined('CLI_DISABLE_TNTSEARCH')) {
return true;
}
$obj = $event['object'] ?: $event['page'];
if ($obj) {
$this->GravTNTSearch()->deleteIndex($obj);
}
return true;
}
/**
* Set some twig vars and load CSS/JS assets for admin
*/
public function onTwigAdminVariables(): void
{
$twig = $this->grav['twig'];
$gtnt = $this->GravTNTSearch();
[$status, $msg] = static::getIndexCount($gtnt);
if ($status === false) {
$message = '<i class="fa fa-binoculars"></i> <a href="/'. trim($this->admin_route, '/') . '/plugins/tntsearch">TNTSearch must be indexed before it will function properly.</a>';
$this->grav['admin']->addTempMessage($message, 'error');
}
$twig->twig_vars['tntsearch_index_status'] = ['status' => $status, 'msg' => $msg];
$this->grav['assets']->addCss('plugin://tntsearch/assets/admin/tntsearch.css');
$this->grav['assets']->addJs('plugin://tntsearch/assets/admin/tntsearch.js');
}
/**
* Add reindex button to the admin QuickTray
*/
public function onAdminMenu(): void
{
$options = [
'authorize' => 'taskReindexTNTSearch',
'hint' => 'reindexes the TNT Search index',
'class' => 'tntsearch-reindex',
'icon' => 'fa-binoculars'
];
$this->grav['twig']->plugins_quick_tray['TNT Search'] = $options;
}
/**
* Wrapper to get the number of documents currently indexed
*
* @param GravTNTSearch $gtnt
* @return array
*/
protected static function getIndexCount($gtnt): array
{
$status = true;
try {
$msg = '';
$gtnt->selectIndex();
$doc_count = $gtnt->tnt->totalDocumentsInCollection();
$language = Grav::instance()['language'];
if ($language->enabled()) {
$msg .= 'Processed ' . count($language->getLanguages()) . ' languages, each with ';
}
$msg .= $doc_count . ' documents reindexed';
} catch (IndexNotFoundException $e) {
$status = false;
$msg = 'Index not created';
}
return [$status, $msg];
}
/**
* Helper function to read form/url values
*
* @param string $val
* @return mixed
*/
protected function getFormValue($val)
{
$uri = $this->grav['uri'];
return $uri->param($val) ?: $uri->query($val) ?: filter_input(INPUT_POST, $val, FILTER_SANITIZE_ENCODED);
}
/**
* @param array $options
* @return GravTNTSearch
*/
public static function getSearchObjectType($options = [])
{
$type = 'Grav\\Plugin\\TNTSearch\\' . Grav::instance()['config']->get('plugins.tntsearch.search_object_type', 'Grav') . 'TNTSearch';
if (class_exists($type)) {
return new $type($options);
}
throw new \RuntimeException('Search class: ' . $type . ' does not exist');
}
/**
* @param string|null $langCode
* @return array
*/
public static function indexJob(string $langCode = null)
{
$grav = Grav::instance();
$grav['debugger']->enabled(false);
/** @var Pages $pages */
$pages = $grav['pages'];
if (method_exists($pages, 'enablePages')) {
$pages->enablePages();
}
ob_start();
/** @var Language $language */
$language = $grav['language'];
$langEnabled = $language->enabled();
// TODO: can be removed when Grav minimum >= v1.6.22
$hasReset = method_exists($pages, 'reset');
if (!$hasReset && !$langCode) {
$langCode = $language->getActive();
}
if ($langCode && (!$langEnabled || !$language->validate($langCode))) {
$langCode = null;
}
$langCodes = $langCode ? [$langCode] : $language->getLanguages();
if ($langCodes) {
foreach ($langCodes as $lang) {
if ($lang !== $language->getActive()) {
$language->init();
$language->setActive($lang);
// TODO: $hasReset test can be removed (keep reset!) when Grav minimum >= v1.6.22
if ($hasReset) {
$pages->reset();
}
}
echo "\nLanguage: {$lang}\n";
$gtnt = static::getSearchObjectType();
$gtnt->createIndex();
}
} else {
$gtnt = static::getSearchObjectType();
$gtnt->createIndex();
}
$output = ob_get_clean();
// Reset and get index count and status
$gtnt = static::getSearchObjectType();
[$status, $msg] = static::getIndexCount($gtnt);
return [$status, $msg, $output];
}
/**
* Helper to initialize TNTSearch if required
*
* @return GravTNTSearch
*/
protected function GravTNTSearch()
{
if (!isset($this->grav['tntsearch'])) {
$this->grav['tntsearch'] = static::getSearchObjectType();
}
return $this->grav['tntsearch'];
}
}