From 8466eb3077d82bc6a61405e554a8738a11432420 Mon Sep 17 00:00:00 2001 From: Kun Lin Date: Mon, 13 Apr 2020 16:44:50 -0700 Subject: [PATCH 1/3] update google scholar meta tag to use actual XPath configured in backend admin menu --- includes/google_scholar.inc | 87 +++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 32 deletions(-) diff --git a/includes/google_scholar.inc b/includes/google_scholar.inc index 5d477051..65141749 100644 --- a/includes/google_scholar.inc +++ b/includes/google_scholar.inc @@ -1,5 +1,4 @@ xpath('mods:name') as $name_xml) { + foreach ($mods_xml->xpath(variable_get('islandora_scholar_xpaths_authors_xpath', 'mods:name')) as $name_xml) { $name_parts = array(); $name_xml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); - $roles = $name_xml->xpath('mods:role/mods:roleTerm'); - $role = strtolower((string) reset($roles)); - if ($role) { - if ($role == 'author') { - 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') { - $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; - } + // 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 (isset($name_parts['no_type'])) { - $tags['citation_author'][] = $name_parts['no_type']; - } + } + 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) { @@ -103,9 +112,8 @@ function islandora_scholar_create_meta_tags($object) { // 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); + $date_string = islandora_google_scholar_parse_date_foryear($date); if ($date_string) { $tags['citation_publication_date'] = $date_string; } @@ -116,7 +124,6 @@ function islandora_scholar_create_meta_tags($object) { $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': @@ -171,7 +178,7 @@ function islandora_scholar_create_meta_tags($object) { $online_date = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_online_date', '//mods:recordInfo/mods:recordCreationDate')); if ($online_date) { - $date_string = islandora_scholar_parse_date_foryear($online_date); + $date_string = islandora_google_scholar_parse_date_foryear($online_date); if ($date_string) { $tags['citation_online_date'] = $date_string; } @@ -207,7 +214,7 @@ function islandora_scholar_create_meta_tags($object) { * @return null|string * returns the year if the date was parsable and NULL otherwise */ -function islandora_scholar_parse_date_foryear($date) { +function islandora_google_scholar_parse_date_foryear($date) { if (is_array($date)) { $date = (string) reset($date); } @@ -234,7 +241,7 @@ function islandora_scholar_parse_date_foryear($date) { * @param array $tags * An associate array containing the name => content of the meta tags. */ -function islandora_scholar_embed_tags($tags) { +function islandora_google_scholar_embed_tags($tags) { $weight = 1000; if ($tags != FALSE) { foreach ($tags as $name => $content) { @@ -267,3 +274,19 @@ function islandora_scholar_embed_tags($tags) { } } } + +/** + * Implements hook_CMODEL_islandora_view_object(). + */ +function islandora_google_scholar_ir_citationCModel_islandora_view_object($object) { + $tags = islandora_google_scholar_create_meta_tags($object); + islandora_google_scholar_embed_tags($tags); +} + +/** + * Implements hook_CMODEL_islandora_view_object(). + */ +function islandora_google_scholar_ir_thesisCModel_islandora_view_object($object) { + $tags = islandora_google_scholar_create_meta_tags($object); + islandora_google_scholar_embed_tags($tags); +} From 4e3524107fb1c4ca08d621a607281e4dbb427fad Mon Sep 17 00:00:00 2001 From: Kun Lin Date: Mon, 13 Apr 2020 16:51:08 -0700 Subject: [PATCH 2/3] update function name to match new 7.x function name --- includes/google_scholar.inc | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/includes/google_scholar.inc b/includes/google_scholar.inc index 65141749..759e4377 100644 --- a/includes/google_scholar.inc +++ b/includes/google_scholar.inc @@ -15,7 +15,7 @@ * Associative array where the key is the name of the tag and the value is * the content. */ -function islandora_google_scholar_create_meta_tags($object) { +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; @@ -113,7 +113,7 @@ function islandora_google_scholar_create_meta_tags($object) { // and we don't have a consistent structure of input we will just return the // year for now. if ($date) { - $date_string = islandora_google_scholar_parse_date_foryear($date); + $date_string = islandora_scholar_parse_date_foryear($date); if ($date_string) { $tags['citation_publication_date'] = $date_string; } @@ -178,7 +178,7 @@ function islandora_google_scholar_create_meta_tags($object) { $online_date = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_online_date', '//mods:recordInfo/mods:recordCreationDate')); if ($online_date) { - $date_string = islandora_google_scholar_parse_date_foryear($online_date); + $date_string = islandora_parse_date_foryear($online_date); if ($date_string) { $tags['citation_online_date'] = $date_string; } @@ -214,7 +214,7 @@ function islandora_google_scholar_create_meta_tags($object) { * @return null|string * returns the year if the date was parsable and NULL otherwise */ -function islandora_google_scholar_parse_date_foryear($date) { +function islandora_scholar_parse_date_foryear($date) { if (is_array($date)) { $date = (string) reset($date); } @@ -241,7 +241,7 @@ function islandora_google_scholar_parse_date_foryear($date) { * @param array $tags * An associate array containing the name => content of the meta tags. */ -function islandora_google_scholar_embed_tags($tags) { +function islandora_scholar_embed_tags($tags) { $weight = 1000; if ($tags != FALSE) { foreach ($tags as $name => $content) { @@ -275,18 +275,3 @@ function islandora_google_scholar_embed_tags($tags) { } } -/** - * Implements hook_CMODEL_islandora_view_object(). - */ -function islandora_google_scholar_ir_citationCModel_islandora_view_object($object) { - $tags = islandora_google_scholar_create_meta_tags($object); - islandora_google_scholar_embed_tags($tags); -} - -/** - * Implements hook_CMODEL_islandora_view_object(). - */ -function islandora_google_scholar_ir_thesisCModel_islandora_view_object($object) { - $tags = islandora_google_scholar_create_meta_tags($object); - islandora_google_scholar_embed_tags($tags); -} From 5fc7f65ab9b67af6c14e1fce4002f6393a44a8a8 Mon Sep 17 00:00:00 2001 From: Kun Lin Date: Fri, 24 Apr 2020 16:42:43 -0700 Subject: [PATCH 3/3] edit the typo on function name "islandora_scholar_parse_date_foryear" and change mods:name default xpath to "//mods:mods[1]/mods:name/mods:role[mods:roleTerm = "author"]/.." for legacy usage that relied on "author" role term --- includes/google_scholar.inc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/google_scholar.inc b/includes/google_scholar.inc index 759e4377..946d6979 100644 --- a/includes/google_scholar.inc +++ b/includes/google_scholar.inc @@ -1,4 +1,5 @@ xpath(variable_get('islandora_scholar_xpaths_authors_xpath', 'mods:name')) as $name_xml) { + 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. @@ -178,7 +179,7 @@ function islandora_scholar_create_meta_tags($object) { $online_date = $mods_xml->xpath(variable_get('islandora_scholar_xpaths_online_date', '//mods:recordInfo/mods:recordCreationDate')); if ($online_date) { - $date_string = islandora_parse_date_foryear($online_date); + $date_string = islandora_scholar_date_foryear($online_date); if ($date_string) { $tags['citation_online_date'] = $date_string; } @@ -274,4 +275,3 @@ function islandora_scholar_embed_tags($tags) { } } } -