-
Notifications
You must be signed in to change notification settings - Fork 2
/
start.php
executable file
·252 lines (202 loc) · 6.97 KB
/
start.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
namespace community\solr;
elgg_register_event_handler('init', 'system', __NAMESPACE__ . '\\init');
function init() {
// remove the default plugin hook
elgg_unregister_plugin_hook_handler('search', 'object:plugin_project', 'plugins_search_hook');
elgg_register_plugin_hook_handler('search', 'object:plugin_project', __NAMESPACE__ . '\\plugin_search');
elgg_register_plugin_hook_handler('elgg_solr:index', 'object', __NAMESPACE__ . '\\plugin_index');
elgg_register_plugin_hook_handler('route', 'plugins', __NAMESPACE__ . '\\plugins_router');
// when a plugin release gets created/updated we should reindex the project
elgg_register_event_handler('create', 'object', __NAMESPACE__ . '\\plugin_release_update');
elgg_register_event_handler('update', 'object', __NAMESPACE__ . '\\plugin_release_update');
}
function plugin_search($hook, $type, $return, $params) {
$select = array(
'start' => $params['offset'],
'rows' => $params['limit'],
'fields' => array('id','title','description')
);
if ($params['select'] && is_array($params['select'])) {
$select = array_merge($select, $params['select']);
}
// create a client instance
$client = elgg_solr_get_client();
// get an update query instance
$query = $client->createSelect($select);
$sorts = array(
'score' => 'desc',
'time_created' => 'desc'
);
if ($params['sorts'] && is_array($params['sorts'])) {
$sorts = $params['sorts'];
}
$query->addSorts($sorts);
$title_boost = elgg_solr_get_title_boost();
$description_boost = elgg_solr_get_description_boost();
// get the dismax component and set a boost query
$dismax = $query->getDisMax();
$qf = "title^{$title_boost} description^{$description_boost}";
if ($params['qf']) {
$qf = $params['qf'];
}
$dismax->setQueryFields($qf);
$dismax->setQueryAlternative('*:*');
$boostQuery = elgg_solr_get_boost_query();
if ($boostQuery) {
$dismax->setBoostQuery($boostQuery);
}
// this query is now a dismax query
$query->setQuery($params['query']);
// make sure we're only getting objects:plugin_project
$params['fq']['type'] = 'type:object';
$params['fq']['subtype'] = 'subtype:plugin_project';
if (($category = get_input('category')) && ($category != 'all')) {
$params['fq']['plugincat'] = 'tags:"' . elgg_solr_escape_special_chars('plugincat%%' . $category) . '"';
}
$default_fq = elgg_solr_get_default_fq($params);
if ($params['fq']) {
$filter_queries = array_merge($default_fq, $params['fq']);
}
else {
$filter_queries = $default_fq;
}
if (!empty($filter_queries)) {
foreach ($filter_queries as $key => $value) {
$query->createFilterQuery($key)->setQuery($value);
}
}
// get highlighting component and apply settings
$hl = $query->getHighlighting();
$hl->setFields(array('title', 'description'));
$hl->setSimplePrefix('<strong class="search-highlight search-highlight-color1">');
$hl->setSimplePostfix('</strong>');
// this executes the query and returns the result
try {
$resultset = $client->select($query);
} catch (Exception $e) {
error_log($e->getMessage());
return null;
}
// Get the highlighted snippet
try {
$highlighting = $resultset->getHighlighting();
} catch (Exception $e) {
error_log($e->getMessage());
return null;
}
// Count the total number of documents found by solr
$count = $resultset->getNumFound();
$search_results = array();
foreach ($resultset as $document) {
$search_results[$document->id] = array();
$snippet = '';
// highlighting results can be fetched by document id (the field defined as uniquekey in this schema)
$highlightedDoc = $highlighting->getResult($document->id);
if($highlightedDoc){
foreach($highlightedDoc as $field => $highlight) {
$snippet = implode(' (...) ', $highlight);
$snippet = search_get_highlighted_relevant_substrings(elgg_strip_tags($snippet), $params['query']);
$search_results[$document->id][$field] = $snippet;
}
}
}
// get the entities
$entities = array();
$entities_unsorted = array();
if ($search_results) {
$entities_unsorted = elgg_get_entities(array(
'guids' => array_keys($search_results),
'limit' => false
));
}
foreach ($search_results as $guid => $matches) {
foreach ($entities_unsorted as $e) {
if ($e->guid == $guid) {
if ($matches['title']) {
$e->setVolatileData('search_matched_title', $matches['title']);
}
else {
$e->setVolatileData('search_matched_title', $e->title);
}
if ($matches['description']) {
$e->setVolatileData('search_matched_description', $matches['description']);
}
else {
$e->setVolatileData('search_matched_description', elgg_get_excerpt($e->description, 100));
}
$entities[] = $e;
}
}
}
return array(
'entities' => $entities,
'count' => $count,
);
}
function plugin_index($h, $t, $doc, $p) {
if (!$doc) {
return $doc; // something else prevented this index
}
if (!elgg_instanceof($p['entity'], 'object', 'plugin_project')) {
return $doc;
}
$entity = $p['entity'];
// add brief description to the full description
$description = trim($entity->summary . ' ' . $entity->description);
$doc->description = $description;
// use generic fields for some filter queries
// eg.
$releases = $entity->getReleases(array('limit' => false));
$elgg_versions = array();
if ($releases) {
foreach ($releases as $release) {
if (is_array($release->elgg_version)) {
foreach ($release->elgg_version as $version) {
$elgg_versions[] = $version;
}
}
else {
$elgg_versions[] = $release->elgg_version;
}
}
}
$screenshots = $entity->getScreenshots();
$doc->version_ss = array_unique($elgg_versions);
$doc->plugin_type_s = $entity->plugin_type;
$doc->plugincat_s = $entity->plugincat;
$doc->license_s = $entity->license;
$doc->screenshots_i = $screenshots ? 1 : 0;
// store category with the tags
$categoryname = 'plugincat';
$tag_registered = false;
$md = elgg_get_registered_tag_metadata_names();
if ($md && in_array($categoryname, $md)) {
$tag_registered = true;
}
if (!$tag_registered) {
elgg_register_tag_metadata_name($categoryname);
}
$doc->tags = elgg_solr_get_tags_array($entity);
if (!$tag_registered) {
// we should unregister it now in case anything else
// wants to use registered tag names
elgg_set_config('registered_tag_metadata_names', $md);
}
return $doc;
}
function plugins_router($h, $t, $r, $p) {
if ($r['segments'][0] == 'search' && !isset($r['segments'][1])) {
include __DIR__ . '/pages/plugins/search.php';
return false;
}
return $r;
}
function plugin_release_update($event, $type, $release) {
if (!elgg_instanceof($release, 'object', 'plugin_release')) {
return true;
}
$project = $release->getProject();
// reindex the project when the release is updated or created
elgg_solr_add_update_entity('update', $project->type, $project);
}