-
Notifications
You must be signed in to change notification settings - Fork 10
/
ting.client.inc
588 lines (507 loc) · 17.6 KB
/
ting.client.inc
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
<?php
/**
* @file
* Wrapper functions for Ting client.
*/
/**
* Load an object from Ting by ID.
*
* @param string $object_id
* Ting object ID.
* @param bool $enrich
* Whether to enrich the object with additional information, covers etc.
* @param string $object_format
* Which object format to request from Ting.
*
* @return mixed
* Object, if found - boolean FALSE if not.
*/
function ting_get_object_by_id($object_id, $enrich = FALSE, $object_format = 'dkabm') {
if (empty($object_id)) {
return FALSE;
}
static $cache = array();
$cache_key = implode(':', array($object_id, $enrich, $format));
if (isset($cache[$cache_key])) {
$object = $cache[$cache_key];
}
else {
$request = ting_get_request_factory()->getObjectRequest();
$request->setObjectId($object_id);
// Set the object format.
$request->setObjectFormat($object_format);
// Add agency and profile.
$request = ting_add_agency($request);
$request = ting_add_profile($request);
if ($enrich) {
$request = ting_add_relations($request);
}
$object = ting_execute($request);
if ($object) {
$object = ting_add_object_info($object);
}
}
if ($object && $enrich && empty($object->enriched)) {
$object = array_shift(ting_add_additional_info(array($object)));
$object->enriched = TRUE;
}
$cache[$cache_key] = $object;
return $object;
}
/**
* Load an object from Ting by local ID (faust number).
*
* @param string $local_id
* Local identifier
* @param bool $enrich
* Whether to enrich the object with additional information, covers etc.
*
* @return mixed
* TingClientObject if found, FALSE if not.
*/
function ting_get_object_by_local_id($local_id, $enrich = FALSE) {
if (empty($local_id)) {
return FALSE;
}
$request = ting_get_request_factory()->getObjectRequest();
$request->setLocalId($local_id);
// Add agency and profile.
$request = ting_add_agency($request);
$request = ting_add_profile($request);
if ($enrich) {
$request = ting_add_relations($request);
}
$object = ting_execute($request);
if ($object) {
$object = ting_add_object_info($object);
$object = ($enrich) ? array_shift(ting_add_additional_info(array($object))) : $object;
}
return ($object) ? $object : FALSE;
}
/**
* Display a Ting collection of objects.
*
* @param string $collection_id
* Ting collection ID.
* @param bool $enrich
* Whether to enrich objects in the collection with additional
* information - covers, etc.
*
* @return mixed
* Collection object, if found - boolean FALSE if not.
*/
function ting_get_collection_by_id($collection_id, $enrich = FALSE) {
// If passed collection_id is empty, save ourselves the trouble of trying
// to load the Ting client, do a request, etc. Big speedup for empty
// Ting reference fields.
if (empty($collection_id)) {
return FALSE;
}
static $cache = array();
if (isset($cache[$collection_id])) {
$collection = $cache[$collection_id];
}
else {
$request = ting_get_request_factory()->getCollectionRequest();
$request->setObjectId($collection_id);
// Add agency and profile.
$request = ting_add_agency($request);
$request = ting_add_profile($request);
$collection = ting_execute($request);
if ($collection) {
$collection = ting_add_collection_info($collection);
}
}
if ($collection && $enrich && empty($collection->enriched)) {
$collection = ting_add_additional_info($collection);
$collection->enriched = TRUE;
}
$cache[$collection_id] = $collection;
return $collection;
}
/**
* Return the possible sort keys and their corresponding labels.
*
* @return array
* A key/value array of sort keys and labels.
*/
function ting_search_rank_options() {
return array(
'rank_creator' => t('Rank by creator'),
'rank_general' => t('Rank by general relevance'),
'rank_title' => t('Rank by title'),
);
}
/**
* Return the possible sort keys and their corresponding labels.
*
* @return array
* A key/value array of sort keys and labels.
*/
function ting_search_sort_options() {
return array(
'' => t('Relevance'),
'title_ascending' => t('Title – A → Z'),
'title_descending' => t('Title – Z → A'),
'creator_ascending' => t('Author – A → Z'),
'creator_descending' => t('Author – Z → A'),
'date_descending' => t('Year of publication – newest first'),
'date_ascending' => t('Year of publication – oldest first'),
);
}
/**
* Performs a search against OpenSearch.
*
* @param string $query
* The search query
* @param int $page
* The page number to retrieve search results for
* @param int $resultsPerPage
* 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.
*
* @return TingClientSearchResult
* The search result
*/
function ting_do_search($query, $page = 1, $resultsPerPage = 10, $options = array()) {
$request = ting_get_request_factory()->getSearchRequest();
$request->setQuery($query);
$request->setStart($resultsPerPage * ($page - 1) + 1);
$request->setNumResults($resultsPerPage);
$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));
$request->setRank((isset($options['rank']) && $options['rank']) ? $options['rank'] : 'rank_general');
$request->setSort((isset($options['sort']) && $options['sort']) ? $options['sort'] : NULL);
$request->setAllObjects(isset($options['allObjects']) ? $options['allObjects'] : FALSE);
// Add agency and profile
$request = ting_add_agency($request);
$request = ting_add_profile($request);
// 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 ranks. Without these, the result set
// will be mangled.
$fields[] = array(
'fieldName' => 'cql.anyIndexes',
'fieldType' => 'phrase',
'weight' => 1,
);
$fields[] = array(
'fieldName' => 'cql.anyIndexes',
'fieldType' => 'word',
'weight' => 1,
);
$request->setUserDefinedRanking(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) {
$request->setUserDefinedBoost($boosts);
// If rank_general is set then boost is not used in the opensearch service.
$request->setRank('');
}
$searchResult = ting_execute($request);
//Decorate search result with additional information
if (is_array($searchResult->collections)) {
foreach ($searchResult->collections as &$collection) {
$collection = ting_add_collection_info($collection);
if (isset($options['enrich']) && $options['enrich']) {
$collection = ting_add_additional_info($collection);
}
}
}
return $searchResult;
}
/**
* @param string $query The prefix to scan for
* @param int $numResults The numver of results to return
* @return TingClientScanResult
*/
function ting_do_scan($query, $numResults = 10) {
$request = ting_get_request_factory()->getScanRequest();
$request->setField('phrase.anyIndexes');
$request->setLower($query);
$request = ting_add_agency($request);
$request->setNumResults($numResults);
return ting_execute($request);
}
/**
* @param string $word The word to get spell suggestions for
* @param $numResults The number of results to return
* @return array An array of TingClientSpellSuggestion objects
*/
function ting_get_spell_suggestions($word, $numResults = 10) {
$request = ting_get_request_factory()->getSpellRequest();
$request->setWord($word);
$request->setNumResults($numResults);
return ting_execute($request);
}
/**
* @param string $isbn ISBN number to get recommendations from
* @param $numResults The number of results to return
* @return array An array of TingClientObjectRecommendation objects
*/
function ting_get_object_recommendations($isbn, $numResults = 10) {
$request = ting_get_request_factory()->getObjectRecommendationRequest();
$request->setIsbn($isbn);
$request->setNumResults($numResults);
return ting_execute($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 {
// Allow other modules to modify the request just before executing
// and the response immediatly after returning
drupal_alter('ting_client_request', $request);
$response = ting_get_client()->execute($request);
drupal_alter('ting_client_response', $response);
return $response;
} catch (TingClientException $e) {
watchdog('ting client', 'Error performing request: '.$e->getMessage(), NULL, WATCHDOG_ERROR, 'http://'.$_SERVER["HTTP_HOST"].$_SERVER["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() : new TingClientVoidLogger();
$client = new TingClient(new TingClientRequestAdapter(), $logger);
}
return $client;
}
/**
* Retrieves an initialized Ting client request factory.
*
* @return TingClientRequestFactory
*/
function ting_get_request_factory() {
static $requestFactory;
if (!isset($requestFactory)) {
$urlVariables = array(
'search' => 'ting_search_url',
'scan' => 'ting_scan_url',
'object' => 'ting_search_url',
'collection' => 'ting_search_url',
'spell' => 'ting_spell_url',
'recommendation' => 'ting_recommendation_server',
);
$urls = array();
foreach ($urlVariables as $name => $setting) {
$urls[$name] = variable_get($setting, false);
if (!$urls[$name]) {
throw new TingClientException('No Ting webservice url defined for '.$name);
}
}
$requestFactory = new TingClientRequestFactory($urls);
}
return $requestFactory;
}
function ting_add_collection_info(TingClientObjectCollection $collection) {
$types = array();
$subject_count = array();
foreach ($collection->objects as $object) {
$object = ting_add_object_info($object);
$types[] = $object->type;
foreach ($object->subjects as $subject) {
if (!isset($subject_count[$subject])) {
$subject_count[$subject] = 0;
}
$subject_count[$subject]++;
}
}
$collection->types = array_unique($types);
asort($subject_count);
// All subjects from all objects with the most common ones first.
$collection->subjects = array_keys($subject_count);
$common_object = $collection->objects[0];
$collection->id = $common_object->id;
$collection->title = $common_object->title;
$collection->abstract = $common_object->abstract;
$collection->creators = $common_object->creators;
$collection->creators_string = $common_object->creators_string;
$collection->date = $common_object->date;
$collection->url = url('ting/collection/' . $collection->id, array('absolute' => true));
return $collection;
}
function ting_add_object_info(TingClientObject $object) {
$object->type = $object->record['dc:type']['dkdcplus:BibDK-Type'][0];
$object->language = !empty($object->record['dc:language'][''][0]) ? $object->record['dc:language'][''][0] : FALSE;
$object->title = $object->record['dc:title'][''][0];
$object->abstract = !empty($object->record['dcterms:abstract'][''][0]) ? $object->record['dcterms:abstract'][''][0] : FALSE;
$object->date = !empty($object->record['dc:date'][''][0]) ? $object->record['dc:date'][''][0] : FALSE;
$object->creators = array();
if (!empty($object->record['dc:creator'])) {
foreach ($object->record['dc:creator'] as $type => $dc_creator) {
if ($type != 'oss:sort') {
$object->creators = array_merge($object->creators, $dc_creator);
}
}
}
$object->creators_string = implode(', ', $object->creators);
$object->subjects = array();
if (!empty($object->record['dc:subject'])) {
foreach ($object->record['dc:subject'] as $type => $dc_subject) {
if (in_array($type, array('dkdcplus:DBCF', 'dkdcplus:DBCS', 'dkdcplus:DBCM', 'dkdcplus:DBCO', 'dkdcplus:DBCN'))) {
$object->subjects = array_merge($object->subjects, $dc_subject);
}
}
}
$object->url = url('ting/object/'.$object->id, array('absolute' => TRUE));
if (function_exists('ting_proxy_rewrite_download_url')) {
if ($object->relationsData) {
foreach ($object->relationsData as $data) {
if ($data->relationType == 'dbcaddi:hasOnlineAccess') {
// Remove non-usefull prefix.
$relationUri = preg_replace('/^\[URL\]/', '', $data->relationUri);
// Check for correct url - some uri is only an id.
if (stripos($relationUri, 'http') === 0) {
$onlineurl = $relationUri;
break;
}
}
}
}
// Fallback to dcterms:URI.
if (empty($onlineurl) && $object->record['dc:identifier']['dcterms:URI']) {
// Take the last URI - compability with earlier version.
$onlineurl = end($object->record['dc:identifier']['dcterms:URI']);
}
if (!empty($onlineurl)) {
$object->online_url = ting_proxy_rewrite_download_url($onlineurl);
}
}
if ($object->relations) {
// Add details to relation objects.
foreach ($object->relations as $key => $relation) {
$object->relations[$key] = ting_add_object_info($relation);
}
}
return $object;
}
/**
* Add additional information info for cover images.
*
* @param mixed $collection
* Either a TingClientObjectCollection or an array of
* TingClientObjects we want additional info for.
*
* @return mixed
* The collection passed in, with any additional data attached.
*/
function ting_add_additional_info($collection) {
$local_ids = array();
// If we're passed a TingClientObjectCollection, extract the array of
// TingClientObjects from it. Otherwise, just make a copy.
$objects = (isset($collection->objects)) ? $collection->objects : $collection;
foreach ($objects as $object) {
if ($object->localId) {
$local_ids[] = (object) array(
'localIdentifier' => $object->localId,
'libraryCode' => $object->ownerId,
);
}
}
if (sizeof($local_ids) > 0) {
foreach (ting_get_additional_info($local_ids) as $local_id => $ai) {
foreach ($objects as &$object) {
if ($local_id == $object->localId) {
$object->additionalInformation = $ai;
}
}
}
}
return $collection;
}
/**
* Get additional info for a number of FAUST numbers.
*
* @param mixed $local_ids
* Expects either a single object with localIdentifier and
* libraryCode attributes, or an array of such objects.
*
* @return array
* Additional info keyed by local ID number.
*/
function ting_get_additional_info($local_ids) {
$settings = array(
'wsdlUrl' => 'addi_wsdl_url',
'username' => 'addi_username',
'group' => 'addi_group',
'password' => 'addi_password',
);
foreach ($settings as $name => &$setting) {
$setting = variable_get($setting, FALSE);
if (!$setting) {
watchdog('TingClient', 'Additional information service setting “@name” not set', array(
'@name' => $setting,
), WATCHDOG_WARNING);
return array();
}
}
$service = new AdditionalInformationService($settings['wsdlUrl'], $settings['username'], $settings['group'], $settings['password']);
try {
return $service->getByLocalIdentifier($local_ids);
}
catch (AdditionalInformationServiceException $e) {
watchdog('TingClient', 'Error retrieving additional information and covers: @message', array(
'@message' => $e->getMessage(),
), WATCHDOG_ERROR);
}
// TODO: SoapFault should probably be catched within the Ting client.
catch (SoapFault $e) {
watchdog('TingClient', 'Error retrieving additional information and covers: @message', array(
'@message' => $e->getMessage(),
), WATCHDOG_ERROR);
}
return array();
}
function ting_add_agency(TingClientRequest $request) {
if ($agency = variable_get('ting_agency', false)) {
$request->setAgency($agency);
}
return $request;
}
function ting_add_profile(TingClientRequest $request) {
if ($profile = variable_get('ting_profile', 'opac')) {
$request->setProfile($profile);
}
return $request;
}
function ting_add_relations($request, $type = 'full') {
$request->setAllRelations(TRUE);
$request->setRelationData($type);
return $request;
}