lib/authority_control/record_update.rb
### Uses MySQL database to resolve a normalized name or name/title heading
###   to a URI;
### then it gets the authorized form
Uses MySQL database to resolve a normalized name or name/title heading
to a URI;
then it gets the authorized form
Uses MySQL database to resolve a normalized name or name / title heading to a URI ; then it gets the authorized form
def add_nametitle_uri(record:, conn:)
  tags_of_interest = %w[100 700 110 710 111 711 130 730 600 610 611 630 651]
  changed_rec = false
  bad_rec = false
  rec = duplicate_record(record)
  rec.fields.each do |field|
    orig_tag = field.tag
    next unless tags_of_interest.include? orig_tag
    next unless field['0'].nil?
    field_index = rec.fields.index(field)
    field_map = field.subfields.map(&:code)
    next if orig_tag[0] == '6' &&
            (field.indicator2 != '0' || field_map.any? { |code| code =~ /[vxyz]/ })
    heading_subfields = name_subfields_for_tag(orig_tag)
    heading_string = get_heading_from_field(field, heading_subfields)
    search_string = normalize_heading_for_local_search(heading: heading_string)
    target_lccn = get_lccn_from_local_db(conn: conn,
                                         heading: search_string,
                                         query: authorized_naf_lccn_query)
    if target_lccn.nil?
      target_lccn = get_lccn_from_local_db(conn: conn,
                                           heading: search_string,
                                           query: variant_naf_lccn_query)
    end
    next unless target_lccn
    heading_info = get_marc_and_uri_from_lccn(conn: conn,
                                              lccn: target_lccn)
    next unless heading_info
    changed_rec = true
    target_uri = heading_info[:uri]
    auth_field = auth_field_from_marc_info(heading_info)
    auth_indicator1 = auth_field.indicator1
    auth_indicator2 = auth_field.indicator2
    auth_tag_portion = auth_field.tag[1..2]
    auth_tag_portion = '10' if orig_tag =~ /^[17]/ && auth_tag_portion == '51'
    new_tag = orig_tag[0] + auth_tag_portion
    orig_field = ''
    field.subfields.each do |subfield|
      next unless heading_subfields.include?(subfield.code)
      orig_field << subfield.value
    end
    orig_field.gsub!(/[[:punct:][:space:]]/, '')
    orig_field.unicode_normalize!(:nfd)
    auth_field_string = ''
    auth_field.subfields.each do |subfield|
      auth_field_string << subfield.value
    end
    auth_field_string.gsub!(/[[:punct:][:space:]]/, '')
    auth_field_string.unicode_normalize!(:nfd)
    bad_rec = true unless orig_field == auth_field_string
    new_field = nil
    case orig_tag
    when /^.[01]|^651$/
      new_field = MARC::DataField.new(new_tag, auth_indicator1, field.indicator2)
    when /^.3/
      new_field = MARC::DataField.new(new_tag, auth_indicator2, field.indicator2)
    end
    auth_subfields = auth_field.subfields
    auth_subfields.each do |auth_subfield|
      new_field.append(auth_subfield)
    end
    case orig_tag
    when /^[167][013]0$|^651$/
      subfe = field.subfields.select { |subfield| subfield.code == 'e' }
      unless subfe.empty?
        new_field.subfields[-1].value = new_field.subfields[-1].value + ',' unless new_field.subfields[-1].value[-1] == '-'
        subfe.each do |subfield|
          new_field.append(subfield)
        end
      end
    when /^[167]11$/
      subfj = field.subfields.select { |subfield| subfield.code == 'j' }
      unless subfj.empty?
        new_field.subfields[-1].value = new_field.subfields[-1].value + ',' unless new_field.subfields[-1].value[-1] == '-'
        subfj.each do |subfield|
          new_field.append(subfield)
        end
      end
    end
    new_field.subfields[-1].value += '.' unless new_field.subfields[-1].value[-1] =~ /.*["\).\!\?\-]$/
    new_field = add_non_heading_subfields_to_name_field(field, new_field)
    new_field.subfields.delete_if { |subfield| subfield.code == '0' }
    new_field.append(MARC::Subfield.new('0', target_uri))
    rec.fields[field_index] = new_field
  end
  { record: rec,
    changed_rec: changed_rec,
    bad_rec: bad_rec }
end
def add_nametitle_uri ( record : , conn : ) tags_of_interest = %w[ 100 700 110 710 111 711 130 730 600 610 611 630 651 ] changed_rec = false bad_rec = false rec = duplicate_record ( record ) rec . fields . each do | field | orig_tag = field . tag next unless tags_of_interest . include? orig_tag next unless field [ '0' ] . nil? field_index = rec . fields . index ( field ) field_map = field . subfields . map ( & :code ) next if orig_tag [ 0 ] == '6' && ( field . indicator2 != '0' || field_map . any? { | code | code =~ / [vxyz] / } ) heading_subfields = name_subfields_for_tag ( orig_tag ) heading_string = get_heading_from_field ( field , heading_subfields ) search_string = normalize_heading_for_local_search ( heading : heading_string ) target_lccn = get_lccn_from_local_db ( conn : conn , heading : search_string , query : authorized_naf_lccn_query ) if target_lccn . nil? target_lccn = get_lccn_from_local_db ( conn : conn , heading : search_string , query : variant_naf_lccn_query ) end next unless target_lccn heading_info = get_marc_and_uri_from_lccn ( conn : conn , lccn : target_lccn ) next unless heading_info changed_rec = true target_uri = heading_info [ :uri ] auth_field = auth_field_from_marc_info ( heading_info ) auth_indicator1 = auth_field . indicator1 auth_indicator2 = auth_field . indicator2 auth_tag_portion = auth_field . tag [ 1 .. 2 ] auth_tag_portion = '10' if orig_tag =~ / ^[17] / && auth_tag_portion == '51' new_tag = orig_tag [ 0 ] + auth_tag_portion orig_field = '' field . subfields . each do | subfield | next unless heading_subfields . include? ( subfield . code ) orig_field << subfield . value end orig_field . gsub! ( / [[:punct:][:space:]] / , '' ) orig_field . unicode_normalize! ( :nfd ) auth_field_string = '' auth_field . subfields . each do | subfield | auth_field_string << subfield . value end auth_field_string . gsub! ( / [[:punct:][:space:]] / , '' ) auth_field_string . unicode_normalize! ( :nfd ) bad_rec = true unless orig_field == auth_field_string new_field = nil case orig_tag when / ^.[01]|^651$ / new_field = MARC :: DataField . new ( new_tag , auth_indicator1 , field . indicator2 ) when / ^.3 / new_field = MARC :: DataField . new ( new_tag , auth_indicator2 , field . indicator2 ) end auth_subfields = auth_field . subfields auth_subfields . each do | auth_subfield | new_field . append ( auth_subfield ) end case orig_tag when / ^[167][013]0$|^651$ / subfe = field . subfields . select { | subfield | subfield . code == 'e' } unless subfe . empty? new_field . subfields [ - 1 ] . value = new_field . subfields [ - 1 ] . value + ',' unless new_field . subfields [ - 1 ] . value [ - 1 ] == '-' subfe . each do | subfield | new_field . append ( subfield ) end end when / ^[167]11$ / subfj = field . subfields . select { | subfield | subfield . code == 'j' } unless subfj . empty? new_field . subfields [ - 1 ] . value = new_field . subfields [ - 1 ] . value + ',' unless new_field . subfields [ - 1 ] . value [ - 1 ] == '-' subfj . each do | subfield | new_field . append ( subfield ) end end end new_field . subfields [ - 1 ] . value += '.' unless new_field . subfields [ - 1 ] . value [ - 1 ] =~ / .*[" \) . \! \? \- ]$ / new_field = add_non_heading_subfields_to_name_field ( field , new_field ) new_field . subfields . delete_if { | subfield | subfield . code == '0' } new_field . append ( MARC :: Subfield . new ( '0' , target_uri ) ) rec . fields [ field_index ] = new_field end { record : rec , changed_rec : changed_rec , bad_rec : bad_rec } end
Uses MySQL database to resolve a normalized name or name/title heading
to a URI;
then it gets the authorized form
Uses MySQL database to resolve a normalized name or name / title heading to a URI ; then it gets the authorized form
#      orig_field << subfield.code  Ignore code errors for now #      auth_field_string << subfield.code  Ignore code errors for now ### Not a priority to identify incorrect indicators #    bad_rec = true if auth_field.tag == '151' && #                      orig_tag == '651' && #                      field.indicator1 != ' ' #    bad_rec = true if auth_field.tag != '151' && #                      auth_tag_portion != orig_tag[1..2] && #                      auth_indicator1 != field.indicator1 #    bad_rec = true if auth_field.tag == '130' && #                      %w[130 730].include?(orig_tag) && #                      auth_indicator2 != field.indicator1
record
conn