From e6d761c925dc08cf422c8db60a8fd04e55d31126 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 10 Dec 2013 10:26:48 -0500 Subject: [PATCH] Bug 11368 - Add script to import Lexile scores Koha needs a script to automate the importing of Lexile score data for titles that have available scores but are not currently in the title's record. This script will take a CSV file of Lexile scores, and locate any matching records in the Koha database ( by ISBN ). If the record already has a score, it will be updated. If not, the Lexile score field will be created. Test Plan: 1) Apply this patch 2) Catalog a record for each of the following ISBNs: 0789170191 9780673779410 3) Download the file LexileTitlesTruncated.txt attached to this bug report 4) Run the script from the command line: ./misc/migraction_tools/import_lexile.pl -v --file /path/to/LexileTitlesTruncated.txt 5) View those records in Koha 6) Note those records now have valid Lexile scores 7) Edit the Lexile score ( 521$a ) and change the value to something else 8) Repeat step 4 9) Note the original Lexile score has been restored --- misc/migration_tools/import_lexile.pl | 149 +++++++++++++++++++++++++++++++++ 1 files changed, 149 insertions(+), 0 deletions(-) create mode 100755 misc/migration_tools/import_lexile.pl diff --git a/misc/migration_tools/import_lexile.pl b/misc/migration_tools/import_lexile.pl new file mode 100755 index 0000000..e0f7be3 --- /dev/null +++ b/misc/migration_tools/import_lexile.pl @@ -0,0 +1,149 @@ +#!/usr/bin/perl +#----------------------------------- +# Copyright 2013 ByWater Solutions +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +#----------------------------------- + +=head1 NAME + +import_lexile.pl Import lexile scores for records from csv. + +=cut + +use Modern::Perl; + +use Getopt::Long; +use Text::CSV; + +use C4::Context; +use C4::Biblio; +use C4::Koha qw( GetVariationsOfISBN ); +use Koha::Database; + +BEGIN { + + # find Koha's Perl modules + # test carefully before changing this + use FindBin; + eval { require "$FindBin::Bin/../kohalib.pl" }; +} + +my $file; +my $verbose; +my $field_number = "521"; +my $subfield_target_audience_note = "a"; +my $subfield_source = "b"; +my $subfield_source_value = "Lexile"; + +GetOptions( + 'f|file=s' => \$file, + 'v|verbose' => \$verbose, + 'field=s' => \$field_number, + 'target-audience-note=s' => $subfield_target_audience_note, + 'source=s' => $subfield_source, + 'source-value=s' => $subfield_source_value, +); + +my $usage = << 'ENDUSAGE'; +import_lexile.pl: Import lexile scores for records from csv. + +import_lexile.pl -f /path/to/LexileTitles.txt + +This script takes the following parameters : + + -f | --file CSV file of lexile scores ( acquired from Lexile.com ) + -v | --verbose Print data on found matches + --field Defines the field number for the Lexile data ( default: 521 ) + --target-audience-note Defines the subfield for the lexile score ( default: a ) + --source Defines the "Source" subfield ( default: b ) + --source-value Defines the value to put stored in the "Source" subfield ( default: "Lexile" ) + +ENDUSAGE + +unless ($file) { + print $usage; + exit(1); +} + +my $schema = Koha::Database->new()->schema(); + +my $csv = Text::CSV->new( { sep_char => "\t" } ) + or die "Cannot use CSV: " . Text::CSV->error_diag(); + +open my $fh, "<:encoding(utf8)", $file or die "test.csv: $!"; + +my $column_names = $csv->getline($fh); +$csv->column_names(@$column_names); + +while ( my $row = $csv->getline_hr($fh) ) { + + # Match by ISBN + my @isbns = ( + $row->{ISBN}, $row->{ISBN13}, + GetVariationsOfISBN( $row->{ISBN} ), + GetVariationsOfISBN( $row->{ISBN13} ) + ); + + my @likes = map { { isbn => { like => '%' . $_ . '%' } } } @isbns; + + my @biblionumbers = + $schema->resultset('Biblioitem')->search( -or => \@likes ) + ->get_column('biblionumber')->all(); + + foreach my $biblionumber (@biblionumbers) { + my $record = GetMarcBiblio($biblionumber); + + if ($verbose) { + say "Found matching record! Biblionumber: $biblionumber"; + say "Title: " . $row->{Title}; + say "Author: " . $row->{Author}; + say "ISBN10: " . $row->{ISBN}; + say "ISBN13: " . $row->{ISBN13}; + say q{}; + } + + # Check for existing embedded lexile score + my $lexile_score_field; + for my $field ( $record->field($field_number) ) { + if ( $field->subfield($subfield_source) eq $subfield_source_value ) + { + $lexile_score_field = $field; + last; # Each item can only have one lexile score + } + } + + if ($lexile_score_field) { + $lexile_score_field->update( + ind1 => '8', + ind2 => '#', + $subfield_target_audience_note => $row->{Lexile}, + $subfield_source => $subfield_source_value, + ); + } + else { + my $field = MARC::Field->new( + $field_number, '8', '#', + $subfield_target_audience_note => $row->{Lexile}, + $subfield_source => $subfield_source_value, + ); + $record->append_fields($field); + } + + ModBiblio( $record, $biblionumber ); + } + +} -- 1.7.2.5