+Removes Non Sorting Block characters
+
+=cut
+sub nsb_clean {
+ my $NSB = '\x88' ; # NSB : begin Non Sorting Block
+ my $NSE = '\x89' ; # NSE : Non Sorting Block end
+ my $NSB2 = '\x98' ; # NSB : begin Non Sorting Block
+ my $NSE2 = '\x9C' ; # NSE : Non Sorting Block end
+ my $C2 = '\xC2' ; # What is this char ? It is sometimes left by the regexp after removing NSB / NSE
+
+ # handles non sorting blocks
+ my ($string) = @_ ;
+ $_ = $string ;
+ s/($C2){0,1}($NSB|$NSB2)//g ;
+ s/($C2){0,1}($NSE|$NSE2)//g ;
+ $string = $_ ;
+
+ return($string) ;
+}
+
+
+=head2 SanitizeRecord
+
+SanitizeRecord($marcrecord);
+
+Sanitize a record
+This routine is called in the maintenance script misc/maintenance/sanitize_records.pl.
+It cleans any string with '&...', replacing it by '&'
+
+=cut
+
+sub SanitizeRecord {
+ my ( $record, $biblionumber ) = @_;
+ my $string;
+ my $record_modified = 0;
+ my $frameworkcode = C4::Biblio::GetFrameworkCode($biblionumber);
+ my ( $url_field, $url_subfield ) =
+ C4::Biblio::GetMarcFromKohaField( 'biblioitems.url', $frameworkcode );
+ foreach my $field ( $record->fields() ) {
+ if ( $field->is_control_field() ) {
+ my $value = $field->data();
+ my $sanitized_value = _clean_ampersand($value);
+ $record_modified = 1 if $sanitized_value ne $value;
+ $field->update($sanitized_value);
+ }
+ else {
+ my @subfields = $field->subfields();
+ my @new_subfields;
+ foreach my $subfield (@subfields) {
+ next
+ if $url_field eq $field->tag()
+ and $url_subfield eq $subfield->[0];
+ my $value = $subfield->[1];
+ my $sanitized_value = _clean_ampersand($value);
+ push @new_subfields, $subfield->[0] => $sanitized_value;
+ $record_modified = 1 if $sanitized_value ne $value;
+ }
+ if ( scalar(@new_subfields) > 0 ) {
+ my $new_field = eval {
+ MARC::Field->new(
+ $field->tag(), $field->indicator(1),
+ $field->indicator(2), @new_subfields
+ );
+ };
+ if ($@) {
+ warn "error : $@";
+ }
+ else {
+ $field->replace_with($new_field);
+ }
+
+ }
+ }
+ }
+
+ return $record, $record_modified;
+}
+
+sub _clean_ampersand {
+ my ($string) = @_;
+ $string =~ s/(&)(amp;)+/$1/g;
+ return $string;
+}
+
+=head1 INTERNAL FUNCTIONS
+
+=head2 _default_marc21_charconv_to_utf8
+
+ my ($new_marc_record, $guessed_charset) = _default_marc21_charconv_to_utf8($marc_record);
+