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

(-)a/misc/export_records.pl (-4 / +78 lines)
Lines 26-37 use Koha::Script; Link Here
26
use C4::Auth;
26
use C4::Auth;
27
use C4::Context;
27
use C4::Context;
28
use C4::Record;
28
use C4::Record;
29
use C4::Reports::Guided qw( execute_query );
29
30
30
use Koha::Biblioitems;
31
use Koha::Biblioitems;
31
use Koha::Database;
32
use Koha::Database;
32
use Koha::CsvProfiles;
33
use Koha::CsvProfiles;
33
use Koha::Exporter::Record;
34
use Koha::Exporter::Record;
34
use Koha::DateUtils qw( dt_from_string output_pref );
35
use Koha::DateUtils qw( dt_from_string output_pref );
36
use Koha::Reports;
35
37
36
my (
38
my (
37
    $output_format,
39
    $output_format,
Lines 55-60 my ( Link Here
55
    $end_accession,
57
    $end_accession,
56
    $marc_conditions,
58
    $marc_conditions,
57
    $embed_see_from_headings,
59
    $embed_see_from_headings,
60
    $report_id,
61
    @report_params,
62
    $report,
63
    $sql,
64
    $params_needed,
58
    $help
65
    $help
59
);
66
);
60
67
Lines 80-85 GetOptions( Link Here
80
    'end_accession=s'         => \$end_accession,
87
    'end_accession=s'         => \$end_accession,
81
    'marc_conditions=s'       => \$marc_conditions,
88
    'marc_conditions=s'       => \$marc_conditions,
82
    'embed_see_from_headings' => \$embed_see_from_headings,
89
    'embed_see_from_headings' => \$embed_see_from_headings,
90
    'report_id=s'             => \$report_id,
91
    'report_param=s'          => \@report_params,
83
    'h|help|?'                => \$help
92
    'h|help|?'                => \$help
84
) || pod2usage(1);
93
) || pod2usage(1);
85
94
Lines 102-108 if ( $output_format eq 'csv' and not $csv_profile_id ) { Link Here
102
    pod2usage(q|Define a csv profile to export in CSV|);
111
    pod2usage(q|Define a csv profile to export in CSV|);
103
}
112
}
104
113
105
106
if ( $record_type ne 'bibs' and $record_type ne 'auths' ) {
114
if ( $record_type ne 'bibs' and $record_type ne 'auths' ) {
107
    pod2usage(q|--record_type is not valid|);
115
    pod2usage(q|--record_type is not valid|);
108
}
116
}
Lines 111-116 if ( $deleted_barcodes and $record_type ne 'bibs' ) { Link Here
111
    pod2usage(q|--deleted_barcodes can only be used with biblios|);
119
    pod2usage(q|--deleted_barcodes can only be used with biblios|);
112
}
120
}
113
121
122
if ( $report_id ) {
123
124
    # Check report exists
125
    $report = Koha::Reports->find( $report_id );
126
    unless ( $report ) {
127
        pod2usage( sprintf( "No saved report (%s) found", $report_id ) );
128
    }
129
    $sql = $report->savedsql;
130
131
    # Check defined report can be used to export the record_type
132
    if ( $sql !~ /biblionumber/ && $record_type eq 'bibs' ) {
133
        pod2usage(q|The --report_id you specified does not fetch a biblionumber|);
134
    } elsif ( $sql !~ /authid/ && $record_type eq 'auths' ) {
135
        pod2usage(q|The --report_id you specified does not fetch an authid|);
136
    }
137
138
    # convert SQL parameters to placeholders
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;
141
}
142
114
$start_accession = dt_from_string( $start_accession ) if $start_accession;
143
$start_accession = dt_from_string( $start_accession ) if $start_accession;
115
$end_accession   = dt_from_string( $end_accession )   if $end_accession;
144
$end_accession   = dt_from_string( $end_accession )   if $end_accession;
116
145
Lines 141-147 my @record_ids; Link Here
141
$timestamp = ($timestamp) ? output_pref({ dt => dt_from_string($timestamp), dateformat => 'iso', dateonly => 0, }): '';
170
$timestamp = ($timestamp) ? output_pref({ dt => dt_from_string($timestamp), dateformat => 'iso', dateonly => 0, }): '';
142
171
143
if ( $record_type eq 'bibs' ) {
172
if ( $record_type eq 'bibs' ) {
144
    if ( $timestamp ) {
173
    if ( $report ) {
174
        # Run the report and fetch biblionumbers
175
        my ($sth) = execute_query(
176
            {
177
                sql        => $sql,
178
                sql_params => \@report_params,
179
                report_id  => $report_id,
180
            }
181
        );
182
        while ( my $row = $sth->fetchrow_hashref() ) {
183
            if ( $row->{biblionumber} ) {
184
                push @record_ids, $row->{biblionumber};
185
            } else {
186
                pod2usage(q|The --report_id you specified returned no biblionumbers|);
187
            }
188
        }
189
    } elsif ( $timestamp ) {
145
        if (!$dont_export_items) {
190
        if (!$dont_export_items) {
146
            push @record_ids, $_->{biblionumber} for @{
191
            push @record_ids, $_->{biblionumber} for @{
147
                $dbh->selectall_arrayref(q| (
192
                $dbh->selectall_arrayref(q| (
Lines 209-215 if ( $record_type eq 'bibs' ) { Link Here
209
    }
254
    }
210
}
255
}
211
elsif ( $record_type eq 'auths' ) {
256
elsif ( $record_type eq 'auths' ) {
212
    if ($timestamp) {
257
    if ( $report ) {
258
        # Run the report and fetch authids
259
        my ($sth) = execute_query(
260
            {
261
                sql        => $sql,
262
                sql_params => \@report_params,
263
                report_id  => $report_id,
264
            }
265
        );
266
        while ( my $row = $sth->fetchrow_hashref() ) {
267
            if ( $row->{authid} ) {
268
                push @record_ids, $row->{authid};
269
            } else {
270
                pod2usage(q|The --report_id you specified returned no authids|);
271
            }
272
        }
273
    } elsif ($timestamp) {
213
        push @record_ids, $_->{authid} for @{
274
        push @record_ids, $_->{authid} for @{
214
            $dbh->selectall_arrayref(
275
            $dbh->selectall_arrayref(
215
                q| (
276
                q| (
Lines 402-407 Print a brief help message. Link Here
402
463
403
 --embed_see_from_headings      Embed see from (non-preferred form) headings in bibliographic record.
464
 --embed_see_from_headings      Embed see from (non-preferred form) headings in bibliographic record.
404
465
466
=item B<--report_id>
467
468
--report_id=ID                  Export biblionumbers or authids from a given saved report output.
469
                                If you want to export authority records then your report must
470
                                select authid and you must define --record-type=auths when
471
                                running this script.
472
473
=item B<--report_param>
474
475
--report_param=PARAM            Repeatable, should provide one param per param requested for the
476
                                report.
477
                                Report params are not combined as on the staff side, so you may
478
                                need to repeat params.
479
405
=back
480
=back
406
481
407
=head1 AUTHOR
482
=head1 AUTHOR
408
- 

Return to bug 36770