-
Notifications
You must be signed in to change notification settings - Fork 6
/
lib4ridora.module
2390 lines (2225 loc) · 101 KB
/
lib4ridora.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
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Lib4Ri specific Islandora functionality.
*/
// Permissions.
define('LIB4RIDORA_ADMINISTRATOR_PERMISSION', 'administer lib4ridora objects');
define('LIB4RIDORA_INTERNAL_DOCUMENT_PERMISSION', 'visibility of internal documents');
define('LIB4RIDORA_JOURNAL_IMPACT_FACTOR_PERMISSION', 'import journal impact factors');
if ( !defined('LIB4RIDORA_SOLR_EXPORT_AUTHOR_WITH_UNIT') ) { define('LIB4RIDORA_SOLR_EXPORT_AUTHOR_WITH_UNIT', 'add affiliation to author in export'); }
// PIDS.
define('LIB4RIDORA_JOURNAL_CONTENT_MODEL', 'lib4ri:journalCModel');
define('LIB4RIDORA_JOURNAL_COLLECTION', 'lib4ri:journalCollection');
// Forms.
define('LIB4RIDORA_CITATION_FORM', 'Lib4RI Citation MODS Form');
define('LIB4RIDORA_JOURNAL_FORM', 'Lib4RI Journal MODS Form');
// Datastream ID.
define('LIB4RIDORA_FACTOR_DSID', 'IMPACT-FACTORS');
const LIB4RIDORA_ISLANDORA_SOLR_METADATA_MODS_AUTHOR_PSEUDO_FIELD = 'lib4ridora_mods_pseudo_field'; // see inside: admin/islandora/search/islandora_solr_metadata
const LIB4RIDORA_ISLANDORA_SOLR_METADATA_MODS_FUNDING_PSEUDO_FIELD = 'lib4ridora_mods_pseudo_field_funding';
/**
* Implements hook_islandora_required_objects().
*/
function lib4ridora_islandora_required_objects(IslandoraTuque $connection) {
$module_path = drupal_get_path('module', 'lib4ridora');
// Journal content model.
$journal_content_model = $connection->repository->constructObject(LIB4RIDORA_JOURNAL_CONTENT_MODEL);
$journal_content_model->owner = 'fedoraAdmin';
$journal_content_model->label = 'Lib4RI Journal Content Model';
$journal_content_model->models = 'fedora-system:ContentModel-3.0';
// DS-COMPOSITE-MODEL Datastream.
$datastream = $journal_content_model->constructDatastream('DS-COMPOSITE-MODEL', 'X');
$datastream->label = 'DS-COMPOSITE-MODEL';
$datastream->mimetype = 'application/xml';
$datastream->setContentFromFile("$module_path/xml/journal_ds_composite.xml", FALSE);
$journal_content_model->ingestDatastream($datastream);
// Journal collection.
$journal_collection = $connection->repository->constructObject(LIB4RIDORA_JOURNAL_COLLECTION);
$journal_collection->owner = 'fedoraAdmin';
$journal_collection->label = 'Lib4RI Journal Collection';
$journal_collection->models = 'islandora:collectionCModel';
$journal_collection->relationships->add(
FEDORA_RELS_EXT_URI,
'isMemberOfCollection',
variable_get('islandora_repository_pid', 'islandora:root')
);
// Collection Policy Datastream.
$datastream = $journal_collection->constructDatastream('COLLECTION_POLICY', 'M');
$datastream->label = 'COLLECTION_POLICY';
$datastream->mimetype = 'application/xml';
$datastream->setContentFromFile("$module_path/xml/journal_collection_policy.xml", FALSE);
$journal_collection->ingestDatastream($datastream);
// TN Datastream.
$datastream = $journal_collection->constructDatastream('TN', 'M');
$datastream->label = 'TN';
$datastream->mimetype = 'image/png';
$islandora_module_path = drupal_get_path('module', 'islandora');
$datastream->setContentFromFile("$islandora_module_path/images/folder.png", FALSE);
$journal_collection->ingestDatastream($datastream);
return array(
'lib4ridora' => array(
'title' => 'Lib4RI',
'objects' => array(
$journal_content_model,
$journal_collection,
),
),
);
}
/**
* Implements hook_islandora_solr_query_blocks().
*/
function lib4ridora_islandora_solr_query_blocks() {
return array(
'current_query_facet_name' => array(
'name' => t('Islandora Query With Facet Title'),
'module' => 'lib4ridora',
'file' => 'includes/solr_results.inc',
'class' => 'IslandoraSolrResultsLib4riBookmark',
'function' => 'currentQuery',
'form' => NULL,
),
);
}
/**
* Implements hook_islandora_xml_form_builder_form_associations().
*/
function lib4ridora_islandora_xml_form_builder_form_associations() {
$associations = array(
'lib4ridora_journal_mods_form' => array(
'content_model' => LIB4RIDORA_JOURNAL_CONTENT_MODEL,
'form_name' => LIB4RIDORA_JOURNAL_FORM,
'dsid' => 'MODS',
'title_field' => array('titleInfo', 'title'),
'transform' => 'mods_to_dc.xsl',
'template' => FALSE,
),
'lib4ridora_citation_mods_form' => array(
'content_model' => 'ir:citationCModel',
'form_name' => LIB4RIDORA_CITATION_FORM,
'dsid' => 'MODS',
'title_field' => array('titleInfo', 'title'),
'transform' => 'mods_to_dc.xsl',
'template' => FALSE,
),
);
$base = array(
'content_model' => 'ir:citationCModel',
'dsid' => 'MODS',
'title_field' => array('titleInfo', 'title'),
'transform' => 'mods_to_dc.xsl',
'template' => FALSE,
);
module_load_include('inc', 'lib4ridora', 'includes/citation.subtypes');
foreach (lib4ridora_citation_form_subtypes() as $type => $info) {
$associations["lib4ridora_{$type}_form"] = $base + array(
'form_name' => $info['name'],
);
}
return $associations;
}
/**
* Implements hook_islandora_solr_primary_display().
*/
function lib4ridora_islandora_solr_primary_display() {
return array(
'bookmark_lib4ri' => array(
'name' => t('Lib4ri Bookmark'),
'module' => 'lib4ridora',
'file' => 'includes/solr_results.inc',
'class' => 'IslandoraSolrResultsLib4riBookmark',
'function' => 'displayResults',
'description' => t('Lib4Ri Custom Bookmark Display'),
),
);
}
/**
* Implements hook_islandora_xml_form_builder_forms().
*/
function lib4ridora_islandora_xml_form_builder_forms() {
$module_path = drupal_get_path('module', 'lib4ridora');
$forms = array(
LIB4RIDORA_JOURNAL_FORM => array(
'form_file' => "$module_path/xml/journal_mods_form.xml",
),
LIB4RIDORA_CITATION_FORM => array(
'form_file' => "$module_path/xml/citation_mods_form.xml",
),
'lib4ridora example text_format form' => array(
'form_file' => "$module_path/xml/text_format_example.xml",
),
);
module_load_include('inc', 'lib4ridora', 'includes/citation.subtypes');
$genre_map = lib4ridora_citation_get_genre_map(TRUE);
foreach (lib4ridora_citation_form_subtypes() as $type => $info) {
$xml_path = "$module_path/xml/subtype_forms/$type.xml";
if (!file_exists($xml_path)) {
$xml_path = "$module_path/xml/subtype_forms/{$genre_map[$info['name']]}.xml";
}
$forms[$info['name']] = array(
'form_file' => $xml_path,
);
}
return $forms;
}
/**
* Implements hook_islandora_ingest_steps_alter().
*/
function lib4ridora_ir_citationCModel_islandora_ingest_steps_alter(array &$steps, array &$form_state) {
unset($steps['xml_form_builder_association_step']);
unset($steps['islandora_scholar_file_upload']);
unset($steps['islandora_populator_select']);
unset($steps['islandora_mini_workflow_form']);
}
/**
* Implements hook_CMODEL_PID_islandora_ingest_steps().
*/
function lib4ridora_ir_citationCModel_islandora_ingest_steps(array &$form_state) {
$steps = array();
$steps['lib4ridora_ingest_selector'] = array(
'weight' => -2,
'type' => 'form',
'form_id' => 'lib4ridora_ingest_selector_form',
'module' => 'lib4ridora',
'file' => 'includes/ingest-selector.form.inc',
);
$steps['lib4ridora_multi_pdf_upload'] = array(
'weight' => 8,
'type' => 'form',
'form_id' => 'lib4ridora_jpdf_form',
'module' => 'lib4ridora',
'file' => 'includes/upload.form.inc',
);
return $steps;
}
/**
* Implements hook_islandora_datastream_access().
*/
function lib4ridora_islandora_datastream_access($op, AbstractDatastream $datastream, $user) {
$models = array(
'ir:citationCModel',
);
$lib4ridora_ds_viewable = !(($user->uid === 0) && ($op == ISLANDORA_VIEW_OBJECTS) && (in_array($datastream->id, array_map('trim', explode(',', variable_get('lib4ridora_restricted_datastreams', 'RELS-INT,RELS-EXT,POLICY'))))));
if (!array_intersect($datastream->parent->models, $models)) {
// Not a model we're interested in...
if (!$lib4ridora_ds_viewable) {
return FALSE;
}
return;
}
$result = array();
if (!$lib4ridora_ds_viewable) {
$result['lib4ridora_ds_viewable'] = FALSE;
}
module_load_include('inc', 'islandora', 'includes/derivatives');
$options = array(
'destination_dsid' => $datastream->id,
);
$hooks = islandora_invoke_hook_list(ISLANDORA_DERIVATIVE_CREATION_HOOK, $datastream->parent->models, array($datastream->parent));
uasort($hooks, 'drupal_sort_weight');
$hooks = islandora_filter_derivatives($hooks, $options, $datastream->parent);
$map_source = function ($hook) {
return isset($hook['source_dsid']) ?
$hook['source_dsid'] :
NULL;
};
$load_datastream = function ($datastream_id) use ($datastream) {
return ($datastream_id != $datastream->id && isset($datastream->parent[$datastream_id])) ?
$datastream->parent[$datastream_id] :
NULL;
};
$sources = array_filter(array_map($map_source, $hooks));
$datastreams = array_filter(array_map($load_datastream, $sources));
foreach ($datastreams as $source_datastream) {
$result["lib4ridora_source_datastream_" . $source_datastream->id] = islandora_datastream_access($op, $source_datastream, $user);
}
module_load_include('inc', 'lib4ridora', 'includes/embargo.form');
$availability = $datastream->relationships->get(LIB4RIDORA_RELS_URI, lib4ridora_multi_embargo_build_predicate('availability'));
$availability = reset($availability);
$doc_version = $datastream->relationships->get(LIB4RIDORA_RELS_URI, lib4ridora_multi_embargo_build_predicate('document_version'));
$doc_version = reset($doc_version);
if ($availability && $doc_version) {
$availability = $availability['object']['value'];
$doc_version = $doc_version['object']['value'];
$is_internal_doc = ( @stripos($doc_version,'internal') !== false && stripos($availability,'private') !== false );
// Note, if a user has the LIB4RIDORA_ADMINISTRATOR_PERMISSION it does not mean the user is a *system* administrator (it is just related to Lib4RI objects)
$can_do_admin4ri = ( user_is_logged_in() && user_access(LIB4RIDORA_ADMINISTRATOR_PERMISSION, $user) );
$can_do_internal = ( $can_do_admin4ri && user_access(LIB4RIDORA_INTERNAL_DOCUMENT_PERMISSION, $user) ); // ...presuming the user must be a 'Lib4RI admin' already.
// Allow owner or admins, expect of internal documents, since these documents also could be uploaded afterwards by somebody else (like an administrator).
if ( !$is_internal_doc && @isset($user->name) && $datastream->parent->owner == $user->name ) {
$result['lib4ridora_admin_or_owner'] = TRUE;
}
elseif ( !$is_internal_doc && $can_do_admin4ri ) {
$result['lib4ridora_admin_or_owner'] = TRUE;
}
elseif ( $is_internal_doc && $can_do_internal ) {
$result['lib4ridora_admin_or_owner'] = TRUE;
}
elseif ($availability == 'private') {
// Private and previous admin/owner check failed; deny access.
$result['lib4ridora_private'] = FALSE;
}
elseif ($availability == 'intranet') {
$result['lib4ridora_intranet'] = lib4ridora_check_ip();
}
elseif ($availability == 'date') {
$embargo_date = $datastream->relationships->get(LIB4RIDORA_RELS_URI, lib4ridora_multi_embargo_build_predicate('embargo_date'));
$embargo_date = reset($embargo_date);
if ($embargo_date) {
$result['lib4ridora_embargo_date'] = REQUEST_TIME > strtotime($embargo_date['object']['value']) || lib4ridora_check_ip();
}
}
}
return $result;
}
/**
* Check if a given IP is in one of our allowed ranges.
*
* @param string $ip_address
* A dotted-decimal IPv4 address.
*
* @return bool
* TRUE if the IP is in an allowed range; otherwise, FALSE.
*/
function lib4ridora_check_ip($ip_address = NULL) {
if ( @isset($_GET['far-away']) ) {
return FALSE; // consider the intranet IP check as failed, as an (test) option for employees and associated workers to see how it looks like beyond the intranet.
}
if (!$ip_address) {
$ip_address = ip_address();
}
/*
$cidr_ranges = array(
'129.129.0.0/16',
'152.88.0.0/16',
'192.33.118.0/24',
'193.134.200.0/21',
'195.176.244.0/23',
);
*/
$cidr_list = variable_get( 'lib4ridora_intranet_ip_range_list', "129.129.0.0/16, 152.88.0.0/16, 192.33.118.0/24, 193.134.200.0/21, 195.176.244.0/23" );
$cidr_ranges = explode(',',strtr(preg_replace('/\s+/','',$cidr_list),';|',',,'));
// Transform an IPv4 CIDR block to an array containing the first and last IPs in the block.
$cidr_range_to_points = function ($cidr_string) {
list($base, $bits) = explode('/', $cidr_string);
$mask = pow(2, 32 - $bits) - 1;
$start = ip2long($base) & ~$mask;
$end = $start | $mask;
return array($start, $end);
};
$client_ip = ip2long($ip_address);
$ip_result = FALSE;
foreach (array_map($cidr_range_to_points, $cidr_ranges) as $range) {
list($start, $end) = $range;
if ($start <= $client_ip && $end >= $client_ip) {
$ip_result = TRUE;
break;
}
}
return $ip_result;
}
/**
* Implements hook_block_view_alter() - currently not needed !(?)
*
function lib4ridora_block_view_alter(&$data, $block) {
if (isset($data['content']) && is_array($data['content']) && isset($data['content']['#markup']) && (strpos($data['content']['#markup'], '<a href="http://www.lib4ri.ch">') !== false)) {
$data['content']['#markup'] = preg_replace('/<a href="http:\/\/www.lib4ri.ch">/', '<a href="http://www.lib4ri.ch" target="_blank">', $data['content']['#markup']);
}
}
*/
/**
* Implements hook_block_view_alter()
*
* Hiding "View at Publisher (DOI)" for Lib4RI's own DOIs, see: http://lib-dora-dev1.emp-eaw.ch:3000/issues/426
*
* Issue: if there is only one/the publisher link, and we remove it, then the block title 'Links:' still will be there.
*/
function lib4ridora_block_view_alter(&$data, $block) {
if ( @strval($data['content']['#contextual_links']['views_ui'][1][0]) == 'publication_links') {
if ( $doiList = @trim(variable_get('lib4ridora_doi_pattern','')) /* pattern list with DOI-suffixes separated by '|' */ ) {
if ( $pos = strpos($data['content']['#markup'],'views-field-mods-identifier-doi-s') ) {
if ( $doi = @strchr($data['content']['#markup'],'dx.doi.org/') ) {
$doi = strtok(strtr(substr($doi,11),"'",'"'),'"');
module_load_include('inc', 'lib4ridora', 'includes/utilities');
if ( lib4ridora_doi_by_lib4ri($doi,$doiList) ) {
$aryTmp = explode('a href',$data['content']['#markup']); // counting the countent parts may fail, so we just check the amount of links
if ( sizeof($aryTmp) < 3 ) { // so if we have just one link (must be the publisher link) then let's hide the entire block:
$data['content']['#markup'] = '<script type="text/javascript"> if ( blk = document.getElementById("block-views-publication-links-block") ) { blk.style = "display:none;" } </script>';
} else {
$aryTmp = explode('>',substr($data['content']['#markup'],$pos),2);
$data['content']['#markup'] = substr($data['content']['#markup'],0,$pos) . implode(' style="display:none;">',$aryTmp);
}
}
}
}
}
}
}
// block-views-publication-links-block
/**
* Access callback, to determine whether or not to create the RoMEO tab.
*
* Checks if it's enabled, we have the relevant content model, and we have an
* ISSN to look up and that a user is not anonymous.
*/
function lib4ridora_romeo_access($object) {
return islandora_scholar_romeo_access($object) && !user_is_anonymous();
}
/**
* Access callback for datastreams.
*
* Positive permissions on object access suggests on the datastream.
* So far however still managed by islandora itself expect of internal documents.
*/
function lib4ridora_datastream_access($op, $datastream, $user = NULL) {
module_load_include('module', 'islandora', 'islandora');
// following may be not required - already before adding this code portion LIB4RIDORA_ADMINISTRATOR_PERMISSION was not examined in this function!(?)
if ( stripos($datastream->id,'PDF') === 0 || stripos($datastream->mimetype,'/pdf') ) {
module_load_include('inc', 'lib4ridora', 'includes/embargo.form');
$infoAry = lib4ridora_get_embargo_info($datastream);
if ( @empty($infoAry['document_version']) || @empty($infoAry['availability']) ) {
return NULL; // as it happens in islandora.module/islandora_datastream_access()
}
$is_internal_doc = ( @stripos($infoAry['document_version'],'internal') !== false && @stripos($infoAry['availability']) !== false );
if ( $is_internal_doc && !user_access(LIB4RIDORA_INTERNAL_DOCUMENT_PERMISSION, $user) ) {
return NULL; // as it happens in islandora.module/islandora_datastream_access()
}
}
return islandora_datastream_access($op, $datastream, $user);
}
/**
* Implements hook_menu().
*/
function lib4ridora_menu() {
$items = array();
$items['islandora/object/%islandora_object/lib4ridora_pdf_upload'] = array(
'title' => 'Ingest PDF',
'page callback' => 'drupal_get_form',
'page arguments' => array('lib4ridora_single_pdf_upload_form', 2),
'access callback' => 'lib4ridora_citation_model_access',
'access arguments' => array(ISLANDORA_MINI_WORKFLOW_USE_PERMISSION, 2),
'type' => MENU_CALLBACK,
'file' => 'includes/pdf-upload.form.inc',
);
$items['islandora/object/%islandora_object/lib4ridora_pdf_management'] = array(
'title' => 'PDF Management',
'page callback' => 'drupal_get_form',
'page arguments' => array('lib4ridora_multi_embargo_edit_form', 2),
'access callback' => 'lib4ridora_citation_access',
'access arguments' => array(LIB4RIDORA_ADMINISTRATOR_PERMISSION, 2),
'type' => MENU_LOCAL_TASK,
'file' => 'includes/embargo.form.inc',
);
$items['islandora/object/%islandora_object/lib4ridora_pdf_list'] = array(
'title' => 'PDF List',
'page callback' => 'lib4ridora_list_pdfs',
'page arguments' => array(2),
'access callback' => 'lib4ridora_citation_access',
'access arguments' => array(ISLANDORA_VIEW_OBJECTS, 2),
'type' => MENU_LOCAL_TASK,
'file' => 'includes/pdf_list.inc',
);
$items['journal-impact-factor'] = array(
'title' => 'Journal Impact Factor',
'page callback' => 'drupal_get_form',
'page arguments' => array('lib4ridora_journal_impact_factor_form'),
'access arguments' => array(LIB4RIDORA_JOURNAL_IMPACT_FACTOR_PERMISSION),
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/journal_impact.form.inc',
);
$items['admin/islandora/solution_pack_config/lib4ridora'] = array(
'title' => 'Lib4RI',
'description' => 'Configure lib4ridora.',
'page callback' => 'drupal_get_form',
'access arguments' => array('administer site configuration'),
'page arguments' => array('lib4ridora_admin'),
'file' => 'includes/admin.form.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/islandora/solution_pack_config/lib4ridora/general'] = array(
'title' => 'General Configuration',
'description' => 'Configure lib4ridora.',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/islandora/solution_pack_config/lib4ridora/publication_types'] = array(
'title' => 'Publication Type Mappings',
'description' => 'Configure lib4ridora publication type mappings.',
'page callback' => 'drupal_get_form',
'access arguments' => array('administer site configuration'),
'page arguments' => array('lib4ridora_citation_subtype_mapping_form'),
'file' => 'includes/citation.subtypes.inc',
'type' => MENU_LOCAL_TASK,
);
$items['islandora/object/%/lib4ridora_edit_factors'] = array(
'title' => 'Impact Factors',
'page callback' => 'drupal_get_form',
'page arguments' => array('lib4ridora_jif_json_form', 2),
'access callback' => 'lib4ridora_factor_management_access',
'access arguments' => array(2),
'type' => MENU_LOCAL_TASK,
'file' => 'includes/jif_json.form.inc',
);
$items['islandora/object/%islandora_object/manage/document_management'] = array(
'title' => 'Document Management',
'page callback' => 'drupal_get_form',
'page arguments' => array('lib4ridora_mini_workflow_document_management_form', 2),
'access callback' => 'lib4ridora_citation_model_access',
'access arguments' => array(ISLANDORA_MINI_WORKFLOW_USE_PERMISSION, 2),
'type' => MENU_LOCAL_TASK,
'file' => 'includes/workflow.form.inc',
);
$items['islandora/object/%islandora_object/lib4ridora_edit_mods'] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array('lib4ridora_citation_select_mods_edit_form', 2),
'type' => MENU_LOCAL_TASK,
'file' => 'includes/citation-mods-edit.inc',
'access arguments' => array(ISLANDORA_METADATA_EDIT, 2),
'access callback' => 'lib4ridora_citation_model_access',
);
$items['doi_finder'] = array(
'title' => 'Find DOIs',
'page callback' => 'drupal_get_form',
'page arguments' => array('lib4ridora_doi_finder_form', 2),
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/doi-finder.form.inc',
'access arguments' => array(ISLANDORA_METADATA_EDIT),
);
$items['lib4ri-access-denied'] = array(
'title' => 'Access Denied',
'page callback' => 'lib4ri_access_denied',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
);
// 'Theme-less' apporach, see https://www.webomelette.com/how-create-autocomplete-form-element-drupal-7
if ( module_exists('lib4ri_funding') ) {
$items['islandora/fundersearch/autocomplete'] = array(
'module' => 'lib4ridora',
'file' => 'includes/advanced_search.funding.inc',
'page callback' => 'lib4ridora_fundersearch_autocomplete',
'page arguments' => array(1),
'type' => MENU_CALLBACK,
'access callback' => 'lib4ridora_fundersearch_access_callback',
);
$items['lib4ridora/fundersearch/autocomplete'] = array(
'module' => 'lib4ridora',
'file' => 'includes/advanced_search.funding.inc',
'page callback' => 'lib4ridora_adv_search_funding_callback',
// 'page arguments' => array(1), /* https://www.drupal.org/forum/support/module-development-and-code-questions/2009-05-08/passing-arguments-to-form-functions */
'type' => MENU_CALLBACK,
'access callback' => 'lib4ridora_fundersearch_access_callback',
);
}
return $items;
}
function lib4ridora_fundersearch_access_callback() {
if ( !user_is_logged_in() ) {
module_load_include('inc', 'lib4ridora', 'includes/utilities');
return ( !lib4ridora_user_is_bot('',false,true) );
}
return true;
}
/**
* Implements hook_menu_alter().
*/
function lib4ridora_menu_alter(&$items) {
// Nuke old "Document" tab.
unset($items['islandora/object/%islandora_object/islandora_scholar_upload']);
// Nuke default "Embargo" submenu.
unset($items['islandora/object/%islandora_object/manage/embargo']);
$items['islandora/object/%islandora_object/islandora_scholar_romeo']['access callback'] = 'lib4ridora_romeo_access';
// to support/tune the islandora_usage_stats module if installed+activated:
if ( module_exists("islandora_usage_stats") ) {
$items['islandora/object/%islandora_object/datastream/%islandora_datastream'] = array(
'title' => ( stripos($_SERVER['REQUEST_URI'],"/datastream/PDF") ? 'Download PDF' : 'View datastream' ),
'page callback' => 'lib4ridora_provide_datastream',
'page arguments' => array(4, FALSE, NULL),
'type' => MENU_CALLBACK,
'file' => 'includes/datastream.stats.inc',
'module' => 'lib4ridora',
'access callback' => 'lib4ridora_datastream_access',
'access arguments' => array(ISLANDORA_VIEW_OBJECTS, 4),
'load arguments' => array(2),
);
}
}
/**
* Custom access denied menu path for Lib4RI.
*
* @return string
* The access denied text.
*/
function lib4ri_access_denied() {
$destination = drupal_get_destination();
$pid = arg(2, $destination['destination']);
$ds = arg(4, $destination['destination']);
$object = islandora_object_load($pid);
if ($object && $ds && isset($object[$ds]) && ( stripos($ds,'PDF') === 0 || stripos($object[$ds]->mimetype,'/pdf') ) && in_array('ir:citationCModel', $object->models)) {
module_load_include('inc', 'lib4ridora', 'includes/embargo.form');
$datastreams = iterator_to_array($object);
$pdf_datastreams = array_filter($datastreams, 'lib4ridora_multi_embargo_pdf_filter');
if (!empty($pdf_datastreams)) {
$return_string = t('You do not have access to this PDF. Return to the parent object: <a href="@url">@label</a>.', array(
'@url' => url("islandora/object/{$object->id}"),
'@label' => $object->label,
));
return check_markup(decode_entities($return_string), 'filtered_html_for_title');
}
}
return t('You are not authorized to access this page');
}
/**
* Implements hook_permission().
*/
function lib4ridora_permission() {
$perms = array();
$perms[LIB4RIDORA_ADMINISTRATOR_PERMISSION] = array(
'title' => t('Administer Lib4Ri objects generally'),
'description' => t('Permit access and management of datastream.'),
);
$perms[LIB4RIDORA_INTERNAL_DOCUMENT_PERMISSION] = array(
'title' => t('Administer Lib4Ri Internal Documents'),
'description' => t("Permit reading/replacing/removing 'internal documents' (usually DORA-private PDF files)."),
);
$perms[LIB4RIDORA_JOURNAL_IMPACT_FACTOR_PERMISSION] = array(
'title' => t('Import Journal Impact Factor CSV'),
'description' => t('Permit importing of journal impact factor CSVs.'),
);
if ( module_exists('lib4ri_affiliation_author1') ) {
// leaving this here (currently) since quite Lib4RI resp. Eawag/Empa/WSL/PSI specific
$perms[LIB4RIDORA_SOLR_EXPORT_AUTHOR_WITH_UNIT] = array(
'title' => t('Extended Solr Export: Author with org. Unit'),
'description' => t("Attach affiliated unit onto author's name in CSV/Excel export."),
);
}
return $perms;
}
/**
* Access callback wrapper.
*
* Only allow when we're on a citation object with PDFs.
*/
function lib4ridora_citation_access($op, $object) {
$has_pdf = function (AbstractObject $object) {
module_load_include('inc', 'lib4ridora', 'includes/embargo.form');
$pdf_datastreams = array_filter(iterator_to_array($object), 'lib4ridora_multi_embargo_pdf_filter');
return !empty($pdf_datastreams);
};
return lib4ridora_citation_model_access($op, $object) && $has_pdf($object) && !user_is_anonymous();
}
/**
* Access callback wrapper.
*
* Only allow if the object has one of our models.
*/
function lib4ridora_citation_model_access($op, $object) {
return $object instanceof AbstractObject && in_array('ir:citationCModel', $object->models) && islandora_object_access($op, $object);
}
/**
* DOI-validation wrapper.
*/
function lib4ridora_doi_validate_ingest($element, &$form_state, $form) {
form_load_include($form_state, 'inc', 'lib4ridora', 'includes/utilities');
return lib4ridora_doi_validate($element, $form_state, $form, TRUE);
}
/**
* Alter ingest form to change ingest button label for citations.
* In addition, pre-select appropriate subtype and add doi-validation.
*/
function lib4ridora_form_islandora_ingest_form_alter(&$form, &$form_state) {
if ($form['form_step_id']['#value'] == 'xml_form_builder_metadata_step') {
$lib4ridora_association_step_storage = &islandora_ingest_form_get_step_storage($form_state, 'xml_form_builder_association_step');
module_load_include('inc', 'lib4ridora', 'includes/citation.subtypes');
$citation_map = lib4ridora_get_citation_mapping_info();
$publication_type_name = $citation_map[$lib4ridora_association_step_storage['ingest_selector']]['name'];
if ( @in_array($publication_type_name, $form['publication_type']['#options'] )){ // index '#options' may not exist sometimes! And since it's an array, it also could be empty!(?)
$form['publication_type']['#default_value'] = $publication_type_name;
$form['publication_type']['#disabled'] = TRUE;
}
if (array_key_exists('subtype_semantics', $form)){
$genre_map = lib4ridora_citation_get_genre_map(FALSE);
$this_genre = $genre_map[$publication_type_name];
$default_subtype_semantics = lib4ridora_citation_get_genre_semantics_map();
$this_subtype_semantics = variable_get('lib4ridora_citation_subtype_semantics', $default_subtype_semantics);
$form['subtype_semantics']['#value'] = $this_subtype_semantics[$this_genre];
}
if (in_array('identifiers', $form) && in_array('doi', $form['identifiers'])) {
$form['identifiers']['doi']['#element_validate'][] = 'lib4ridora_doi_validate_ingest';
}
}
if ($form['form_step_id']['#value'] == 'lib4ridora_license') {
$form['next']['#value'] = t('I accept the terms of this license');
}
}
/**
* Implements hook_islandora_solr_search_rss_item_alter().
*/
function lib4ridora_islandora_solr_search_rss_item_alter(&$item, $doc) {
// Bail out if citation isn't requested.
if (!isset($_GET['citation']) || $_GET['citation'] != 'true') {
return;
}
if (isset($doc['content_models']) && in_array('info:fedora/ir:citationCModel', $doc['content_models'])) {
$citation = islandora_object_load($doc['PID']);
if (isset($citation['MODS'])) {
$items_to_set = array();
$items_to_set['pubDate'] = $citation->createdDate->format(DATETIME::RSS);
// $items_to_set['pubDate'] = $citation->createdDate->format(DATETIME::RFC822);
foreach ($items_to_set as $key => $value) {
foreach ($item['items'] as &$sub_item) {
if ($sub_item['key'] == $key) {
$sub_item['value'] = $value;
break;
}
}
unset($sub_item);
}
$mods_content = $citation['MODS']->content;
$item['description'] = citeproc_bibliography_from_mods(
citeproc_default_style(),
$mods_content
);
$mods_doc = new DOMDocument();
$mods_doc->loadXML($mods_content);
$mods_xpath = new DOMXPath($mods_doc);
$mods_xpath->registerNamespace('mods', 'http://www.loc.gov/mods/v3');
$mods_results = $mods_xpath->query('//mods:mods/mods:titleInfo/mods:title');
if ($mods_results->length) {
$item['title'] = $mods_results->item(0)->nodeValue;
}
}
}
}
/**
* Implements hook_islandora_solr_config_rss_item_post_render_alter().
*/
function lib4ridora_islandora_solr_config_rss_item_post_render_alter(&$rendered_item, $doc) {
// Bail out if citation isn't requested.
if (!isset($_GET['citation']) || $_GET['citation'] != 'true') {
return;
}
if (isset($doc['content_models']) && in_array('info:fedora/ir:citationCModel', $doc['content_models'])) {
$item = new DOMDocument();
$item->preserveWhiteSpace = FALSE;
$item->formatOutput = TRUE;
$item->loadXML($rendered_item);
$object = islandora_object_load($doc['PID']);
if (isset($object['MODS'])) {
$mods_doc = new DOMDocument();
$mods_doc->loadXML($object['MODS']->content);
module_load_include('inc', 'lib4ridora', 'includes/rss');
$pared_mods = lib4ridora_mods_for_rss($mods_doc);
$imported_doc = $item->importNode($pared_mods->documentElement, TRUE);
$item->documentElement->appendChild($imported_doc);
$rendered_item = $item->saveXML($item->documentElement);
}
}
}
/**
* Check that the user has permission to edit the factors.
*
* @param string $object_id
* The journal object in question.
*
* @return bool
* TRUE if the user should have access; otherwise, FALSE.
*/
function lib4ridora_factor_management_access($object_id) {
$object = islandora_object_load($object_id);
// Object loaded, has our model and has either an editable stream or no
// stream but we can ingest one.
return $object &&
in_array(LIB4RIDORA_JOURNAL_CONTENT_MODEL, $object->models) &&
((isset($object[LIB4RIDORA_FACTOR_DSID]) && islandora_datastream_access(ISLANDORA_METADATA_EDIT, $object[LIB4RIDORA_FACTOR_DSID])) ||
(!isset($object[LIB4RIDORA_FACTOR_DSID]) && islandora_object_access(ISLANDORA_INGEST, $object)));
}
/**
* Implements hook_islandora_edit_datastream_registry().
*/
function lib4ridora_islandora_edit_datastream_registry(AbstractObject $object, AbstractDatastream $datastream) {
$items = array();
if ($datastream->id == LIB4RIDORA_FACTOR_DSID && in_array(LIB4RIDORA_JOURNAL_CONTENT_MODEL, $object->models)) {
$items[] = array(
'name' => t('Edit Journal Factors'),
'url' => "islandora/object/{$object->id}/lib4ridora_edit_factors",
'weight' => 0,
);
}
return $items;
}
/**
* Implements hook_islandora_solr_object_result_alter().
*/
function lib4ridora_ir_citationcmodel_islandora_solr_object_result_alter(&$result, $processor) {
$batch = batch_get();
if (empty($batch)) {
// We only want to ingest the factor in the CSV export... Don't have a nice
// way to hook in/identify when we're there, so let's ensure we're inside of
// a batch.
return;
}
$term_failed_ary = str_getcsv( variable_get('lib4ridora_pseudo_solr_field_factor_failed', '') ); // empty default by intention (and not N/A)
$term_failed_factor = trim( $term_failed_ary[0] );
$term_failed_year = ( @!isset($term_failed_ary[1]) ) ? $term_failed_factor : trim( $term_failed_ary[1] );
$id_field = variable_get('lib4ridora_solr_field_article_host_journal', 'mods_relatedItem_host_identifier_ms');
$date_issued_field = variable_get('lib4ridora_solr_field_article_date_issued', 'mods_originInfo_dateIssued_ms');
$factor_pseudo_field = variable_get('lib4ridora_pseudo_solr_field_factor', 'journal_impact_factor');
$year_pseudo_field = variable_get('lib4ridora_pseudo_solr_field_year', 'journal_impact_factor_year');
$solr_doc =& $result['solr_doc'];
if (!isset($solr_doc[$date_issued_field]) || !isset($solr_doc[$id_field])) {
$solr_doc[$factor_pseudo_field] = t($term_failed_factor);
$solr_doc[$year_pseudo_field] = t($term_failed_year);
return;
}
// Four sequential digits with no other digits touching is understood to be a
// year.
// XXX: Since things could be in wacky encodings, try to use all values.
$issued_years = preg_filter('/^.*(?<!\d)(\d{4})(?!\d).*$/', '\1', (array) $solr_doc[$date_issued_field]);
if (empty($issued_years)) {
$solr_doc[$factor_pseudo_field] = t($term_failed_factor);
$solr_doc[$year_pseudo_field] = t($term_failed_year);
return;
}
module_load_include('inc', 'islandora', 'includes/utilities');
module_load_include('inc', 'lib4ridora', 'includes/utilities');
$valid_ids = array_filter((array) $solr_doc[$id_field], 'islandora_is_valid_pid');
foreach ($valid_ids as $id) {
$object = islandora_object_load($id);
if (!$object) {
continue;
}
$factor = lib4ridora_get_factor_with_fallback($object, $issued_years);
if ($factor !== FALSE) {
list($solr_doc[$factor_pseudo_field], $solr_doc[$year_pseudo_field]) = $factor;
return;
}
}
// Didn't find one... Set "N/A" as the default.
$solr_doc[$factor_pseudo_field] = t($term_failed_factor);
$solr_doc[$year_pseudo_field] = t($term_failed_year);
}
/**
* Implements hook_cmodel_pid_dsid_islandora_datastream_alter().
*
* Prevents out of date Journal PID links.
*/
function lib4ridora_ir_citationcmodel_mods_islandora_datastream_alter(AbstractObject $object, AbstractDatastream $datastream, array &$context) {
$mods_doc = new DOMDocument();
if ($context['action'] == 'ingest') {
$mods_doc->loadXML($datastream->content);
}
// There is a case where an inline XML datastream is changing to manage where
// the first alter will trigger when the MIME type is changed to managed and
// will be no content actually present.
elseif (isset($context['params']['dsString']) || isset($context['params']['dsFile'])) {
// When object is in Fedora.
if (isset($context['params']['dsString'])) {
$content = $context['params']['dsString'];
}
else {
$content = file_get_contents($context['params']['dsFile']);
}
$mods_doc->loadXML($content);
}
// Ensure there's content before doing anything else.
if ($mods_doc->hasChildNodes()) {
$mods_xpath = new DOMXPath($mods_doc);
$mods_xpath->registerNamespace('mods', 'http://www.loc.gov/mods/v3');
$journal_results = $mods_xpath->query('/mods:mods/mods:relatedItem[@type="host"]/mods:identifier[not(@type) and node()]');
// Bail out if no journal linked.
if (!$journal_results->length) {
return;
}
$journal_item = $journal_results->item(0);
$journal = islandora_object_load($journal_item->nodeValue);
$label_results = $mods_xpath->query('/mods:mods/mods:relatedItem[@type="host"]/mods:titleInfo/mods:title[not(@type)]');
$journal_mods_doc = new DOMDocument();
// Assumes journal has MODS.
$journal_mods_doc->loadXML($journal['MODS']->content);
$journal_mods_xpath = new DOMXPath($journal_mods_doc);
$journal_mods_xpath->registerNamespace('mods', 'http://www.loc.gov/mods/v3');
$journal_label_results = $journal_mods_xpath->query('/mods:mods/mods:titleInfo/mods:title');
// Destroy link if necessary.
if (!$label_results->length || !$journal_label_results->length ||
$label_results->item(0)->nodeValue != $journal_label_results->item(0)->nodeValue) {
$journal_item->parentNode->removeChild($journal_item);
$mods = $mods_doc->saveXML();
if ($context['action'] == 'ingest') {
$datastream->content = $mods;
}
else {
// When object is in Fedora.
$context['params']['dsString'] = $mods;
}
}
}
}
/**
* Implements hook_theme().
*
* Used to overrider islandora-solr-wrapper and add citation=true to rss link.
*/
function lib4ridora_theme($existing, $type, $theme, $path) {
return array(
'islandora_solr_wrapper' => array(
'template' => 'theme/lib4ridora-solr-wrapper',
),
'islandora_default' => array(
'template' => 'theme/lib4ridora-custom-object-view',
),
'lib4ridora_citation_solr_results' => array(
'template' => 'theme/lib4ridora-citation-solr-results',
'variables' => array(
'citations' => NULL,
),
'file' => 'theme/theme.inc',
),
'lib4ridora_pdf_materials' => array(
'template' => 'theme/lib4ridora-pdf-materials',
'variables' => array(
'object' => NULL,
'statement' => FALSE,
),
'file' => 'theme/theme.inc',
),
'lib4ridora_pdf_link' => array(
'file' => 'theme/theme.inc',
'template' => 'theme/lib4ridora-pdf-link',
'variables' => array(
'datastream' => NULL,
'statement' => FALSE,
'availability' => FALSE,
),
),
'lib4ridora_islandora_solr_metadata_injected_author_info' => array(
'file' => 'theme/theme.inc',
'template' => 'theme/lib4ridora-islandora-solr-metadata-injected-author-info',
'variables' => array(
'object' => NULL,
'info' => NULL,
'author_attributes' => array(
'class' => array(
'author',
),
),
'org_attributes' => array(
'class' => array(
'org',
),
),
),
),
'lib4ridora_islandora_solr_metadata_injected_funder_info' => array(
'file' => 'theme/theme.inc',
'template' => 'theme/lib4ridora-islandora-solr-metadata-injected-funder-info',
'variables' => array(
'object' => NULL,
'info' => NULL,
'fundername_attributes' => array(
'class' => array(
'fundername',
),
),