forked from Islandora/islandora_scholar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_scholar.inc
277 lines (253 loc) · 9.42 KB
/
google_scholar.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
<?php
/**
* @file
* Module used to embed meta tags in the HEAD for use in indexing in Google
* Scholar and Zotero.
*/
/**
* Creates meta tags to be placed in the head of the HTML.
*
* @param AbstractObject $object
* A AbstractObject.
*
* @return array
* Associative array where the key is the name of the tag and the value is
* the content.
*/
function islandora_scholar_create_meta_tags($object) {
// Need at least title, full name of at least one author, publication year.
if (!isset($object['MODS']) || !islandora_datastream_access(ISLANDORA_VIEW_OBJECTS, $object['MODS'])) {
return FALSE;
}
else {
$tags = array();
$tags['citation_author'] = array();
$mods = $object['MODS']->content;
$mods_xml = new SimpleXMLElement($mods);
$mods_xml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
$title_results = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_title', '//mods:mods[1]/mods:titleInfo/mods:title'));
$title = (string) reset($title_results);
$subtitle_results = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_title_sub_title', '//mods:mods[1]/mods:titleInfo/mods:subTitle'));
$subtitle = (string) reset($subtitle_results);
if (!empty($title)) {
if (!empty($subtitle)) {
$tags['citation_title'] = "{$title}: {$subtitle}";
}
else {
$tags['citation_title'] = $title;
}
}
else {
return FALSE;
}
foreach ($mods_xml->xpath(variable_get('islandora_scholar_xpaths_authors_xpath', '//mods:mods[1]/mods:name/mods:role[mods:roleTerm = "author"]/..')) as $name_xml) {
$name_parts = array();
$name_xml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
// Use Mods DisplayForm as a backup source for name.
// Only taking the first result for displayForm.
$name_displayForm = '';
$mods_displayform = $name_xml->xpath('mods:displayForm');
if (is_array($mods_displayform)) {
$name_displayForm = array_shift($mods_displayform);
}
foreach ($name_xml->xpath('mods:namePart') as $name_part) {
if ((string) $name_part != '') {
// Strip periods off the end.
$np = (string) rtrim($name_part, '.');
if ($name_part['type'] == 'given') {
// Middle name is also marked as given name in MODS.
if (isset($name_parts['first_name'])) {
$name_parts['first_name'] = $name_parts['first_name'] . ' ' . (string) $name_part;
}
else {
$name_parts['first_name'] = (string) $name_part;
}
}
if ($name_part['type'] == 'family') {
$name_parts['last_name'] = $np;
}
if (!isset($name_part['type'])) {
$name_parts['no_type'] = $np;
}
}
}
if (isset($name_parts['last_name']) && isset($name_parts['first_name'])) {
$tags['citation_author'][] = $name_parts['last_name'] . ', ' . $name_parts['first_name'];
}
elseif ($name_displayForm != '') {
$tags['citation_author'][] = $name_displayForm;
}
elseif (isset($name_parts['no_type'])) {
$tags['citation_author'][] = $name_parts['no_type'];
}
}
if (count($tags['citation_author']) == 0) {
return FALSE;
}
$origin_date = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_origin_date', '//mods:originInfo/mods:dateIssued'));
$part_date = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_part_date', '//mods:part/mods:date'));
$related_date = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_related_date', '//mods:relatedItem[@type="host"]//mods:date'));
$created_date = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_created_date', '//mods:originInfo/mods:dateCreated'));
if ($origin_date) {
$date = (string) reset($origin_date);
}
elseif ($part_date) {
$date = (string) reset($part_date);
}
elseif ($related_date) {
$date = (string) reset($related_date);
}
elseif ($created_date) {
$date = (string) reset($created_date);
}
else {
return FALSE;
}
// Google requires dates in yy/mm/dd format or just the year. As dates suck
// and we don't have a consistent structure of input we will just return the
// year for now.
if ($date) {
$date_string = islandora_scholar_parse_date_foryear($date);
if ($date_string) {
$tags['citation_publication_date'] = $date_string;
}
else {
return FALSE;
}
}
$host_title = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_host_title', '//mods:relatedItem[@type="host"]//mods:title'));
$genre = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_genre', '//mods:mods[1]/mods:genre'));
$genre = strtolower((string) reset($genre));
switch ($genre) {
case 'book section':
case 'book chapter':
$host_tag = 'citation_book_title';
break;
case 'conference proceeding':
$host_tag = 'citation_conference_title';
break;
default:
$host_tag = 'citation_journal_title';
break;
}
if ($host_title) {
$tags[$host_tag] = (string) reset($host_title);
}
$volume = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_volume', '//mods:mods[1]/mods:part/mods:detail[@type="volume"]/mods:number'));
if ($volume) {
$tags['citation_volume'] = (string) reset($volume);
}
$issn = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_issn', '//mods:identifier[@type="issn"]'));
if ($issn) {
$tags['citation_issn'] = str_replace(
array("&", "=", ",", ";"),
array('', '', '', ''),
(string) reset($issn));
}
$isbn = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_isbn', '//mods:identifier[@type="isbn"]'));
if ($isbn) {
$tags['citation_isbn'] = (string) reset($isbn);
}
$issue = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_issue', '//mods:mods[1]/mods:part/mods:detail[@type="issue"]/mods:number'));
if ($issue) {
$tags['citation_issue'] = (string) reset($issue);
}
$start_page = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_start_page', '//mods:extent[@unit="page"]/mods:start'));
if ($start_page) {
$tags['citation_firstpage'] = (string) reset($start_page);
}
$end_page = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_end_page', '//mods:extent[@unit="page"]/mods:end'));
if ($end_page) {
$tags['citation_lastpage'] = (string) reset($end_page);
}
$online_date = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_online_date', '//mods:recordInfo/mods:recordCreationDate'));
if ($online_date) {
$date_string = islandora_scholar_date_foryear($online_date);
if ($date_string) {
$tags['citation_online_date'] = $date_string;
}
}
$doi = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_doi', '//mods:identifier[@type="doi"]'));
if ($doi) {
$tags['citation_doi'] = (string) reset($doi);
}
$degree_grantor = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_degree_grantor', '//mods:mods/mods:name[@type="corporate"][mods:role/mods:roleTerm = "Degree grantor"]/mods:namePart'));
if ($degree_grantor) {
$tags['citation_dissertation_institution'] = (string) reset($degree_grantor);
}
if ($object['PDF']) {
$tags['citation_pdf_url'] = url("islandora/object/$object->id/datastream/PDF/view", array('absolute' => TRUE));
}
$tags['citation_abstract_html_url'] = url("islandora/object/$object->id/", array('absolute' => TRUE));
}
// Array filter so no tags are created for empty values!
return array_filter($tags);
}
/**
* Return the year portion of a date.
*
* @param string|array $date
* The date to parse. If it is array we use the first element in the array
*
* @return null|string
* returns the year if the date was parsable and NULL otherwise
*/
function islandora_scholar_parse_date_foryear($date) {
if (is_array($date)) {
$date = (string) reset($date);
}
try {
// Check if date is currently just a year.
if (!(DateTime::createFromFormat('Y', $date))) {
$date_obj = new DateTime($date);
$date_string = date_format($date_obj, 'Y');
return $date_string;
}
else {
return $date;
}
}
catch (Exception $e) {
// TODO: should we log this?
return NULL;
}
}
/**
* Adds the meta tags to the HEAD of the html document.
*
* @param array $tags
* An associate array containing the name => content of the meta tags.
*/
function islandora_scholar_embed_tags($tags) {
$weight = 1000;
if ($tags != FALSE) {
foreach ($tags as $name => $content) {
if (is_array($content)) {
foreach ($content as $key => $nested_val) {
if ($name == 'citation_author') {
$weight++;
}
$element = array(
'#tag' => 'meta',
'#attributes' => array(
'name' => $name,
'content' => htmlspecialchars($nested_val),
),
'#weight' => $weight,
);
drupal_add_html_head($element, $key . '_' . $nested_val);
}
}
else {
$element = array(
'#tag' => 'meta',
'#attributes' => array(
'name' => $name,
'content' => htmlspecialchars($content),
),
);
drupal_add_html_head($element, $name);
}
}
}
}