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 #-----------------------------------
23 import_lexile.pl Import lexile scores for records from csv.
37 use C4
::Koha
qw( GetVariationsOfISBN );
42 binmode STDOUT
, ':encoding(UTF-8)';
46 # find Koha's Perl modules
47 # test carefully before changing this
49 eval { require "$FindBin::Bin/../kohalib.pl" };
59 my $field_number = "521";
60 my $subfield_target_audience_note = "a";
61 my $subfield_source = "b";
62 my $subfield_source_value = "Lexile";
66 'c|confirm' => \
$confirm,
69 'v|verbose+' => \
$verbose,
70 's|start=s' => \
$start,
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
100 if ( $help || !$file || !$confirm ) {
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);
117 while ( my $row = $csv->getline_hr($fh) ) {
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
};
134 for ( 'ISBN', 'ISBN13' ) {
135 if ( $row->{$_} && $row->{$_} ne "None" ) {
136 push( @isbns, $row->{$_} );
137 eval { push( @isbns, GetVariationsOfISBN
( $row->{$_} ) ) };
140 @isbns = grep( $_, @isbns );
143 say "Searching for ISBNs: " . join( ' : ', @isbns ) if ( $verbose > 2 );
145 my @likes = map { { isbn
=> { like
=> '%' . $_ . '%' } } } @isbns;
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) {
156 my $record = GetMarcBiblio
({ biblionumber
=> $biblionumber });
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
165 say "Author from record: " . $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
};
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(
193 $subfield_target_audience_note => $row->{Lexile
},
194 $subfield_source => $subfield_source_value,
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;