1 package Koha::MetadataRecord::Authority;
3 # Copyright 2012 C & P Bibliography Services
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 Koha::MetadataRecord::Authority - class to encapsulate authority records in Koha
26 Object-oriented class that encapsulates authority records in Koha.
43 use base qw(Koha::MetadataRecord);
45 __PACKAGE__->mk_accessors(qw( authid authtypecode ));
49 my $auth = Koha::MetadataRecord::Authority->new($record);
51 Create a new Koha::MetadataRecord::Authority object based on the provided record.
56 my ( $class, $record, $params ) = @_;
59 my $self = $class->SUPER::new(
62 'schema' => lc C4::Context->preference("marcflavour"),
72 =head2 get_from_authid
74 my $auth = Koha::MetadataRecord::Authority->get_from_authid($authid);
76 Create the Koha::MetadataRecord::Authority object associated with the provided authid.
77 Note that this routine currently retrieves a MARC record because
78 authorities in Koha are MARC records by definition. This is an
79 unfortunate but unavoidable fact.
86 my $marcflavour = lc C4::Context->preference("marcflavour");
88 my $dbh=C4::Context->dbh;
89 my $sth=$dbh->prepare("select authtypecode, marcxml from auth_header where authid=?");
90 $sth->execute($authid);
91 my ($authtypecode, $marcxml) = $sth->fetchrow;
92 my $record=eval {MARC::Record->new_from_xml(StripNonXmlChars($marcxml),'UTF-8',
93 (C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")))};
95 $record->encoding('UTF-8');
97 my $self = $class->SUPER::new( { authid => $authid,
98 authtypecode => $authtypecode,
99 schema => $marcflavour,
100 record => $record });
106 =head2 get_from_breeding
108 my $auth = Koha::MetadataRecord::Authority->get_from_authid($authid);
110 Create the Koha::MetadataRecord::Authority object associated with the provided authid.
114 sub get_from_breeding {
116 my $import_record_id = shift;
117 my $marcflavour = lc C4::Context->preference("marcflavour");
119 my $dbh=C4::Context->dbh;
120 my $sth=$dbh->prepare("select marcxml from import_records where import_record_id=? and record_type='auth';");
121 $sth->execute($import_record_id);
122 my $marcxml = $sth->fetchrow;
123 my $record=eval {MARC::Record->new_from_xml(StripNonXmlChars($marcxml),'UTF-8',
124 (C4::Context->preference("marcflavour") eq "UNIMARC"?"UNIMARCAUTH":C4::Context->preference("marcflavour")))};
126 $record->encoding('UTF-8');
128 # NOTE: GuessAuthTypeCode has no business in Koha::MetadataRecord::Authority, which is an
129 # object-oriented class. Eventually perhaps there will be utility
130 # classes in the Koha:: namespace, but there are not at the moment,
131 # so this shim seems like the best option all-around.
132 require C4::AuthoritiesMarc;
133 my $authtypecode = C4::AuthoritiesMarc::GuessAuthTypeCode($record);
135 my $self = $class->SUPER::new( {
136 schema => $marcflavour,
137 authtypecode => $authtypecode,
138 record => $record });
144 sub authorized_heading {
146 if ($self->schema =~ m/marc/) {
147 return Koha::Util::MARC::getAuthorityAuthorizedHeading($self->record, $self->schema);
152 =head2 get_all_authorities_iterator
154 my $it = Koha::MetadataRecord::Authority->get_all_authorities_iterator();
156 This will provide an iterator object that will, one by one, provide the
157 Koha::MetadataRecord::Authority of each authority.
159 The iterator is a Koha::MetadataIterator object.
163 sub get_all_authorities_iterator {
164 my $database = Koha::Database->new();
165 my $schema = $database->schema();
167 $schema->resultset('AuthHeader')->search( { marcxml => { '!=', undef } },
168 { columns => [qw/ authid authtypecode marcxml /] } );
169 my $next_func = sub {
170 my $row = $rs->next();
172 my $authid = $row->authid;
173 my $authtypecode = $row->authtypecode;
174 my $marcxml = $row->marcxml;
177 MARC::Record->new_from_xml(
178 StripNonXmlChars($marcxml),
181 C4::Context->preference("marcflavour") eq "UNIMARC"
183 : C4::Context->preference("marcflavour")
187 confess "$@" if ($@);
188 $record->encoding('UTF-8');
190 # I'm not sure why we don't use the authtypecode from the database,
191 # but this is how the original code does it.
192 require C4::AuthoritiesMarc;
193 $authtypecode = C4::AuthoritiesMarc::GuessAuthTypeCode($record);
195 my $auth = __PACKAGE__->new( $record, { authid => $authid, id => $authid, authtypecode => $authtypecode } );
199 return Koha::MetadataIterator->new($next_func);