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