Bug 26922: Regression tests
[koha.git] / misc / migration_tools / import_lexile.pl
blob72347870a107ccdf1d54ada0fdca1b220e4e87d4
1 #!/usr/bin/perl
2 #-----------------------------------
3 # Copyright 2015 ByWater Solutions
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>.
19 #-----------------------------------
21 =head1 NAME
23 import_lexile.pl Import lexile scores for records from csv.
25 =cut
27 use utf8;
29 use Modern::Perl;
31 use Getopt::Long;
32 use Text::CSV;
34 use Koha::Script;
35 use C4::Context;
36 use C4::Biblio;
37 use C4::Koha qw( GetVariationsOfISBN );
39 use Koha::Biblios;
40 use Koha::Database;
42 binmode STDOUT, ':encoding(UTF-8)';
44 BEGIN {
46 # find Koha's Perl modules
47 # test carefully before changing this
48 use FindBin;
49 eval { require "$FindBin::Bin/../kohalib.pl" };
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";
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,
78 my $usage = << 'ENDUSAGE';
79 import_lexile.pl: Import lexile scores for records from csv.
81 import_lexile.pl -f /path/to/LexileTitles.txt
83 This script takes the following parameters :
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" )
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
98 ENDUSAGE
100 if ( $help || !$file || !$confirm ) {
101 say $usage;
102 exit(1);
105 my $schema = Koha::Database->new()->schema();
107 my $csv = Text::CSV->new( { binary => 1, sep_char => "\t" } )
108 or die "Cannot use CSV: " . Text::CSV->error_diag();
110 open my $fh, "<:encoding(utf8)", $file or die "test.csv: $!";
112 my $column_names = $csv->getline($fh);
113 $csv->column_names(@$column_names);
115 my $counter = 0;
116 my $i = 0;
117 while ( my $row = $csv->getline_hr($fh) ) {
118 $i++;
120 next if ( $start && $i < $start );
121 last if ( $end && $i >= $end );
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{};
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->{$_} ) ) };
140 @isbns = grep( $_, @isbns );
141 next unless @isbns;
143 say "Searching for ISBNs: " . join( ' : ', @isbns ) if ( $verbose > 2 );
145 my @likes = map { { isbn => { like => '%' . $_ . '%' } } } @isbns;
147 my @biblionumbers =
148 $schema->resultset('Biblioitem')->search( { -or => \@likes } )
149 ->get_column('biblionumber')->all();
151 say "Found matching records! Biblionumbers: " . join( " ,", @biblionumbers )
152 if ( @biblionumbers && $verbose > 2 );
154 foreach my $biblionumber (@biblionumbers) {
155 $counter++;
156 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
158 if ($verbose) {
159 say "Found matching record! Biblionumber: $biblionumber";
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;
170 say "Title: " . $row->{Title};
171 say "Author: " . $row->{Author};
172 say "ISBN10: " . $row->{ISBN};
173 say "ISBN13: " . $row->{ISBN13};
174 say q{};
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 )
184 $lexile_score_field = $field;
185 last; # Each item can only have one lexile score
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,
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,
203 $record->append_fields($field);
206 ModBiblio( $record, $biblionumber ) unless ( $test );
210 say "Update $counter records" if $verbose;