forked from ding2/ting-client
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathting_client.module
More file actions
321 lines (274 loc) · 9.1 KB
/
ting_client.module
File metadata and controls
321 lines (274 loc) · 9.1 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
<?php
/**
* @file
* Ting Client module for Drupal
*
* This is a module and not a library due to the dependency on the NanoSOAP
* module.
*
* All code are found in the lib/ directory and subdirectories. Inclusion of
* code (classes) is done by ting_client.info.
*/
require_once('ting_client_class.inc');
define('TING_DEFAULT_CACHE_LIFETIME', 0);
function ting_client_validate_xsd($classname, $action) {
$client = new ting_client_class();
$xsd_url = FALSE;
foreach ($client->getUrls() as $service) {
if (!empty($service['class']) && $service['class'] == $classname) {
$xsd_url = $service['xsd_url'];
}
}
if (!$xsd_url) {
throw new Exception('ting-client: Webservice XSD is not defined for ' .
$classname);
return array();
}
$parts = parse_url(variable_get($xsd_url));
$xsd_file_name = file_default_scheme() . '://ting/ting-client/xsd/' .
basename($parts['path']);
$schema = new xml_schema();
$schema->get_from_file($xsd_file_name);
$seq = $schema->get_sequence($action);
$arr[] = 'action';
foreach ($seq as $element) {
$s = $schema->get_element_attributes($element);
$arr[] = $s['name'];
}
return $arr;
}
/**
* Implements hook_permission().
*/
function ting_openformat_permission() {
return array(
'administer webservices settings' => array(
'title' => t('Administer webservices settings'),
),
);
}
/* Implements hook_menu */
function ting_client_menu() {
$items['admin/config/serviceclient'] = array(
'title' => 'Webservice client settings',
'description' => 'Manage webservices.',
'position' => 'right',
'weight' => 20,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('access administration pages'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/config/serviceclient/settings'] = array(
'title' => 'Urls and settings',
'description' => 'Manage webservice settings.',
'weight' => -20,
'page callback' => 'drupal_get_form',
'page arguments' => array('ting_client_admin_webservices_settings'),
'access arguments' => array('administer webservices settings'),
'file' => 'ting_client.admin.inc'
);
$items['admin/config/serviceclient/cache_settings'] = array(
'title' => 'Cache settings',
'description' => 'Configure cache settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('ting_client_cache_admin_form'),
'access arguments' => array('administer webservices settings'),
'file' => 'ting_client.admin.inc',
);
$items['admin/config/serviceclient/overview'] = array(
'title' => 'View services in use',
'description' => 'View services in use.',
'page callback' => 'ting_client_view_services',
'access arguments' => array('administer webservices settings')
);
return $items;
}
/**
* Get a table view of active webservices
*
* @return string
* html table definition
* @throws Exception
* Drupals theme function might throw an exception - ignore
*/
function ting_client_view_services() {
$client = new ting_client_class();
$services = $client->getUrls();
$header = array('NAME', 'URL');
$rows = array();
foreach ($services as $name => $service) {
$rows[] = array($name, $service['url']);
}
$rows = array_merge($rows, ting_client_loose_services());
return theme('table', array('header' => $header, 'rows' => $rows));
}
/**
* Get an array of services NOT handled by ting_client
* @return array
*/
function ting_client_loose_services() {
$rows = array();
//autocomplete
$rows[] = array('Autocomplete', variable_get('bibdk_autocomplete_rest_url'));
//openuserinfo
$rows[] = array('OpenUserInfo', variable_get
('bibdk_provider_webservice_url'));
return $rows;
}
/**
* support for ting_client_class
* set urls for request factory
* */
function ting_client_set_request_factory($url_variables) {
// has other modules any webservices
$more = module_invoke_all('ting_client_webservice');
if (!empty($more)) {
$url_variables = array_merge($url_variables, $more);
}
// get the real url (variable_get)
foreach ($url_variables as $name => $settings) {
if (!($url = (isset($settings['url'])) ? $settings['url'] : NULL)) {
throw new Exception('ting-client: Webservice URL is not defined for ' .
$name);
}
// if ( !($class = ( isset($settings['class']) ) ? $settings['class'] : NULL) ) {
// throw new Exception( 'ting-client: Webservice class is not defined for ' . $name);
// }
$webservice_check[$name]['url'] = $url;
$webservice_check[$name]['xsd_url'] =
(isset($url_variables[$name]['xsd_url'])) ?
$url_variables[$name]['xsd_url'] : NULL;
$url_variables[$name]['url'] = variable_get($url);
}
ting_client_webservice_check($webservice_check);
return $url_variables;
}
/**
* support for ting_client_class
* check if webservice URL, or XSD, has changed, and update XSD if yes.
* */
function ting_client_webservice_check($check_data) {
// get stored values
$webservice_check = variable_get('webservice_check', array());
foreach ($check_data as $name => $settings) {
$webservice_check_url = (!empty($webservice_check[$name]['url'])) ?
$webservice_check[$name]['url'] : NULL;
$webservice_check_xsd = (!empty($webservice_check[$name]['xsd_url'])) ?
$webservice_check[$name]['xsd_url'] : NULL;
$webservice_check_file = NULL;
if (!empty($settings['xsd_url'])) {
$parts = parse_url(variable_get($settings['xsd_url']));
$xsd_file_name = file_default_scheme() . '://ting/ting-client/xsd/' .
basename($parts['path']);
$webservice_check_file = is_file($xsd_file_name);
}
if (!isset($webservice_check[$name]) ||
variable_get($settings['url']) != $webservice_check_url ||
variable_get($settings['xsd_url']) != $webservice_check_xsd ||
($settings['xsd_url'] && !$webservice_check_xsd) ||
!$webservice_check_file
) {
$webservice_check[$name]['url'] = variable_get($settings['url']);
if (!empty($settings['xsd_url']) &&
$xsd_url = variable_get($settings['xsd_url'])
) {
// update XSD if webservice has changed URL.
$result = drupal_http_request($xsd_url);
// Bail if the HTTP request failed
if (!empty($result->error)) {
watchdog('ting client', 'ting-client: HTTP request failed for ' . $xsd_url .'Error: @error', array('@error' => $result->error ), WATCHDOG_ERROR);
return;
}
// Get the folder for the final location of this preset...
$directory = dirname($xsd_file_name);
// Build the destination folder tree if it doesn't already exist.
if (!file_prepare_directory($directory,
FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)
) {
throw new Exception('ting-client: Failed to create directory: ' .
$directory);
}
file_save_data($result->data, $xsd_file_name, FILE_EXISTS_REPLACE);
$webservice_check[$name]['xsd_url'] =
variable_get($settings['xsd_url']);
}
}
}
variable_set('webservice_check', $webservice_check);
}
/**
* support for ting_client_class
* get method for caching
* use drupal::cache_get
* */
function ting_client_cache_get($cache_key, $storage = NULL) {
$cache = cache_get($cache_key, $storage);
// handle cache timeout
if ($cache && $cache->expire > 0 && $cache->expire > REQUEST_TIME) {
return $cache;
}
return FALSE;
}
/**
* support for ting_client_class
* set method for caching
* use drupal::cache_set
* */
function ting_client_cache_set($cache_key, $data, $storage = NULL,
$expire = NULL) {
cache_set($cache_key, $data, $storage, $expire);
}
/**
* support for ting_client_class
* add a drupal message and write to watchdog
* */
function ting_client_set_message($message = '', $type = 'status',
$repeat = NULL, $watchdog = array()) {
if (!empty($message)) {
drupal_set_message($message, $type, $repeat);
}
if (!empty($watchdog)) {
watchdog($watchdog['type'], $watchdog['message'], array(), WATCHDOG_ERROR,
$watchdog['link']);
}
else if ($type == 'error') {
watchdog('ting_client', $message, array(), WATCHDOG_ERROR, NULL);
}
}
/**
* support for ting_client_class
* enable/disable caching
* */
function ting_client_enable_cache() {
return variable_get('search_client_enable_cache', FALSE);
}
/**
* support for ting_client_class
* enable/disable logging
* */
function ting_client_enable_logging() {
return variable_get('ting_enable_logging', FALSE);
}
/**
* support for ting_client_class
* start/stop drupal timer
* */
function ting_client_timer($action, $name) {
$function = 'timer_' . $action;
call_user_func_array($function, array($name));
}
/**
* Implements hook_ding_install_tasks().
*/
function ting_client_ding_install_tasks() {
module_load_include('inc', 'ting_client', 'ting_client.admin');
return array(
'ting_client_admin_webservices_settings' => array(
'display_name' => st('Service settings'),
'type' => 'form',
'file' => drupal_get_path('module', 'ting_client') .
'/ting_client.admin.inc',
),
);
}