Bug 12478: fix some issues on rebase
authorRobin Sheat <robin@catalyst.net.nz>
Wed, 30 Sep 2015 02:46:18 +0000 (15:46 +1300)
committerBrendan Gallagher <brendan@bywatersolutions.com>
Tue, 26 Apr 2016 20:20:09 +0000 (20:20 +0000)
There were rebase conflicts that it was just easier to postpone until
afterwards.

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Jesse Weaver <jweaver@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Brendan Gallagher <brendan@bywatersolutions.com>
Koha/Biblio/Iterator.pm [deleted file]
Koha/BiblioUtils.pm
Koha/BiblioUtils/Iterator.pm [new file with mode: 0644]
installer/data/mysql/updatedatabase.pl
misc/search_tools/rebuild_elastic_search.pl
t/db_dependent/Koha/Biblio.t [deleted file]
t/db_dependent/Koha/BiblioUtils.t [new file with mode: 0755]

diff --git a/Koha/Biblio/Iterator.pm b/Koha/Biblio/Iterator.pm
deleted file mode 100644 (file)
index fc150f4..0000000
+++ /dev/null
@@ -1,126 +0,0 @@
-package Koha::Biblio::Iterator;
-
-# This contains an iterator over biblio records
-
-# Copyright 2014 Catalyst IT
-#
-# 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::Biblio::Iterator - iterates over biblios provided by a DBIx::Class::ResultSet
-
-=head1 DESCRIPTION
-
-This provides an iterator that gives the MARC::Record of each biblio that's
-returned by a L<DBIx::Class::ResultSet> that provides a C<biblionumber>, and
-C<marc> or C<marcxml> column from the biblioitems table.
-
-=head1 SYNOPSIS
-
-  use Koha::Biblio::Iterator;
-  my $rs = $schema->resultset('biblioitems');
-  my $iterator = Koha::Biblio::Iterator->new($rs);
-  while (my $record = $iterator->next()) {
-      // do something with $record
-  }
-
-=head1 METHODS
-
-=cut
-
-use C4::Biblio;    # :( - for EmbedItemsInMarcBiblio
-
-use Carp;
-use MARC::Record;
-use MARC::File::XML;
-use Modern::Perl;
-
-=head2 new
-
-    my $it = new($sth, option => $value, ...);
-
-Takes a ResultSet to iterate over, and gives you an iterator on it. Optional
-options may be specified.
-
-=head3 Options
-
-=over 4
-
-=item items
-
-Set to true to include item data in the resulting MARC record.
-
-=back
-
-=cut
-
-sub new {
-    my ( $class, $rs, %options ) = @_;
-
-    bless {
-        rs => $rs,
-        %options,
-    }, $class;
-}
-
-=head2 next()
-
-In a scalar context, provides the next MARC::Record from the ResultSet, or
-C<undef> if there are no more.
-
-In a list context it will provide ($biblionumber, $record).
-
-=cut
-
-sub next {
-    my ($self) = @_;
-
-    my $marc;
-    my $row = $self->{rs}->next();
-    return if !$row;
-    if ( $row->marc ) {
-        $marc = MARC::Record->new_from_usmarc( $row->marc );
-    }
-    elsif ( $row->marcxml ) {
-        $marc = MARC::Record->new_from_xml( $row->marcxml );
-    }
-    else {
-        confess "No marc or marcxml column returned in the request.";
-    }
-
-    my $bibnum;
-    if ( $self->{items} ) {
-        $bibnum = $row->get_column('biblionumber');
-        confess "No biblionumber column returned in the request."
-          if ( !defined($bibnum) );
-
-        # TODO this should really be in Koha::Biblio or something similar.
-        C4::Biblio::EmbedItemsInMarcBiblio( $marc, $bibnum );
-    }
-
-    if (wantarray) {
-        $bibnum //= $row->get_column('biblionumber');
-        confess "No biblionumber column returned in the request."
-          if ( !defined($bibnum) );
-        return ( $bibnum, $marc );
-    }
-    else {
-        return $marc;
-    }
-}
-
-1;
index 58ae55c..a136132 100644 (file)
@@ -1,4 +1,4 @@
-package Koha::BiblioUtils;
+package Koha::BiblioUtilsUtils;
 
 # This contains functions to do with managing biblio records.
 
@@ -21,36 +21,90 @@ package Koha::BiblioUtils;
 
 =head1 NAME
 
