-
Notifications
You must be signed in to change notification settings - Fork 2
/
lr_publish.module
416 lines (357 loc) · 11.5 KB
/
lr_publish.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
<?php
/**
* @file
* A module to publish drupal node metadata to a Learning Registry node.
*
* This module takes the specified node content and publishes it to a
* Learning Registry node.
*/
/**
* Display help and module information.
*
* @param string $path
* A string that specifies which path of the site we're displaying help.
* @param string $arg
* An array that holds that current path as would be returned from arg().
*
* @return string $help
* A string that contains the help text for the specified path.
*/
function lr_publish_help($path, $arg) {
$output = '';
switch ($path) {
case "admin/help#lr_publish":
$output = '<p>' .
t("Pushes Drupal node data to the specified Learning Registry node.") .
'<p>';
break;
}
return $output;
} // function lr_publish_help
/**
* Valid permissions for this module
*
* @return array
* An array of valid permissions for the lr_publish module.
*/
function lr_publish_perm() {
return array('administer lr_publish_perm');
} // function lr_publish_perm
/**
* Implementation of hook_menu().
*/
function lr_publish_menu() {
$items['admin/settings/lr_publish'] = array(
'title' => t('LR publish settings'),
'description' => t('Publish selected node metadata to a Learning Registry node.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('lr_publish_admin_settings'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
'file' => 'lr_publish.admin.inc',
);
return $items;
}
/**
* Implementation of hook_nodeapi()
*/
function lr_publish_nodeapi(&$node, $op) {
$nodes_to_publish = variable_get('lr_publish_node_types', array());
if (in_array($node->type, $nodes_to_publish) &&
$node->nid);
switch($op) {
case 'insert':
if ($node->status == 1) {
_lr_publish_publish($node);
}
break;
// case 'update':
// if ($node->status == 0) {
// _lr_publish_set_deleted($node);
// }
// else if ($node->status == 1) {
// _lr_publish_update($node);
// }
// break;
// case 'delete':
// _lr_publish_set_deleted($node);
// break;
}
}
/**
* Publish node metadata to specified Learning Registry node.
*
* @param object &$node
* A reference to the node object. We have to be very careful to not set
* anything, only read from the node object reference since the node
* reference allows us to edit the node content.
*/
function _lr_publish_publish(&$node) {
$content_info = _lr_get_content_info(&$node);
$resource_data = _lr_def_resource_data($content_info);
$empty_submission = _lr_build_envelope();
//watchdog('lr_publish', json_encode($empty_submission));
$prepared_submission =
_lr_add_resource_data_to_submission($empty_submission, $resource_data);
$resource_post_payload = json_encode($prepared_submission);
//watchdog('lr_publish', $resource_post_payload, array(), WATCHDOG_DEBUG);
_lr_send_to_registry($node->nid, $resource_post_payload);
}
/**
* Sets the deleted status for a deleted Drupal node in the LR node.
*
* @param object &$node
* A reference to the node object. We have to be very careful to not set
* anything, only read from the node object reference since the node
* reference allows us to edit the node content.
*/
function _lr_publish_set_deleted(&$node) {
return;
}
/**
* Update the info published to the node.
*
* @param object &$node
* A reference to the node object. We have to be very careful to not set
* anything, only read from the node object reference since the node
* reference allows us to edit the node content.
*/
function _lr_publish_update(&$node) {
return;
}
/**
* Return info we submit about nodes to the LR.
*
* @param object &$node
* A reference to the node object. We have to be very careful to not set
* anything, only read from the node object reference since the node
* reference allows us to edit the node content.
*/
function _lr_get_content_info(&$node) {
$content_info = array();
$content_info['resource_locator'] = _lr_get_node_url($node->nid);
// TODO: correctly specify resource data type
// resource_data_type is currently hardcoded, will be fixed in a later version
$content_info['resource_data_type'] = 'metadata';
$content_info['submitter'] = variable_get('lr_publish_submitter', NULL);
// currently set curator and submitter the same, once we get a better sense
// of what each field means, this will change
$content_info['curator'] = $content_info['submitter'];
// TODO: correctly set active value, currently hardcoded to TRUE
$content_info['active' ] = TRUE;
$content_info['payload_schema'] = "DC 1.1";
// define the node fields we send.
// TODO: actually send a useful subset of fields
$resource_data->title = $node->title;
$resource_data->creation_timestamp = $node->created;
$resource_data->modification_timestamp = $node->changed;
$content_info['resource_data'] = $resource_data;
return $content_info;
}
/**
* Return the absolute URL to a node.
*
* @param int $nid
* The numerical node id.
*
* @return string $node_url
* The absolute URL to the node.
*/
function _lr_get_node_url($nid) {
$url = $GLOBALS['base_url'] . $GLOBALS['base_path'] . "node/" . $nid;
return $url;
}
/**
* Make the curl calls to send to the LR node.
*
* All the specified nodeapi operations ($ops) call this function to actually send the data.
*
* @param string $nid
* A string containing the node ID for submission into the registry.
* @param string $json
* A string containing the json object that will be sent to the registry node.
* @param int $service_num
* An int that specifies the service that will be called.
* 0 - publish (default value)
* 1 - update
* 2 - delete
*/
function _lr_send_to_registry($nid, $json, $service_num = 0) {
$services = array(
'publish',
'update',
'delete',
);
if ($service_num >= count($services)) {
return;
}
$ch = curl_init();
$curl_options = array(
// TODO: allow more than publish! Currently hardcoding the publish
// bit. this function needs to take an argument to add the appropriate
// service at the end of the curl URL
CURLOPT_URL => variable_get('lr_publish_node_url', NULL) . "/publish",
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $json,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => array("Content-type: application/json",),
);
// file_put_contents("/tmp/foo.json", $json);
curl_setopt_array($ch, $curl_options);
$result=curl_exec($ch);
if ($result)
_lr_update_submission_status($nid, $result);
}
/**
* Define the Learning Registry resource data.
*
* @param array $content_info
* @returns object $resource_data
*/
function _lr_def_resource_data ($content_info) {
// values specified as immutable in the LR spec.
define("LR_DOC_TYPE", "resource_data");
define("LR_DOC_VERSION", "0.23.0");
define("LR_SUBMITTER_TYPE", "agent");
define("LR_SUBMISSION_TOS", "yes");
// commented since we aren't signing submissions
// define("LR_SIGNING_METHOD", "LR-PGP.1.0");
define("LR_PAYLOAD_PLACEMENT", "inline");
$opt_id_fields = array(
'curator',
'owner',
'signer',
);
$opt_res_fields = array(
'submitter_timestamp',
'submitter_TTL',
'keys',
'resource_TTL',
'payload_schema_locator',
'payload_schema_format',
);
// we don't really do much with these since we aren't currently signing
// submissions.
$opt_sig_fields = array(
'signature',
'key_server',
'key_locations',
'key_owner',
'signing_method',
);
$opt_tos_fields = array(
'tos_submission_attribution', // need to figure out more about this value.
);
$identity->submitter_type = LR_SUBMITTER_TYPE;
$identity->submitter = $content_info["submitter"];
// not sure what the value for this should be, does it depend on the LR
// node,currently set to "yes" to indicate agreement?
$tos->submission_TOS = LR_SUBMISSION_TOS;
//TODO: extract these foreach calls into their own function
// Add the optional keys / objects if they are passed in $content_info
// optional identity values.
foreach ($opt_id_fields as $field) {
if (array_key_exists($field, $content_info)) {
$identity->$field = $content_info[$field];
}
}
// optional resource_data values
foreach ($opt_res_fields as $field) {
if (array_key_exists($field, $content_info)) {
$resource_data->$field = $content_info[$field];
}
}
// create an empty stdClass since the digital_signature is completely
// optional.
$digital_signature = new stdClass;
// optional digital signature values
foreach ($opt_sig_fields as $field) {
if (array_key_exists($field, $content_info)) {
$digital_signature->$field = $content_info[$field];
}
}
// optional TOS values
foreach ($opt_tos_fields as $field) {
if (array_key_exists($field, $content_info)) {
$tos->$field = $content_info[$field];
}
}
$resource_data->doc_type = LR_DOC_TYPE;
$resource_data->doc_version = LR_DOC_VERSION;
$resource_data->resource_data_type = $content_info['resource_data_type'];
$resource_data->active = $content_info['active'];
$resource_data->identity = $identity;
$resource_data->TOS = $tos;
// only add the signature key if signature data are defined.
if (!empty($digital_signature)) {
$resource_data->digital_signature = $digital_signature;
}
$resource_data->resource_locator = $content_info['resource_locator'];
$resource_data->payload_placement = LR_PAYLOAD_PLACEMENT;
$resource_data->payload_schema = array($content_info['payload_schema']);
$resource_data->resource_data = $content_info['resource_data'];
return $resource_data;
}
/**
* Return a basic LR submission envelope.
*
* @return object $submission
* An empty LR submission envelop.
*/
function _lr_build_envelope() {
$submission->documents = array();
return $submission;
}
/**
* Adds resource data to a LR submission envelope.
*
* @param object $submission
* An LR submission object.
* @param object $resource_data
* An LR resource data object.
*
* @return object $submission
* The submission object with $resource data pushed on to the documents array.
*/
function _lr_add_resource_data_to_submission($submission, $resource_data) {
$submission->documents[] = $resource_data;
return $submission;
}
/**
* Adds a status record in the lr_publish table to record submission
*
* @param int $nid
* Drupal node ID of item to submit.
* @param string $result
* JSON response from Learning Registry.
*
*/
function _lr_update_submission_status($nid, $result) {
if ($result) {
$result_array = json_decode($result);
if ($result_array->document_results[0]->OK) {
// check to see if submission already in database
$submission_check_result = db_query('SELECT COUNT(nid) as CNT FROM {lr_publish} WHERE nid = %d', $nid);
$count = db_fetch_object($submission_check_result);
if ($count->CNT) {
// Record already exists, update timestamp
db_query('UPDATE {lr_publish} SET updated=%d WHERE nid = %d', time(), $nid);
}
else {
// Insert new record
// fill parameter array
$data = array(
'nid' => $nid,
'published' => time(),
'docid' => $result_array->document_results[0]->doc_ID,
);
// write it to the database
drupal_write_record('lr_publish', $data);
}
}
else {
watchdog('lr_publish', "Error publishing node ID %nid to the registry. Full response: %msg",
array ('%nid' => $nid,'%msg' => $result));
}
}
}