From 3783457beabef943ada3f1165221e092133a7ebe Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Thu, 2 May 2024 23:40:09 +0000 Subject: [PATCH] Bug 36770: WIP Enable export_records.pl to use report output --- misc/export_records.pl | 57 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/misc/export_records.pl b/misc/export_records.pl index 1ec3bd8d0f4..003aa157639 100755 --- a/misc/export_records.pl +++ b/misc/export_records.pl @@ -26,12 +26,14 @@ use Koha::Script; use C4::Auth; use C4::Context; use C4::Record; +use C4::Reports::Guided qw( execute_query ); use Koha::Biblioitems; use Koha::Database; use Koha::CsvProfiles; use Koha::Exporter::Record; use Koha::DateUtils qw( dt_from_string output_pref ); +use Koha::Reports; my ( $output_format, @@ -55,6 +57,11 @@ my ( $end_accession, $marc_conditions, $embed_see_from_headings, + $report_id, + @report_params, + $report, + $sql, + $params_needed, $help ); @@ -80,6 +87,8 @@ GetOptions( 'end_accession=s' => \$end_accession, 'marc_conditions=s' => \$marc_conditions, 'embed_see_from_headings' => \$embed_see_from_headings, + 'report_id=s' => \$report_id, + 'report_param=s' => \@report_params, 'h|help|?' => \$help ) || pod2usage(1); @@ -111,6 +120,26 @@ if ( $deleted_barcodes and $record_type ne 'bibs' ) { pod2usage(q|--deleted_barcodes can only be used with biblios|); } +if ( $report_id ) { + + # Check report exists + $report = Koha::Reports->find( $report_id ); + unless ( $report ) { + pod2usage( sprintf( "No saved report (%s) found", $report_id ) ); + } + + # Check report can be used to export records + unless ( $sql =~ /biblionumber/ || $sql =~ /authid/) { + pod2usage(q|The --report_id you specified does not fetch a biblionumber or authid|); + } + $sql = $report->savedsql; + + # convert SQL parameters to placeholders + my $params_needed = ( $sql =~ s/(<<[^>]+>>)/\?/g ); + die("You supplied ". scalar @report_params . " parameter(s) and $params_needed are required by the report") if scalar @report_params != $params_needed; + +} + $start_accession = dt_from_string( $start_accession ) if $start_accession; $end_accession = dt_from_string( $end_accession ) if $end_accession; @@ -141,7 +170,22 @@ my @record_ids; $timestamp = ($timestamp) ? output_pref({ dt => dt_from_string($timestamp), dateformat => 'iso', dateonly => 0, }): ''; if ( $record_type eq 'bibs' ) { - if ( $timestamp ) { + + if ( $report ) { + my ($sth) = execute_query( + { + sql => $sql, + sql_params => \@report_params, + report_id => $report_id, + } + ); + my $count = scalar($sth->rows); + + while ( my $row = $sth->fetchrow_hashref() ) { + push @record_ids, $row->{biblionumber}; + } + + } elsif ( $timestamp ) { if (!$dont_export_items) { push @record_ids, $_->{biblionumber} for @{ $dbh->selectall_arrayref(q| ( @@ -402,6 +446,17 @@ Print a brief help message. --embed_see_from_headings Embed see from (non-preferred form) headings in bibliographic record. +=item B<--report_id> + +--report_id=ID Export biblionumbers or authids from a given saved report output. + +=item B<--report_param> + +--report_param=PARAM Repeatable, should provide one param per param requested for the + report. + Report params are not combined as on the staff side, so you may + need to repeat params. + =back =head1 AUTHOR -- 2.30.2