From a0120ec24a898a934a29c01a8182212365c20995 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Delaune?= <stephane.delaune@biblibre.com>
Date: Fri, 16 May 2014 11:36:16 +0200
Subject: [PATCH] [PASSED QA] Bug 12238: tools export.pl: file list of ids as input parameter

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
Work as described. No errors

Tested writing txt file with biblionumbers, loading in tools and exporting

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
---
 .../intranet-tmpl/prog/en/modules/tools/export.tt  |   22 ++++++++++++++++-
 tools/export.pl                                    |   24 +++++++++++++++++++-
 2 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/export.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/export.tt
index b5072da..f4edddf 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/export.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/export.tt
@@ -38,7 +38,7 @@ $(document).ready(function() {
     <b>Note : The items are exported by this tool unless specified.</b>
 </p>
 
-<form method="post" action="/cgi-bin/koha/tools/export.pl">
+<form method="post" enctype="multipart/form-data" action="/cgi-bin/koha/tools/export.pl">
     <fieldset class="rows">
     <legend> Select records to export </legend>
         <ol><li>
@@ -97,6 +97,15 @@ $(document).ready(function() {
 </ul></li></ol>
     </fieldset>
     <fieldset class="rows">
+    <legend>
+        Use a file
+    </legend>
+        <ol>
+        <li>File containing a biblionumber's list with one biblionumber per line. This list works as a filter: it's compatible with other parameters.</li>
+        <li><label for="id_list_file">File : </label> <input type="file" id="id_list_file" name="id_list_file" /></li>
+        </ol>
+    </fieldset>
+    <fieldset class="rows">
     <legend> Options</legend>
 <ol>        <li>
         <label for="dont_export_item">Don't export items</label>
@@ -136,7 +145,7 @@ $(document).ready(function() {
 </div>
 
 <div id="auths">
-<form method="post" action="/cgi-bin/koha/tools/export.pl">
+<form method="post" enctype="multipart/form-data" action="/cgi-bin/koha/tools/export.pl">
     <fieldset class="rows">
     <legend> Select records to export </legend>
         <ol><li>
@@ -163,6 +172,15 @@ $(document).ready(function() {
         </ol>
     </fieldset>
     <fieldset class="rows">
+    <legend>
+        Use a file
+    </legend>
+        <ol>
+        <li>File containing an authid's list with one authid per line. This list works as a filter: it's compatible with other parameters.</li>
+        <li><label for="id_list_file">File : </label> <input type="file" id="id_list_file" name="id_list_file" /></li>
+        </ol>
+    </fieldset>
+    <fieldset class="rows">
     <legend>Options</legend>
         <ol>
         <li>
diff --git a/tools/export.pl b/tools/export.pl
index 9bbed0b..3bb58a7 100755
--- a/tools/export.pl
+++ b/tools/export.pl
@@ -38,6 +38,7 @@ my $dont_export_items;
 my $deleted_barcodes;
 my $timestamp;
 my $record_type;
+my $id_list_file;
 my $help;
 my $op       = $query->param("op")       || '';
 my $filename = $query->param("filename") || 'koha.mrc';
@@ -60,12 +61,13 @@ if ( $commandline ) {
         'clean'             => \$clean,
         'filename=s'        => \$filename,
         'record-type=s'     => \$record_type,
+        'id_list_file=s'    => \$id_list_file,
         'help|?'            => \$help
     );
 
     if ($help) {
         print <<_USAGE_;
-export.pl [--format=format] [--date=date] [--record-type=TYPE] [--dont_export_items] [--deleted_barcodes] [--clean] --filename=outputfile
+export.pl [--format=format] [--date=date] [--record-type=TYPE] [--dont_export_items] [--deleted_barcodes] [--clean] [--id_list_file=PATH] --filename=outputfile
 
 
  --format=FORMAT        FORMAT is either 'xml' or 'marc' (default)
@@ -82,6 +84,11 @@ export.pl [--format=format] [--date=date] [--record-type=TYPE] [--dont_export_it
                         specified). Used only if TYPE is 'bibs'
 
  --clean                removes NSE/NSB
+
+ --id_list_file=PATH    PATH is an absolute path to a file containing a list of
+                        IDs(biblionumber or authid) with only one ID per line.
+                        This IDs list works as a filter: it's compatible with
+                        other parameters
 _USAGE_
         exit;
     }
@@ -93,6 +100,7 @@ _USAGE_
     $deleted_barcodes  ||= 0;
     $clean             ||= 0;
     $record_type       ||= "bibs";
+    $id_list_file       ||= 0;
 
     # Redirect stdout
     open STDOUT, '>', $filename if $filename;
@@ -196,6 +204,19 @@ if ( $op eq "export" ) {
         my $starting_authid = $query->param('starting_authid');
         my $ending_authid   = $query->param('ending_authid');
         my $authtype        = $query->param('authtype');
+        my $filefh;
+        if ($commandline) {
+            open $filefh,"<", $id_list_file or die "cannot open $id_list_file: $!";
+        } else {
+            $filefh = $query->upload("id_list_file");
+        }
+        my %id_filter;
+        if ($filefh) {
+            while (my $number=<$filefh>){
+                $number=~s/[\r\n]*$//;
+                $id_filter{$number}=1 if $number=~/^\d+$/;
+            }
+        }
 
         if ( $record_type eq 'bibs' and not @biblionumbers ) {
             if ($timestamp) {
@@ -308,6 +329,7 @@ if ( $op eq "export" ) {
             push @recordids, map {
                 map { $$_[0] } $_
             } @{ $sth->fetchall_arrayref };
+            @recordids = grep { exists($id_filter{$_}) } @recordids if scalar(%id_filter);
         }
 
         my $xml_header_written = 0;
-- 
1.7.2.5