Bug 19040: Refactor GetMarcBiblio parameters
[koha.git] / misc / migration_tools / import_lexile.pl
1 #!/usr/bin/perl
2 #-----------------------------------
3 # Copyright 2015 ByWater Solutions
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #-----------------------------------
20
21 =head1 NAME
22
23 import_lexile.pl  Import lexile scores for records from csv.
24
25 =cut
26
27 use utf8;
28
29 use Modern::Perl;
30
31 use Getopt::Long;
32 use Text::CSV;
33
34 use C4::Context;
35 use C4::Biblio;
36 use C4::Koha qw( GetVariationsOfISBN );
37
38 use Koha::Biblios;
39 use Koha::Database;
40
41 binmode STDOUT, ':encoding(UTF-8)';
42
43 BEGIN {
44
45     # find Koha's Perl modules
46     # test carefully before changing this
47     use FindBin;
48     eval { require "$FindBin::Bin/../kohalib.pl" };
49 }
50
51 my $help;
52 my $confirm;
53 my $test;
54 my $file;
55 my $verbose;
56 my $start;
57 my $end;
58 my $field_number                  = "521";
59 my $subfield_target_audience_note = "a";
60 my $subfield_source               = "b";
61 my $subfield_source_value         = "Lexile";
62
63 GetOptions(
64     'h|help'                 => \$help,
65     'c|confirm'              => \$confirm,
66     't|test'                 => \$test,
67     'f|file=s'               => \$file,
68     'v|verbose+'             => \$verbose,
69     's|start=s'              => \$start,
70     'e|end=s'                => \$end,
71     'field=s'                => \$field_number,
72     'target-audience-note=s' => $subfield_target_audience_note,
73     'source=s'               => $subfield_source,
74     'source-value=s'         => $subfield_source_value,
75 );
76
77 my $usage = << 'ENDUSAGE';
78 import_lexile.pl: Import lexile scores for records from csv.
79
80 import_lexile.pl -f /path/to/LexileTitles.txt
81
82 This script takes the following parameters :
83
84     -h --help               Display this help
85     -c --confirm            Confirms you want to really run this script ( otherwise print help )
86     -t --test               Runs the script in test mode ( no changes will be made to your database )
87     -f --file               CSV file of lexile scores ( acquired from Lexile.com )
88     -v --verbose            Print data on found matches. Use -v -v for more data, and -v -v -v will give the most data.
89     --field                 Defines the field number for the Lexile data ( default: 521 )
90     --target-audience-note  Defines the subfield for the lexile score ( default: a )
91     --source                Defines the "Source" subfield ( default: b )
92     --source-value          Defines the value to put stored in the "Source" subfield ( default: "Lexile" )
93
94     The CSV file must have the following columns ( with the first line being the column headers ) in tab delimited format:
95     Title, Author, ISBN, ISBN13, Lexile
96
97 ENDUSAGE
98
99 if ( $help || !$file || !$confirm ) {
100     say $usage;
101     exit(1);
102 }
103
104 my $schema = Koha::Database->new()->schema();
105
106 my $csv = Text::CSV->new( { binary => 1, sep_char => "\t" } )
107   or die "Cannot use CSV: " . Text::CSV->error_diag();
108
109 open my $fh, "<:encoding(utf8)", $file or die "test.csv: $!";
110
111 my $column_names = $csv->getline($fh);
112 $csv->column_names(@$column_names);
113
114 my $counter = 0;
115 my $i       = 0;
116 while ( my $row = $csv->getline_hr($fh) ) {
117     $i++;
118
119     next if ( $start && $i < $start );
120     last if ( $end   && $i >= $end );
121
122     if ( $verbose > 1 ) {
123         say "Searching for matching record for row $i...";
124         say "Title: " . $row->{Title};
125         say "Author: " . $row->{Author};
126         say "ISBN10: " . $row->{ISBN};
127         say "ISBN13: " . $row->{ISBN13};
128         say q{};
129     }
130
131     # Match by ISBN
132     my @isbns;
133     for ( 'ISBN', 'ISBN13' ) {
134         if ( $row->{$_} && $row->{$_} ne "None" ) {
135             push( @isbns, $row->{$_} );
136             eval { push( @isbns, GetVariationsOfISBN( $row->{$_} ) ) };
137         }
138     }
139     @isbns = grep( $_, @isbns );
140     next unless @isbns;
141
142     say "Searching for ISBNs: " . join( ' : ', @isbns ) if ( $verbose > 2 );
143
144     my @likes = map { { isbn => { like => '%' . $_ . '%' } } } @isbns;
145
146     my @biblionumbers =
147       $schema->resultset('Biblioitem')->search( { -or => \@likes } )
148       ->get_column('biblionumber')->all();
149
150     say "Found matching records! Biblionumbers: " . join( " ,", @biblionumbers )
151       if ( @biblionumbers && $verbose > 2 );
152
153     foreach my $biblionumber (@biblionumbers) {
154         $counter++;
155         my $record = GetMarcBiblio({ biblionumber => $biblionumber });
156
157         if ($verbose) {
158             say "Found matching record! Biblionumber: $biblionumber";
159
160             if ( $verbose > 2 ) {
161                 my $biblio = Koha::Biblios->find( $biblionumber );
162                 say "Title from record: " . $biblio->title
163                   if $biblio->title;
164                 say "Author from record: " . $biblio->author
165                   if $biblio->author;
166                 say "ISBN from record: " . $biblio->biblioitem->isbn
167                   if $biblio->biblioitem->isbn;
168             }
169             say "Title: " . $row->{Title};
170             say "Author: " . $row->{Author};
171             say "ISBN10: " . $row->{ISBN};
172             say "ISBN13: " . $row->{ISBN13};
173             say q{};
174         }
175
176         # Check for existing embedded lexile score
177         my $lexile_score_field;
178         for my $field ( $record->field($field_number) ) {
179             if ( defined( $field->subfield($subfield_source) )
180                 && $field->subfield($subfield_source) eq
181                 $subfield_source_value )
182             {
183                 $lexile_score_field = $field;
184                 last;    # Each item can only have one lexile score
185             }
186         }
187
188         if ($lexile_score_field) {
189             $lexile_score_field->update(
190                 ind1                           => '8',
191                 ind2                           => '#',
192                 $subfield_target_audience_note => $row->{Lexile},
193                 $subfield_source               => $subfield_source_value,
194             );
195         }
196         else {
197             my $field = MARC::Field->new(
198                 $field_number, '8', '#',
199                 $subfield_target_audience_note => $row->{Lexile},
200                 $subfield_source               => $subfield_source_value,
201             );
202             $record->append_fields($field);
203         }
204
205         ModBiblio( $record, $biblionumber ) unless ( $test );
206     }
207
208 }
209 say "Update $counter records" if $verbose;