|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class EpubAccessibilityMetadataIndexingService |
| 4 | + class << self |
| 5 | + # This will be called from MonographIndexer. Given that nothing indexed here is crucial to Fulcrum's core features,... |
| 6 | + # no errors should be raised that would prevent the Monograph document from being created. |
| 7 | + # For all of the fields here I'll use string (not English text), stored and indexed and use the expected cardinality. |
| 8 | + # This should allow any/all to be used in Blacklight facets. |
| 9 | + def index(epub_file_set_id, solr_doc) |
| 10 | + root_path = UnpackService.root_path_from_noid(epub_file_set_id, 'epub') |
| 11 | + return unless Dir.exist? root_path |
| 12 | + |
| 13 | + container_file = File.join(root_path, "META-INF/container.xml") |
| 14 | + return unless File.exist? container_file |
| 15 | + |
| 16 | + container = Nokogiri::XML(File.open(container_file)).remove_namespaces! |
| 17 | + container.xpath("//rootfile").length > 1 ? 'yes' : 'no' |
| 18 | + # Accessibility metadata extraction requires one, and only one, rendition to be present. |
| 19 | + return unless container.xpath("//rootfile").length == 1 |
| 20 | + |
| 21 | + content_file = container.xpath("//rootfile/@full-path").text |
| 22 | + content_file = File.join(root_path, content_file) |
| 23 | + return unless File.exist? content_file |
| 24 | + |
| 25 | + content = Nokogiri::XML(File.open(content_file)).remove_namespaces! |
| 26 | + package_element = content.at_css('package') |
| 27 | + epub_version = package_element['version']&.strip if package_element.present? |
| 28 | + return if epub_version.blank? |
| 29 | + |
| 30 | + # Indexing this for no particular reason. Might be useful at some point. |
| 31 | + solr_doc['epub_version_ssi'] = epub_version |
| 32 | + |
| 33 | + # This should allow the relevant values to be detected for both EPUB 2 and EPUB 3, though only the latter should... |
| 34 | + # be present on Fulcrum. See Daisy links below. |
| 35 | + @epub_2 = epub_version.start_with?('2') |
| 36 | + @meta_attribute = @epub_2 ? 'name' : 'property' |
| 37 | + |
| 38 | + @content_metadata = content.at_css('metadata') |
| 39 | + return if @content_metadata.blank? |
| 40 | + |
| 41 | + # for these schema values see https://kb.daisy.org/publishing/docs/metadata/schema.org/ |
| 42 | + solr_doc['epub_a11y_accessibility_summary_ssi'] = accessibility_summary |
| 43 | + solr_doc['epub_a11y_accessibility_features_ssim'] = accessibility_features |
| 44 | + solr_doc['epub_a11y_access_mode_ssim'] = access_mode |
| 45 | + @access_mode_sufficient = access_mode_sufficient |
| 46 | + solr_doc['epub_a11y_access_mode_sufficient_ssim'] = @access_mode_sufficient |
| 47 | + |
| 48 | + # for these evaluation values see https://kb.daisy.org/publishing/docs/metadata/evaluation.html |
| 49 | + solr_doc['epub_a11y_conforms_to_ssi'] = conforms_to |
| 50 | + solr_doc['epub_a11y_certified_by_ssi'] = certified_by |
| 51 | + solr_doc['epub_a11y_certifier_credential_ssi'] = certifier_credential |
| 52 | + |
| 53 | + # This is a derived value for convenience in Fulcrum UI use. |
| 54 | + solr_doc['epub_a11y_screen_reader_friendly_ssi'] = screen_reader_friendly |
| 55 | + end |
| 56 | + |
| 57 | + def accessibility_summary |
| 58 | + value = @content_metadata.at_css("meta[#{@meta_attribute}='schema:accessibilitySummary']") |
| 59 | + return nil if value.blank? |
| 60 | + @epub_2 ? value['content']&.strip : value.text&.strip |
| 61 | + end |
| 62 | + |
| 63 | + def accessibility_features |
| 64 | + # this involves multiple entries in separate meta tags |
| 65 | + values = @content_metadata.css("meta[#{@meta_attribute}='schema:accessibilityFeature']") |
| 66 | + |
| 67 | + values = if @epub_2 |
| 68 | + values&.map { |value| value['content']&.strip } |
| 69 | + else |
| 70 | + values&.map { |value| value&.text&.strip } |
| 71 | + end |
| 72 | + # want to ensure the indexer is set to nil not [] if these are not present, keeping the field off the doc entirely |
| 73 | + values.presence |
| 74 | + end |
| 75 | + |
| 76 | + def access_mode |
| 77 | + # this involves multiple entries in separate meta tags |
| 78 | + values = @content_metadata.css("meta[#{@meta_attribute}='schema:accessMode']") |
| 79 | + |
| 80 | + values = if @epub_2 |
| 81 | + values&.map { |value| value['content']&.strip } |
| 82 | + else |
| 83 | + values&.map { |value| value&.text&.strip } |
| 84 | + end |
| 85 | + # want to ensure the indexer is set to nil not [] if these are not present, keeping the field off the doc entirely |
| 86 | + values.presence |
| 87 | + end |
| 88 | + |
| 89 | + def access_mode_sufficient |
| 90 | + # this one has multiple entries in one value, comma separated |
| 91 | + values = @content_metadata.at_css("meta[#{@meta_attribute}='schema:accessModeSufficient']") |
| 92 | + return nil if values.blank? |
| 93 | + values = @epub_2 ? values['content']&.split(',') : values.text&.split(',') |
| 94 | + values&.reject(&:blank?)&.map(&:strip) |
| 95 | + end |
| 96 | + |
| 97 | + def conforms_to |
| 98 | + value = @content_metadata.at_css("meta[#{@meta_attribute}='dcterms:conformsTo']") |
| 99 | + return nil if value.blank? |
| 100 | + @epub_2 ? value['content']&.strip : value.text&.strip |
| 101 | + end |
| 102 | + |
| 103 | + def certified_by |
| 104 | + value = @content_metadata.at_css("meta[#{@meta_attribute}='a11y:certifiedBy']") |
| 105 | + return nil if value.blank? |
| 106 | + @epub_2 ? value['content']&.strip : value.text&.strip |
| 107 | + end |
| 108 | + |
| 109 | + def certifier_credential |
| 110 | + value = @content_metadata.at_css("meta[#{@meta_attribute}='a11y:certifierCredential']") |
| 111 | + return nil if value.blank? |
| 112 | + @epub_2 ? value['content']&.strip : value.text&.strip |
| 113 | + end |
| 114 | + |
| 115 | + def screen_reader_friendly |
| 116 | + if @access_mode_sufficient.present? |
| 117 | + if @access_mode_sufficient.count == 1 && @access_mode_sufficient[0] == 'textual' |
| 118 | + 'true' |
| 119 | + else |
| 120 | + 'false' |
| 121 | + end |
| 122 | + else |
| 123 | + # I guess it's OK that this will always have a value even if all the other a11y metadata is missing. |
| 124 | + 'unknown' |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | +end |
0 commit comments