View | Details | Raw Unified | Return to bug 36831
Collapse All | Expand All

(-)a/Koha/BackgroundJob/ImportKBARTFile.pm (-5 / +55 lines)
Lines 218-224 sub format_title { Link Here
218
218
219
=head3 read_file
219
=head3 read_file
220
220
221
Reads a file to provide report headers and lines to be processed
221
Reads a file to provide report headers and lines to be processed.
222
Automatically detects whether the file is TSV or CSV based on the first line
222
223
223
=cut
224
=cut
224
225
Lines 226-239 sub read_file { Link Here
226
    my ($file) = @_;
227
    my ($file) = @_;
227
228
228
    my $file_content = defined( $file->{file_content} ) ? decode_base64( $file->{file_content} ) : "";
229
    my $file_content = defined( $file->{file_content} ) ? decode_base64( $file->{file_content} ) : "";
229
    my $delimiter    = $file->{filename} =~ /\.tsv$/    ? "\t"                                   : ",";
230
    my $delimiter    = identify_delimiter($file);
230
    my $quote_char   = $file->{filename} =~ /\.tsv$/    ? "\""                                   : "\"";
231
232
    return ( undef, undef, "unknown_delimiter" ) unless $delimiter;
231
233
232
    open my $fh, "<", \$file_content or die;
234
    open my $fh, "<", \$file_content or die;
233
    my $csv = Text::CSV_XS->new(
235
    my $csv = Text::CSV_XS->new(
234
        {
236
        {
235
            sep_char           => $delimiter,
237
            sep_char           => $delimiter,
236
            quote_char         => $quote_char,
238
            quote_char         => "\"",
237
            binary             => 1,
239
            binary             => 1,
238
            allow_loose_quotes => 1
240
            allow_loose_quotes => 1
239
        }
241
        }
Lines 251-256 sub read_file { Link Here
251
    return ( $column_headers, $lines, $error );
253
    return ( $column_headers, $lines, $error );
252
}
254
}
253
255
256
257
254
=head3 create_title_hash_from_line_data
258
=head3 create_title_hash_from_line_data
255
259
256
Takes a line and creates a hash of the values mapped to the column headings
260
Takes a line and creates a hash of the values mapped to the column headings
Lines 466-469 sub rescue_EBSCO_files { Link Here
466
    return $column_headers;
470
    return $column_headers;
467
}
471
}
468
472
473
=head3 identify_delimiter
474
475
Identifies the delimiter used in the KBART file. Checks first for a TSV file as this is what the KBART standard specifies.
476
If the TSV file is not detected then it checks for a CSV file.
477
Returns the delimiter required to parse the file correctly
478
479
=cut
480
481
sub identify_delimiter {
482
    my ($file) = @_;
483
484
    my $file_content = defined( $file->{file_content} ) ? decode_base64( $file->{file_content} ) : "";
485
    open my $tsv_fh, "<", \$file_content or die;
486
487
    # Check for TSV
488
    my $tsv = Text::CSV_XS->new(
489
        {
490
            sep_char           => "\t",
491
            quote_char         => "\"",
492
            binary             => 1,
493
            allow_loose_quotes => 1
494
        }
495
    );
496
    my $check_tsv = $tsv->getline($tsv_fh);
497
    close($tsv_fh);
498
499
    #If the sep_char is wrong then it will read the first row as one string
500
    return "\t" if scalar(@$check_tsv) > 1;
501
502
    open my $csv_fh, "<", \$file_content or die;
503
504
    # Check for CSV
505
    my $csv = Text::CSV_XS->new(
506
        {
507
            sep_char           => ",",
508
            quote_char         => "\"",
509
            binary             => 1,
510
            allow_loose_quotes => 1
511
        }
512
    );
513
    my $check_csv = $csv->getline($csv_fh);
514
    close($csv_fh);
515
516
    return ',' if scalar(@$check_csv) > 1;
517
    return 0;
518
}
519
469
1;
520
1;
470
- 

Return to bug 36831