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