Bug 15380: Move Koha::Authority to Koha::MetadataRecord::Authority
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 15 Dec 2015 10:21:02 +0000 (10:21 +0000)
committerBrendan Gallagher <brendan@bywatersolutions.com>
Thu, 31 Dec 2015 18:50:41 +0000 (18:50 +0000)
The existing Koha::Authority does not use Koha::Objects and does not
exclusively deal with DB objects.
It makes much sense to move it to let the space free for a
Koha::Authority and Koha::Authorities modules based on Koha::Object.

Signed-off-by: Marc VĂ©ron <veron@veron.ch>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Brendan Gallagher brendan@bywatersolutions.com
12 files changed:
C4/AuthoritiesMarc.pm
Koha/Authority.pm [deleted file]
Koha/Exporter/Record.pm
Koha/Filter/MARC/EmbedSeeFromHeadings.pm
Koha/MetadataRecord/Authority.pm [new file with mode: 0644]
authorities/merge.pl
misc/export_records.pl
svc/records/preview
t/db_dependent/Koha_Authority.t
t/db_dependent/RecordProcessor_EmbedSeeFromHeadings.t
tools/batch_record_modification.pl
tools/export.pl

index dd8be73..b1a7196 100644 (file)
@@ -26,7 +26,7 @@ use C4::AuthoritiesMarc::MARC21;
 use C4::AuthoritiesMarc::UNIMARC;
 use C4::Charset;
 use C4::Log;
-use Koha::Authority;
+use Koha::MetadataRecord::Authority;
 
 use vars qw($VERSION @ISA @EXPORT);
 
@@ -801,7 +801,7 @@ Returns MARC::Record of the authority passed in parameter.
 
 sub GetAuthority {
     my ($authid)=@_;
-    my $authority = Koha::Authority->get_from_authid($authid);
+    my $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
     return unless $authority;
     return ($authority->record);
 }
