-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimplepubmed.module
More file actions
executable file
·581 lines (551 loc) · 19 KB
/
simplepubmed.module
File metadata and controls
executable file
·581 lines (551 loc) · 19 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
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
<?php
/**
* @file
* The Simplepubmed module call on data from the PubMed database.
*
* The module creates a content type to store the data pulled from
* the Pubmed database. It also creates a field of type text PUBMEDID
* used to call the data from PubMed.
*
* @todo Do we want to update the result list only when saving the field?
*/
/**
* Implements hook_menu().
*/
function simplepubmed_menu() {
$items = array();
$items['admin/config/services/manage-simplepubmed'] = array(
'title' => 'Simple PubMed Configurations',
'description' => 'Simple PubMed Configurations',
'page callback' => 'drupal_get_form',
'page arguments' => array('manage_simplepubmed_settings'),
'access arguments' => array('administer Simple PubMed Configurations'),
'access callback' => 'simplepubmed__check_access',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implementation of access function.
*/
function simplepubmed__check_access($account = NULL) {
if (user_is_anonymous()) {
return FALSE;
}
return TRUE;
}
/**
* Simple PubMed Configuration Form.
*/
function manage_simplepubmed_settings() {
$form['access'] = array(
'#type' => 'vertical_tabs',
'#type' => 'fieldset',
'#title' => t('Simple PubMed Configuration'),
'#tree' => TRUE,
);
$form['access']['filter'] = array(
'#type' => 'vertical_tabs',
'#group' => 'access',
'#title' => t('Filter'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
$form['access']['filter']['submit'] = array(
'#type' => 'submit',
'#value' => 'Import PubMed Data',
'#weight' => 1,
'#submit' => array('data_simplepubmed_import'),
);
return $form;
}
/**
* Implements hook_field_info().
*
* Provides the field 'pubmed_field_pubmedids'.
*/
function simplepubmed_field_info() {
return array(
'pubmed_field_pubmedids' => array(
'label' => t('PubMed ID'),
'description' => t('Stores PubMed terms used for querying the PubMed database (http://www.ncbi.nlm.nih.gov/pubmed?term=)'),
'default_widget' => 'simplepubmed_textfield',
'default_formatter' => 'simplepubmed_pubmedids',
),
);
}
/**
* Implements hook_field_validate().
*
* Nothing to validate yet.
*
* @see pubmed_field_textfield_validate()
*
* @todo Implement validation.
*/
function simplepubmed_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {
if (!empty($item['pubmed_ids'])) {
if (FALSE) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'simplepubmed_term_invalid',
'message' => t('Error message'),
);
}
}
}
}
/**
* Implements hook_field_is_empty().
*/
function simplepubmed_field_is_empty($item, $field) {
return empty($item['pubmed_ids']);
}
/**
* Implements hook_field_formatter_info().
*
* We provide a simple formatter that lists links to the publications queried
* using the given term.
*/
function simplepubmed_field_formatter_info() {
return array(
// This displays a list of links to the search results of the given term.
'simplepubmed_pubmedids' => array(
'label' => t('Simple list of links to the publications given by querying the PubMed db using this field`s input as the term.'),
'field types' => array('pubmed_field_pubmedids'),
),
);
}
/**
* Implements hook_field_formatter_view().
*
* Implements views for the formater defined in
* pubmed_field_field_formatter_info().
*
* @see pubmed_field_field_formatter_info()
*
* @todo Not implemented yet!
*/
function simplepubmed_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
// This formatter simply outputs a list of links to the publications.
case 'simplepubmed_textfield':
foreach ($items as $delta => $item) {
// The outpur. $item['pubmed_ids'] is the array of publications.
if (!is_array($item['pubmed_ids'])) {
$item['pubmed_ids'] = array();
}
$element[$delta] = array(
'#markup' => theme('simplepubmed_pubmedids-list', array('publications' => $item['pubmed_ids'])),
);
}
break;
}
return $element;
}
/**
* Implements hook_field_widget_info().
*
* Provides a simple text widget as input for the term.
*
* @see pubmed_field_field_widget_form()
*/
function simplepubmed_field_widget_info() {
return array(
'simplepubmed_textfield' => array(
'label' => t('Textfield'),
'field types' => array('pubmed_field_pubmedids'),
),
);
}
/**
* Implements hook_field_widget_form().
*
* We provide the form widget for our field. For now it is only a simple text
* field but we could implement some kind of live search feature. We are adding
* pubmed_field_validate_id() as a validation callback validate intput
* and process the query.
*
* @see pubmed_field_validate_id()
*/
function simplepubmed_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$widget = $element;
$widget['#delta'] = $delta;
// Defines a simple text widget holding the term string.
switch ($instance['widget']['type']) {
case 'simplepubmed_pubmedids':
$widget['#type'] = 'textfield';
$widget['#weight'] = 1;
$widget['#title'] = t('PubMed Search IDS');
$widget['#default_value'] = isset($items[$delta]['pubmed_ids']) ? $items[$delta]['pubmed_ids'] : '';
$widget['#size'] = 60;
$widget['#maxlength'] = 255;
$widget['#description'] = t('Put in the PubMed Id.');
break;
case 'simplepubmed_textfield':
$widget['#type'] = 'textfield';
$widget['#weight'] = 1;
$widget['#title'] = t('PubMed Search IDS');
$widget['#default_value'] = isset($items[$delta]['pubmed_ids']) ? $items[$delta]['pubmed_ids'] : '';
$widget['#size'] = 60;
$widget['#maxlength'] = 255;
$widget['#description'] = t('Put in the PubMed Id.');
break;
}
$element['pubmed_ids'] = $widget;
$element['#type'] = 'fieldset';
return $element;
}
/**
* A validation callback for pubmed_field_field_widget_form().
*
* We use this to query the Pubmed db and set results as values of the given
* field.
*
* @see pubmed_field_field_widget_form()
* @see pubmed_field_field_presave()
*/
function simplepubmed_field_validate_id($element, &$form_state) {
$delta = $element['#delta'];
// TODO: Isn't there a better way to find out which element?
$field = $form_state['field'][$element['#field_name']][$element['#language']]['field'];
$field_name = $field['field_name'];
// Only do something if correct field was found.
if (!isset($form_state['values'][$field_name][$element['#language']][$delta]['pubmed_ids'])) {
return;
}
// Get the input term string to use for the query.
$pubmed_term = (string) $form_state['values'][$field_name][$element['#language']][$delta]['pubmed_ids'];
// If no term is specified, set result NULL for result ids and return.
if (empty($pubmed_term)) {
form_set_value($element, NULL, $form_state);
return;
}
}
/**
* Queries the PubMed db.
*
* @param string $term
* The term to query for.
* @param string $nid
* The nid to query for.
*
* @return array
* Returns an associative array of the form (id => title).
*/
function _simplepubmed_query_pubmed($term, $nid) {
watchdog('simplepubmed', 'Query pubmed for PubMed ID: @id.', array('@id' => $term));
// Start the PubMed Query.
try {
if (!module_load_include('inc', 'simplepubmed', 'simplepubmed.entrezclient')) {
return TRUE;
}
$client = new PubmedFieldEntrezClientnew();
$client->setTerm($term);
// Set maximum number of results.
$client->setReturnMax((int) variable_get('simplepubmed_limit_results', 100));
$result = $client->search(0);
// Start search query.
// $pubmed_ids will store the result ids of the query.
$idlist = (array) $result->IdList;
$pubmed_ids = is_array($idlist['Id']) ? array_combine($idlist['Id'], $idlist['Id'])
: array($idlist['Id'] => $idlist['Id']);
// Fetch results.
$results = $client->fetchRecords(0, TRUE);
$authorlist = array();
foreach ($results as $result) {
$result = (array) $result;
if (isset($result['Id']) && isset($result['Item']) && isset($result['Item'][5]) && is_string($result['Item'][5])) {
$pubmed_ids[$result['Id']] = new stdClass();
$pubmed_ids[$result['Id']]->field_pubmed_id = (int) $result['Id'];
$pubmed_ids[$result['Id']]->title = check_plain($result['Item'][5]);
$pubmed_ids[$result['Id']]->author = check_plain($result['Item'][4]);
$pubmed_ids[$result['Id']]->issue = check_plain($result['Item'][7]);
$pubmed_ids[$result['Id']]->volume_ = check_plain($result['Item'][6]);
$pubmed_ids[$result['Id']]->pagination = check_plain($result['Item'][8]);
$pubmed_ids[$result['Id']]->paginationcombined = check_plain($result['Item'][2]);
$pubmed_ids[$result['Id']]->paginationcombined .= ' ';
$pubmed_ids[$result['Id']]->paginationcombined .= check_plain($result['Item'][23]);
unset($authorlist);
$authorlist = (array) $result['Item'][3];
$pubmed_ids[$result['Id']]->authorlist = $authorlist['Item'];
$pubmed_ids[$result['Id']]->journal = check_plain($result['Item'][22]);
$givendate = check_plain($result['Item'][0]);
$createddate = date("Y-m-d H:i:s", strtotime($givendate));
$pubmed_ids[$result['Id']]->date_published = $createddate;
$pubmed_ids[$result['Id']]->pubmed_url = 'http://www.ncbi.nlm.nih.gov/pubmed/' . (int) $result['Id'];
}
}
$shorttext = '';
try {
$newresults = $client->fetchRecords(0, FALSE);
$newresults = (array) $newresults;
$pubmedarticle = (array) $newresults['PubmedArticle'];
$medlinecitation = (array) $pubmedarticle['MedlineCitation'];
$article = (array) $medlinecitation['Article'];
$teaser = (array) $article['Abstract'];
if (isset($teaser['AbstractText'])) {
$shorttext = $teaser['AbstractText'];
}
$pubmed_ids[$result['Id']]->shorttext = $shorttext;
}
catch (Exception $e) {
}
// Set defaults if item not fetched.
$dummy = new stdClass();
$dummy->pubmedid = NULL;
$dummy->title = '';
$dummy->field_date_published = NULL;
$dummy->pubmed_url = '';
foreach ($pubmed_ids as $pubmed_id => $publication) {
if (!is_object($pubmed_ids[$pubmed_id])) {
$pubmed_ids[$pubmed_id] = $dummy;
$pubmed_ids[$pubmed_id]->id = (int) $pubmed_ids[$pubmed_id];
$pubmed_ids[$pubmed_id]->pubmed_url = 'http://www.ncbi.nlm.nih.gov/pubmed/' . (int) $pubmed_ids[$pubmed_id];
$pubmed_ids[$pubmed_id]->shorttext = $shorttext;
}
}
}
catch (Exception $e) {
drupal_set_message(t('The PubMed query could not be perfomed.'), 'error');
$pubmed_ids = array();
}
return $pubmed_ids[$pubmed_id];
}
/**
* Implements hook_field_presave().
*
* Serialize pubmed_ids.
*/
function simplepubmed_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
if ($field['type'] == 'pubmed_field_pubmedids') {
foreach ($items as $delta => $item) {
if (isset($item['pubmed_ids'])) {
$items[$delta]['pubmed_ids'] = serialize($item['pubmed_ids']);
}
}
}
}
/**
* Implements hook_field_load().
*
* Unserializes pubmed_ids.
*/
function simplepubmed_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) {
if ($field['type'] == 'pubmed_field_pubmedids') {
$items[$id][$delta]['pubmed_ids'] = isset($item['pubmed_ids']) ?
unserialize($item['pubmed_ids']) : array();
}
}
}
}
/**
* Implements hook_cronapi().
*
* This hook is provided by elysia_cron and we use it to overwrite the default
* period pubmed_field_cron() is executed.
*
* Note that this module does not require elysia_cron to be active so this will
* just be ignored if not.
*
* @see simplepubmed_cron()
*/
function simplepubmed_cronapi($op, $job = NULL) {
$items['pubmed_field_cron'] = array(
'rule' => '1 2 * * *',
// Once a day.
);
return $items;
}
/**
* Implements hook_cron().
*
* Updates all fields marked for autoupdate.
*
* @todo Split tasks into cron queue and use cronapi.
*/
function simplepubmed_cron() {
// Call the cron function.
data_simplepubmed_import();
}
/**
* Implements cron pulling function.
*/
function data_simplepubmed_import() {
get_all_nids_to_delete();
$result = db_query('SELECT field_name FROM {field_config} WHERE type = :type',
array(':type' => 'pubmed_field_pubmedids'));
while ($field = $result->fetchObject()) {
$subquery = db_select('field_data_' . $field->field_name, 'fd');
$subquery->addField('fd', $field->field_name . '_pubmed_ids', 'pubmed_ids');
$subquery->addField('fd', 'entity_id', 'nid');
$subquery->addField('fd', 'entity_type', 'type');
$subresult = $subquery->execute();
while ($field_data = $subresult->fetchObject()) {
if ($field_data->pubmed_ids) {
$pid = unserialize($field_data->pubmed_ids);
$nid = $field_data->nid;
$type = $field_data->type;
$data = _simplepubmed_query_pubmed($pid, $nid);
save_new_publication($data, $nid, $type);
}
}
}
drupal_set_message(t('All records imported successfully.'), 'status');
}
/**
* Triggers hook pubmed_field_update preprocessing pubmed data first.
*/
function simplepubmed_invoke_pubmed_field_update($new_pubmed_ids, $old_pubmed_ids, $nid) {
// Update in.
}
/**
* Implements importing datas in to node.
*/
function save_new_publication($data, $nid, $type = NULL) {
$dummysave = '';
if (isset($data->field_pubmed_id) && $nid) {
$node_type = variable_get('simplepubmed_content_type', 'pubmed_content');
$check = check_pumbid_exists($data->field_pubmed_id, $nid);
if ($check) {
try {
$dummy = new stdClass();
$dummy->title = $data->title;
$dummy->uid = 1;
$dummy->status = 1;
$dummy->type = $node_type;
$dummy->field_date_published = array();
$dummy->field_pubmed_pubmed_url = array();
$dummy->field_pubmed_abstract = array();
$dummy->field_pubmed_author = array();
$dummy->field_pubmed_author_list = array();
$dummy->field_pubmed_journal = array();
$dummy->field_pubmed_pubmed_id = array();
$dummy->field_pubmed_related_entity = array();
$dummy->field_pubmed_entity_type = array();
$dummy->field_pubmed_pagination = array();
$dummy->field_pubmed_pag_combined = array();
$dummy->field_pubmed_volume_ = array();
$dummy->field_pubmed_issue = array();
if ($data->date_published) {
$dummy->field_pubmed_date_published[LANGUAGE_NONE][0]['value'] = $data->date_published;
$dummy->field_pubmed_date_published[LANGUAGE_NONE][0]['timezone'] = 'America/New_York';
$dummy->field_pubmed_date_published[LANGUAGE_NONE][0]['timezone_db'] = 'America/New_York';
$dummy->field_pubmed_date_published[LANGUAGE_NONE][0]['date_type'] = 'datetime';
}
if ($data->pubmed_url) {
$dummy->field_pubmed_pubmed_url[LANGUAGE_NONE][0]['value'] = $data->pubmed_url;
}
if ($data->pagination) {
$dummy->field_pubmed_pagination[LANGUAGE_NONE][0]['value'] = $data->pagination;
}
if ($data->paginationcombined) {
$dummy->field_pubmed_pag_combined[LANGUAGE_NONE][0]['value'] = $data->paginationcombined;
}
if ($data->issue) {
$dummy->field_pubmed_issue[LANGUAGE_NONE][0]['value'] = $data->issue;
}
if ($data->volume_) {
$dummy->field_pubmed_volume_[LANGUAGE_NONE][0]['value'] = $data->volume_;
}
if ($data->shorttext) {
$dummy->field_pubmed_abstract[LANGUAGE_NONE][0]['value'] = '';
if (is_array($data->shorttext)) {
foreach ($data->shorttext as $val) {
$dummy->field_pubmed_abstract[LANGUAGE_NONE][0]['value'] .= $val;
}
}
else {
$dummy->field_pubmed_abstract[LANGUAGE_NONE][0]['value'] = $data->shorttext;
}
}
if ($data->author) {
$dummy->field_pubmed_author[LANGUAGE_NONE][0]['value'] = $data->author;
}
if (count($data->authorlist)) {
if (is_array($data->authorlist)) {
foreach ($data->authorlist as $val) {
$dummy->field_pubmed_author_list[LANGUAGE_NONE][]['value'] = $val;
}
}
else {
$dummy->field_pubmed_author_list[LANGUAGE_NONE][]['value'] = '';
}
}
if ($data->journal) {
$dummy->field_pubmed_journal[LANGUAGE_NONE][0]['value'] = $data->journal;
}
if ($data->field_pubmed_id) {
$dummy->field_pubmed_pubmed_id[LANGUAGE_NONE][0]['value'] = $data->field_pubmed_id;
}
if ($nid) {
$dummy->field_pubmed_related_entity[LANGUAGE_NONE][0]['value'] = $nid;
}
if ($type) {
$dummy->field_pubmed_entity_type[LANGUAGE_NONE][0]['value'] = $type;
}
$dummysave = node_save($dummy);
}
catch (Exception $e) {
}
}
}
return $dummysave;
}
/**
* Checks to see if the PubMed Ids is already Imported.
*/
function check_pumbid_exists($pumbid, $nid) {
$pumbid = check_plain($pumbid);
$result = db_query("SELECT * FROM {field_data_field_pubmed_pubmed_id} WHERE `field_pubmed_pubmed_id_value`= :d", array(":d" => $pumbid));
$check = 1;
if ($result->rowCount()) {
$check = 0;
}
return $check;
}
/**
* Find nids not to delete.
*/
function get_all_nids_to_delete($nidstodelete = NULL) {
$nids = array();
$nidstodelete = array();
$result = db_query('SELECT field_name FROM {field_config} WHERE type = :type',
array(':type' => 'pubmed_field_pubmedids'));
while ($field = $result->fetchObject()) {
$subquery = db_select('field_data_' . $field->field_name, 'fd');
$subquery->addField('fd', $field->field_name . '_pubmed_ids', 'pubmed_ids');
$subquery->addField('fd', 'entity_id', 'nid');
$subresult = $subquery->execute();
while ($field_data = $subresult->fetchObject()) {
if ($field_data->pubmed_ids) {
$nids[] = unserialize($field_data->pubmed_ids);
}
}
}
if (count($nids)) {
$nidsuneque = array_unique($nids);
$nidstosearch = implode(',', $nidsuneque);
// Get all unwanted imported node ids.
$result = db_query("SELECT * FROM {field_data_field_pubmed_pubmed_id} WHERE field_pubmed_pubmed_id_value NOT IN (:d)", array(":d" => $nidstosearch));
foreach ($result as $record) {
$nidstodelete[] = $record->entity_id;
}
if (count($nidstodelete)) {
$nidstodelete = array_unique($nidstodelete);
foreach ($nidstodelete as $val) {
try {
// Delete the unwanted imported node.
node_delete($val);
}
catch (Exception $e) {
}
}
}
}
return $nidstodelete;
}