init.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. class Search_Sphinx extends Plugin {
  3. function about() {
  4. return array(1.0,
  5. "Delegate searching for articles to Sphinx (don't forget to set options in config.php)",
  6. "hoelzro",
  7. true);
  8. }
  9. function init($host) {
  10. $host->add_hook($host::HOOK_SEARCH, $this);
  11. // idk if that would work but checking for the class being loaded is somehow not enough
  12. if (class_exists("SphinxClient") && !defined('SEARCHD_COMMAND_SEARCH')) {
  13. user_error("Your PHP has a separate systemwide Sphinx client installed which conflicts with the client library used by tt-rss. Either remove the system library or disable Sphinx support.");
  14. }
  15. require_once __DIR__ . "/sphinxapi.php";
  16. }
  17. function hook_search($search) {
  18. $offset = 0;
  19. $limit = 500;
  20. $sphinxClient = new SphinxClient();
  21. $sphinxpair = explode(":", SPHINX_SERVER, 2);
  22. $sphinxClient->SetServer($sphinxpair[0], (int)$sphinxpair[1]);
  23. $sphinxClient->SetConnectTimeout(1);
  24. $sphinxClient->SetFieldWeights(array('title' => 70, 'content' => 30,
  25. 'feed_title' => 20));
  26. $sphinxClient->SetMatchMode(SPH_MATCH_EXTENDED2);
  27. $sphinxClient->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
  28. $sphinxClient->SetLimits($offset, $limit, 1000);
  29. $sphinxClient->SetArrayResult(false);
  30. $sphinxClient->SetFilter('owner_uid', array($_SESSION['uid']));
  31. $result = $sphinxClient->Query($search, SPHINX_INDEX);
  32. $ids = array();
  33. if (is_array($result['matches'])) {
  34. foreach (array_keys($result['matches']) as $int_id) {
  35. $ref_id = $result['matches'][$int_id]['attrs']['ref_id'];
  36. array_push($ids, $ref_id);
  37. }
  38. }
  39. $ids = join(",", $ids);
  40. if ($ids)
  41. return array("ref_id IN ($ids)", array());
  42. else
  43. return array("ref_id = -1", array());
  44. }
  45. function api_version() {
  46. return 2;
  47. }
  48. }
  49. ?>