GravConnector.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Grav\Plugin\TNTSearch;
  3. use Grav\Common\Config\Config;
  4. use Grav\Common\Grav;
  5. use Grav\Common\Yaml;
  6. use Grav\Common\Page\Page;
  7. use Grav\Plugin\TNTSearchPlugin;
  8. use PDO;
  9. class GravConnector extends PDO
  10. {
  11. public function __construct()
  12. {
  13. }
  14. /**
  15. * @param int $attribute
  16. * @return bool
  17. */
  18. public function getAttribute($attribute): bool
  19. {
  20. return false;
  21. }
  22. /***
  23. * @param string $statement
  24. * @param int|null $fetch_style
  25. * @param mixed ...$extra
  26. * @return GravResultObject
  27. */
  28. public function query(string $statement, ?int $fetch_style = null, ...$extra): GravResultObject
  29. {
  30. $counter = 0;
  31. $results = [];
  32. /** @var Config $config */
  33. $config = Grav::instance()['config'];
  34. $filter = $config->get('plugins.tntsearch.filter');
  35. $default_process = $config->get('plugins.tntsearch.index_page_by_default');
  36. $gtnt = TNTSearchPlugin::getSearchObjectType();
  37. if ($filter && array_key_exists('items', $filter)) {
  38. if (is_string($filter['items'])) {
  39. $filter['items'] = Yaml::parse($filter['items']);
  40. }
  41. $page = new Page;
  42. $collection = $page->collection($filter, false);
  43. } else {
  44. $collection = Grav::instance()['pages']->all();
  45. $collection->published()->routable();
  46. }
  47. foreach ($collection as $page) {
  48. $counter++;
  49. $process = $default_process;
  50. $header = $page->header();
  51. $url = $page->url();
  52. if (isset($header->tntsearch['process'])) {
  53. $process = $header->tntsearch['process'];
  54. }
  55. // Only process what's configured
  56. if (!$process) {
  57. echo("Skipped {$counter} {$url}\n");
  58. continue;
  59. }
  60. try {
  61. $fields = $gtnt->indexPageData($page);
  62. $results[] = (array) $fields;
  63. echo("Added {$counter} {$url}\n");
  64. } catch (\Exception $e) {
  65. echo("Skipped {$counter} {$url} - {$e->getMessage()}\n");
  66. continue;
  67. }
  68. }
  69. return new GravResultObject($results);
  70. }
  71. }