Skip to content

Commit 2e76a42

Browse files
committed
Merge branch 'release/3.2.0'
2 parents eb9fbb7 + 3cbbb8b commit 2e76a42

File tree

7 files changed

+86
-28
lines changed

7 files changed

+86
-28
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# v3.2.0
2+
## 06/08/2020
3+
4+
1. [](#new)
5+
* Added support for CLI `bin/plugin index` to index only a single language (`--language=en`)
6+
1. [](#improved)
7+
* Renamed CLI classes to avoid class name conflicts
8+
1. [](#bugfix)
9+
* Fixed non-routable and non-published pages showing up in search results
10+
* Fixed indexing in multi-language sites
11+
* Use CLI command directly in scheduler command to work [#95](https://github.com/trilbymedia/grav-plugin-tntsearch/issues/95)
12+
113
# v3.1.1
214
## 02/12/2020
315

blueprints.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
name: TNT Search
2-
version: 3.1.1
2+
slug: tntsearch
3+
type: plugin
4+
version: 3.2.0
35
testing: false
46
description: Powerful indexed-based full text search engine powered by TNTSearch
57
icon: binoculars

classes/GravConnector.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,24 @@ public function query($query)
4848
$counter++;
4949
$process = $default_process;
5050
$header = $page->header();
51-
$route = $page->route();
51+
$url = $page->url();
5252

5353
if (isset($header->tntsearch['process'])) {
5454
$process = $header->tntsearch['process'];
5555
}
5656

5757
// Only process what's configured
5858
if (!$process) {
59-
echo("Skipped {$counter} {$route}\n");
59+
echo("Skipped {$counter} {$url}\n");
6060
continue;
6161
}
6262

6363
try {
6464
$fields = $gtnt->indexPageData($page);
6565
$results[] = (array) $fields;
66-
$display_route = $fields->display_route ?? $route;
67-
echo("Added {$counter} {$display_route}\n");
66+
echo("Added {$counter} {$url}\n");
6867
} catch (\Exception $e) {
69-
echo("Skipped {$counter} {$route} - {$e->getMessage()}\n");
68+
echo("Skipped {$counter} {$url} - {$e->getMessage()}\n");
7069
continue;
7170
}
7271
}

classes/GravTNTSearch.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,12 @@ public function indexPageData($page)
301301
$header = (array) $page->header();
302302
$redirect = (bool) $page->redirect();
303303

304+
if (!$page->published()) {
305+
throw new \RuntimeException('not published...');
306+
}
307+
if (!$page->routable()) {
308+
throw new \RuntimeException('not routable...');
309+
}
304310
if ($redirect || (isset($header['tntsearch']['index']) && $header['tntsearch']['index'] === false )) {
305311
throw new \RuntimeException('redirect only...');
306312
}
@@ -312,10 +318,6 @@ public function indexPageData($page)
312318
$fields->name = $page->title();
313319
$fields->content = static::getCleanContent($page);
314320

315-
if ($this->language) {
316-
$fields->display_route = '/' . $this->language . $route;
317-
}
318-
319321
Grav::instance()->fireEvent('onTNTSearchIndex', new Event(['page' => $page, 'fields' => $fields]));
320322

321323
return $fields;

cli/IndexerCommand.php renamed to cli/TNTSearchIndexerCommand.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @package Grav\Plugin\Console
1212
*/
13-
class IndexerCommand extends ConsoleCommand
13+
class TNTSearchIndexerCommand extends ConsoleCommand
1414
{
1515
/**
1616
* @var array
@@ -37,7 +37,18 @@ protected function configure()
3737
{
3838
$this
3939
->setName('index')
40-
->addOption('alt', null, InputOption::VALUE_NONE, 'alternative output')
40+
->addOption(
41+
'alt',
42+
null,
43+
InputOption::VALUE_NONE,
44+
'alternative output'
45+
)
46+
->addOption(
47+
'language',
48+
'l',
49+
InputOption::VALUE_OPTIONAL,
50+
'optional language to index (multi-language sites only)'
51+
)
4152
->setDescription('TNTSearch Indexer')
4253
->setHelp('The <info>index command</info> re-indexes the search engine');
4354
}
@@ -47,32 +58,41 @@ protected function configure()
4758
*/
4859
protected function serve()
4960
{
61+
$langCode = $this->input->getOption('language');
62+
63+
error_reporting(1);
64+
$this->setLanguage($langCode);
5065
$this->initializePages();
5166

5267
$alt_output = $this->input->getOption('alt') ? true : false;
5368

5469
if ($alt_output) {
55-
$this->doIndex($alt_output);
70+
$output = $this->doIndex($langCode);
71+
$this->output->write($output);
72+
$this->output->writeln('');
5673
} else {
5774
$this->output->writeln('');
5875
$this->output->writeln('<magenta>Re-indexing</magenta>');
5976
$this->output->writeln('');
6077
$start = microtime(true);
61-
$this->doIndex($alt_output);
78+
$output = $this->doIndex($langCode);
79+
$this->output->write($output);
80+
$this->output->writeln('');
6281
$end = number_format(microtime(true) - $start,1);
6382
$this->output->writeln('');
6483
$this->output->writeln('Indexed in ' . $end . 's');
6584
}
6685
}
6786

68-
private function doIndex($alt_output = false)
87+
/**
88+
* @param string|null $langCode
89+
* @return string
90+
*/
91+
private function doIndex(string $langCode = null): string
6992
{
70-
error_reporting(1);
71-
72-
[$status, $msg, $output] = TNTSearchPlugin::indexJob();
93+
[,,$output] = TNTSearchPlugin::indexJob($langCode);
7394

74-
$this->output->write($output);
75-
$this->output->writeln('');
95+
return $output;
7696
}
7797
}
7898

cli/QueryCommand.php renamed to cli/TNTSearchQueryCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* @package Grav\Plugin\Console
1313
*/
14-
class QueryCommand extends ConsoleCommand
14+
class TNTSearchQueryCommand extends ConsoleCommand
1515
{
1616
/**
1717
* @var array

tntsearch.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function onSchedulerInitialized(Event $e): void
110110
$scheduler = $e['scheduler'];
111111
$at = $this->config->get('plugins.tntsearch.scheduled_index.at');
112112
$logs = $this->config->get('plugins.tntsearch.scheduled_index.logs');
113-
$job = $scheduler->addFunction('Grav\Plugin\TNTSearchPlugin::indexJob', [], 'tntsearch-index');
113+
$job = $scheduler->addCommand('bin/plugin tntsearch index', [], 'tntsearch-index');
114114
$job->at($at);
115115
$job->output($logs);
116116
$job->backlink('/plugins/tntsearch');
@@ -434,7 +434,11 @@ public static function getSearchObjectType($options = [])
434434
throw new \RuntimeException('Search class: ' . $type . ' does not exist');
435435
}
436436

437-
public static function indexJob()
437+
/**
438+
* @param string|null $langCode
439+
* @return array
440+
*/
441+
public static function indexJob(string $langCode = null)
438442
{
439443
$grav = Grav::instance();
440444
$grav['debugger']->enabled(false);
@@ -445,15 +449,34 @@ public static function indexJob()
445449
$pages->enablePages();
446450
}
447451

452+
ob_start();
453+
448454
/** @var Language $language */
449455
$language = $grav['language'];
456+
$langEnabled = $language->enabled();
450457

451-
ob_start();
458+
// TODO: can be removed when Grav minimum >= v1.6.22
459+
$hasReset = method_exists($pages, 'reset');
460+
if (!$hasReset && !$langCode) {
461+
$langCode = $language->getActive();
462+
}
452463

453-
if ($language->enabled()) {
454-
foreach ($language->getLanguages() as $lang) {
455-
$language->init();
456-
$language->setActive($lang);
464+
if ($langCode && (!$langEnabled || !$language->validate($langCode))) {
465+
$langCode = null;
466+
}
467+
468+
$langCodes = $langCode ? [$langCode] : $language->getLanguages();
469+
if ($langCodes) {
470+
foreach ($langCodes as $lang) {
471+
if ($lang !== $language->getActive()) {
472+
$language->init();
473+
$language->setActive($lang);
474+
475+
// TODO: $hasReset test can be removed (keep reset!) when Grav minimum >= v1.6.22
476+
if ($hasReset) {
477+
$pages->reset();
478+
}
479+
}
457480

458481
echo "\nLanguage: {$lang}\n";
459482

0 commit comments

Comments
 (0)