This repository has been archived by the owner on Oct 15, 2020. It is now read-only.
forked from easySuite/ting_subsearch_suggestions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathting_subsearch_suggestions.module
477 lines (407 loc) · 14.8 KB
/
ting_subsearch_suggestions.module
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
/**
* @file
* Ting subsearch using suggestion services: Opensuggestion and KPI-Webtrekk.
*/
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\ClientException as GuzzleClientException;
use Ting\Search\NullSearchResult;
module_load_include('inc', 'ting_subsearch_suggestions', 'ting_subsearch_suggestions.admin');
define('TING_SUBSEARCH_SUGGESTIONS_WEEKLY_CRON', 604800);
/**
* Implements hook_menu().
*/
function ting_subsearch_suggestions_menu() {
$items['subsearch_suggestions'] = [
'title' => 'Subsearch Suggestions',
'description' => 'AJAXify request for subsearch suggestions',
'page callback' => 'ting_subsearch_suggestions_ajax_callback',
'access callback' => TRUE,
];
return $items;
}
/**
* Custom AJAX menu callback.
*/
function ting_subsearch_suggestions_ajax_callback() {
$message = '';
if (!empty($_POST)) {
$keys = $_POST['keys'];
$conditions = $_POST['conditions'];
$results = unserialize($_POST['results']);
$message = ting_subsearch_suggestions_get_message($keys, $conditions, $results);
}
drupal_json_output($message);
drupal_exit();
}
/**
* Generation of suggestion message.
*
* @param $keys
* @param $conditions
* @param $results
*
* @return string|void
* @throws \TingClientException
* @throws \Ting\Search\SearchProviderException
*/
function ting_subsearch_suggestions_get_message($keys, $conditions, $results) {
$service = variable_get('ting_subsearch_suggestions_service', 'kpi');
if ($results instanceof NullSearchResult) {
return;
}
$num_total_objects = $results->getNumTotalObjects();
if (!empty($service)) {
$message = '';
// When a zero-hit search is executed and we're able to match "correct"
// keys, a new search is done with the "correct" keys.
// Then use the query-parameter original-keys to display what was done to
// the visitor.
$parameters = drupal_get_query_parameters();
if (isset($parameters['original-search'])) {
$msg = t('See results for "!keys", the search for "!original-keys" returned 0 hits.', [
'!original-keys' => $parameters['original-search'],
'!keys' => $keys,
]);
$message = '<div id="subsearch-suggestions-first">' . $msg . '</div>';
}
if ($num_total_objects == 0) {
// Find suggestions for "correct" search phrase.
$suggestion = ting_subsearch_common_suggested_keys($keys, 'ting_subsearch_suggestions_suggest_keys');
if (strpos($suggestion, 'ting_search:') !== FALSE) {
$clear_suggestion = explode(':', $suggestion);
$suggestion = $clear_suggestion[1];
}
if (!empty($suggestion)) {
// Do secondary search for "correct" keys.
$secondary_result = ting_subsearch_common_do_secondary_search($suggestion);
// Only redirect to "correct" keys search if it has a positive result.
if (!empty($secondary_result) && $secondary_result->numTotalObjects > 0) {
$additional_query = [
'original-search' => $keys,
];
$params['query'] = ting_subsearch_common_query_params(variable_get("ting_subsearch_{$service}_additional_url_params_0_hit", ""), $additional_query);
global $base_url;
$url = $base_url . '/search/ting/' . strtolower($suggestion) . '?' . drupal_http_build_query($params['query']);
header('Location: ' . $url, TRUE, 301);
}
}
}
if ($num_total_objects <= variable_get("ting_subsearch_{$service}_trigger_limit", 5)) {
$suggestion = ting_subsearch_common_suggested_keys($keys, 'ting_subsearch_suggestions_suggest_keys');
if ($service == 'kpi') {
if (strpos($suggestion, 'ting_search:') !== FALSE) {
$clear_suggestion = explode(':', $suggestion);
$suggestion = $clear_suggestion[1];
}
}
if ($suggestion) {
// Do additional search.
$suggested_result = ting_subsearch_common_do_secondary_search($suggestion);
$sug_total_number = $suggested_result->numTotalObjects;
if (!empty($suggested_result) && $sug_total_number >= variable_get("ting_subsearch_{$service}_message_limit", 10)) {
$ratio = $sug_total_number / $num_total_objects;
if ($ratio > variable_get('ting_subsearch_suggestions_ps_factor')) {
$options = [];
if (module_exists('ting_field_search')) {
$profile = ting_field_search_get_active_profile();
if ($profile) {
$options['query'] = ['profile' => $profile->name];
}
}
if (!empty(variable_get("ting_subsearch_{$service}_additional_url_params_inlimit", "")) && !empty($options)) {
$options['query'] += ting_subsearch_common_query_params(variable_get("ting_subsearch_{$service}_additional_url_params_inlimit", ""));
}
else {
if (empty($options)) {
$options['query'] = ting_subsearch_common_query_params(variable_get("ting_subsearch_{$service}_additional_url_params_inlimit", ""));
}
}
// Set message!
$msg = t('Search for <strong>"!suggested-key"</strong> (!suggested-num-results)', [
'!suggested-key' => strtolower($suggestion),
'!suggested-num-results' => $suggested_result->numTotalObjects,
]);
$wrapper = '<div id="subsearch-suggestions-second">' . $msg . '</div>';
$url = url('/search/ting/' . strtolower($suggestion), [
'query' => $options['query'],
'absolute' => TRUE,
]);
$message = l($wrapper, $url, [
'html' => TRUE,
'absolute' => TRUE,
'attributes' => ['target' => 'blank'],
]);
}
}
}
}
return $message;
}
}
/**
* Implements hook_ting_search_results_prefix().
*/
function ting_subsearch_suggestions_ting_search_results_prefix($keys, $conditions, $results) {
if ($results->getNumTotalObjects() != 0) {
drupal_add_js(
[
'subsearch_suggestions' => [
'keys' => $keys,
'conditions' => $conditions,
'results' => serialize($results),
],
],
'setting'
);
drupal_add_js(drupal_get_path('module', 'ting_subsearch_suggestions') . '/js/subsearch_suggestions.js', [
'type' => 'file',
'scope' => 'footer',
]);
}
else {
ting_subsearch_suggestions_get_message($keys, $conditions, $results);
}
}
/**
* Find the first suggestion for given keys.
*
* @param string $keys
* The original keys that you want to find suggestion for.
*
* @return string
* The suggestion.
*
* @throws Exception
*/
function ting_subsearch_suggestions_suggest_keys($keys) {
$service = variable_get('ting_subsearch_suggestions_service', 'kpi');
switch ($service) {
case 'os':
if (!variable_get('opensearch_search_autocomplete_suggestion_url', FALSE)) {
return FALSE;
}
$settings = opensearch_search_autocomplete_settings();
$url = variable_get('opensearch_search_autocomplete_suggestion_url', FALSE) . 'rest/facetSpell';
$params = $options['query'] = ['query' => check_plain($keys)] + $settings;
$options['maxTime'] = 300;
try {
$client = new HttpClient();
$response = $client->get(
$url,
[
'query' => $params,
'headers' => [
'Accept' => 'application/json',
],
]
);
$result = json_decode($response->getBody());
} catch (GuzzleClientException $e) {
throw new Exception($e->getMessage(), $e->getCode(), $e);
}
$items = [];
// First child of $result changes name from suggestions to suggestion when
// switching between version 3.0 and 3.5.
if (!empty(current($result))) {
foreach (current($result) as $suggestion) {
return $suggestion->phrase;
}
}
if (empty($items)) {
return FALSE;
}
break;
case 'kpi':
$keys_lowered = strtolower($keys);
$ting_kpi_minimum_searches = variable_get('ting_subsearch_kpi_minimum_searches', 100);
$ting_kpi_distance = variable_get('ting_subsearch_kpi_distance', 0);
$weeks_selected = variable_get('ting_subsearch_kpi_week_nrs', 1);
$weeks_in_db = db_query('SELECT DISTINCT tyw.weekno, tyw.year FROM {ting_subsearch_kpi_index_popular_keys} tyw ORDER BY tyw.year DESC, tyw.weekno DESC')->fetchAll();
$weeks_for_processing = array_slice($weeks_in_db, 0, $weeks_selected + 1);
$grouped_array = [];
foreach ($weeks_for_processing as $item) {
$grouped_array[$item->year][] = $item->weekno;
}
$items = [];
foreach ($grouped_array as $year => $weeks) {
$items[] = db_query('SELECT k.popular_keys FROM {ting_subsearch_kpi_index_popular_keys} k WHERE k.numtotalsearches > :numtotalsearches AND k.year = :year AND k.weekno IN (:weekno) ORDER BY k.weekno DESC', [
':numtotalsearches' => $ting_kpi_minimum_searches,
':year' => $year,
':weekno' => $weeks,
])->fetchCol();
}
$best_results = [];
foreach ($items as $item) {
if (!empty($item)) {
foreach ($item as $i) {
$best_results[] = $i;
}
}
}
foreach ($best_results as $best_result) {
if (strpos($best_result, '_')) {
$popular_keys = strtolower(str_replace('_', '.', $best_result));
}
else {
$popular_keys = $best_result;
}
$distance = levenshtein($keys_lowered, $popular_keys);
$raw_array[$popular_keys] = $distance;
}
$prepared_array = array_filter($raw_array, function ($distance, $key) use ($ting_kpi_distance) {
if ($distance > 0 && $distance <= $ting_kpi_distance) {
return $prepared_array[$key] = $distance;
}
}, ARRAY_FILTER_USE_BOTH);
if (!empty($prepared_array)) {
$result = array_keys($prepared_array, min($prepared_array));
}
return isset($result[0]) ? $result[0] : '';
break;
}
}
/**
* Implements hook_cron().
*/
function ting_subsearch_suggestions_cron() {
$service = variable_get('ting_subsearch_suggestions_service', 'kpi');
$prepared = FALSE;
if (!empty($service) && $service == 'kpi') {
// Ensure that local data is only renewed once a day.
if (!(variable_get('subsearch_kpi_cron_run', 0) < REQUEST_TIME)) {
return;
}
$url = variable_get('ting_subsearch_kpi_webservice_endpoint', NULL);
if (empty($url)) {
watchdog('ting_subsearch_opensuggestions', 'KPI: Module not configured properly', [], WATCHDOG_WARNING);
return;
}
$temp_files_folder = 'public://subsearch_kpi/';
if (!is_dir($temp_files_folder)) {
file_prepare_directory($temp_files_folder, FILE_CREATE_DIRECTORY);
}
$path_to_file = drupal_realpath("public://subsearch_kpi/temp_file.csv");
copyfile_chunked($url, $path_to_file);
print_r("File downloaded.\n");
$file = fopen(drupal_realpath("public://subsearch_kpi/temp_file.csv"), "r");
print_r("Insert feed data into database.\n");
while (($data = fgetcsv($file))) {
if ($data[0] != 'year') {
$normalized_string = mb_convert_encoding($data[2], 'UTF-8', 'ISO-8859-15');
/** @var \InsertQuery $query */
$query = db_insert('ting_subsearch_kpi_index_popular_keys')->fields(
[
'popular_keys' => $normalized_string,
'numtotalsearches' => (int) $data[3],
'weekno' => $data[1],
'year' => $data[0],
]
);
$query->execute();
}
}
$prepared = TRUE;
fclose($file);
if (!empty($prepared)) {
print_r("Start database cleaning.\n");
// Get general "meta" present in db.
$min_year = db_query("SELECT MIN(year) FROM {ting_subsearch_kpi_index_popular_keys}")->fetchField();
$min_week = db_query("SELECT MIN(weekno) FROM {ting_subsearch_kpi_index_popular_keys} WHERE year = {$min_year}")->fetchField();
$max_year = db_query("SELECT MAX(year) FROM {ting_subsearch_kpi_index_popular_keys}")->fetchField();
$max_week = db_query("SELECT MAX(weekno) FROM {ting_subsearch_kpi_index_popular_keys} WHERE year = {$max_year}")->fetchField();
foreach (range($min_week, $max_week - 1) as $week) {
db_delete('ting_subsearch_kpi_index_popular_keys')
->condition('weekno', $week)
->condition('year', $min_year)
->execute();
}
variable_set('subsearch_kpi_cron_run', REQUEST_TIME + TING_SUBSEARCH_SUGGESTIONS_WEEKLY_CRON);
watchdog('ting_subsearch_suggestions', 'Data fetched from KPI. Will re-run in a week.', [], WATCHDOG_INFO);
}
else {
watchdog('ting_subsearch_suggestions', 'No data fetched from feed', [], WATCHDOG_WARNING);
}
}
}
/**
* Copy remote file over HTTP one small chunk at a time.
*
* @param string $infile
* The full URL to the remote file
* @param string $outfile
* The path where to save the file
*
* @return bool|false|int $cnt
*/
function copyfile_chunked($infile, $outfile) {
$chunksize = 10 * (1024 * 1024); // 10 Megs
/**
* parse_url breaks a part a URL into it's parts, i.e. host, path,
* query string, etc.
*/
$parts = parse_url($infile);
$i_handle = fsockopen($parts['host'], 80, $errstr, $errcode, 5);
$o_handle = fopen($outfile, 'wb');
if ($i_handle == FALSE || $o_handle == FALSE) {
return FALSE;
}
if (!empty($parts['query'])) {
$parts['path'] .= '?' . $parts['query'];
}
/**
* Send the request to the server for the file
*/
$request = "GET {$parts['path']} HTTP/1.1\r\n";
$request .= "Host: {$parts['host']}\r\n";
$request .= "User-Agent: Mozilla/5.0\r\n";
$request .= "Keep-Alive: 115\r\n";
$request .= "Connection: keep-alive\r\n\r\n";
fwrite($i_handle, $request);
/**
* Now read the headers from the remote server. We'll need
* to get the content length.
*/
$headers = [];
while (!feof($i_handle)) {
$line = fgets($i_handle);
if ($line == "\r\n") {
break;
}
$headers[] = $line;
}
/**
* Look for the Content-Length header, and get the size
* of the remote file.
*/
$length = 0;
foreach ($headers as $header) {
if (stripos($header, 'Content-Length:') === 0) {
$length = (int) str_replace('Content-Length: ', '', $header);
break;
}
}
/**
* Start reading in the remote file, and writing it to the
* local file one chunk at a time.
*/
$cnt = 0;
while (!feof($i_handle)) {
$buf = '';
$buf = fread($i_handle, $chunksize);
$bytes = fwrite($o_handle, $buf);
if ($bytes == FALSE) {
return FALSE;
}
$cnt += $bytes;
/**
* We're done reading when we've reached the content length
*/
if ($cnt >= $length) {
break;
}
}
fclose($i_handle);
fclose($o_handle);
return $cnt;
}