]> git.wh0rd.org Git - tt-rss.git/blob - plugins/search_sphinx/init.php
update phpmd ruleset to use (subset) of cleancode
[tt-rss.git] / plugins / search_sphinx / init.php
1 <?php
2
3 class Search_Sphinx extends Plugin {
4         function about() {
5                 return array(1.0,
6                         "Delegate searching for articles to Sphinx (don't forget to set options in config.php)",
7                         "hoelzro",
8                         true);
9         }
10
11         function init($host) {
12                 $host->add_hook($host::HOOK_SEARCH, $this);
13
14                 // idk if that would work but checking for the class being loaded is somehow not enough
15                 if (class_exists("SphinxClient") && !defined('SEARCHD_COMMAND_SEARCH')) {
16                         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.");
17                 }
18
19                 require_once __DIR__ . "/sphinxapi.php";
20         }
21
22         function hook_search($search) {
23                 $offset = 0;
24                 $limit  = 500;
25
26                 $sphinxClient = new SphinxClient();
27
28                 $sphinxpair = explode(":", SPHINX_SERVER, 2);
29
30                 $sphinxClient->SetServer($sphinxpair[0], (int)$sphinxpair[1]);
31                 $sphinxClient->SetConnectTimeout(1);
32
33                 $sphinxClient->SetFieldWeights(array('title' => 70, 'content' => 30,
34                         'feed_title' => 20));
35
36                 $sphinxClient->SetMatchMode(SPH_MATCH_EXTENDED2);
37                 $sphinxClient->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
38                 $sphinxClient->SetLimits($offset, $limit, 1000);
39                 $sphinxClient->SetArrayResult(false);
40                 $sphinxClient->SetFilter('owner_uid', array($_SESSION['uid']));
41
42                 $result = $sphinxClient->Query($search, SPHINX_INDEX);
43
44                 $ids = array();
45
46                 if (is_array($result['matches'])) {
47                         foreach (array_keys($result['matches']) as $int_id) {
48                                 $ref_id = $result['matches'][$int_id]['attrs']['ref_id'];
49                                 array_push($ids, $ref_id);
50                         }
51                 }
52
53                 $ids = join(",", $ids);
54
55                 if ($ids)
56                         return array("ref_id IN ($ids)", array());
57                 else
58                         return array("ref_id = -1", array());
59         }
60
61         function api_version() {
62                 return 2;
63         }
64 }