Bug 17600: Standardize our EXPORT_OK
[srvgit] / Koha / MetadataRecord / Authority.pm
index c3b3314..d16970a 100644 (file)
@@ -4,18 +4,18 @@ package Koha::MetadataRecord::Authority;
 #
 # 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 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.
+# 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.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 =head1 NAME
 
@@ -36,7 +36,7 @@ use warnings;
 use C4::Context;
 use MARC::Record;
 use MARC::File::XML;
-use C4::Charset;
+use C4::Charset qw( StripNonXmlChars );
 use Koha::Util::MARC;
 
 use base qw(Koha::MetadataRecord);
@@ -93,13 +93,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,
@@ -157,52 +150,72 @@ sub authorized_heading {
 
 =head2 get_all_authorities_iterator
 
-    my $it = Koha::MetadataRecord::Authority->get_all_authorities_iterator();
+    my $it = Koha::MetadataRecord::Authority->get_all_authorities_iterator(%options);
 
 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.
 
+Possible options are:
+
+=over 4
+
+=item C<slice>
+
+slice may be defined as a hash of two values: index and count. index
+is the slice number to process and count is total number of slices.
+With this information the iterator returns just the given slice of
+records instead of all.
+
+=back
+
 =cut
 
 sub get_all_authorities_iterator {
+    my ($self, %options) = @_;
+
+    my $search_terms = {
+        marcxml => { '!=', undef }
+    };
+    my ($slice_modulo, $slice_count);
+    if ($options{slice}) {
+        $slice_count = $options{slice}->{count};
+        $slice_modulo = $options{slice}->{index};
+        $search_terms = {
+            '-and' => [
+                %{$search_terms},
+                \[ 'mod(authid, ?) = ?', $slice_count, $slice_modulo ]
+            ]
+        };
+    }
+
+    my $search_options->{columns} = [qw/ authid /];
+    if ($options{desc}) {
+        $search_options->{order_by} = { -desc => 'authid' };
+    }
+
     my $database = Koha::Database->new();
     my $schema   = $database->schema();
     my $rs =
-      $schema->resultset('AuthHeader')->search( { marcxml => { '!=', undef } },
-        { columns => [qw/ authid authtypecode marcxml /] } );
+      $schema->resultset('AuthHeader')->search(
+        $search_terms,
+        $search_options);
     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);
+        # Warn and skip bad records, otherwise we break the loop
+        while (1) {
+            my $row = $rs->next();
+            return if !$row;
+
+            my $auth = __PACKAGE__->get_from_authid($row->authid);
+            if (!$auth) {
+                warn "Something went wrong reading record for authority $row->authid: $@\n";
+                next;
+            }
+            return $auth;
+        }
+    };
+    return Koha::MetadataIterator->new($next_func);
 }
 
 1;