Koha/misc/migration_tools/build_oai_sets.pl
Mark Tompsett e1b5fa657d Bug 14385: Squash of a lot of patches rebased
- Added missing GetHiddenItems parameter change case
     Without this prove t had a failure.

- Always use mocks, not set_preference

- Tweaks so t/db_dependent/00-strict.t passes
     There was a typo botcat vs borcat and borrowernumber was never
     defined. Grabbing from userenv, like other code does.

- Tweak t/db_dependent/Items.t to fully test changes
     This will test all the if structures fully in GetHiddenItemnumbers.
         prove t/db_dependent/Items.t

- Tweak borrower category code
     $borrower->{categorycode} on a Koha::Patron is not the
     same as $borrower->categorycode. Fixed error.

- Search was returning URLS for wrong interface
     There was one search context place wrong. Changed it to $is_opac
     as the logic for setting $is_opac was modified correctly.

- Corrected issues with category code.
     When a user isn't logged in, $borrower is undef and causes error
     when determining category code. Added conditional check.

- Properly trigger all changes in C4/Search.pm

- Fix QA Test tool failures
     C4/Search.pm had some tabs.

- Add some commenting to make sense of logic

- Refactor EmbedItemsInMarcBiblio parameters to hashref

- Trigger GetMarcBiblio's EmbedItemsInMarcBiblio call.
     prove t/db_dependent/Items.t

- Add missing test to trigger Koha/BiblioUtils/Iterator change

- Add borrower category overrides
     These files generally add borcat parameter to GetMarcBiblio.
     Others might include correction of filtering of items
     (opac-basket), or a comment as to why no changes were done
     (opac-search).

     In the case of opac-search, correcting the first FIXME will
     likely correct the OpacHiddenItems issues on tags. As such,
     that is beyond this bugs scope.

     Some code had loop optimizations and fixes made, like a
     'next unless $record' when the biblio shouldn't even be in
     the list.

