From 011182420f3376eb5cb1422baba1f51d0d757e47 Mon Sep 17 00:00:00 2001 From: David Cook Date: Mon, 9 Dec 2024 05:01:19 +0000 Subject: [PATCH] Bug 37486: Only build OAI-PMH set for one set at a time This change adds a "-s" parameter to build_oai_sets.pl, so that you can selectively build OAI-PMH sets. This is useful when you have many sets, and you just need to build or rebuild 1 set. Test plan: 0. Apply the patch 1. Go to http://localhost:8081/cgi-bin/koha/admin/oai_sets.pl 2. Create setA and setB 3. Add mapping for setA: 245$a is equal to Gairm. 4. Add mapping for setB: 245$a is equal to Continuous delivery / 5. In the database, run the following SQL: select * from oai_sets; 6. Identify the ID for setB 7. Run the following command where XXXX is the ID for setB: perl misc/migration_tools/build_oai_sets.pl -v -s XXXX 8. Note that the only data added to the "oai_sets_biblios" table is for setB 9. prove t/db_dependent/OAI/Sets.t Signed-off-by: David Nind --- C4/OAI/Sets.pm | 25 ++++++++++++++++++++++--- misc/migration_tools/build_oai_sets.pl | 20 ++++++++++++++------ t/db_dependent/OAI/Sets.t | 8 +++++++- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/C4/OAI/Sets.pm b/C4/OAI/Sets.pm index a81183bb32..ff68fa81b8 100644 --- a/C4/OAI/Sets.pm +++ b/C4/OAI/Sets.pm @@ -68,12 +68,22 @@ The hash references looks like this: =cut sub GetOAISets { + my ($args) = @_; + my $where = ''; + my @bind = (); + if ($args){ + if ($args->{set_id}){ + $where = ' WHERE id = ? '; + push(@bind,$args->{set_id}); + } + } my $dbh = C4::Context->dbh; my $query = qq{ SELECT * FROM oai_sets + $where }; my $sth = $dbh->prepare($query); - $sth->execute; + $sth->execute(@bind); my $results = $sth->fetchall_arrayref({}); $query = qq{ @@ -330,12 +340,21 @@ The first hashref keys are the sets IDs, so it looks like this: =cut sub GetOAISetsMappings { + my ($args) = @_; + my $where = ''; + my @bind = (); + if ($args){ + if ($args->{set_id}){ + $where = " WHERE set_id = ? "; + push(@bind,$args->{set_id}); + } + } my $dbh = C4::Context->dbh; my $query = qq{ - SELECT * FROM oai_sets_mappings ORDER BY set_id, rule_order + SELECT * FROM oai_sets_mappings $where ORDER BY set_id, rule_order }; my $sth = $dbh->prepare($query); - $sth->execute; + $sth->execute(@bind); my $mappings = {}; while(my $result = $sth->fetchrow_hashref) { diff --git a/misc/migration_tools/build_oai_sets.pl b/misc/migration_tools/build_oai_sets.pl index 24b842b4de..8e276364f3 100755 --- a/misc/migration_tools/build_oai_sets.pl +++ b/misc/migration_tools/build_oai_sets.pl @@ -25,7 +25,7 @@ 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]] + build_oai_sets.pl [-h] [-v] [-r] [-i] [-l LENGTH [-o OFFSET]] [-s set_id] -h Print help message; -v Be verbose -r Truncate table oai_sets_biblios before inserting new rows @@ -33,6 +33,7 @@ oai_sets_mappings, and then fill table oai_sets_biblios with builded infos. on item fields -l LENGTH Process LENGTH biblios -o OFFSET If LENGTH is defined, start processing from OFFSET + -s set_id If set_id is defined, only add records for this OAI-PMH set =cut @@ -58,7 +59,7 @@ use Koha::Biblio::Metadata; my %opts; $Getopt::Std::STANDARD_HELP_VERSION = 1; -my $go = getopts('vo:l:ihr', \%opts); +my $go = getopts('vo:l:ihrs:', \%opts); if(!$go or $opts{h}){ &print_usage; @@ -70,11 +71,17 @@ my $offset = $opts{o}; my $length = $opts{l}; my $embed_items = $opts{i}; my $reset = $opts{r}; +my $set = $opts{s}; + +my $selective_set = {}; +if ($set){ + $selective_set->{set_id} = $set; +} my $dbh = C4::Context->dbh; # Get OAI sets mappings -my $mappings = GetOAISetsMappings; +my $mappings = GetOAISetsMappings($selective_set); # Get all biblionumbers and marcxml print "Retrieving biblios... " if $verbose; @@ -102,7 +109,7 @@ my $results = $sth->fetchall_arrayref({}); print "done.\n" if $verbose; # Build lists of parents sets -my $sets = GetOAISets; +my $sets = GetOAISets($selective_set); my $parentsets; foreach my $set (@$sets) { my $setSpec = $set->{'spec'}; @@ -179,10 +186,11 @@ 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 "Usage: build_oai_sets.pl [-h] [-v] [-i] [-l LENGTH [-o OFFSET]] [ -s set_id]\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"; + print "\t-o OFFSET\tIf LENGTH is defined, start processing from OFFSET\n"; + print "\t-s set_id\tIf set_id is defined, only add recods for this OAI-PMH set\n\n"; } diff --git a/t/db_dependent/OAI/Sets.t b/t/db_dependent/OAI/Sets.t index 327643f267..15a18f0b3d 100755 --- a/t/db_dependent/OAI/Sets.t +++ b/t/db_dependent/OAI/Sets.t @@ -18,7 +18,7 @@ use Modern::Perl; -use Test::More tests => 153; +use Test::More tests => 157; use Test::MockModule; use Test::Warn; use MARC::Record; @@ -144,6 +144,9 @@ is_deeply ($oai_sets->[1]->{descriptions}, ['descSet2'], 'description field is " ok(!defined($oai_sets->[2]), 'There are only 2 sets'); +my $selective_oai_sets = GetOAISets( { set_id => $oai_sets->[0]->{id} } ); +is( scalar @$selective_oai_sets, 1, 'Only 1 set retrieved via set_id' ); +is_deeply( $oai_sets->[0], $selective_oai_sets->[0], 'Correct OAI set retrieved using selective get' ); # ---------- Testing GetOAISet ------------------ ok (!defined(GetOAISet), 'GetOAISet without argument is undef'); @@ -330,6 +333,9 @@ is ($mappings->{$set2_id}->[0]->{marcsubfield}, 'c', 'marcsubfield field is "c"' is ($mappings->{$set2_id}->[0]->{operator}, 'equal', 'operator field is "equal"'); is ($mappings->{$set2_id}->[0]->{marcvalue}, 'myOtherMarcValue', 'marcvalue field is "myOtherMarcValue"'); +my $selective_mappings = GetOAISetsMappings( { set_id => $set1_id } ); +is( scalar keys %$selective_mappings, 1, 'Selective mappings only returns mappings for selected set_id' ); +is_deeply($selective_mappings->{$set1_id},$mappings->{$set1_id}, 'Correct mapping returned for selective mappings'); # ---------- Testing GetOAISetMappings ---------- ok (!defined(GetOAISetMappings), 'GetOAISetMappings without argument is undef'); -- 2.39.5