init.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. class Search_Sphinx extends Plugin {
  3. function about() {
  4. return array(1.0,
  5. "Delegate searching for articles to Sphinx",
  6. "hoelzro",
  7. true);
  8. }
  9. function init($host) {
  10. $host->add_hook($host::HOOK_SEARCH, $this);
  11. require_once __DIR__ . "/sphinxapi.php";
  12. }
  13. function hook_search($search) {
  14. $offset = 0;
  15. $limit = 500;
  16. $sphinxClient = new SphinxClient();
  17. $sphinxpair = explode(":", SPHINX_SERVER, 2);
  18. $sphinxClient->SetServer($sphinxpair[0], (int)$sphinxpair[1]);
  19. $sphinxClient->SetConnectTimeout(1);
  20. $sphinxClient->SetFieldWeights(array('title' => 70, 'content' => 30,
  21. 'feed_title' => 20));
  22. $sphinxClient->SetMatchMode(SPH_MATCH_EXTENDED2);
  23. $sphinxClient->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
  24. $sphinxClient->SetLimits($offset, $limit, 1000);
  25. $sphinxClient->SetArrayResult(false);
  26. $sphinxClient->SetFilter('owner_uid', array($_SESSION['uid']));
  27. $result = $sphinxClient->Query($search, SPHINX_INDEX);
  28. $ids = array();
  29. if (is_array($result['matches'])) {
  30. foreach (array_keys($result['matches']) as $int_id) {
  31. $ref_id = $result['matches'][$int_id]['attrs']['ref_id'];
  32. array_push($ids, $ref_id);
  33. }
  34. }
  35. $ids = join(",", $ids);
  36. if ($ids)
  37. return array("ref_id IN ($ids)", array());
  38. else
  39. return array("ref_id = -1", array());
  40. }
  41. function api_version() {
  42. return 2;
  43. }
  44. }
  45. ?>