Bug 26922: Regression tests
[koha.git] / misc / migration_tools / build_oai_sets.pl
blobc7b73a2d7d1e92b03bcf13c3e82cba17fbd5c4da
1 #!/usr/bin/perl
3 # Copyright 2011 BibLibre
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 =head1 DESCRIPTION
22 This script build OAI-PMH sets (to be used by opac/oai.pl) according to sets
23 and mappings defined in Koha. It reads informations from oai_sets and
24 oai_sets_mappings, and then fill table oai_sets_biblios with builded infos.
26 =head1 USAGE
28 build_oai_sets.pl [-h] [-v] [-r] [-i] [-l LENGTH [-o OFFSET]]
29 -h Print help message;
30 -v Be verbose
31 -r Truncate table oai_sets_biblios before inserting new rows
32 -i Embed items informations, mandatory if you defined mappings
33 on item fields
34 -l LENGTH Process LENGTH biblios
35 -o OFFSET If LENGTH is defined, start processing from OFFSET
37 =cut
39 use Modern::Perl;
40 use MARC::Record;
41 use MARC::File::XML;
42 use List::MoreUtils qw/uniq/;
43 use Getopt::Std;
45 use Koha::Script;
46 use C4::Context;
47 use C4::Charset qw/StripNonXmlChars/;
48 use C4::Biblio;
49 use C4::OAI::Sets;
51 my %opts;
52 $Getopt::Std::STANDARD_HELP_VERSION = 1;
53 my $go = getopts('vo:l:ihr', \%opts);
55 if(!$go or $opts{h}){
56 &print_usage;
57 exit;
60 my $verbose = $opts{v};
61 my $offset = $opts{o};
62 my $length = $opts{l};
63 my $embed_items = $opts{i};
64 my $reset = $opts{r};
66 my $dbh = C4::Context->dbh;
68 # Get OAI sets mappings
69 my $mappings = GetOAISetsMappings;
71 # Get all biblionumbers and marcxml
72 print "Retrieving biblios... " if $verbose;
73 my $query = qq{
74 SELECT biblionumber, metadata
75 FROM biblio_metadata
76 WHERE format='marcxml'
77 AND `schema` = ?
78 UNION
79 SELECT biblionumber, metadata
80 FROM deletedbiblio_metadata
81 WHERE format='marcxml'
82 AND `schema` = ?
84 if($length) {
85 $query .= "LIMIT $length";
86 if($offset) {
87 $query .= " OFFSET $offset";
90 my $sth = $dbh->prepare($query);
91 $sth->execute( C4::Context->preference('marcflavour'), C4::Context->preference('marcflavour'));
92 my $results = $sth->fetchall_arrayref({});
93 print "done.\n" if $verbose;
95 # Build lists of parents sets
96 my $sets = GetOAISets;
97 my $parentsets;
98 foreach my $set (@$sets) {
99 my $setSpec = $set->{'spec'};
100 while($setSpec =~ /^(.+):(.+)$/) {
101 my $parent = $1;
102 my $parent_set = GetOAISetBySpec($parent);
103 if($parent_set) {
104 push @{ $parentsets->{$set->{'id'}} }, $parent_set->{'id'};
105 $setSpec = $parent;
106 } else {
107 last;
112 my $num_biblios = scalar @$results;
113 my $i = 1;
114 my $sets_biblios = {};
115 foreach my $res (@$results) {
116 my $biblionumber = $res->{'biblionumber'};
117 my $marcxml = $res->{'metadata'};
118 if($verbose and $i % 1000 == 0) {
119 my $percent = ($i * 100) / $num_biblios;
120 $percent = sprintf("%.2f", $percent);
121 say "Progression: $i/$num_biblios ($percent %)";
123 # The following lines are copied from GetMarcBiblio
124 # We don't call GetMarcBiblio to avoid a sql query to be executed each time
125 $marcxml = StripNonXmlChars($marcxml);
126 MARC::File::XML->default_record_format(C4::Context->preference('marcflavour'));
127 my $record;
128 eval {
129 $record = MARC::Record::new_from_xml($marcxml, "UTF-8", C4::Context->preference('marcflavour'));
131 if($@) {
132 warn "(biblio $biblionumber) Error while creating record from marcxml: $@";
133 next;
135 if($embed_items) {
136 C4::Biblio::EmbedItemsInMarcBiblio({
137 marc_record => $record,
138 biblionumber => $biblionumber });
141 my @biblio_sets = CalcOAISetsBiblio($record, $mappings);
142 foreach my $set_id (@biblio_sets) {
143 push @{ $sets_biblios->{$set_id} }, $biblionumber;
144 foreach my $parent_set_id ( @{ $parentsets->{$set_id} } ) {
145 push @{ $sets_biblios->{$parent_set_id} }, $biblionumber;
148 $i++;
150 say "Progression: done." if $verbose;
152 say "Summary:";
153 foreach my $set_id (keys %$sets_biblios) {
154 $sets_biblios->{$set_id} = [ uniq @{ $sets_biblios->{$set_id} } ];
155 my $set = GetOAISet($set_id);
156 my $setSpec = $set->{'spec'};
157 say "Set '$setSpec': ". scalar(@{$sets_biblios->{$set_id}}) ." biblios";
160 print "Updating database... ";
161 if($reset) {
162 ModOAISetsBiblios( {} );
164 AddOAISetsBiblios($sets_biblios);
165 print "done.\n";
167 sub print_usage {
168 print "build_oai_sets.pl: Build OAI-PMH sets, according to mappings defined in Koha\n";
169 print "Usage: build_oai_sets.pl [-h] [-v] [-i] [-l LENGTH [-o OFFSET]]\n\n";
170 print "\t-h\t\tPrint this help and exit\n";
171 print "\t-v\t\tBe verbose\n";
172 print "\t-i\t\tEmbed items informations, mandatory if you defined mappings on item fields\n";
173 print "\t-l LENGTH\tProcess LENGTH biblios\n";
174 print "\t-o OFFSET\tIf LENGTH is defined, start processing from OFFSET\n\n";