Bug 22723: Correct syntax error on confess call in Koha/MetadataRecord/Authority.pm
[koha-equinox.git] / Koha / MetadataRecord / Authority.pm
index da218af..062d19c 100644 (file)
@@ -33,6 +33,7 @@ Authority data.
 
 use strict;
 use warnings;
+use Carp;
 use C4::Context;
 use MARC::Record;
 use MARC::File::XML;
@@ -52,13 +53,14 @@ Create a new Koha::MetadataRecord::Authority object based on the provided record
 =cut
 
 sub new {
-    my $class = shift;
-    my $record = shift;
+    my ( $class, $record, $params ) = @_;
 
+    $params //= {};
     my $self = $class->SUPER::new(
         {
             'record' => $record,
-            'schema' => lc C4::Context->preference("marcflavour")
+            'schema' => lc C4::Context->preference("marcflavour"),
+            %$params,
         }
     );
 
@@ -92,13 +94,6 @@ sub get_from_authid {
     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,
                                      authtypecode => $authtypecode,
                                      schema => $marcflavour,
@@ -154,4 +149,54 @@ sub authorized_heading {
     return;
 }
 
+=head2 get_all_authorities_iterator
+
+    my $it = Koha::MetadataRecord::Authority->get_all_authorities_iterator();
+
+This will provide an iterator object that will, one by one, provide the
+Koha::MetadataRecord::Authority of each authority.
+
+The iterator is a Koha::MetadataIterator object.
+
+=cut
+
+sub get_all_authorities_iterator {
+    my $database = Koha::Database->new();
+    my $schema   = $database->schema();
+    my $rs =
+      $schema->resultset('AuthHeader')->search( { marcxml => { '!=', undef } },
+        { columns => [qw/ authid authtypecode marcxml /] } );
+    my $next_func = sub {
+        my $row = $rs->next();
+        return if !$row;
+        my $authid       = $row->authid;
+        my $authtypecode = $row->authtypecode;
+        my $marcxml      = $row->marcxml;
+
+        my $record = eval {
+            MARC::Record->new_from_xml(
+                StripNonXmlChars($marcxml),
+                'UTF-8',
+                (
+                    C4::Context->preference("marcflavour") eq "UNIMARC"
+                    ? "UNIMARCAUTH"
+                    : C4::Context->preference("marcflavour")
+                )
+            );
+        };
+        confess "$@" if ($@);
+        $record->encoding('UTF-8');
+
+        # I'm not sure why we don't use the authtypecode from the database,
+        # but this is how the original code does it.
+        require C4::AuthoritiesMarc;
+        $authtypecode = C4::AuthoritiesMarc::GuessAuthTypeCode($record);
+
+        my $auth = __PACKAGE__->new( $record, { authid => $authid, id => $authid, authtypecode => $authtypecode } );
+
+        return $auth;
+      };
+      return Koha::MetadataIterator->new($next_func);
+}
+
 1;