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
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 );
41 binmode STDOUT
, ':encoding(UTF-8)';
45 # find Koha's Perl modules
46 # test carefully before changing this
48 eval { require "$FindBin::Bin/../kohalib.pl" };
58 my $field_number = "521";
59 my $subfield_target_audience_note = "a";
60 my $subfield_source = "b";
61 my $subfield_source_value = "Lexile";
65 'c|confirm' => \
$confirm,
68 'v|verbose+' => \
$verbose,
69 's|start=s' => \
$start,
71 'field=s' => \
$field_number,
72 'target-audience-note=s' => $subfield_target_audience_note,
73 'source=s' => $subfield_source,
74 'source-value=s' => $subfield_source_value,
77 my $usage = << 'ENDUSAGE';
78 import_lexile
.pl
: Import lexile scores
for records from csv
.
80 import_lexile
.pl
-f
/path/to
/LexileTitles
.txt
82 This script takes the following parameters
:
84 -h
--help Display this help
85 -c
--confirm Confirms you want to really run this script
( otherwise
print help
)
86 -t
--test Runs the script
in test mode
( no changes will be made to your database
)
87 -f
--file CSV file of lexile scores
( acquired from Lexile
.com
)
88 -v
--verbose Print data on found matches
. Use
-v
-v
for more data
, and -v
-v
-v will give the most data
.
89 --field Defines the field number
for the Lexile data
( default: 521 )
90 --target
-audience
-note Defines the subfield
for the lexile score
( default: a
)
91 --source Defines the
"Source" subfield
( default: b
)
92 --source
-value Defines the value to put stored
in the
"Source" subfield
( default: "Lexile" )
94 The CSV file must have the following columns
( with the first line being the column headers
) in tab delimited format
:
95 Title
, Author
, ISBN
, ISBN13
, Lexile
99 if ( $help || !$file || !$confirm ) {
104 my $schema = Koha
::Database
->new()->schema();
106 my $csv = Text
::CSV
->new( { binary
=> 1, sep_char
=> "\t" } )
107 or die "Cannot use CSV: " . Text
::CSV
->error_diag();
109 open my $fh, "<:encoding(utf8)", $file or die "test.csv: $!";
111 my $column_names = $csv->getline($fh);
112 $csv->column_names(@
$column_names);
116 while ( my $row = $csv->getline_hr($fh) ) {
119 next if ( $start && $i < $start );
120 last if ( $end && $i >= $end );
122 if ( $verbose > 1 ) {
123 say "Searching for matching record for row $i...";
124 say "Title: " . $row->{Title
};
125 say "Author: " . $row->{Author
};
126 say "ISBN10: " . $row->{ISBN
};
127 say "ISBN13: " . $row->{ISBN13
};
133 for ( 'ISBN', 'ISBN13' ) {
134 if ( $row->{$_} && $row->{$_} ne "None" ) {
135 push( @isbns, $row->{$_} );
136 eval { push( @isbns, GetVariationsOfISBN
( $row->{$_} ) ) };
139 @isbns = grep( $_, @isbns );
142 say "Searching for ISBNs: " . join( ' : ', @isbns ) if ( $verbose > 2 );
144 my @likes = map { { isbn
=> { like
=> '%' . $_ . '%' } } } @isbns;
147 $schema->resultset('Biblioitem')->search( { -or => \
@likes } )
148 ->get_column('biblionumber')->all();
150 say "Found matching records! Biblionumbers: " . join( " ,", @biblionumbers )
151 if ( @biblionumbers && $verbose > 2 );
153 foreach my $biblionumber (@biblionumbers) {
155 my $record = GetMarcBiblio
({ biblionumber
=> $biblionumber });
158 say "Found matching record! Biblionumber: $biblionumber";
160 if ( $verbose > 2 ) {
161 my $biblio = Koha
::Biblios
->find( $biblionumber );
162 say "Title from record: " . $biblio->title
164 say "Author from record: " . $biblio->author
166 say "ISBN from record: " . $biblio->biblioitem->isbn
167 if $biblio->biblioitem->isbn;
169 say "Title: " . $row->{Title
};
170 say "Author: " . $row->{Author
};
171 say "ISBN10: " . $row->{ISBN
};
172 say "ISBN13: " . $row->{ISBN13
};
176 # Check for existing embedded lexile score
177 my $lexile_score_field;
178 for my $field ( $record->field($field_number) ) {
179 if ( defined( $field->subfield($subfield_source) )
180 && $field->subfield($subfield_source) eq
181 $subfield_source_value )
183 $lexile_score_field = $field;
184 last; # Each item can only have one lexile score
188 if ($lexile_score_field) {
189 $lexile_score_field->update(
192 $subfield_target_audience_note => $row->{Lexile
},
193 $subfield_source => $subfield_source_value,
197 my $field = MARC
::Field
->new(
198 $field_number, '8', '#',
199 $subfield_target_audience_note => $row->{Lexile
},
200 $subfield_source => $subfield_source_value,
202 $record->append_fields($field);
205 ModBiblio
( $record, $biblionumber ) unless ( $test );
209 say "Update $counter records" if $verbose;