X-Git-Url: http://git.equinoxoli.org/?p=migration-tools.git;a=blobdiff_plain;f=sql%2Fbase%2Fbase.sql;h=569dd07feb62c9285eebade702fbc21fa7dede8b;hp=b9703b27688693768de02fadc89581908327ff8b;hb=96965bbc9a9d05e52cf71d5c721383038eddcbda;hpb=e555ac5beea87cbe0bb85b640266dfae4917e53c diff --git a/sql/base/base.sql b/sql/base/base.sql index b9703b2..569dd07 100644 --- a/sql/base/base.sql +++ b/sql/base/base.sql @@ -705,7 +705,7 @@ CREATE OR REPLACE FUNCTION migration_tools.attempt_phone (TEXT,TEXT) RETURNS TEX temp := REGEXP_REPLACE(temp, '^1*[^0-9]*(?=[0-9])', ''); temp := REGEXP_REPLACE(temp, '[^0-9]*([0-9]{3})[^0-9]*([0-9]{3})[^0-9]*([0-9]{4})', E'\\1-\\2-\\3'); n_digits := LENGTH(REGEXP_REPLACE(REGEXP_REPLACE(temp, '(.*)?[a-zA-Z].*', E'\\1') , '[^0-9]', '', 'g')); - IF n_digits = 7 THEN + IF n_digits = 7 AND areacode <> '' THEN temp := REGEXP_REPLACE(temp, '[^0-9]*([0-9]{3})[^0-9]*([0-9]{4})', E'\\1-\\2'); output := (areacode || '-' || temp); ELSE @@ -769,3 +769,46 @@ CREATE OR REPLACE FUNCTION migration_tools.is_staff_profile (INT) RETURNS BOOLEA END; $$ LANGUAGE PLPGSQL STRICT STABLE; + +CREATE OR REPLACE FUNCTION migration_tools.is_blank (TEXT) RETURNS BOOLEAN AS $$ + BEGIN + RETURN CASE WHEN $1 = '' THEN TRUE ELSE FALSE END; + END; +$$ LANGUAGE PLPGSQL STRICT STABLE; + + +CREATE OR REPLACE FUNCTION migration_tools.insert_tags (TEXT, TEXT) RETURNS TEXT AS $$ + + my ($marcxml, $tags) = @_; + + use MARC::Record; + use MARC::File::XML; + + my $xml = $marcxml; + + eval { + my $marc = MARC::Record->new_from_xml($marcxml, 'UTF-8'); + my $to_insert = MARC::Record->new_from_xml("$tags", 'UTF-8'); + + my @incumbents = (); + + foreach my $field ( $marc->fields() ) { + push @incumbents, $field->as_formatted(); + } + + foreach $field ( $to_insert->fields() ) { + if (!grep {$_ eq $field->as_formatted()} @incumbents) { + $marc->insert_fields_ordered( ($field) ); + } + } + + $xml = $marc->as_xml_record; + $xml =~ s/^<\?.+?\?>$//mo; + $xml =~ s/\n//sgo; + $xml =~ s/>\s+