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