- Modify opac-ISBDdetail and opac-MARCdetail
     Both files had similar logic. They were rearranged and
     optimized, so that both files would have practically identical
     initial blocks of code.

     Optimizations were possible, because GetMarcBiblio
     returns a filtered record, so that there is no double call
     (once in the opac-### file and once in GetMarcBiblio) to
     GetHiddenItemnumbers.

- Fix hiding in opac-tags
     opac/opac-tags.pl was not properly hiding.

     There is currently one known bug associated with tags left.
     If you have two biblios tagged by different people with the
     same tag, the opac-search will show the one you tagged that
     is supposed to be hidden, because tag searches work differently
     than regular searches. This is beyond the scope of this bug.
     See the FIXME's in opac/opac-search.pl

- Trigger the C4::ILSDI::Services changes
     prove t/db_dependent/ILSDI_Services.t

- Added missing 'my'

- Test C4/Labels/Label.pm changes

- Improve C4::Record::marcrecord2csv test cases

- Corrected opac-details searchResult call

- Fix breaking issues constraint in ITerator test

- Fix ILSDI_Services test when clubs with branch exist

- Rebased again!
- Rebased t/db_dependent/Items.t conflict.

The test plan is in comment #112 last I checked.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
2018-11-02 10:33:12 +00:00

169 lines
5.1 KiB
Perl
Executable file

#!/usr/bin/perl
# Copyright 2011 BibLibre
#
# 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>.
=head1 DESCRIPTION
This script build OAI-PMH sets (to be used by opac/oai.pl) according to sets
and mappings defined in Koha. It reads informations from oai_sets and
oai_sets_mappings, and then fill table oai_sets_biblios with builded infos.
=head1 USAGE
build_oai_sets.pl [-h] [-v] [-r] [-i] [-l LENGTH [-o OFFSET]]
-h Print help message;
-v Be verbose
-r Truncate table oai_sets_biblios before inserting new rows
-i Embed items informations, mandatory if you defined mappings
on item fields
-l LENGTH Process LENGTH biblios
-o OFFSET If LENGTH is defined, start processing from OFFSET
=cut
use Modern::Perl;
use MARC::Record;
use MARC::File::XML;
use List::MoreUtils qw/uniq/;
use Getopt::Std;
use C4::Context;
use C4::Charset qw/StripNonXmlChars/;
use C4::Biblio;
use C4::OAI::Sets;
my %opts;
$Getopt::Std::STANDARD_HELP_VERSION = 1;
my $go = getopts('vo:l:ihr', \%opts);
if(!$go or $opts{h}){
&print_usage;
exit;
}
my $verbose = $opts{v};
my $offset = $opts{o};
my $length = $opts{l};
my $embed_items = $opts{i};
my $reset = $opts{r};
my $dbh = C4::Context->dbh;
# Get OAI sets mappings
my $mappings = GetOAISetsMappings;
# Get all biblionumbers and marcxml
print "Retrieving biblios... " if $verbose;
my $query = qq{
SELECT biblionumber, metadata
FROM biblio_metadata
WHERE format='marcxml'
AND marcflavour = ?
};
if($length) {
$query .= "LIMIT $length";
if($offset) {
$query .= " OFFSET $offset";
}
}
my $sth = $dbh->prepare($query);
$sth->execute( C4::Context->preference('marcflavour') );
my $results = $sth->fetchall_arrayref({});
print "done.\n" if $verbose;
# Build lists of parents sets
my $sets = GetOAISets;
my $parentsets;
foreach my $set (@$sets) {
my $setSpec = $set->{'spec'};
while($setSpec =~ /^(.+):(.+)$/) {
my $parent = $1;
my $parent_set = GetOAISetBySpec($parent);
if($parent_set) {
push @{ $parentsets->{$set->{'id'}} }, $parent_set->{'id'};
$setSpec = $parent;
} else {
last;
}
}
}
my $num_biblios = scalar @$results;
my $i = 1;
my $sets_biblios = {};
foreach my $res (@$results) {
my $biblionumber = $res->{'biblionumber'};
my $marcxml = $res->{'metadata'};
if($verbose and $i % 1000 == 0) {
my $percent = ($i * 100) / $num_biblios;
$percent = sprintf("%.2f", $percent);
say "Progression: $i/$num_biblios ($percent %)";
}
# The following lines are copied from GetMarcBiblio
# We don't call GetMarcBiblio to avoid a sql query to be executed each time
$marcxml = StripNonXmlChars($marcxml);
MARC::File::XML->default_record_format(C4::Context->preference('marcflavour'));
my $record;
eval {
$record = MARC::Record::new_from_xml($marcxml, "utf8", C4::Context->preference('marcflavour'));
};
if($@) {
warn "(biblio $biblionumber) Error while creating record from marcxml: $@";
next;
}
if($embed_items) {
C4::Biblio::EmbedItemsInMarcBiblio({
marc_record => $record,
biblionumber => $biblionumber });
}
my @biblio_sets = CalcOAISetsBiblio($record, $mappings);
foreach my $set_id (@biblio_sets) {
push @{ $sets_biblios->{$set_id} }, $biblionumber;
foreach my $parent_set_id ( @{ $parentsets->{$set_id} } ) {
push @{ $sets_biblios->{$parent_set_id} }, $biblionumber;
}
}
$i++;
}
say "Progression: done." if $verbose;
say "Summary:";
foreach my $set_id (keys %$sets_biblios) {
$sets_biblios->{$set_id} = [ uniq @{ $sets_biblios->{$set_id} } ];
my $set = GetOAISet($set_id);
my $setSpec = $set->{'spec'};
say "Set '$setSpec': ". scalar(@{$sets_biblios->{$set_id}}) ." biblios";
}
print "Updating database... ";
if($reset) {
ModOAISetsBiblios( {} );
}
AddOAISetsBiblios($sets_biblios);
print "done.\n";
sub print_usage {
print "build_oai_sets.pl: Build OAI-PMH sets, according to mappings defined in Koha\n";
print "Usage: build_oai_sets.pl [-h] [-v] [-i] [-l LENGTH [-o OFFSET]]\n\n";
print "\t-h\t\tPrint this help and exit\n";
print "\t-v\t\tBe verbose\n";
print "\t-i\t\tEmbed items informations, mandatory if you defined mappings on item fields\n";
print "\t-l LENGTH\tProcess LENGTH biblios\n";
print "\t-o OFFSET\tIf LENGTH is defined, start processing from OFFSET\n\n";
}