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