TNTSearchIndexerCommand.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Grav\Plugin\Console;
  3. use Grav\Console\ConsoleCommand;
  4. use Grav\Plugin\TNTSearchPlugin;
  5. use Symfony\Component\Console\Input\InputOption;
  6. /**
  7. * Class IndexerCommand
  8. *
  9. * @package Grav\Plugin\Console
  10. */
  11. class TNTSearchIndexerCommand extends ConsoleCommand
  12. {
  13. /** @var array */
  14. protected $options = [];
  15. /** @var array */
  16. protected $colors = [
  17. 'DEBUG' => 'green',
  18. 'INFO' => 'cyan',
  19. 'NOTICE' => 'yellow',
  20. 'WARNING' => 'yellow',
  21. 'ERROR' => 'red',
  22. 'CRITICAL' => 'red',
  23. 'ALERT' => 'red',
  24. 'EMERGENCY' => 'magenta'
  25. ];
  26. /**
  27. * @return void
  28. */
  29. protected function configure(): void
  30. {
  31. $this
  32. ->setName('index')
  33. ->addOption(
  34. 'alt',
  35. null,
  36. InputOption::VALUE_NONE,
  37. 'alternative output'
  38. )
  39. ->addOption(
  40. 'language',
  41. 'l',
  42. InputOption::VALUE_OPTIONAL,
  43. 'optional language to index (multi-language sites only)'
  44. )
  45. ->setDescription('TNTSearch Indexer')
  46. ->setHelp('The <info>index command</info> re-indexes the search engine');
  47. }
  48. /**
  49. * @return int
  50. */
  51. protected function serve(): int
  52. {
  53. /** @var string|null $langCode */
  54. $langCode = $this->input->getOption('language');
  55. error_reporting(1);
  56. $this->setLanguage($langCode);
  57. $this->initializePages();
  58. $alt_output = $this->input->getOption('alt') ? true : false;
  59. if ($alt_output) {
  60. $output = $this->doIndex($langCode);
  61. $this->output->write($output);
  62. $this->output->writeln('');
  63. } else {
  64. $this->output->writeln('');
  65. $this->output->writeln('<magenta>Re-indexing</magenta>');
  66. $this->output->writeln('');
  67. $start = microtime(true);
  68. $output = $this->doIndex($langCode);
  69. $this->output->write($output);
  70. $this->output->writeln('');
  71. $end = number_format(microtime(true) - $start,1);
  72. $this->output->writeln('');
  73. $this->output->writeln('Indexed in ' . $end . 's');
  74. }
  75. return 0;
  76. }
  77. /**
  78. * @param string|null $langCode
  79. * @return string
  80. */
  81. private function doIndex(string $langCode = null): string
  82. {
  83. [,,$output] = TNTSearchPlugin::indexJob($langCode);
  84. return $output;
  85. }
  86. }