Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controlled Vocabulary Fix #181

Merged
merged 13 commits into from
Jan 10, 2022
3 changes: 0 additions & 3 deletions app/assets/javascripts/facets.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,5 @@ $(document).on('turbolinks:load', function() {
url = $(this).attr('href');
$(this).attr('href', url.replace(/\/collections\/[a-zA-Z_]+\//i,"/catalog/"))
});

// In collection facet panels, set the keyword search attribute to 'q' instead of 'cq' so it displays properly
$('input#collection_search').attr('name','q')

});
1 change: 1 addition & 0 deletions app/forms/hyrax/work_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def self.build_permitted_params
permitted << {"#{field_name}_attributes".to_sym => [:id, :_destroy]}
end
permitted << :visibility
permitted << {:in_works_ids => [:id, :_destroy]}
permitted << :admin_set_id
permitted << :member_of_collection_ids
permitted << :permissions_attributes
Expand Down
91 changes: 47 additions & 44 deletions app/indexers/controlled_indexer_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module ControlledIndexerBehavior
class_methods do

def fetch_remote_label(url)

if url.is_a? ActiveTriples::Resource
resource = url
url = resource.id.dup
Expand All @@ -19,64 +20,66 @@ def fetch_remote_label(url)
end
end

begin

begin
# handle local qa table based vocabs
if url.to_s.include?("ucsc.edu") or url.to_s.include?("http://localhost")
url.gsub!('http://','https://') if url.to_s.include? "library.ucsc.edu"
label = JSON.parse(Net::HTTP.get_response(URI(url)).body)["label"]
# handle geonames specially
elsif url.include? "geonames.org"
# make sure we fetch the rdf record, not the normal html one
if (res_url = url.dup) =~ /geonames.org\/[0-9]+.*\z/ && !res_url.include?("/about.rdf")
res_url = url.gsub(/(geonames.org\/[0-9]+).*\z/,"\\1/about.rdf")
end
# Interpret the xml result ourselves
doc = Nokogiri::XML(open(res_url))
label = doc.xpath('//gn:name').first.children.first.text.dup
# fetch from other normal authorities
if url.to_s.include?("ucsc.edu") or url.to_s.include?("http://localhost")
url.gsub!('http://','https://') if url.to_s.include? "library.ucsc.edu"
label = JSON.parse(Net::HTTP.get_response(URI(url)).body)["label"]
# handle geonames specially
elsif url.include? "geonames.org"
# make sure we fetch the rdf record, not the normal html one
if (res_url = url.dup) =~ /geonames.org\/[0-9]+.*\z/ && !res_url.include?("/about.rdf")
res_url = url.gsub(/(geonames.org\/[0-9]+).*\z/,"\\1/about.rdf")
end
# Interpret the xml result ourselves
doc = Nokogiri::XML(open(res_url))
label = doc.xpath('//gn:name').first.children.first.text.dup
# fetch from other normal authorities
else
# Smoothly handle some common syntax issues
cleaned_url = url.dup
if url[0..6] == "info:lc"
cleaned_url = cleaned_url.gsub!("info:lc","http://id.loc.gov")
elsif url.include?("vocab.getty.edu")
cleaned_url = cleaned_url.gsub!("/page/","/")
end
resource = ActiveTriples::Resource.new(cleaned_url)
labels = resource.fetch(headers: { 'Accept'.freeze => default_accept_header }).rdf_label
if labels.count == 1
label = labels.first.dup.to_s
else
# Smoothly handle some common syntax issues
cleaned_url = url.dup
cleaned_url.gsub!("info:lc","http://id.loc.gov") if (url[0..6] == "info:lc")
cleaned_url.gsub!("/page/","/") if url.include?("vocab.getty.edu")
resource ||= ActiveTriples::Resource.new(cleaned_url)
labels = resource.fetch(headers: { 'Accept'.freeze => default_accept_header }).rdf_label
if labels.count == 1
label = labels.first.dup.to_s
else
label = labels.find{|label| label.language.to_s =~ /en/ }.dup.to_s
end
label = labels.find{|label| label.language.to_s =~ /en/ }.dup.to_s
end
end

Rails.logger.info "Adding buffer entry - label: #{label}, url: #{url.to_s}"
LdBuffer.create(url: url, label: label)
Rails.logger.info "Adding buffer entry - label: #{label}, url: #{url.to_s}"
LdBuffer.create(url: url, label: label)

