commit e4e621464593fa5d93bad514986c33c1041c7f00 Author: ppound Date: Thu Jun 3 14:37:34 2021 -0300 first commit diff --git a/udm_citeproc.info b/udm_citeproc.info new file mode 100644 index 0000000..95fbe00 --- /dev/null +++ b/udm_citeproc.info @@ -0,0 +1,6 @@ +name =UdM citeproc alters +description = Implements some citeproc alters to help handle edge cases +package = UdM +dependencies[] = citeproc +version = 7.x-dev +core = 7.x \ No newline at end of file diff --git a/udm_citeproc.module b/udm_citeproc.module new file mode 100644 index 0000000..6346736 --- /dev/null +++ b/udm_citeproc.module @@ -0,0 +1,250 @@ +name->role->roleTerm=editor is plausible. + * + * Note also this section is *highly* repetitive of the section above and this + * should probably be generalized, but for now a strict procedural reckoning + * will have to suffice. The difference is in the very last section, where the + * appropriate cs:names type is specified. + * + * @param SimpleXMLElement $mods + * A MODS document. + * + * @return string + * The type property for the Citation. + */ +function udm_convert_mods_to_citeproc_json_names(SimpleXMLElement $mods) { + $queries = array( + 0 => array( + // Path. + variable_get('islandora_scholar_xpaths_local_author', '//mods:mods[1]/mods:name[normalize-space(mods:namePart)]'), + // Default Role. + 'author', + // Valid Roles. + array( + 'editor' => 'editor', + 'translator' => 'translator', + 'interviewer' => 'interviewer', + 'composer' => 'composer', + 'original' => 'original-author', + 'recipient' => 'recipient', + 'author' => 'author', + ), + ), + 1 => array( + variable_get('islandora_scholar_xpaths_local_container-author', '//mods:mods[1]/mods:relatedItem[@type="host"]/mods:name'), + 'container-author', + array( + 'editor' => 'editor', + 'translator' => 'translator', + 'author' => 'container-author', + ), + ), + 2 => array( + variable_get('islandora_scholar_xpaths_local_collection-editor', '//mods:mods[1]/mods:relatedItem[@type="series"]/mods:name'), + NULL, + array( + 'editor' => 'collection-editor', + ), + ), + ); + $output = array(); + foreach ($queries as $query) { + list($path, $default_role, $valid_roles) = $query; + $names = $mods->xpath($path); + if (!empty($names)) { + foreach ($names as $name) { + + add_mods_namespace($name); + $role = udm_convert_mods_to_citeproc_json_name_role($name, $valid_roles, $default_role); + if ($role !== FALSE) { + $parsed_name = udm_convert_mods_to_citeproc_json_name($name); + + if ($parsed_name) { + $output[$role][] = $parsed_name; + } + } + } + } + } + return $output; +} + +/** + * Gets the array repersentation of the javascript Citation's name properties. + * + * @param SimpleXMLElement $name + * A name element. + * + * @return array + * An array that embodies the name properties of a Citation javascript object. + */ +function udm_convert_mods_to_citeproc_json_name(SimpleXMLElement $name) { + $type = (string) $name->attributes()->type; + $output = ($type == 'personal') ? + udm_convert_mods_to_citeproc_json_name_personal($name) : + udm_convert_mods_to_citeproc_json_name_corporate($name); + + $output = array_map('trim', $output); + $output = array_map('ucfirst', $output); + $output = array_filter($output); + return $output; +} + +/** + * Gets the array repersentation of the Citation's personal name properties. + * + * @param SimpleXMLElement $name + * A name element. + * + * @return array + * An array that embodies the name properties of a Citation javascript object. + */ +function udm_convert_mods_to_citeproc_json_name_personal(SimpleXMLElement $name) { + $output = array(); + $name_parts = $name->xpath('mods:namePart'); + + foreach ($name_parts as $name_part) { + $type = (string) $name_part->attributes()->type; + $content = (string) $name_part; + + // If the type is empty, it is a combined name last, first. + if (empty($type)) { + // Filter empty things out of the output, so we may potentially replace + // them. + $output = array_filter(array_map('trim', $output)); + + $names = array_map('trim', explode(',', $content)); + $exploded = array(); + $exploded['family'] = array_shift($names); + $exploded['given'] = array_shift($names); + + // If we appear to have a family (and potentially given) name part in our + // combined field and are not currently outputting them, make them get + // output. + // XXX: May combine unexpectedly in the case a "given" typed part name is + // specified, and our untyped field only contains a "family" name. + $non_empty = array_filter($exploded); + $parts_not_present = array_diff_key($non_empty, $output); + if (!empty($parts_not_present)) { + $output = $non_empty + $output; + } + } + else { + // Throw a period after single character values, as they likely represent + // initials. + $content .= (strlen($content) == 1) ? '. ' : ' '; + $output[$type] = isset($output[$type]) ? + $output[$type] . $content : + $content; + } + } + return $output; +} + +/** + * Gets the array repersentation of the Citation's corporate name properties. + * + * @param SimpleXMLElement $name + * A name element. + * + * @return array + * An array that embodies the name properties of a Citation javascript object. + */ +function udm_convert_mods_to_citeproc_json_name_corporate(SimpleXMLElement $name) { + $output = array(); + $name_parts = $name->xpath(variable_get('islandora_scholar_xpaths_local_author', 'mods:namePart')); + foreach ($name_parts as $name_part) { + $content = (string) $name_part . ' '; + $output['literal'] = isset($output['literal']) ? $output['literal'] . $content : $content; + } + return $output; +} + + + +/** + * Gets the role for the given name element. + * + * If no role can be determined it returns 'author' as a default. + * + * @param SimpleXMLElement $name + * A MODS name element. + * @param array $valid_roles + * A map of mods role names to their citeproc equivalents. + * @param string $default_role + * The role to use if a valid role is not found. + * + * @return string + * Gets the role of the given name. + */ +function udm_convert_mods_to_citeproc_json_name_role(SimpleXMLElement $name, array $valid_roles, $default_role) { + module_load_include('inc', 'citeproc', 'includes/marcrelator_conversion'); + $role_term = $name->xpath(variable_get('islandora_scholar_xpaths_role_term', 'mods:role/mods:roleTerm')); + + if (isset($role_term[0])) { + $role = strtolower((string) $role_term[0]); + $role_term = $role_term[0]; + } + else { + $role = NULL; + } + + if ($role) { + $role_authority = (string) $role_term->attributes()->authority; + $role_type = (string) $role_term->attributes()->type; + + if ($role_authority == 'marcrelator' && $role_type == 'code') { + $role = marcrelator_code_to_term($role); + } + $role = ($role == 'auteur') ? 'author' : $role; + + return array_key_exists($role, $valid_roles) ? $valid_roles[$role] : FALSE; + } + return $default_role; +} \ No newline at end of file