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

(-)a/misc/export_records.pl (-67 / +72 lines)
Lines 19-26 Link Here
19
use Modern::Perl;
19
use Modern::Perl;
20
use MARC::File::XML;
20
use MARC::File::XML;
21
use List::MoreUtils qw( uniq );
21
use List::MoreUtils qw( uniq );
22
use Getopt::Long qw( GetOptions );
22
use Getopt::Long    qw( GetOptions );
23
use Pod::Usage qw( pod2usage );
23
use Pod::Usage      qw( pod2usage );
24
24
25
use Koha::Script;
25
use Koha::Script;
26
use C4::Auth;
26
use C4::Auth;
Lines 96-104 if ($help) { Link Here
96
    pod2usage(1);
96
    pod2usage(1);
97
}
97
}
98
98
99
$filename ||= 'koha.mrc';
99
$filename      ||= 'koha.mrc';
100
$output_format ||= 'iso2709';
100
$output_format ||= 'iso2709';
101
$record_type ||= 'bibs';
101
$record_type   ||= 'bibs';
102
102
103
# Retrocompatibility for the format parameter
103
# Retrocompatibility for the format parameter
104
$output_format = 'iso2709' if $output_format eq 'marc';
104
$output_format = 'iso2709' if $output_format eq 'marc';
Lines 119-129 if ( $deleted_barcodes and $record_type ne 'bibs' ) { Link Here
119
    pod2usage(q|--deleted_barcodes can only be used with biblios|);
119
    pod2usage(q|--deleted_barcodes can only be used with biblios|);
120
}
120
}
121
121
122
if ( $report_id ) {
122
if ($report_id) {
123
123
124
    # Check report exists
124
    # Check report exists
125
    $report = Koha::Reports->find( $report_id );
125
    $report = Koha::Reports->find($report_id);
126
    unless ( $report ) {
126
    unless ($report) {
127
        pod2usage( sprintf( "No saved report (%s) found", $report_id ) );
127
        pod2usage( sprintf( "No saved report (%s) found", $report_id ) );
128
    }
128
    }
129
    $sql = $report->savedsql;
129
    $sql = $report->savedsql;
Lines 137-159 if ( $report_id ) { Link Here
137
137
138
    # convert SQL parameters to placeholders
138
    # convert SQL parameters to placeholders
139
    my $params_needed = ( $sql =~ s/(<<[^>]+>>)/\?/g );
139
    my $params_needed = ( $sql =~ s/(<<[^>]+>>)/\?/g );
140
    die("You supplied ". scalar @report_params . " parameter(s) and $params_needed are required by the report") if scalar @report_params != $params_needed;
140
    die( "You supplied " . scalar @report_params . " parameter(s) and $params_needed are required by the report" )
141
        if scalar @report_params != $params_needed;
141
}
142
}
142
143
143
$start_accession = dt_from_string( $start_accession ) if $start_accession;
144
$start_accession = dt_from_string($start_accession) if $start_accession;
144
$end_accession   = dt_from_string( $end_accession )   if $end_accession;
145
$end_accession   = dt_from_string($end_accession)   if $end_accession;
145
146
146
# Parse marc conditions
147
# Parse marc conditions
147
my @marc_conditions;
148
my @marc_conditions;
148
if ($marc_conditions) {
149
if ($marc_conditions) {
149
    foreach my $condition (split(/,\s*/, $marc_conditions)) {
150
    foreach my $condition ( split( /,\s*/, $marc_conditions ) ) {
150
        if ($condition =~ /^(\d{3})([\w\d]?)(=|(?:!=)|>|<)([^,]+)$/) {
151
        if ( $condition =~ /^(\d{3})([\w\d]?)(=|(?:!=)|>|<)([^,]+)$/ ) {
151
            push @marc_conditions, [$1, $2, $3, $4];
152
            push @marc_conditions, [ $1, $2, $3, $4 ];
152
        }
153
        } elsif ( $condition =~ /^(exists|not_exists)\((\d{3})([\w\d]?)\)$/ ) {
153
        elsif ($condition =~ /^(exists|not_exists)\((\d{3})([\w\d]?)\)$/) {
154
            push @marc_conditions, [ $2, $3, $1 eq 'exists' ? '?' : '!?' ];
154
            push @marc_conditions, [$2, $3, $1 eq 'exists' ? '?' : '!?'];
155
        } else {
155
        }
156
        else {
157
            die("Invalid condititon: $condition");
156
            die("Invalid condititon: $condition");
158
        }
157
        }
159
    }
158
    }
Lines 164-176 my $dbh = C4::Context->dbh; Link Here
164
# Redirect stdout
163
# Redirect stdout
165
open STDOUT, '>', $filename if $filename;
164
open STDOUT, '>', $filename if $filename;
166
165
167
168
my @record_ids;
166
my @record_ids;
169
167
170
$timestamp = ($timestamp) ? output_pref({ dt => dt_from_string($timestamp), dateformat => 'iso', dateonly => 0, }): '';
168
$timestamp =
169
    ($timestamp) ? output_pref( { dt => dt_from_string($timestamp), dateformat => 'iso', dateonly => 0, } ) : '';
171
170
172
if ( $record_type eq 'bibs' ) {
171
if ( $record_type eq 'bibs' ) {
173
    if ( $report ) {
172
    if ($report) {
173
174
        # Run the report and fetch biblionumbers
174
        # Run the report and fetch biblionumbers
175
        my ($sth) = execute_query(
175
        my ($sth) = execute_query(
176
            {
176
            {
Lines 186-195 if ( $record_type eq 'bibs' ) { Link Here
186
                pod2usage(q|The --report_id you specified returned no biblionumbers|);
186
                pod2usage(q|The --report_id you specified returned no biblionumbers|);
187
            }
187
            }
188
        }
188
        }
189
    } elsif ( $timestamp ) {
189
    } elsif ($timestamp) {
190
        if (!$dont_export_items) {
190
        if ( !$dont_export_items ) {
191
            push @record_ids, $_->{biblionumber} for @{
191
            push @record_ids, $_->{biblionumber} for @{
192
                $dbh->selectall_arrayref(q| (
192
                $dbh->selectall_arrayref(
193
                    q| (
193
                    SELECT biblio_metadata.biblionumber
194
                    SELECT biblio_metadata.biblionumber
194
                    FROM biblio_metadata
195
                    FROM biblio_metadata
195
                      LEFT JOIN items USING(biblionumber)
196
                      LEFT JOIN items USING(biblionumber)
Lines 201-248 if ( $record_type eq 'bibs' ) { Link Here
201
                      LEFT JOIN deleteditems USING(biblionumber)
202
                      LEFT JOIN deleteditems USING(biblionumber)
202
                    WHERE biblio_metadata.timestamp >= ?
203
                    WHERE biblio_metadata.timestamp >= ?
203
                      OR deleteditems.timestamp >= ?
204
                      OR deleteditems.timestamp >= ?
204
                ) |, { Slice => {} }, ( $timestamp ) x 4 );
205
                ) |, { Slice => {} }, ($timestamp) x 4
206
                );
205
            };
207
            };
206
        } else {
208
        } else {
207
            push @record_ids, $_->{biblionumber} for @{
209
            push @record_ids, $_->{biblionumber} for @{
208
                $dbh->selectall_arrayref(q| (
210
                $dbh->selectall_arrayref(
211
                    q| (
209
                    SELECT biblio_metadata.biblionumber
212
                    SELECT biblio_metadata.biblionumber
210
                    FROM biblio_metadata
213
                    FROM biblio_metadata
211
                    WHERE biblio_metadata.timestamp >= ?
214
                    WHERE biblio_metadata.timestamp >= ?
212
                ) |, { Slice => {} }, $timestamp );
215
                ) |, { Slice => {} }, $timestamp
216
                );
213
            };
217
            };
214
        }
218
        }
215
    } else {
219
    } else {
216
        my $conditions = {
220
        my $conditions = {
217
            ( $starting_biblionumber or $ending_biblionumber )
221
            ( $starting_biblionumber or $ending_biblionumber )
218
                ? (
222
            ? (
219
                    "me.biblionumber" => {
223
                "me.biblionumber" => {
220
                        ( $starting_biblionumber ? ( '>=' => $starting_biblionumber ) : () ),
224
                    ( $starting_biblionumber ? ( '>=' => $starting_biblionumber ) : () ),
221
                        ( $ending_biblionumber   ? ( '<=' => $ending_biblionumber   ) : () ),
225
                    ( $ending_biblionumber   ? ( '<=' => $ending_biblionumber )   : () ),
222
                    }
226
                }
223
                )
227
                )
224
                : (),
228
            : (),
225
            ( $starting_callnumber or $ending_callnumber )
229
            ( $starting_callnumber or $ending_callnumber )
226
                ? (
230
            ? (
227
                    callnumber => {
231
                callnumber => {
228
                        ( $starting_callnumber ? ( '>=' => $starting_callnumber ) : () ),
232
                    ( $starting_callnumber ? ( '>=' => $starting_callnumber ) : () ),
229
                        ( $ending_callnumber   ? ( '<=' => $ending_callnumber   ) : () ),
233
                    ( $ending_callnumber   ? ( '<=' => $ending_callnumber )   : () ),
230
                    }
234
                }
231
                )
235
                )
232
                : (),
236
            : (),
233
            ( $start_accession or $end_accession )
237
            ( $start_accession or $end_accession )
234
                ? (
238
            ? (
235
                    dateaccessioned => {
239
                dateaccessioned => {
236
                        ( $start_accession ? ( '>=' => $start_accession ) : () ),
240
                    ( $start_accession ? ( '>=' => $start_accession ) : () ),
237
                        ( $end_accession   ? ( '<=' => $end_accession   ) : () ),
241
                    ( $end_accession   ? ( '<=' => $end_accession )   : () ),
238
                    }
242
                }
239
                )
243
                )
240
                : (),
244
            : (),
241
            ( $itemtype
245
            (
242
                ?
246
                  $itemtype
243
                  C4::Context->preference('item-level_itypes')
247
                ? C4::Context->preference('item-level_itypes')
244
                    ? ( 'items.itype' => $itemtype )
248
                        ? ( 'items.itype' => $itemtype )
245
                    : ( 'me.itemtype' => $itemtype )
249
                        : ( 'me.itemtype' => $itemtype )
246
                : ()
250
                : ()
247
            ),
251
            ),
248
252
Lines 252-260 if ( $record_type eq 'bibs' ) { Link Here
252
            push @record_ids, $biblioitem->biblionumber;
256
            push @record_ids, $biblioitem->biblionumber;
253
        }
257
        }
254
    }
258
    }
255
}
259
} elsif ( $record_type eq 'auths' ) {
256
elsif ( $record_type eq 'auths' ) {
260
    if ($report) {
257
    if ( $report ) {
261
258
        # Run the report and fetch authids
262
        # Run the report and fetch authids
259
        my ($sth) = execute_query(
263
        my ($sth) = execute_query(
260
            {
264
            {
Lines 304-341 if ( @record_ids and $id_list_file ) { Link Here
304
    open my $fh, '<', $id_list_file or die "Cannot open file $id_list_file ($!)";
308
    open my $fh, '<', $id_list_file or die "Cannot open file $id_list_file ($!)";
305
    my @filter_record_ids = <$fh>;
309
    my @filter_record_ids = <$fh>;
306
    @filter_record_ids = map { my $id = $_; $id =~ s/[\r\n]*$//; $id } @filter_record_ids;
310
    @filter_record_ids = map { my $id = $_; $id =~ s/[\r\n]*$//; $id } @filter_record_ids;
311
307
    # intersection
312
    # intersection
308
    my %record_ids = map { $_ => 1 } @record_ids;
313
    my %record_ids = map { $_ => 1 } @record_ids;
309
    @record_ids = grep $record_ids{$_}, @filter_record_ids;
314
    @record_ids = grep $record_ids{$_}, @filter_record_ids;
310
}
315
}
311
316
312
if ($deleted_barcodes) {
317
if ($deleted_barcodes) {
313
    for my $record_id ( @record_ids ) {
318
    for my $record_id (@record_ids) {
314
        my $barcode = $dbh->selectall_arrayref(q|
319
        my $barcode = $dbh->selectall_arrayref(
320
            q|
315
            SELECT DISTINCT barcode
321
            SELECT DISTINCT barcode
316
            FROM deleteditems
322
            FROM deleteditems
317
            WHERE deleteditems.biblionumber = ?
323
            WHERE deleteditems.biblionumber = ?
318
            AND barcode IS NOT NULL AND barcode != ''
324
            AND barcode IS NOT NULL AND barcode != ''
319
        |, { Slice => {} }, $record_id );
325
        |, { Slice => {} }, $record_id
326
        );
320
        say $_->{barcode} for @$barcode;
327
        say $_->{barcode} for @$barcode;
321
    }
328
    }
322
}
329
} else {
323
else {
324
    Koha::Exporter::Record::export(
330
    Koha::Exporter::Record::export(
325
        {   record_type        => $record_type,
331
        {
326
            record_ids         => \@record_ids,
332
            record_type             => $record_type,
327
            record_conditions  => @marc_conditions ? \@marc_conditions : undef,
333
            record_ids              => \@record_ids,
328
            format             => $output_format,
334
            record_conditions       => @marc_conditions ? \@marc_conditions : undef,
329
            csv_profile_id     => $csv_profile_id,
335
            format                  => $output_format,
330
            export_items       => (not $dont_export_items),
336
            csv_profile_id          => $csv_profile_id,
331
            clean              => $clean || 0,
337
            export_items            => ( not $dont_export_items ),
338
            clean                   => $clean                   || 0,
332
            embed_see_from_headings => $embed_see_from_headings || 0,
339
            embed_see_from_headings => $embed_see_from_headings || 0,
333
        }
340
        }
334
    );
341
    );
335
}
342
}
336
exit;
343
exit;
337
344
338
339
=head1 NAME
345
=head1 NAME
340
346
341
export records - This script exports record (biblios or authorities)
347
export records - This script exports record (biblios or authorities)
342
- 

Return to bug 36770