-Koha::BiblioUtils - contains some handy biblio-related functions
+Koha::BiblioUtils - contains fundamental biblio-related functions
 
 =head1 DESCRIPTION
 
-This contains functions for operations on biblio records.
+This contains functions for normal operations on biblio records.
 
-Note: really, C4::Biblio does the main functions, but the Koha namespace is
+Note: really, C4::BiblioUtils does the main functions, but the Koha namespace is
 the new thing that should be used.
 
 =cut
 
-use C4::Biblio; # EmbedItemsInMarcBiblio
-use Koha::Biblio::Iterator;
+use C4::BiblioUtils; # EmbedItemsInMarcBiblio
+use Koha::MetadataIterator;
 use Koha::Database;
 use Modern::Perl;
 
-use base qw(Class::Accessor);
+use base qw(Koha::MetadataRecord);
 
-__PACKAGE__->mk_accessors(qw());
+__PACKAGE__->mk_accessors(qw( record schema idnumber datatype ));
 
 =head1 FUNCTIONS
 
+=head2 new
+
+    my $biblio = Koha::BiblioUtils->new($marc_record, [$biblionumber]);
+
+Creates an instance of C<Koha::BiblioUtils> based on the marc record. If known,
+the biblionumber can be provided too.
+
+=cut
+
+sub new {
+    my $class = shift;
+    my $record = shift;
+    my $biblionumber = shift;
+
+    my $self = $class->SUPER::new(
+        {
+            'record'   => $record,
+            'schema'   => lc C4::Context->preference("marcflavour"),
+            'idnumber' => $biblionumber,
+            'datatype' => 'biblio',
+        }
+    );
+    bless $self, $class;
+    return $self;
+}
+
+=head2 get_from_biblionumber
+
+    my $biblio = Koha::BiblioUtils->get_from_biblionumber($biblionumber, %options);
+
+This will give you an instance of L<Koha::BiblioUtils> that is the biblio that
+you requested.
+
+Options are:
+
+=over 4
+
+=item C<$item_data>
+
+If true, then the item data will be merged into the record when it's loaded.
+
+=back
+
+It will return C<undef> if the biblio doesn't exist.
+
+=cut
+
+sub get_from_biblionumber {
+    my ($class, $bibnum, %options) = @_;
+
+    my $marc = $class->get_marc_biblio($bibnum, %options);
+    return $class->new($marc, $bibnum);
+}
+
 =head2 get_all_biblios_iterator
 
-    my $it = get_all_biblios_iterator();
+    my $it = Koha::BiblioUtils->get_all_biblios_iterator();
 
 This will provide an iterator object that will, one by one, provide the
-MARC::Record of each biblio. This will include the item data.
+Koha::BiblioUtils of each biblio. This will include the item data.
 
-The iterator is a Koha::Biblio::Iterator object.
+The iterator is a Koha::MetadataIterator object.
 
 =cut
 
@@ -58,17 +112,24 @@ sub get_all_biblios_iterator {
     my $database = Koha::Database->new();
     my $schema   = $database->schema();
     my $rs =
-      $schema->resultset('Biblioitem')->search( { marc => { '!=', undef } },
-        { columns => [qw/ biblionumber marc /] } );
-    return Koha::Biblio::Iterator->new($rs, items => 1);
+      $schema->resultset('Biblio')->search( {},
+        { columns => [qw/ biblionumber /] } );
+    my $next_func = sub {
+        my $row = $rs->next();
+        return undef if !$row;
+        my $marc = C4::Biblio::GetMarcBiblio( $row->biblionumber, 1 );
+        return __PACKAGE__->new($marc, $row->biblionumber);
+    };
+    return Koha::MetadataIterator->new($next_func);
 }
 
 =head2 get_marc_biblio
 
-    my $marc = get_marc_biblio($bibnum, %options);
+    my $marc = Koha::BiblioUtils->get_marc_biblio($bibnum, %options);
 
-This fetches the MARC::Record for the given biblio number. Nothing is returned
-if the biblionumber couldn't be found (or it somehow has no MARC data.)
+This non-class function fetches the MARC::Record for the given biblio number.
+Nothing is returned if the biblionumber couldn't be found (or it somehow has no
+MARC data.)
 
 Options are:
 
