From 92e6be3a3708af8fe0e363872a335ff825825d42 Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Wed, 15 Apr 2015 01:02:08 -0400 Subject: [PATCH] Bug 10625: Inventory/Stocktaking tool cannot handle windows file uploads The current code uses $barcode = ; logic. This reads until \n, as far as I can tell. EOL is indicated by \n, \r, and \r\n depending on OS and software. So, to this end, rather than File::Slurp (which is a potential memory hog, which is already an issue with no filters), a loop to pre-read the barcodes was written. This loop includes: $barcode =~ s/\r/\n/g; $barcode =~ s/\n\n/\n/g; $barcode =~ s/\n$//; $barcode =~ s/^\n//; my @data = split(/\n/, $barcode); push @uploadedbarcodes,@data; So, that means that lines ending in \n would have it stripped and pushed into the uploaded barcodes array. Lines ending in \r would likely be read as one giant block, have everything converted to single \n's and then using a split, the set of barcodes are pushed into the uploaded barcodes array. Lines ending in \r\n would get that stripped and pushed into the uploaded barcodes array. It is then the uploaded barcodes array that is looped over for validating the barcodes. TEST PLAN --------- 1) Create a file with at least three different barcodes. --- BEGIN EXAMPLE --- BARCODE1\n BARCODE2\r BARCODE3\r\n --- END EXAMPLE --- $ echo "BARCODE1\nBARCODE2\rBARCODE3\r\n" | sed -e "s#\\\\n#\\n#g" | sed -e "s#\\\\r#\\r#g" > test.txt 2) Log into staff client 3) Tools -> Inventory/stocktaking 4) Click 'Browse' and select the file to upload (e.g. test.txt) 5) Click 'Submit' 6) A Javascript warning comes up, click 'Yes' -- This will take a LONG time, at least 1 minute. -- eventual results will be wrong. 7) Apply patch 8) repeat steps 3-6 -- This time correct number of barcodes will be given. 9) run koha qa test tools --- tools/inventory.pl | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tools/inventory.pl b/tools/inventory.pl index b8cc31d..f09abf9 100755 --- a/tools/inventory.pl +++ b/tools/inventory.pl @@ -161,6 +161,7 @@ if ( $uploadbarcodes && length($uploadbarcodes) > 0 ) { my $count = 0; my @barcodes; + my @uploadedbarcodes; my $sth = $dbh->column_info(undef,undef,"items","barcode"); my $barcode_def = $sth->fetchall_hashref('COLUMN_NAME'); @@ -169,8 +170,15 @@ if ( $uploadbarcodes && length($uploadbarcodes) > 0 ) { my $err_data=0; my $lines_read=0; binmode($uploadbarcodes, ":encoding(UTF-8)"); - while (my $barcode=<$uploadbarcodes>){ - $barcode =~ s/\r?\n$//; + while (my $barcode=<$uploadbarcodes>) { + $barcode =~ s/\r/\n/g; + $barcode =~ s/\n\n/\n/g; + $barcode =~ s/\n$//; + $barcode =~ s/^\n//; + my @data = split(/\n/,$barcode); + push @uploadedbarcodes, @data; + } + for my $barcode (@uploadedbarcodes) { next unless $barcode; ++$lines_read; if (length($barcode)>$barcode_size) { -- 1.9.1