--- /dev/null
+CREATE OR REPLACE FUNCTION conifer.cleanup_old_springer(record BIGINT) RETURNS TEXT AS $func$
+use strict;
+use MARC::Record;
+use MARC::File::XML (BinaryEncoding => 'UTF-8');
+use MARC::Charset;
+use Encode;
+use Unicode::Normalize;
+use LWP::UserAgent;
+use JSON::XS;
+
+MARC::Charset->assume_unicode(1);
+
+my $q = spi_prepare('SELECT marc FROM biblio.record_entry WHERE id = $1', 'BIGINT');
+my $marc = spi_exec_prepared($q, $_[0])->{rows}->[0]->{marc};
+
+my $record = MARC::Record->new_from_xml($marc);
+
+# Fixing "Software and video game" type
+# Set type to book ('a')
+my $leader = $record->leader();
+my $newleader = substr $leader, 6, 1, 'a';
+$record->leader($leader);
+
+# Set form 008/23 to online ('o')
+my @fates = $record->field('008');
+foreach my $fate (@fates) {
+ my $data = $fate->data();
+ my $newdata = substr $data, 23, 1, 'o';
+ $fate->update($data);
+}
+
+# Add explicit publisher relator code
+my @added = $record->field('710');
+foreach my $entry (@added) {
+ next if $entry->subfield('4');
+ if ($entry->subfield('a') =~ 'SpringerLink \(Online service\)') {
+ $entry->add_subfields('4' => 'pbl');
+ }
+}
+
+my @eights = $record->field('856');
+foreach my $ocho (@eights) {
+ my @ous = $ocho->subfield('9');
+ foreach my $ou (@ous) {
+ if ($ou eq 'WINDSYS' or $ou eq 'OWA') {
+ $record->delete_field($ocho);
+ }
+ if ($ou eq 'OSUL') {
+ $ocho->update('9' => 'LUSYS');
+ }
+ }
+
+ # Fix URIs that raise SSL cert issues
+ my $uri = $ocho->subfield('u');
+ if ($uri =~ m#^https://librweb#) {
+ $uri =~ s{^https://librweb}{http://librweb};
+ $ocho->update('u' => $uri);
+ }
+
+ # Provide an indication of the platform
+ if ($uri =~ m#scholarsportal#) {
+ $ocho->update('y' => 'Available online / disponible en ligne (ScholarsPortal)');
+ $ocho->delete_subfield(code => 'z', match => qr/Available/);
+ } elsif ($uri =~ m#dx.doi.org/10.1007#) {
+ $ocho->update('y' => 'Available online / disponible en ligne (Springer)');
+ $ocho->delete_subfield(code => 'z', match => qr/Available/);
+ } elsif ($uri =~ m#myilibrary#) {
+ $ocho->update('y' => 'Available online / disponible en ligne (MyiLibrary)');
+ $ocho->delete_subfield(code => 'z', match => qr/Available/);
+ } elsif ($uri =~ m#sagepub#) {
+ $ocho->update('y' => 'Available online / disponible en ligne (Sage)');
+ $ocho->delete_subfield(code => 'z', match => qr/Available/);
+ }
+}
+
+# Do not need access notes anymore
+my @access = $record->field('506');
+foreach my $note (@access) {
+ my @ous = $note->subfield('9');
+ foreach my $ou (@ous) {
+ if ($ou eq 'OWA') {
+ $record->delete_field($note);
+ }
+ }
+}
+
+# Do not need 596 holding fields anymore
+my @access = $record->field('596');
+foreach my $note (@access) {
+ my @ous = $note->subfield('a');
+ foreach my $ou (@ous) {
+ if ($ou eq '1') {
+ $record->delete_field($note);
+ }
+ }
+}
+
+my $xml = $record->as_xml_record();
+
+$xml =~ s/\n//sgo;
+$xml =~ s/^<\?xml.+\?\s*>//go;
+$xml =~ s/>\s+</></go;
+$xml =~ s/\p{Cc}//go;
+
+# Embed a version of OpenILS::Application::AppUtils->entityize()
+# to avoid having to set PERL5LIB for PostgreSQL as well
+
+$xml = NFC($xml);
+
+# Convert raw ampersands to entities
+$xml =~ s/&(?!\S+;)/&/gso;
+
+# Convert Unicode characters to entities
+$xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
+
+$xml =~ s/[\x00-\x1f]//go;
+
+return $xml;
+$func$ LANGUAGE PLPERLU;
+
+
# Provide an indication of the platform
if ($uri =~ m#scholarsportal#) {
$ocho->update('y' => 'Available online / disponible en ligne (ScholarsPortal)');
+ $ocho->delete_subfield(code => 'z', match => qr/Available/);
+ } elsif ($uri =~ m#dx.doi.org/10.1007#) {
+ $ocho->update('y' => 'Available online / disponible en ligne (Springer)');
+ $ocho->delete_subfield(code => 'z', match => qr/Available/);
} elsif ($uri =~ m#myilibrary#) {
$ocho->update('y' => 'Available online / disponible en ligne (MyiLibrary)');
+ $ocho->delete_subfield(code => 'z', match => qr/Available/);
} elsif ($uri =~ m#sagepub#) {
$ocho->update('y' => 'Available online / disponible en ligne (Sage)');
+ $ocho->delete_subfield(code => 'z', match => qr/Available/);
}
}
}
}
+# Do not need 596 holding fields anymore
+my @access = $record->field('596');
+foreach my $note (@access) {
+ my @ous = $note->subfield('a');
+ foreach my $ou (@ous) {
+ if ($ou eq '1') {
+ $record->delete_field($note);
+ }
+ }
+}
+
# Update provenance of the records
my @provs = $record->field('040');
foreach my $prov (@provs) {
RETURN next_id;
END;
$func$ LANGUAGE PLPGSQL;
+