diff --git a/Koha/Authority.pm b/Koha/Authority.pm
deleted file mode 100644 (file)
index 7097bd9..0000000
+++ /dev/null
@@ -1,157 +0,0 @@
-package Koha::Authority;
-
-# Copyright 2012 C & P Bibliography Services
-#
-# This file is part of Koha.
-#
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
-#
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-
-=head1 NAME
-
-Koha::Authority - class to encapsulate authority records in Koha
-
-=head1 SYNOPSIS
-
-Object-oriented class that encapsulates authority records in Koha.
-
-=head1 DESCRIPTION
-
-Authority data.
-
-=cut
-
-use strict;
-use warnings;
-use C4::Context;
-use MARC::Record;
-use MARC::File::XML;
-use C4::Charset;
-use Koha::Util::MARC;
-
-use base qw(Koha::MetadataRecord);
-
-__PACKAGE__->mk_accessors(qw( authid authtype ));
-
-=head2 new
-
-    my $auth = Koha::Authority->new($record);
-
-Create a new Koha::Authority object based on the provided record.
-
-=cut
-
-sub new {
-    my $class = shift;
-    my $record = shift;
-
-    my $self = $class->SUPER::new(
-        {
-            'record' => $record,
-            'schema' => lc C4::Context->preference("marcflavour")
-        }
-    );
-
-    bless $self, $class;
-    return $self;
-}
-
-
-=head2 get_from_authid
-
-    my $auth = Koha::Authority->get_from_authid($authid);
-
-Create the Koha::Authority object associated with the provided authid.
-Note that this routine currently retrieves a MARC record because
-authorities in Koha are MARC records by definition. This is an
-unfortunate but unavoidable fact.
-
-=cut
-
-sub get_from_authid {
-    my $class = shift;
-    my $authid = shift;
-    my $marcflavour = lc C4::Context->preference("marcflavour");
-
-    my $dbh=C4::Context->dbh;
-    my $sth=$dbh->prepare("select authtypecode, marcxml from auth_header where authid=?");
-    $sth->execute($authid);
-    my ($authtypecode, $marcxml) = $sth->fetchrow;
-    my $record=eval {MARC::Record->new_from_xml(StripNonXmlChars($marcxml),'UTF-8',
-        (C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")))};
-    return if ($@);
-    $record->encoding('UTF-8');
-
-    # NOTE: GuessAuthTypeCode has no business in Koha::Authority, which is an
-    #       object-oriented class. Eventually perhaps there will be utility
-    #       classes in the Koha:: namespace, but there are not at the moment,
-    #       so this shim seems like the best option all-around.
-    require C4::AuthoritiesMarc;
-    $authtypecode ||= C4::AuthoritiesMarc::GuessAuthTypeCode($record);
-
-    my $self = $class->SUPER::new( { authid => $authid,
-                                     authtype => $authtypecode,
-                                     schema => $marcflavour,
-                                     record => $record });
-
-    bless $self, $class;
-    return $self;
-}
-
-=head2 get_from_breeding
-
-    my $auth = Koha::Authority->get_from_authid($authid);
-
-Create the Koha::Authority object associated with the provided authid.
-
-=cut
-
-sub get_from_breeding {
-    my $class = shift;
-    my $import_record_id = shift;
-    my $marcflavour = lc C4::Context->preference("marcflavour");
-
-    my $dbh=C4::Context->dbh;
-    my $sth=$dbh->prepare("select marcxml from import_records where import_record_id=? and record_type='auth';");
-    $sth->execute($import_record_id);
-    my $marcxml = $sth->fetchrow;
-    my $record=eval {MARC::Record->new_from_xml(StripNonXmlChars($marcxml),'UTF-8',
-        (C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")))};
-    return if ($@);
-    $record->encoding('UTF-8');
-
-    # NOTE: GuessAuthTypeCode has no business in Koha::Authority, which is an
-    #       object-oriented class. Eventually perhaps there will be utility
-    #       classes in the Koha:: namespace, but there are not at the moment,
-    #       so this shim seems like the best option all-around.
-    require C4::AuthoritiesMarc;
-    my $authtypecode = C4::AuthoritiesMarc::GuessAuthTypeCode($record);
-
-    my $self = $class->SUPER::new( {
-                                     schema => $marcflavour,
-                                     authtype => $authtypecode,
-                                     record => $record });
-
-    bless $self, $class;
-    return $self;
-}
-
-sub authorized_heading {
-    my ($self) = @_;
-    if ($self->schema =~ m/marc/) {
-        return Koha::Util::MARC::getAuthorityAuthorizedHeading($self->record, $self->schema);
-    }
-    return;
-}
-
-1;
index 0dbd7cd..ee76d8a 100644 (file)
@@ -52,7 +52,7 @@ sub _get_record_for_export {
 sub _get_authority_for_export {
     my ($params) = @_;
     my $authid = $params->{authid} || return;
-    my $authority = Koha::Authority->get_from_authid($authid);
+    my $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
     return unless $authority;
     return $authority->record;
 }
index fc8d1eb..1a4a143 100644 (file)
@@ -33,7 +33,7 @@ Filter to embed see from headings into MARC records.
 use strict;
 use warnings;
 use Carp;
-use Koha::Authority;
+use Koha::MetadataRecord::Authority;
 use C4::Biblio qw/GetMarcFromKohaField/;
 
 use base qw(Koha::RecordProcessor::Base);
@@ -83,7 +83,7 @@ sub _processrecord {
 
         next unless $authid;
 
-        my $authority = Koha::Authority->get_from_authid($authid);
+        my $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
         next unless $authority;
         my $auth_marc = $authority->record;
         my @seefrom = $auth_marc->field('4..');
diff --git a/Koha/MetadataRecord/Authority.pm b/Koha/MetadataRecord/Authority.pm
new file mode 100644 (file)
index 0000000..8d59771
--- /dev/null
@@ -0,0 +1,157 @@
+package Koha::MetadataRecord::Authority;
+
+# Copyright 2012 C & P Bibliography Services
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+=head1 NAME
+
+Koha::MetadataRecord::Authority - class to encapsulate authority records in Koha
+
+=head1 SYNOPSIS
+
+Object-oriented class that encapsulates authority records in Koha.
+
+=head1 DESCRIPTION
+
+Authority data.
+
+=cut
+
+use strict;
+use warnings;
+use C4::Context;
+use MARC::Record;
+use MARC::File::XML;
+use C4::Charset;
+use Koha::Util::MARC;
+
+use base qw(Koha::MetadataRecord);
+
+__PACKAGE__->mk_accessors(qw( authid authtype ));
+
+=head2 new
+
+    my $auth = Koha::MetadataRecord::Authority->new($record);
+
+Create a new Koha::MetadataRecord::Authority object based on the provided record.
+
+=cut
+
+sub new {
+    my $class = shift;
+    my $record = shift;
+
+    my $self = $class->SUPER::new(
+        {
+            'record' => $record,
+            'schema' => lc C4::Context->preference("marcflavour")
+        }
+    );
+
+    bless $self, $class;
+    return $self;
+}
+
+
+=head2 get_from_authid
+
+    my $auth = Koha::MetadataRecord::Authority->get_from_authid($authid);
+
+Create the Koha::MetadataRecord::Authority object associated with the provided authid.
+Note that this routine currently retrieves a MARC record because
+authorities in Koha are MARC records by definition. This is an
+unfortunate but unavoidable fact.
+
+=cut
+
+sub get_from_authid {
+    my $class = shift;
+    my $authid = shift;
+    my $marcflavour = lc C4::Context->preference("marcflavour");
+
+    my $dbh=C4::Context->dbh;
+    my $sth=$dbh->prepare("select authtypecode, marcxml from auth_header where authid=?");
+    $sth->execute($authid);
+    my ($authtypecode, $marcxml) = $sth->fetchrow;
+    my $record=eval {MARC::Record->new_from_xml(StripNonXmlChars($marcxml),'UTF-8',
+        (C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")))};
+    return if ($@);
+    $record->encoding('UTF-8');
+
+    # NOTE: GuessAuthTypeCode has no business in Koha::MetadataRecord::Authority, which is an
+    #       object-oriented class. Eventually perhaps there will be utility
+    #       classes in the Koha:: namespace, but there are not at the moment,
+    #       so this shim seems like the best option all-around.
+    require C4::AuthoritiesMarc;
+    $authtypecode ||= C4::AuthoritiesMarc::GuessAuthTypeCode($record);
+
+    my $self = $class->SUPER::new( { authid => $authid,
+                                     authtype => $authtypecode,
+                                     schema => $marcflavour,
+                                     record => $record });
+
+    bless $self, $class;
+    return $self;
+}
+
+=head2 get_from_breeding
+
+    my $auth = Koha::MetadataRecord::Authority->get_from_authid($authid);
+
+Create the Koha::MetadataRecord::Authority object associated with the provided authid.
+
+=cut
+
+sub get_from_breeding {
+    my $class = shift;
+    my $import_record_id = shift;
+    my $marcflavour = lc C4::Context->preference("marcflavour");
+
+    my $dbh=C4::Context->dbh;
+    my $sth=$dbh->prepare("select marcxml from import_records where import_record_id=? and record_type='auth';");
+    $sth->execute($import_record_id);
+    my $marcxml = $sth->fetchrow;
+    my $record=eval {MARC::Record->new_from_xml(StripNonXmlChars($marcxml),'UTF-8',
+        (C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")))};
+    return if ($@);
+    $record->encoding('UTF-8');
+
+    # NOTE: GuessAuthTypeCode has no business in Koha::MetadataRecord::Authority, which is an
+    #       object-oriented class. Eventually perhaps there will be utility
+    #       classes in the Koha:: namespace, but there are not at the moment,
+    #       so this shim seems like the best option all-around.
+    require C4::AuthoritiesMarc;
+    my $authtypecode = C4::AuthoritiesMarc::GuessAuthTypeCode($record);
+
+    my $self = $class->SUPER::new( {
+                                     schema => $marcflavour,
+                                     authtype => $authtypecode,
+                                     record => $record });
+
+    bless $self, $class;
+    return $self;
+}
+
+sub authorized_heading {
+    my ($self) = @_;
+    if ($self->schema =~ m/marc/) {
+        return Koha::Util::MARC::getAuthorityAuthorizedHeading($self->record, $self->schema);
+    }
+    return;
+}
+
+1;
index 476b774..d74abc5 100755 (executable)
@@ -23,7 +23,7 @@ use CGI qw ( -utf8 );
 use C4::Output;
 use C4::Auth;
 use C4::AuthoritiesMarc;
-use Koha::Authority;
+use Koha::MetadataRecord::Authority;
 use C4::Koha;
 use C4::Biblio;
 
@@ -92,13 +92,13 @@ else {
         push @errors, { code => "WRONG_COUNT", value => scalar(@authid) };
     }
     else {
-        my $recordObj1 = Koha::Authority->get_from_authid($authid[0]) || Koha::Authority->new();
+        my $recordObj1 = Koha::MetadataRecord::Authority->get_from_authid($authid[0]) || Koha::MetadataRecord::Authority->new();
         my $recordObj2;
 
         if (defined $mergereference && $mergereference eq 'breeding') {
-            $recordObj2 =  Koha::Authority->get_from_breeding($authid[1]) || Koha::Authority->new();
+            $recordObj2 =  Koha::MetadataRecord::Authority->get_from_breeding($authid[1]) || Koha::MetadataRecord::Authority->new();
         } else {
-            $recordObj2 =  Koha::Authority->get_from_authid($authid[1]) || Koha::Authority->new();
+            $recordObj2 =  Koha::MetadataRecord::Authority->get_from_authid($authid[1]) || Koha::MetadataRecord::Authority->new();
         }
 
         if ($mergereference) {
index b00dde8..e6c3d98 100755 (executable)
@@ -167,7 +167,7 @@ elsif ( $record_type eq 'auths' ) {
             : (),
         ( $authtype ? ( authtypecode => $authtype ) : () ),
     };
-    # Koha::Authority is not a Koha::Object...
+    # Koha::MetadataRecord::Authority is not a Koha::Object...
     my $authorities = Koha::Database->new->schema->resultset('AuthHeader')->search( $conditions );
     @record_ids = map { $_->authid } $authorities->all;
 }
index 39fcb92..c1bdc67 100755 (executable)
@@ -23,7 +23,7 @@ use C4::Auth qw( get_template_and_user );
 use C4::Biblio qw( GetMarcBiblio );
 use C4::MarcModificationTemplates qw( ModifyRecordWithTemplate );
 use C4::Output qw( output_html_with_http_headers );
-use Koha::Authority;
+use Koha::MetadataRecord::Authority;
 
 my $query = CGI->new();
 my $record_id = $query->param('record_id');
@@ -34,7 +34,7 @@ my $record;
 if ( $record_type eq 'biblio' ) {
     $record = GetMarcBiblio( $record_id );
 } else {
-    my $authority = Koha::Authority->get_from_authid( $record_id );
+    my $authority = Koha::MetadataRecord::Authority->get_from_authid( $record_id );
     $record = $authority->record;
 }
 
index 92c3998..f4203b4 100755 (executable)
@@ -24,7 +24,7 @@ use C4::Context;
 use Test::More;
 
 BEGIN {
-        use_ok('Koha::Authority');
+        use_ok('Koha::MetadataRecord::Authority');
 }
 
 my $record = MARC::Record->new;
@@ -34,9 +34,9 @@ $record->add_fields(
         [ '150', ' ', ' ', a => 'Cooking' ],
         [ '450', ' ', ' ', a => 'Cookery' ],
         );
-my $authority = Koha::Authority->new($record);
+my $authority = Koha::MetadataRecord::Authority->new($record);
 
-is(ref($authority), 'Koha::Authority', 'Created valid Koha::Authority object');
+is(ref($authority), 'Koha::MetadataRecord::Authority', 'Created valid Koha::MetadataRecord::Authority object');
 
 is($authority->authorized_heading(), 'Cooking', 'Authorized heading was correct');
 
@@ -53,15 +53,15 @@ SKIP:
         $authid = $row->{'authid'};
     }
     skip 'No authorities', 3 unless $authid;
-    $authority = Koha::Authority->get_from_authid($authid);
+    $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
 
-    is(ref($authority), 'Koha::Authority', 'Retrieved valid Koha::Authority object');
+    is(ref($authority), 'Koha::MetadataRecord::Authority', 'Retrieved valid Koha::MetadataRecord::Authority object');
 
     is($authority->authid, $authid, 'Object authid is correct');
 
     is($authority->record->field('001')->data(), $authid, 'Retrieved correct record');
 
-    $authority = Koha::Authority->get_from_authid('alphabetsoup');
+    $authority = Koha::MetadataRecord::Authority->get_from_authid('alphabetsoup');
     is($authority, undef, 'No invalid record is retrieved');
 }
 
@@ -77,15 +77,15 @@ SKIP:
     }
 
     skip 'No authorities in reservoir', 3 unless $import_record_id;
-    $authority = Koha::Authority->get_from_breeding($import_record_id);
+    $authority = Koha::MetadataRecord::Authority->get_from_breeding($import_record_id);
 
-    is(ref($authority), 'Koha::Authority', 'Retrieved valid Koha::Authority object');
+    is(ref($authority), 'Koha::MetadataRecord::Authority', 'Retrieved valid Koha::MetadataRecord::Authority object');
 
     is($authority->authid, undef, 'Records in reservoir do not have an authid');
 
     is(ref($authority->record), 'MARC::Record', 'MARC record attached to authority');
 
-    $authority = Koha::Authority->get_from_breeding('alphabetsoup');
+    $authority = Koha::MetadataRecord::Authority->get_from_breeding('alphabetsoup');
     is($authority, undef, 'No invalid record is retrieved from reservoir');
 }
 
index 0b9d5f0..664a0fa 100755 (executable)
@@ -21,7 +21,7 @@ use strict;
 use warnings;
 use File::Spec;
 use MARC::Record;
-use Koha::Authority;
+use Koha::MetadataRecord::Authority;
 
 use Test::More;
 use Test::MockModule;
@@ -82,7 +82,7 @@ subtest "EmbedSeeFromHeadings should skip holdings fields" => sub {
     );
 
 
-    my $koha_authority = new Test::MockModule('Koha::Authority');
+    my $koha_authority = new Test::MockModule('Koha::MetadataRecord::Authority');
     $koha_authority->mock( 'get_from_authid', sub {
 
         my $auth_record = MARC::Record->new;
index f96ec5d..51095c3 100755 (executable)
@@ -29,7 +29,7 @@ use C4::AuthoritiesMarc qw( BuildSummary GetAuthTypeCode ModAuthority );
 use C4::BackgroundJob;
 use C4::Biblio qw( GetMarcBiblio ModBiblio );
 use C4::MarcModificationTemplates qw( GetModificationTemplateActions GetModificationTemplates ModifyRecordWithTemplate );
-use Koha::Authority;
+use Koha::MetadataRecord::Authority;
 
 my $input = new CGI;
 our $dbh = C4::Context->dbh;
@@ -128,7 +128,7 @@ if ( $op eq 'form' ) {
             push @records, $biblio;
         } else {
             # Retrieve authority information
-            my $authority = Koha::Authority->get_from_authid( $record_id );
+            my $authority = Koha::MetadataRecord::Authority->get_from_authid( $record_id );
             unless ( $authority ) {
                 push @messages, {
                     type => 'warning',
@@ -213,7 +213,7 @@ if ( $op eq 'form' ) {
             # Authorities
             my $authid = $record_id;
             my $error = eval {
-                my $authority = Koha::Authority->get_from_authid( $authid );
+                my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
                 my $record = $authority->record;
                 ModifyRecordWithTemplate( $mmtid, $record );
                 ModAuthority( $authid, $record, GetAuthTypeCode( $authid ) );
index 0186eaf..7d96834 100755 (executable)
@@ -168,7 +168,7 @@ if ( $op eq "export" ) {
                         : (),
                     ( $authtype ? ( authtypecode => $authtype ) : () ),
                 };
-                # Koha::Authority is not a Koha::Object...
+                # Koha::MetadataRecord::Authority is not a Koha::Object...
                 my $authorities = Koha::Database->new->schema->resultset('AuthHeader')->search( $conditions );
                 @record_ids = map { $_->authid } $authorities->all;
             }