2 #-----------------------------------
3 # Copyright 2013 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 2 of the License, or (at your option) any later
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 #-----------------------------------
23 import_lexile.pl Import lexile scores for records from csv.
36 use C4::Koha qw( GetVariationsOfISBN );
39 binmode STDOUT, ':utf8';
43 # find Koha's Perl modules
44 # test carefully before changing this
46 eval { require "$FindBin::Bin/../kohalib.pl" };
53 my $field_number = "521";
54 my $subfield_target_audience_note = "a";
55 my $subfield_source = "b";
56 my $subfield_source_value = "Lexile";
60 'v|verbose+' => \$verbose,
61 's|start=s' => \$start,
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,
69 my $usage = << 'ENDUSAGE';
70 import_lexile.pl: Import lexile scores for records from csv.
72 import_lexile.pl -f /path/to/LexileTitles.txt
74 This script takes the following parameters :
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" )
90 my $schema = Koha::Database->new()->schema();
92 my $csv = Text::CSV->new( { binary => 1, sep_char => "\t" } )
93 or die "Cannot use CSV: " . Text::CSV->error_diag();
95 open my $fh, "<:encoding(utf8)", $file or die "test.csv: $!";
97 my $column_names = $csv->getline($fh);
98 $csv->column_names(@$column_names);
102 while ( my $row = $csv->getline_hr($fh) ) {
105 next if ( $start && $i < $start );
106 last if ( $end && $i >= $end );
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};
119 for ( 'ISBN', 'ISBN13' ) {
120 if ( $row->{$_} && $row->{$_} ne "None" ) {
121 push( @isbns, $row->{$_} );
122 eval { push( @isbns, GetVariationsOfISBN( $row->{$_} ) ) };
125 @isbns = grep( $_, @isbns );
128 say "Searching for ISBNs: " . join( ' : ', @isbns ) if ( $verbose > 2 );
130 my @likes = map { { isbn => { like => '%' . $_ . '%' } } } @isbns;
133 $schema->resultset('Biblioitem')->search( -or => \@likes )
134 ->get_column('biblionumber')->all();
136 say "Found matching records! Biblionumbers: " . join( " ,", @biblionumbers )
137 if ( @biblionumbers && $verbose > 2 );
139 foreach my $biblionumber (@biblionumbers) {
141 my $record = GetMarcBiblio($biblionumber);
144 say "Found matching record! Biblionumber: $biblionumber";
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} );
155 say "Title: " . $row->{Title};
156 say "Author: " . $row->{Author};
157 say "ISBN10: " . $row->{ISBN};
158 say "ISBN13: " . $row->{ISBN13};
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 )
169 $lexile_score_field = $field;
170 last; # Each item can only have one lexile score
174 if ($lexile_score_field) {
175 $lexile_score_field->update(
178 $subfield_target_audience_note => $row->{Lexile},
179 $subfield_source => $subfield_source_value,
183 my $field = MARC::Field->new(
184 $field_number, '8', '#',
185 $subfield_target_audience_note => $row->{Lexile},
186 $subfield_source => $subfield_source_value,
188 $record->append_fields($field);
191 ModBiblio( $record, $biblionumber );
195 say "Update $counter records" if $verbose;