# Delete oldest records if we have more than 5K in the buffer
if (cnt = LdBuffer.count - 5000) > 0
ids = LdBuffer.order('created_at ASC').limit(cnt).pluck(:id)
LdBuffer.where(id: ids).delete_all
end
# Delete oldest records if we have more than 5K in the buffer
if (cnt = LdBuffer.count - 5000) > 0
ids = LdBuffer.order('created_at ASC').limit(cnt).pluck(:id)
LdBuffer.where(id: ids).delete_all
end

if label == url && url.include?("id.loc.gov")
#handle weird alternative syntax
response = JSON.parse(Net::HTTP.get_response(URI(url)).body)
response.each do |index, node|
if node["@id"] == url
label = node["http://www.loc.gov/mads/rdf/v1#authoritativeLabel"].first["@value"].dup
end
if label == url && url.include?("id.loc.gov")
#handle weird alternative syntax
response = JSON.parse(Net::HTTP.get_response(URI(url)).body)
response.each do |index, node|
if node["@id"] == url
label = node["http://www.loc.gov/mads/rdf/v1#authoritativeLabel"].first["@value"].dup
end
end
end

raise Exception if label.to_s == url.to_s
raise Exception if label.to_s == url.to_s

return label.to_s
return label.to_s

rescue Exception => e
# IOError could result from a 500 error on the remote server
# SocketError results if there is no server to connect to
Rails.logger.error "Unable to fetch #{url} from the authorative source.\n#{e.message}"
return false
Rails.logger.error "Unable to fetch #{url} from the authorative source.\n#{e.message}"
return false
end
end

Expand Down
65 changes: 33 additions & 32 deletions app/models/work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ class Work < ActiveFedora::Base
index.as :stored_sortable
end

# include ::Hyrax::BasicMetadata

# include ::Hyrax::BasicMetadata
include ::ScoobySnacks::WorkModelBehavior

include ::Ucsc::UntitledBehavior

self.indexer = ::WorkIndexer

# Change this to restrict which works can be added as a child.
# self.valid_child_concerns = []
# validates :title, presence: { message: 'Your work must have a title.' }
self.valid_child_concerns = [Work]

# validates :title, presence: { message: 'Your work must have a title.' }

# self.human_readable_type = 'Work'

Expand All @@ -27,36 +28,36 @@ def solr_doc
end

def save *args

# This section handles alternate date formates and standardizes them
::ScoobySnacks::METADATA_SCHEMA.fields.select{|name, field| field.input == "date"}.each do |field_name, field|
Copy link
Contributor

@rschwab rschwab Dec 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sara-g Did this sneak in from the work to remove Scooby Snacks, or is it part of making controlled vocabs work?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rschwab this was part of the work on date fields.

date_changed = false
dateUpdate = self.send(field_name.to_sym).map do |date|
next date unless date.to_s.match?(/\A[12][0-9]{3}[-\/][0-9]{1,2}\z/)
year, month = date.to_s.split(/[-\/]/)
date_changed = true
"#{month}/#{year}"
end
self.send("#{field_name.to_s}=".to_sym,dateUpdate) if date_changed
end
# ::ScoobySnacks::METADATA_SCHEMA.fields.select{|name, field| field.input == "date"}.each do |field_name, field|
# date_changed = false
# dateUpdate = self.send(field_name.to_sym).map do |date|
# next date unless date.to_s.match?(/\A[12][0-9]{3}[-\/][0-9]{1,2}\z/)
# year, month = date.to_s.split(/[-\/]/)
# date_changed = true
# "#{month}/#{year}"
# end
# self.send("#{field_name.to_s}=".to_sym,dateUpdate) if date_changed
# end