@@ -83,23 +144,9 @@ If set to true, item data is embedded in the record. Default is to not do this.
 =cut
 
 sub get_marc_biblio {
-    my ($class,$bibnum, %options) = @_;
-
-    my $database = Koha::Database->new();
-    my $schema   = $database->schema();
-    my $rs =
-      $schema->resultset('Biblioitem')
-      ->search( { marc => { '!=', undef }, biblionumber => $bibnum },
-        { columns => [qw/ marc /] } );
-
-    my $row = $rs->next();
-    return unless $row;
-    my $marc = MARC::Record->new_from_usmarc($row->marc);
-
-    # TODO implement this in this module
-    C4::Biblio::EmbedItemsInMarcBiblio($marc, $bibnum) if $options{item_data};
+    my ($class, $bibnum, %options) = @_;
 
-    return $marc;
+    return C4::Biblio::GetMarcBiblio( $bibnum, ($options{item_data} ? 1 : 0 ) );
 }
 
 1;
diff --git a/Koha/BiblioUtils/Iterator.pm b/Koha/BiblioUtils/Iterator.pm
new file mode 100644 (file)
index 0000000..bc00036
--- /dev/null
@@ -0,0 +1,126 @@
+package Koha::BiblioUtils::Iterator;
+
+# This contains an iterator over biblio records
+
+# Copyright 2014 Catalyst IT
+#
+# 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::BiblioUtils::Iterator - iterates over biblios provided by a DBIx::Class::ResultSet
+
+=head1 DESCRIPTION
+
+This provides an iterator that gives the MARC::Record of each biblio that's
+returned by a L<DBIx::Class::ResultSet> that provides a C<biblionumber>, and
+C<marc> or C<marcxml> column from the biblioitems table.
+
+=head1 SYNOPSIS
+
+  use Koha::BiblioUtils::Iterator;
+  my $rs = $schema->resultset('biblioitems');
+  my $iterator = Koha::BiblioUtils::Iterator->new($rs);
+  while (my $record = $iterator->next()) {
+      // do something with $record
+  }
+
+=head1 METHODS
+
+=cut
+
+use C4::Biblio;    # :( - for EmbedItemsInMarcBiblio
+
+use Carp;
+use MARC::Record;
+use MARC::File::XML;
+use Modern::Perl;
+
+=head2 new
+
+    my $it = new($sth, option => $value, ...);
+
+Takes a ResultSet to iterate over, and gives you an iterator on it. Optional
+options may be specified.
+
+=head3 Options
+
+=over 4
+
+=item items
+
+Set to true to include item data in the resulting MARC record.
+
+=back
+
+=cut
+
+sub new {
+    my ( $class, $rs, %options ) = @_;
+
+    bless {
+        rs => $rs,
+        %options,
+    }, $class;
+}
+
+=head2 next()
+
+In a scalar context, provides the next MARC::Record from the ResultSet, or
+C<undef> if there are no more.
+
+In a list context it will provide ($biblionumber, $record).
+
+=cut
+
+sub next {
+    my ($self) = @_;
+
+    my $marc;
+    my $row = $self->{rs}->next();
+    return if !$row;
+    if ( $row->marc ) {
+        $marc = MARC::Record->new_from_usmarc( $row->marc );
+    }
+    elsif ( $row->marcxml ) {
+        $marc = MARC::Record->new_from_xml( $row->marcxml );
+    }
+    else {
+        confess "No marc or marcxml column returned in the request.";
+    }
+
+    my $bibnum;
+    if ( $self->{items} ) {
+        $bibnum = $row->get_column('biblionumber');
+        confess "No biblionumber column returned in the request."
+          if ( !defined($bibnum) );
+
+        # TODO this should really be in Koha::BiblioUtils or something similar.
+        C4::Biblio::EmbedItemsInMarcBiblio( $marc, $bibnum );
+    }
+
+    if (wantarray) {
+        $bibnum //= $row->get_column('biblionumber');
+        confess "No biblionumber column returned in the request."
+          if ( !defined($bibnum) );
+        return ( $bibnum, $marc );
+    }
+    else {
+        return $marc;
+    }
+}
+
+1;
index 0b844d8..f7ca13d 100755 (executable)
@@ -10355,7 +10355,7 @@ if ( CheckVersion($DBversion) ) {
     |);
 
     print "Upgrade to $DBversion done (Bug 8007: Add System Preferences useDischarge, the discharge notice and the new table discharges)\n";
-    SetVersion ($DBversion);
+    SetVersion($DBversion);
 }
 
 $DBversion = "3.19.00.036";
index 804bb64..e8eabc7 100755 (executable)
@@ -84,7 +84,7 @@ Full documentation.
 use autodie;
 use Getopt::Long;
 use Koha::Authority;
