Bug 42566: Remove C4::Koha::GetNormalizedUPC in favor of MetadataExtractor

GetNormalizedUPC duplicated logic already available via the
MetadataExtractor (Bug 34828) and exposed as $biblio->normalized_upc.

All callers migrated:
- Scripts with $biblio available: use $biblio->normalized_upc
- C4::Search (only MARC::Record): reuses the extractor instance
  already created for OCLC

Assisted-by: Sonnet 4.6 (Anthropic)

Test plan:
1. Apply patch
2. Run:
   $ ktd --shell
   k$ prove t/db_dependent/Koha.t \
          t/db_dependent/Koha/Biblio.t
=> SUCCESS: Tests pass
3. Verify UPC numbers still display in OPAC and staff detail pages
   (Syndetics cover images)
4. Sign off :-D

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Pedro Amorim <pedro.amorim@openfifth.co.uk>
This commit is contained in:
Tomás Cohen Arazi 2026-05-08 15:47:38 -03:00 committed by Pedro Amorim
parent 5fb7fd5f2d
commit 3d3f625b56
No known key found for this signature in database
GPG Key ID: 06B75BA7F3894D55
12 changed files with 24 additions and 68 deletions

View File

@ -33,7 +33,6 @@ BEGIN {
getitemtypeimagesrc
getitemtypeimagelocation
GetAuthorisedValues
GetNormalizedUPC
GetNormalizedISBN
GetNormalizedEAN
xml_escape
@ -600,39 +599,6 @@ sub display_marc_indicators {
return $indicators;
}
=head2 GetNormalizedUPC
Missing POD for GetNormalizedUPC.
=cut
sub GetNormalizedUPC {
my ( $marcrecord, $marcflavour ) = @_;
$marcflavour ||= C4::Context->preference('marcflavour');
return unless $marcrecord;
if ( $marcflavour eq 'UNIMARC' ) {
my @fields = $marcrecord->field('072');
foreach my $field (@fields) {
my $upc = _normalize_match_point( $field->subfield('a') );
if ($upc) {
return $upc;
}
}
} else { # assume marc21 if not unimarc
my @fields = $marcrecord->field('024');
foreach my $field (@fields) {
my $indicator = $field->indicator(1);
my $upc = _normalize_match_point( $field->subfield('a') );
if ( $upc && $indicator eq '1' ) {
return $upc;
}
}
}
}
# Normalizes and returns the first valid ISBN found in the record
# ISBN13 are converted into ISBN10. This is required to get some book cover images.

View File

@ -35,8 +35,7 @@ BEGIN {
use C4::Context;
use C4::Biblio qw( TransformMarcToKoha GetMarcFromKohaField GetFrameworkCode GetAuthorisedValueDesc GetBiblioData );
use C4::Koha
qw( getFacets GetVariationsOfISBN GetNormalizedUPC GetNormalizedEAN GetNormalizedISBN getitemtypeimagelocation );
use C4::Koha qw( getFacets GetVariationsOfISBN GetNormalizedEAN GetNormalizedISBN getitemtypeimagelocation );
use Koha::Biblio::Metadata::Extractor;
use Koha::DateUtils;
use Koha::Libraries;
@ -1769,10 +1768,10 @@ sub searchResults {
my $oldbiblio = TransformMarcToKoha( { record => $marcrecord, limit_table => 'no_items' } );
$oldbiblio->{result_number} = $i + 1;
$oldbiblio->{normalized_upc} = GetNormalizedUPC( $marcrecord, $marcflavour );
$oldbiblio->{normalized_ean} = GetNormalizedEAN( $marcrecord, $marcflavour );
$oldbiblio->{normalized_oclc} =
Koha::Biblio::Metadata::Extractor->new( { metadata => $marcrecord } )->get_normalized_oclc;
my $extractor = Koha::Biblio::Metadata::Extractor->new( { metadata => $marcrecord } );
$oldbiblio->{normalized_upc} = $extractor->get_normalized_upc;
$oldbiblio->{normalized_ean} = GetNormalizedEAN( $marcrecord, $marcflavour );
$oldbiblio->{normalized_oclc} = $extractor->get_normalized_oclc;
$oldbiblio->{normalized_isbn} = GetNormalizedISBN( $oldbiblio->{isbn}, $marcrecord, $marcflavour )
; # Use existing ISBN from record if we got one
$oldbiblio->{content_identifier_exists} = 1

View File

@ -28,8 +28,7 @@ BEGIN {
use C4::Biblio qw( GetAuthorisedValueDesc );
use C4::Context;
use C4::Koha qw( GetNormalizedUPC GetNormalizedISBN GetNormalizedEAN );
use Koha::Biblio::Metadata::Extractor;
use C4::Koha qw( GetNormalizedISBN GetNormalizedEAN );
use Koha::Biblios;
use Koha::Libraries;
@ -236,9 +235,8 @@ sub GetShelfInfo {
$item->{part_number} = $biblio->part_number;
$item->{part_name} = $biblio->part_name;
my $this_record = $biblio->metadata->record;
$item->{'browser_normalized_upc'} = GetNormalizedUPC( $this_record, $marcflavour );
$item->{'browser_normalized_oclc'} =
Koha::Biblio::Metadata::Extractor->new( { metadata => $this_record } )->get_normalized_oclc;
$item->{'browser_normalized_upc'} = $biblio->normalized_upc;
$item->{'browser_normalized_oclc'} = $biblio->normalized_oclc;
$item->{'browser_normalized_isbn'} = GetNormalizedISBN( undef, $this_record, $marcflavour );
$item->{'browser_normalized_ean'} = GetNormalizedEAN( $this_record, $marcflavour );
push @valid_items, $item;

View File

@ -25,7 +25,6 @@ use C4::Koha qw(
getitemtypeimagelocation
GetNormalizedEAN
GetNormalizedISBN
GetNormalizedUPC
);
use C4::Serials qw( CountSubscriptionFromBiblionumber SearchSubscriptions GetLatestSerials );
use C4::Output qw( output_html_with_http_headers );
@ -168,7 +167,7 @@ $template->param( ocoins => !$invalid_marc_record ? $biblio->get_coins : undef )
# some useful variables for enhanced content;
# in each case, we're grabbing the first value we find in
# the record and normalizing it
my $upc = GetNormalizedUPC( $marc_record, $marcflavour );
my $upc = $biblio->normalized_upc;
my $ean = GetNormalizedEAN( $marc_record, $marcflavour );
my $oclc = $biblio->normalized_oclc;
my $isbn = GetNormalizedISBN( undef, $marc_record, $marcflavour );

View File

@ -56,7 +56,6 @@ use C4::Serials qw( CountSubscriptionFromBiblionumber SearchSubscriptions GetLa
use C4::Koha qw(
GetNormalizedEAN
GetNormalizedISBN
GetNormalizedUPC
);
use Koha::CirculationRules;
use Koha::ItemTypes;
@ -131,7 +130,7 @@ my $marcflavour = C4::Context->preference("marcflavour");
# some useful variables for enhanced content;
# in each case, we're grabbing the first value we find in
# the record and normalizing it
my $upc = GetNormalizedUPC( $record, $marcflavour );
my $upc = $biblio->normalized_upc;
my $ean = GetNormalizedEAN( $record, $marcflavour );
my $oclc = $extractor->get_normalized_oclc;
my $isbn = GetNormalizedISBN( undef, $record, $marcflavour );

View File

@ -28,7 +28,6 @@ use C4::Koha qw(
getitemtypeimagelocation
GetNormalizedEAN
GetNormalizedISBN
GetNormalizedUPC
);
use C4::Search qw( new_record_from_zebra searchResults getRecords );
use C4::Serials qw( CountSubscriptionFromBiblionumber SearchSubscriptions GetLatestSerials );
@ -874,7 +873,7 @@ foreach ( keys %{$dat} ) {
# some useful variables for enhanced content;
# in each case, we're grabbing the first value we find in
# the record and normalizing it
my $upc = GetNormalizedUPC( $record, $marcflavour );
my $upc = $extractor->get_normalized_upc;
my $oclc = $extractor->get_normalized_oclc;
my $isbn = GetNormalizedISBN( undef, $record, $marcflavour );
my $content_identifier_exists;

View File

@ -25,7 +25,6 @@ use C4::Biblio qw( GetBiblioData GetFrameworkCode );
use C4::Koha qw(
GetNormalizedEAN
GetNormalizedISBN
GetNormalizedUPC
);
use C4::Members;
use C4::Output qw( pagination_bar output_with_http_headers );
@ -43,6 +42,7 @@ use Koha::Patrons;
use Koha::Virtualshelfshares;
use Koha::Virtualshelves;
use Koha::RecordProcessor;
use Koha::Biblio::Metadata::Extractor;
use constant ANYONE => 2;
use constant STAFF => 3;
@ -388,8 +388,9 @@ if ( $op eq 'view' ) {
$this_item->{description} = $itemtype->description; #FIXME Should not it be translated_description?
$this_item->{notforloan} = $itemtype->notforloan;
}
$this_item->{'coins'} = $biblio->get_coins;
$this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
$this_item->{'coins'} = $biblio->get_coins;
$this_item->{'normalized_upc'} =
Koha::Biblio::Metadata::Extractor->new( { metadata => $record } )->get_normalized_upc;
$this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
$this_item->{'normalized_oclc'} =
Koha::Biblio::Metadata::Extractor->new( { metadata => $record } )->get_normalized_oclc;

View File

@ -25,7 +25,6 @@ use C4::Auth qw( get_template_and_user );
use C4::Koha qw(
GetNormalizedEAN
GetNormalizedISBN
GetNormalizedUPC
);
use C4::Output qw( output_html_with_http_headers );
use Koha::DateUtils qw( dt_from_string );
@ -93,7 +92,7 @@ for my $result (@$reviews) {
my $biblio = Koha::Biblios->find($biblionumber);
my $biblioitem = $biblio->biblioitem;
my $record = $biblio->metadata->record;
$result->{normalized_upc} = GetNormalizedUPC( $record, $marcflavour );
$result->{normalized_upc} = $biblio->normalized_upc;
$result->{normalized_ean} = GetNormalizedEAN( $record, $marcflavour );
$result->{normalized_oclc} = Koha::Biblio::Metadata::Extractor->new( { metadata => $record } )->get_normalized_oclc;
$result->{normalized_isbn} = GetNormalizedISBN( undef, $record, $marcflavour );

View File

@ -25,7 +25,6 @@ use C4::Auth qw( get_template_and_user );
use C4::Koha qw(
getitemtypeimagelocation
GetNormalizedISBN
GetNormalizedUPC
);
use C4::Circulation qw( CanBookBeRenewed GetRenewCount GetIssuingCharges );
use C4::Reserves qw( GetReserveStatus );
@ -33,6 +32,7 @@ use C4::Members;
use C4::Output qw( output_html_with_http_headers );
use Koha::Account::Lines;
use Koha::Biblios;
use Koha::Biblio::Metadata::Extractor;
use Koha::Libraries;
use Koha::DateUtils qw( output_pref );
use Koha::Holds;
@ -306,9 +306,9 @@ if ( $pending_checkouts->count ) { # Useless test
patron => $patron
}
);
$issue->{normalized_upc} = GetNormalizedUPC( $marcrecord, C4::Context->preference('marcflavour') );
$issue->{normalized_oclc} =
Koha::Biblio::Metadata::Extractor->new( { metadata => $marcrecord } )->get_normalized_oclc;
my $extractor = Koha::Biblio::Metadata::Extractor->new( { metadata => $marcrecord } );
$issue->{normalized_upc} = $extractor->get_normalized_upc;
$issue->{normalized_oclc} = $extractor->get_normalized_oclc;
}
if ( C4::Context->preference('UseRecalls') ) {

View File

@ -173,7 +173,7 @@ subtest 'Authorized Values Tests' => sub {
};
subtest 'isbn tests' => sub {
plan tests => 32;
plan tests => 31;
my $isbn13 = "9780330356473";
my $isbn13D = "978-0-330-35647-3";
@ -279,7 +279,6 @@ subtest 'isbn tests' => sub {
my $test_isbn10 = Business::ISBN->new($test_normalized_isbn10);
is( $test_isbn10->type, 'ISBN10', 'ISBN not starting with 979 is normalized to valid ISBN-10 format' );
is( C4::Koha::GetNormalizedUPC(), undef, 'GetNormalizedUPC should return undef if no record is passed' );
is(
C4::Koha::GetNormalizedISBN(), undef,
'GetNormalizedISBN should return undef if no record and no isbn are passed'

View File

@ -2027,12 +2027,10 @@ subtest 'normalized_upc() tests' => sub {
$schema->storage->txn_begin;
# We will move the tests from GetNormalizedUPC here when it will get replaced
# Note that only a single test exist and it's not really meaningful...
my $biblio = $builder->build_sample_biblio();
is(
$biblio->normalized_upc, C4::Koha::GetNormalizedUPC( $biblio->metadata->record ),
'normalized_upc is a wrapper around C4::Koha::GetNormalizedUPC'
$biblio->normalized_upc, $biblio->metadata_extractor->get_normalized_upc,
'normalized_upc delegates to the metadata extractor'
);
$schema->storage->txn_rollback;

View File

@ -25,7 +25,6 @@ use C4::Context;
use C4::Koha qw(
GetNormalizedEAN
GetNormalizedISBN
GetNormalizedUPC
);
use C4::Members;
use C4::Output qw( pagination_bar output_html_with_http_headers output_and_exit_if_error );
@ -352,7 +351,7 @@ if ( $op eq 'view' ) {
$itemtype ? $itemtype->description : q{}; #FIXME Should this be translated_description ?
$this_item->{notforloan} = $itemtype->notforloan if $itemtype;
$this_item->{'coins'} = $biblio->get_coins;
$this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
$this_item->{'normalized_upc'} = $biblio->normalized_upc;
$this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
$this_item->{'normalized_oclc'} = $biblio->normalized_oclc;
$this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );