forked from ding2/ting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathting.client.inc
More file actions
503 lines (447 loc) · 15 KB
/
ting.client.inc
File metadata and controls
503 lines (447 loc) · 15 KB
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
<?php
/**
* @file
* Wrapper functions for Ting client.
*/
function ting_get_object_request($object_id) {
$request = ting_get_request_factory()->getObjectRequest();
if ($agency = variable_get('ting_agency', FALSE)) {
$request->setAgency($agency);
}
$request->setObjectId($object_id);
$request->setAllRelations(TRUE);
$request->setRelationData('full');
// Set search profile, if applicable.
$profile = variable_get('ting_search_profile', '');
if (!empty($profile) && method_exists($request, 'setProfile')) {
$request->setProfile($profile);
}
$object = ting_execute_cache($request);
return $object;
}
/**
* Get an ting object or collection.
*
* Returns the search response for the given id. This will cache the
* result, and any sub-objects, so fetching objects from a recently
* fetched collection won't trigger another backend request.
*
* @param $object_id The id to fetch.
* @param $collection Whether to return a collection, if possible, or
* an object.
*
* @todo Should use getObject, but the ting-client lib doesn't implement that.
*/
function ting_get_object($object_id, $collection = FALSE) {
if (empty($object_id)) {
return FALSE;
}
// Check the cache first.
$object = ting_cache_get($object_id, $collection);
if (!$object) {
// Put a negative reply in the cache. It will be overwritten by the
// object, or ensure that we won't try to fetch this id again.
ting_cache_set($object_id, NULL);
if (!$object) {
$request = ting_get_request_factory()->getCollectionRequest();
$request->setObjectId($object_id);
if ($agency = variable_get('ting_agency', FALSE)) {
$request->setAgency($agency);
}
$profile = variable_get('ting_search_profile', '');
if (!empty($profile) && method_exists($request, 'setProfile')) {
$request->setProfile($profile);
}
$request->setAllObjects(false);
$object = ting_execute_cache($request);
}
}
// If not asking for a collection, and the object is, return the
// sub-object with the same id.
if (!$collection && isset($object->objects)) {
foreach ($object->objects as $sub_object) {
if ($sub_object->id == $object_id) {
return $sub_object;
}
}
// No sub-object had the same id. Somethings broken.
return NULL;
}
return $object;
}
/**
* Get a bunch of objects in one request.
*
* @todo Should use getObject when getObject supports getting multiple.
*/
function ting_get_objects($ids) {
$objects = array();
// Prefill from cache.
foreach ($ids as $id) {
$objects[$id] = ting_cache_get($id);
if (isset($objects[$id]) && isset($objects[$id]->objects)) {
foreach ($objects[$id]->objects as $sub_object) {
if ($sub_object->id == $id) {
$objects[$id] = $sub_object;
continue 2;
}
}
// No sub-object had the same id. Somethings broken.
$objects[$id] = NULL;
}
}
$query = array();
foreach ($objects as $id => $object) {
if (!isset($object)) {
$query[] = 'rec.id=' . $id;
}
}
if (sizeof($query)) {
$request = ting_get_request_factory()->getSearchRequest();
if ($agency = variable_get('ting_agency', FALSE)) {
$request->setAgency($agency);
}
$profile = variable_get('ting_search_profile', '');
if (!empty($profile) && method_exists($request, 'setProfile')) {
$request->setProfile($profile);
}
$request->setQuery(join(' OR ', $query));
$request->setStart(1);
$request->setNumResults(1000);
$request->setAllObjects(TRUE);
$result = ting_execute_cache($request);
if ($result && is_array($result->collections)) {
foreach ($result->collections as $collection) {
if (is_array($collection->objects) && sizeof($collection->objects)) {
foreach ($collection->objects as $object) {
$objects[$object->id] = $object;
}
}
}
}
}
return $objects;
}
/**
* Performs a search agains the well
*
* @param string $query
* The search query
* @param int $page
* The page number to retrieve search results for
* @param int $results_per_page
* The number of results to include per page
* @param array $options
* Options to pass to the search. Possible options are:
* - facets: Array of facet names for which to return results. Default: facet.subject, facet.creator, facet.type, facet.date, facet.language
* - numFacets: The number of terms to include with each facet. Default: 10
* - enrich: Whether to include additional information and cover images with each object. Default: false
* - sort: The key to sort the results by. Default: "" (corresponds to relevance). The possible values are defined by the sortType type in the XSD.
* - rank: The ranking type, as defined in the XSD.
* - supportingTypes: Whether to include supporting types such as reviews. Default: false
* - reply_only: Don't change the result objects to TingCollection objects.
* @return TingClientSearchResult
* The search result
*/
function ting_do_search($query, $page = 1, $results_per_page = 10, $options = array()) {
$request = ting_get_request_factory()->getSearchRequest();
$request->setQuery($query);
if ($agency = variable_get('ting_agency', FALSE)) {
$request->setAgency($agency);
}
$request->setStart($results_per_page * ($page - 1) + 1);
$request->setNumResults($results_per_page);
if (!isset($options['facets']) and module_exists('ding_facetbrowser')) {
$options['facets'] = array();
// Populate facets with configured facets.
foreach (variable_get('ding_facetbrowser_facets', array()) as $facet) {
$options['facets'][] = $facet['name'];
}
}
$request->setFacets((isset($options['facets'])) ? $options['facets'] : array('facet.subject', 'facet.creator', 'facet.type', 'facet.category', 'facet.language', 'facet.date', 'facet.acSource'));
$request->setNumFacets((isset($options['numFacets'])) ? $options['numFacets'] : ((sizeof($request->getFacets()) == 0) ? 0 : 10));
if (isset($options['sort']) && $options['sort']) {
$request->setSort($options['sort']);
}
$request->setAllObjects(isset($options['allObjects']) ? $options['allObjects'] : FALSE);
// Set search profile, if applicable.
$profile = variable_get('ting_search_profile', '');
if (!empty($profile) && method_exists($request, 'setProfile')) {
$request->setProfile($profile);
}
// Apply custom ranking if enabled.
if (variable_get('ting_ranking_custom', FALSE)) {
$fields = array();
foreach (variable_get('ting_ranking_fields', array()) as $field) {
$fields[] = array(
'fieldName' => $field['field_name'],
'fieldType' => $field['field_type'],
'weight' => $field['weight'],
);
}
if (!empty($fields)) {
// Add the default anyIndex boosts.
$fields[] = array(
'fieldName' => 'cql.anyIndexes',
'fieldType' => 'phrase',
'weight' => 1,
);
$fields[] = array(
'fieldName' => 'cql.anyIndexes',
'fieldType' => 'word',
'weight' => 1,
);
$request->userDefinedRanking = array('tieValue' => 0.1, 'rankField' => $fields);
}
}
// Otherwise, use the ranking setting.
else {
$request->setRank((isset($options['rank']) && $options['rank']) ? $options['rank'] : 'rank_general');
}
// Apply custom boosts if any.
$boosts = variable_get('ting_boost_fields', array());
if ($boosts) {
$uboosts = array();
foreach ($boosts as $boost_field) {
$uboosts[] = array(
'fieldName' => $boost_field['field_name'],
'fieldValue' => $boost_field['field_value'],
'weight' => $boost_field['weight'],
);
}
$request->userDefinedBoost = array('boostField' => $uboosts);
}
$search_result = ting_execute_cache($request);
// Replace collections with proper TingCollection objects.
if ($search_result && is_array($search_result->collections)) {
$ids = array();
foreach ($search_result->collections as &$collection) {
if (isset($collection->objects[0])) {
$ids[] = $collection->objects[0]->id;
}
}
if (!isset($options['reply_only']) || !$options['reply_only']) {
$search_result->collections = entity_load('ting_collection', array(), array('ding_entity_id' => $ids));
}
}
return $search_result;
}
/**
* Calls ting_execute() and caches the result.
*
* Executes the request and caches sub-objects.
*
* @param $request the request.
*/
function ting_execute_cache($request) {
$calls = &drupal_static(__FUNCTION__);
if (!isset($calls)) {
$calls = array();
}
$params = $request->getRequest()->getParameters();
$start_time = microtime(TRUE);
$reply = ting_execute($request);
$params['time'] = microtime(TRUE) - $start_time;
$calls[] = $params;
// Cache any sub-objects (mostly true for collections).
if (isset($reply->objects)) {
foreach ($reply->objects as $object) {
ting_cache_set($object->id, $object);
// Cache any relations.
if (isset($object->relations)) {
foreach ($object->relations as $relation) {
if (isset($relation->id)) {
ting_cache_set($relation->id, $relation);
}
}
}
}
// Cache the reply as the first object's id. This is for collections.
if (!isset($reply->id) and isset($reply->objects[0])) {
ting_cache_set($reply->objects[0]->id, $reply);
}
}
// Cache any collections. Done after objects to ensure that collections take
// precedence.
if (isset($reply->collections)) {
foreach ($reply->collections as &$collection) {
if (isset($collection->objects[0])) {
foreach ($collection->objects as $object) {
// Cache any relations.
if (isset($object->relations)) {
foreach ($object->relations as $relation) {
if (isset($relation->id)) {
ting_cache_set($relation->id, $relation);
}
}
}
ting_cache_set($object->id, $object);
}
ting_cache_set($collection->objects[0]->id, $collection);
}
}
}
// Cache any relations.
if (isset($reply->relations)) {
foreach ($reply->relations as $object) {
ting_cache_set($object->id, $object);
}
}
// Lastly cache the reply itself if it has an id.
if (isset($reply->id)) {
ting_cache_set($reply->id, $reply);
}
return $reply;
}
/**
* Get item from static cache.
*/
function ting_cache_get($id, $collection = FALSE) {
$cid = 'ting-' . ($collection ? 'collection' : 'object') . ':' . $id;
if ($ttl = variable_get('ting_cache_lifetime', TING_DEFAULT_CACHE_LIFETIME)) {
$cache = cache_get($cid);
if ($cache && ($cache->expire > REQUEST_TIME)) {
return $cache->data;
}
return NULL;
}
else {
// Without proper caching, use a request cache.
$cache = &drupal_static('ting_cache_set');
if (!isset($cache)) {
$cache = array();
}
// Using array_key_exists, as we might contain NULL values (which is !isset()).
if (array_key_exists($cid, $cache)) {
return $cache[$cid];
}
return NULL;
}
}
/**
* Put item in the static cache.
*/
function ting_cache_set($id, $value) {
$cid = 'ting-object';
if ($value instanceof TingClientObjectCollection) {
$cid = 'ting-collection';
}
$cid .= ':' . $id;
if ($ttl = variable_get('ting_cache_lifetime', TING_DEFAULT_CACHE_LIFETIME)) {
$cache = cache_set($cid, $value, 'cache', REQUEST_TIME + $ttl);
}
else {
// Without proper caching, use a request cache.
$cache = &drupal_static(__FUNCTION__);
if (!isset($cache)) {
$cache = array();
}
$cache[$cid] = $value;
}
}
/**
* Retrieves an initialized Ting client request factory.
*
* @return TingClientRequestFactory
*/
function ting_get_request_factory() {
static $request_factory;
if (!isset($request_factory)) {
$url_variables = array(
'search' => 'ting_search_url',
'scan' => 'ting_scan_url',
'object' => 'ting_search_url',
'collection' => 'ting_search_url',
'spell' => 'ting_spell_url',
'recommendation' => 'ting_recommendation_url',
);
// TODO: This should probably be rethought.
if (module_exists('ting_infomedia') && variable_get('ting_infomedia_url', FALSE)) {
$url_variables['infomedia'] = 'ting_infomedia_url';
}
$urls = array();
foreach ($url_variables as $name => $setting) {
$urls[$name] = variable_get($setting, FALSE);
if (!$urls[$name]) {
throw new TingClientException('No Ting webservice url defined for ' . $name);
}
}
$request_factory = new TingClientRequestFactory($urls);
}
return $request_factory;
}
function ting_add_relations($request, $type = 'full') {
$request->setAllRelations(TRUE);
$request->setRelationData($type);
return $request;
}
/**
* Perform a request against Ting and perform error handling if necessary
*
* @param $request The request
* @return mixed Result of the request or false if an error occurs
*/
function ting_execute($request) {
try {
timer_start('ting');
$res = ting_get_client()->execute($request);
timer_stop('ting');
return $res;
} catch (TingClientException $e) {
timer_stop('ting');
watchdog('ting client', 'Error performing request: ' . $e->getMessage(), NULL, WATCHDOG_ERROR, 'http://' . $_SERVER["HTTP_HOST"] . request_uri());
return FALSE;
}
}
/**
* Retrieves an initialized Ting client with appropriate request adapter and logger
*
* @return TingClient
*/
function ting_get_client() {
static $client;
if (!isset($client)) {
$logger = (variable_get('ting_enable_logging', FALSE)) ? new TingClientDrupalWatchDogLogger(ting_get_request_factory()) : new TingClientVoidLogger();
$client = new TingClient(new TingClientRequestAdapter(), $logger);
}
return $client;
}
/**
* Use OpenScan to search for keyword, check
* http://oss.dbc.dk/twiki/bin/view/Databroend/OpenSearchDocIndexes
* for which phrase index to search, default is 'anyIndexes'
*
* @param string $query The prefix to scan for
* @param string $phrase Which phrase index to search
* @param int $num_results The numver of results to return
* @return TingClientScanResult
*/
function ting_do_scan($query, $phrase = 'anyIndexes', $num_results = 10) {
$request = ting_get_request_factory()->getScanRequest();
$request->setField('phrase.' . $phrase);
$request->setLower($query);
$request = ting_add_agency($request);
$request->setNumResults($num_results);
return ting_execute($request);
}
/**
* @param object $request - The TingClient object
* @return TingClientScanRequest
*/
function ting_add_agency(TingClientScanRequest $request) {
if ($agency = variable_get('ting_agency', FALSE)) {
$request->setAgency($agency);
}
return $request;
}
/**
* @param string $word The word to get spell suggestions for
* @param $num_results The number of results to return
* @return array An array of TingClientSpellSuggestion objects
*/
function ting_get_spell_suggestions($word, $num_results = 10) {
$request = ting_get_request_factory()->getSpellRequest();
$request->setWord($word);
$request->setNumResults($num_results);
return ting_execute($request);
}