# This section standardized ambiguous controlled vocab urls
::ScoobySnacks::METADATA_SCHEMA.controlled_field_names.each do |field_name|
attributes = []
props = self.send(field_name)
props = Array(props) if !props.kind_of?(Array)
props.each do |node|
next unless node.respond_to?('id')
if node.id.starts_with?('info:lc')
attributes << {id: fix_loc_id(node.id) }
attributes << {id: node.id, _destroy: true}
elsif node.id.include?("vocab.getty.edu") && node.id.include?("/page/")
attributes << {id: fix_getty_id(node.id) }
attributes << {id: node.id, _destroy: true}
end
end
self.send(field_name.to_s+"_attributes=",attributes) unless attributes.empty?
end
# ::ScoobySnacks::METADATA_SCHEMA.controlled_field_names.each do |field_name|
# attributes = []
# props = self.send(field_name)
# props = Array(props) if !props.kind_of?(Array)
# props.each do |node|
# next unless node.respond_to?('id')
# if node.id.starts_with?('info:lc')
# attributes << {id: fix_loc_id(node.id) }
# attributes << {id: node.id, _destroy: true}
# elsif node.id.include?("vocab.getty.edu") && node.id.include?("/page/")
# attributes << {id: fix_getty_id(node.id) }
# attributes << {id: node.id, _destroy: true}
# end
# end
# self.send(field_name.to_s+"_attributes=",attributes) unless attributes.empty?
# end

# This section attempts to ensure that each work has a representative ID
if representative_id.blank? && members.present?
Expand Down
1 change: 0 additions & 1 deletion app/views/hyrax/base/_attribute_rows.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<%= presenter.attribute_to_html(:contributor, render_as: :faceted) %>
<%= presenter.attribute_to_html(:subject_label, render_as: :faceted, label: "Subject") %>
<%= presenter.attribute_to_html(:publisher_label, render_as: :faceted, label: "Publisher") %>

<%= presenter.attribute_to_html(:resourceType, render_as: :faceted, label: "Resource Type") %>
<%= presenter.attribute_to_html(:rightsStatement, label: "Rights Statement") %>
<%= presenter.attribute_to_html(:access_rights, label: "Access Rights") %>
Expand Down
6 changes: 2 additions & 4 deletions app/views/hyrax/collections/show_all.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
<h4 class="media-heading">
<a href="/collections/<%= collection.id %>?locale=en"><%= collection.title.first %></a>
</h4>
<p><%= collection.description.first.truncate(160) %>

</p>
</div>
<p><%= collection.description.first.truncate(160) if collection.description.present? %></p>
</div>
</div>
<% end %>

Expand Down
3 changes: 2 additions & 1 deletion config/branch_hosts.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
sandbox: digitalcollections-staging-sandbox.library.ucsc.edu
staging: digitalcollections-staging.library.ucsc.edu
master: digitalcollections.library.ucsc.edu
production: digitalcollections.libraru.ucsc.edu
production: digitalcollections.library.ucsc.edu
n8_staging: ucsc-hyrax-staging.notch8.cloud
2 changes: 1 addition & 1 deletion config/initializers/session_store.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Be sure to restart your server when you modify this file.

domain = %w[production staging sandbox].include?(Rails.env) ? ENV.fetch('SITE_DOMAIN', '.library.ucsc.edu') : :all
domain = %w[production staging sandbox n8_staging].include?(Rails.env) ? ENV.fetch('SITE_DOMAIN', '.library.ucsc.edu') : :all
Rails.application.config.session_store :cookie_store, key: '_ucsc_hyrax_session', domain: domain
10 changes: 5 additions & 5 deletions stack_car/ops/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ echo "Running Test Database Setup"
#echo "Initialize Default Admin Set"
#bundle exec rake hyrax:default_admin_set:create

echo 'alias repl="cd /srv/hycruz; unset BUNDLE_PATH; unset BUNDLE_BIN; GEM_HOME=/srv/bundle; bundle exec rails c"' >> /home/hycruz/.bashrc
echo 'alias errors="tail -n 1000 /srv/hyrax/logs/development.log | grep FATAL -A 20 -B 20"' >> /home/hycruz/.bashrc
echo 'unset BUNDLE_PATH' >> /home/hycruz/.bashrc
echo 'unset BUNDLE_BIN' >> /home/hycruz/.bashrc

# (S.Geezy - This is not working. Do we need it?)
# echo 'alias repl="cd /srv/hycruz; unset BUNDLE_PATH; unset BUNDLE_BIN; GEM_HOME=/srv/bundle; bundle exec rails c"' >> /home/hycruz/.bashrc
# echo 'alias errors="tail -n 1000 /srv/hyrax/logs/development.log | grep FATAL -A 20 -B 20"' >> /home/hycruz/.bashrc
# echo 'unset BUNDLE_PATH' >> /home/hycruz/.bashrc
# echo 'unset BUNDLE_BIN' >> /home/hycruz/.bashrc

echo "Starting the Rails Server"
bundle exec rails s -b 0.0.0.0