Bug 28617: Remove kohalib.pl and rely on PERL5LIB
[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
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.
11 #
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.
16 #
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>.
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 qw( GetOptions );
32 use Text::CSV;
33
34 use Koha::Script;
35 use C4::Context;
36 use C4::Biblio qw( GetMarcBiblio ModBiblio );
37 use C4::Koha qw( GetVariationsOfISBN );
38
39 use Koha::Biblios;
40 use Koha::Database;
41
42 binmode STDOUT, ':encoding(UTF-8)';
43
44 my $help;
45 my $confirm;
46 my $test;
47 my $file;
48 my $verbose;
49 my $start;
50 my $end;
51 my $field_number                  = "521";
52 my $subfield_target_audience_note = "a";
53 my $subfield_source               = "b";
54 my $subfield_source_value         = "Lexile";
55
56 GetOptions(
57     'h|help'                 => \$help,
58     'c|confirm'              => \$confirm,
59     't|test'                 => \$test,
60     'f|file=s'               => \$file,
61     'v|verbose+'             => \$verbose,
62     's|start=s'              => \$start,
63     'e|end=s'                => \$end,
64     'field=s'                => \$field_number,
65     'target-audience-note=s' => $subfield_target_audience_note,
66     'source=s'               => $subfield_source,
67     'source-value=s'         => $subfield_source_value,
68 );
69
70 my $usage = << 'ENDUSAGE';
71 import_lexile.pl: Import lexile scores for records from csv.
72
73 import_lexile.pl -f /path/to/LexileTitles.txt
74
75 This script takes the following parameters :
76
77     -h --help               Display this help
78     -c --confirm            Confirms you want to really run this script ( otherwise print help )
79     -t --test               Runs the script in test mode ( no changes will be made to your database )
80     -f --file               CSV file of lexile scores ( acquired from Lexile.com )
81     -v --verbose            Print data on found matches. Use -v -v for more data, and -v -v -v will give the most data.
82     --field                 Defines the field number for the Lexile data ( default: 521 )
83     --target-audience-note  Defines the subfield for the lexile score ( default: a )
84     --source                Defines the "Source" subfield ( default: b )
85     --source-value          Defines the value to put stored in the "Source" subfield ( default: "Lexile" )
86
87     The CSV file must have the following columns ( with the first line being the column headers ) in tab delimited format:
88     Title, Author, ISBN, ISBN13, Lexile
89
90 ENDUSAGE
91
92 if ( $help || !$file || !$confirm ) {
93     say $usage;
94     exit(1);
95 }
96
97 my $schema = Koha::Database->new()->schema();
98
99 my $csv = Text::CSV->new( { binary => 1, sep_char => "\t" } )
100   or die "Cannot use CSV: " . Text::CSV->error_diag();
101
102 open my $fh, "<:encoding(utf8)", $file or die "test.csv: $!";
103
104 my $column_names = $csv->getline($fh);
105 $csv->column_names(@$column_names);
106
107 my $counter = 0;
108 my $i       = 0;
109 while ( my $row = $csv->getline_hr($fh) ) {
110     $i++;
111
112     next if ( $start && $i < $start );
113     last if ( $end   && $i >= $end );
114
115     if ( $verbose > 1 ) {
116         say "Searching for matching record for row $i...";
117         say "Title: " . $row->{Title};
118         say "Author: " . $row->{Author};
119         say "ISBN10: " . $row->{ISBN};
120         say "ISBN13: " . $row->{ISBN13};
121         say q{};
122     }
123
124     # Match by ISBN
125     my @isbns;
126     for ( 'ISBN', 'ISBN13' ) {
127         if ( $row->{$_} && $row->{$_} ne "None" ) {
128             push( @isbns, $row->{$_} );
129             eval { push( @isbns, GetVariationsOfISBN( $row->{$_} ) ) };
130         }
131     }
132     @isbns = grep( $_, @isbns );
133     next unless @isbns;
134
135     say "Searching for ISBNs: " . join( ' : ', @isbns ) if ( $verbose > 2 );
136
137     my @likes = map { { isbn => { like => '%' . $_ . '%' } } } @isbns;
138
139     my @biblionumbers =
140       $schema->resultset('Biblioitem')->search( { -or => \@likes } )
141       ->get_column('biblionumber')->all();
142
143     say "Found matching records! Biblionumbers: " . join( " ,", @biblionumbers )
144       if ( @biblionumbers && $verbose > 2 );
145
146     foreach my $biblionumber (@biblionumbers) {
147         $counter++;
148         my $record = GetMarcBiblio({ biblionumber => $biblionumber });
149
150         if ($verbose) {
151             say "Found matching record! Biblionumber: $biblionumber";
152
153             if ( $verbose > 2 ) {
154                 my $biblio = Koha::Biblios->find( $biblionumber );
155                 say "Title from record: " . $biblio->title
156                   if $biblio->title;
157                 say "Author from record: " . $biblio->author
158                   if $biblio->author;
159                 say "ISBN from record: " . $biblio->biblioitem->isbn
160                   if $biblio->biblioitem->isbn;
161             }
162             say "Title: " . $row->{Title};
163             say "Author: " . $row->{Author};
164             say "ISBN10: " . $row->{ISBN};
165             say "ISBN13: " . $row->{ISBN13};
166             say q{};
167         }
168
169         # Check for existing embedded lexile score
170         my $lexile_score_field;
171         for my $field ( $record->field($field_number) ) {
172             if ( defined( $field->subfield($subfield_source) )
173                 && $field->subfield($subfield_source) eq
174                 $subfield_source_value )
175             {
176                 $lexile_score_field = $field;
177                 last;    # Each item can only have one lexile score
178             }
179         }
180
181         if ($lexile_score_field) {
182             $lexile_score_field->update(
183                 ind1                           => '8',
184                 ind2                           => '#',
185                 $subfield_target_audience_note => $row->{Lexile},
186                 $subfield_source               => $subfield_source_value,
187             );
188         }
189         else {
190             my $field = MARC::Field->new(
191                 $field_number, '8', '#',
192                 $subfield_target_audience_note => $row->{Lexile},
193                 $subfield_source               => $subfield_source_value,
194             );
195             $record->append_fields($field);
196         }
197
198         ModBiblio( $record, $biblionumber, undef, { overlay_context => { source => 'import_lexile' } } ) unless ( $test );
199     }
200
201 }
202 say "Update $counter records" if $verbose;