-use Koha::Biblio;
+use Koha::BiblioUtils;
 use Koha::ElasticSearch::Indexer;
 use MARC::Field;
 use MARC::Record;
@@ -125,10 +125,10 @@ if ($index_biblios) {
         $next = sub {
             my $r = shift @biblionumbers;
             return () unless defined $r;
-            return ($r, Koha::Biblio->get_from_biblionumber($r, item_data => 1 ));
+            return ($r, Koha::BiblioUtils->get_from_biblionumber($r, item_data => 1 ));
         };
     } else {
-        my $records = Koha::Biblio->get_all_biblios_iterator();
+        my $records = Koha::BiblioUtils->get_all_biblios_iterator();
         $next = sub {
             $records->next();
         }
diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t
deleted file mode 100755 (executable)
index b247d8b..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-#!/usr/bin/perl
-#
-# Copyright 2014 Catalyst IT
-#
-# 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, see <http://www.gnu.org/licenses>.
-
-use Modern::Perl;
-
-use C4::Context;
-use C4::Biblio qw( AddBiblio );
-use Koha::Database;
-use Koha::Libraries;
-use Koha::Patrons;
-
-use Test::More tests => 4;
-
-use_ok('Koha::Biblio');
-use_ok('Koha::Biblios');
-
-my $schema = Koha::Database->new()->schema();
-$schema->storage->txn_begin();
-
-my $dbh = C4::Context->dbh;
-$dbh->{RaiseError} = 1;
-
-my @branches = Koha::Libraries->search();
-my $borrower = Koha::Patrons->search()->next();
-
-my $biblio = MARC::Record->new();
-$biblio->append_fields(
-    MARC::Field->new( '100', ' ', ' ', a => 'Hall, Kyle' ),
-    MARC::Field->new( '245', ' ', ' ', a => "Test Record", b => "Test Record Subtitle", b => "Another Test Record Subtitle" ),
-);
-my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
-
-my $field_mappings = Koha::Database->new()->schema()->resultset('Fieldmapping');
-$field_mappings->delete();
-$field_mappings->create( { field => 'subtitle', fieldcode => '245', subfieldcode => 'b' } );
-
-$biblio = Koha::Biblios->find( $biblionumber );
-my @subtitles = $biblio->subtitles();
-is( $subtitles[0], 'Test Record Subtitle', 'Got first subtitle correctly' );
-is( $subtitles[1], 'Another Test Record Subtitle', 'Got second subtitle correctly' );
-
-$schema->storage->txn_rollback();
-
-1;
diff --git a/t/db_dependent/Koha/BiblioUtils.t b/t/db_dependent/Koha/BiblioUtils.t
new file mode 100755 (executable)
index 0000000..b247d8b
--- /dev/null
@@ -0,0 +1,60 @@
+#!/usr/bin/perl
+#
+# Copyright 2014 Catalyst IT
+#
+# 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, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use C4::Context;
+use C4::Biblio qw( AddBiblio );
+use Koha::Database;
+use Koha::Libraries;
+use Koha::Patrons;
+
+use Test::More tests => 4;
+
+use_ok('Koha::Biblio');
+use_ok('Koha::Biblios');
+
+my $schema = Koha::Database->new()->schema();
+$schema->storage->txn_begin();
+
+my $dbh = C4::Context->dbh;
+$dbh->{RaiseError} = 1;
+
+my @branches = Koha::Libraries->search();
+my $borrower = Koha::Patrons->search()->next();
+
+my $biblio = MARC::Record->new();
+$biblio->append_fields(
+    MARC::Field->new( '100', ' ', ' ', a => 'Hall, Kyle' ),
+    MARC::Field->new( '245', ' ', ' ', a => "Test Record", b => "Test Record Subtitle", b => "Another Test Record Subtitle" ),
+);
+my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
+
+my $field_mappings = Koha::Database->new()->schema()->resultset('Fieldmapping');
+$field_mappings->delete();
+$field_mappings->create( { field => 'subtitle', fieldcode => '245', subfieldcode => 'b' } );
+
+$biblio = Koha::Biblios->find( $biblionumber );
+my @subtitles = $biblio->subtitles();
+is( $subtitles[0], 'Test Record Subtitle', 'Got first subtitle correctly' );
+is( $subtitles[1], 'Another Test Record Subtitle', 'Got second subtitle correctly' );
+
+$schema->storage->txn_rollback();
+
+1;