Bug 20393: Remove redundant 'koha.psgi' and 'plackup.sh' files
[koha.git] / misc / export_records.pl
index cbb4bb0..6e36368 100755 (executable)
@@ -24,11 +24,11 @@ use Pod::Usage;
 
 use C4::Auth;
 use C4::Context;
-use C4::Csv;
 use C4::Record;
 
 use Koha::Biblioitems;
 use Koha::Database;
+use Koha::CsvProfiles;
 use Koha::Exporter::Record;
 use Koha::DateUtils qw( dt_from_string output_pref );
 
@@ -67,6 +67,13 @@ $record_type ||= 'bibs';
 # Retrocompatibility for the format parameter
 $output_format = 'iso2709' if $output_format eq 'marc';
 
+if ( $output_format eq 'csv' and $record_type eq 'auths' ) {
+    pod2usage(q|CSV output is only available for biblio records|);
+}
+
+if ( $output_format eq 'csv' and not $csv_profile_id ) {
+    pod2usage(q|Define a csv profile to export in CSV|);
+}
 
 if ( $timestamp and $record_type ne 'bibs' ) {
     pod2usage(q|--timestamp can only be used with biblios|);
@@ -91,22 +98,22 @@ open STDOUT, '>', $filename if $filename;
 
 my @record_ids;
 
-$timestamp = ($timestamp) ? output_pref({ dt => dt_from_string($timestamp), dateformat => 'iso', dateonly => 1, }): '';
+$timestamp = ($timestamp) ? output_pref({ dt => dt_from_string($timestamp), dateformat => 'iso', dateonly => 0, }): '';
 
 if ( $record_type eq 'bibs' ) {
     if ( $timestamp ) {
         push @record_ids, $_->{biblionumber} for @{
             $dbh->selectall_arrayref(q| (
-                SELECT biblionumber
-                FROM biblioitems
+                SELECT biblio_metadata.biblionumber
+                FROM biblio_metadata
                   LEFT JOIN items USING(biblionumber)
-                WHERE biblioitems.timestamp >= ?
+                WHERE biblio_metadata.timestamp >= ?
                   OR items.timestamp >= ?
             ) UNION (
-                SELECT biblionumber
-                FROM biblioitems
+                SELECT biblio_metadata.biblionumber
+                FROM biblio_metadata
                   LEFT JOIN deleteditems USING(biblionumber)
-                WHERE biblioitems.timestamp >= ?
+                WHERE biblio_metadata.timestamp >= ?
                   OR deleteditems.timestamp >= ?
             ) |, { Slice => {} }, ( $timestamp ) x 4 );
         };
@@ -140,7 +147,7 @@ if ( $record_type eq 'bibs' ) {
                 ?
                   C4::Context->preference('item-level_itypes')
                     ? ( 'items.itype' => $itemtype )
-                    : ( 'biblioitems.itemtype' => $itemtype )
+                    : ( 'me.itemtype' => $itemtype )
                 : ()
             ),
 
@@ -163,15 +170,16 @@ elsif ( $record_type eq 'auths' ) {
             : (),
         ( $authtype ? ( authtypecode => $authtype ) : () ),
     };
-    # Koha::Authority is not a Koha::Object...
+    # Koha::MetadataRecord::Authority is not a Koha::Object...
     my $authorities = Koha::Database->new->schema->resultset('AuthHeader')->search( $conditions );
     @record_ids = map { $_->authid } $authorities->all;
 }
 
 @record_ids = uniq @record_ids;
-if ( @record_ids and my $id_list_file ) {
-    my @filter_record_ids = <$id_list_file>;
-    @filter_record_ids = map { my $id = $_; $id =~ s/[\r\n]*$// } @filter_record_ids;
+if ( @record_ids and $id_list_file ) {
+    open my $fh, '<', $id_list_file or die "Cannot open file $id_list_file ($!)";
+    my @filter_record_ids = <$fh>;
+    @filter_record_ids = map { my $id = $_; $id =~ s/[\r\n]*$//; $id } @filter_record_ids;
     # intersection
     my %record_ids = map { $_ => 1 } @record_ids;
     @record_ids = grep $record_ids{$_}, @filter_record_ids;
@@ -179,14 +187,12 @@ if ( @record_ids and my $id_list_file ) {
 
 if ($deleted_barcodes) {
     for my $record_id ( @record_ids ) {
-        my $q = q|
-        |;
-        my $barcode = $dbh->selectall_arrayref(q| (
+        my $barcode = $dbh->selectall_arrayref(q|
             SELECT DISTINCT barcode
             FROM deleteditems
             WHERE deleteditems.biblionumber = ?
         |, { Slice => {} }, $record_id );
-        say $_->{barcode} for @$barcode
+        say $_->{barcode} for @$barcode;
     }
 }
 else {
@@ -194,7 +200,7 @@ else {
         {   record_type        => $record_type,
             record_ids         => \@record_ids,
             format             => $output_format,
-            csv_profile_id     => ( $csv_profile_id || GetCsvProfileId( C4::Context->preference('ExportWithCsvProfile') ) || undef ),
+            csv_profile_id     => $csv_profile_id,
             export_items       => (not $dont_export_items),
             clean              => $clean || 0,
         }
@@ -209,7 +215,7 @@ export records - This script exports record (biblios or authorities)
 
 =head1 SYNOPSIS
 
-export_records.pl [-h|--help] [--format=format] [--date=date] [--record-type=TYPE] [--dont_export_items] [--deleted_barcodes] [--clean] [--id_list_file=PATH] --filename=outputfile
+export_records.pl [-h|--help] [--format=format] [--date=datetime] [--record-type=TYPE] [--dont_export_items] [--deleted_barcodes] [--clean] [--id_list_file=PATH] --filename=outputfile
 
 =head1 OPTIONS
 
@@ -221,14 +227,14 @@ Print a brief help message.
 
 =item B<--format>
 
- --format=FORMAT        FORMAT is either 'xml', 'csv'  or 'marc' (default).
+ --format=FORMAT        FORMAT is either 'xml', 'csv' (biblio records only) or 'marc' (default).
 
 =item B<--date>
 
- --date=DATE            DATE should be entered as the 'dateformat' syspref is
-                        set (dd/mm/yyyy for metric, yyyy-mm-dd for iso,
-                        mm/dd/yyyy for us) records exported are the ones that
-                        have been modified since DATE.
+ --date=DATETIME        DATETIME should be entered as the 'dateformat' syspref is
+                        set (dd/mm/yyyy[ hh:mm:ss] for metric, yyyy-mm-dd[ hh:mm:ss] for iso,
+                        mm/dd/yyyy[ hh:mm:ss] for us) records exported are the ones that
+                        have been modified since DATETIME.
 
 =item B<--record-type>
 
@@ -241,7 +247,7 @@ Print a brief help message.
 =item B<--csv_profile_id>
 
  --csv_profile_id=ID    Generate a CSV file with the given CSV profile id (see tools/csv-profiles.pl)
-                        Unless provided, the one defined in the system preference 'ExportWithCsvProfile' will be used.
+                        This can only be used to export biblio records.
 
 =item B<--deleted_barcodes>
 
@@ -266,24 +272,44 @@ Print a brief help message.
 
 =item B<--starting_authid>
 
+ --starting_authid=ID  Export authorities with authid >= ID
+
 =item B<--ending_authid>
 
+ --ending_authid=ID    Export authorities with authid <= ID
+
 =item B<--authtype>
 
+ --authtype=AUTHTYPE   Export authorities from the given AUTHTYPE
+
 =item B<--starting_biblionumber>
 
+ --starting_biblionumber=ID  Export biblio with biblionumber >= ID
+
 =item B<--ending_biblionumber>
 
+ --ending_biblionumber=ID    Export biblio with biblionumber <= ID
+
 =item B<--itemtype>
 
+ --itemtype=ITEMTYPE         Export biblio from the given ITEMTYPE
+
 =item B<--starting_callnumber>
 
+ --starting_callnumber=CALLNUMBER Export biblio with callnumber >=CALLNUMBER
+
 =item B<--ending_callnumber>
 
+ --ending_callnumber=CALLNUMBER Export biblio with callnumber <=CALLNUMBER
+
 =item B<--start_accession>
 
+ --starting_accession=DATE      Export biblio with an item accessionned after DATE
+
 =item B<--end_accession>
 
+ --end_accession=DATE           Export biblio with an item accessionned after DATE
+
 =back
 
 =head1 AUTHOR