Bug 17744: Remove the oai_dc:dcCollection wrapper in OAI responses
[koha.git] / misc / export_records.pl
blob2bb553d03e803c77f4f23b93724743b46994376f
1 #!/usr/bin/perl
4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 use Modern::Perl;
20 use MARC::File::XML;
21 use List::MoreUtils qw(uniq);
22 use Getopt::Long;
23 use Pod::Usage;
25 use C4::Auth;
26 use C4::Context;
27 use C4::Record;
29 use Koha::Biblioitems;
30 use Koha::Database;
31 use Koha::CsvProfiles;
32 use Koha::Exporter::Record;
33 use Koha::DateUtils qw( dt_from_string output_pref );
35 my ( $output_format, $timestamp, $dont_export_items, $csv_profile_id, $deleted_barcodes, $clean, $filename, $record_type, $id_list_file, $starting_authid, $ending_authid, $authtype, $starting_biblionumber, $ending_biblionumber, $itemtype, $starting_callnumber, $ending_callnumber, $start_accession, $end_accession, $help );
36 GetOptions(
37 'format=s' => \$output_format,
38 'date=s' => \$timestamp,
39 'dont_export_items' => \$dont_export_items,
40 'csv_profile_id=s' => \$csv_profile_id,
41 'deleted_barcodes' => \$deleted_barcodes,
42 'clean' => \$clean,
43 'filename=s' => \$filename,
44 'record-type=s' => \$record_type,
45 'id_list_file=s' => \$id_list_file,
46 'starting_authid=s' => \$starting_authid,
47 'ending_authid=s' => \$ending_authid,
48 'authtype=s' => \$authtype,
49 'starting_biblionumber=s' => \$starting_biblionumber,
50 'ending_biblionumber=s' => \$ending_biblionumber,
51 'itemtype=s' => \$itemtype,
52 'starting_callnumber=s' => \$starting_callnumber,
53 'ending_callnumber=s' => \$ending_callnumber,
54 'start_accession=s' => \$start_accession,
55 'end_accession=s' => \$end_accession,
56 'h|help|?' => \$help
57 ) || pod2usage(1);
59 if ($help) {
60 pod2usage(1);
63 $filename ||= 'koha.mrc';
64 $output_format ||= 'iso2709';
65 $record_type ||= 'bibs';
67 # Retrocompatibility for the format parameter
68 $output_format = 'iso2709' if $output_format eq 'marc';
70 if ( $output_format eq 'csv' and $record_type eq 'auths' ) {
71 pod2usage(q|CSV output is only available for biblio records|);
75 if ( $timestamp and $record_type ne 'bibs' ) {
76 pod2usage(q|--timestamp can only be used with biblios|);
79 if ( $record_type ne 'bibs' and $record_type ne 'auths' ) {
80 pod2usage(q|--record_type is not valid|);
83 if ( $deleted_barcodes and $record_type ne 'bibs' ) {
84 pod2usage(q|--deleted_barcodes can only be used with biblios|);
87 $start_accession = dt_from_string( $start_accession ) if $start_accession;
88 $end_accession = dt_from_string( $end_accession ) if $end_accession;
90 my $dbh = C4::Context->dbh;
92 # Redirect stdout
93 open STDOUT, '>', $filename if $filename;
96 my @record_ids;
98 $timestamp = ($timestamp) ? output_pref({ dt => dt_from_string($timestamp), dateformat => 'iso', dateonly => 0, }): '';
100 if ( $record_type eq 'bibs' ) {
101 if ( $timestamp ) {
102 push @record_ids, $_->{biblionumber} for @{
103 $dbh->selectall_arrayref(q| (
104 SELECT biblionumber
105 FROM biblioitems
106 LEFT JOIN items USING(biblionumber)
107 WHERE biblioitems.timestamp >= ?
108 OR items.timestamp >= ?
109 ) UNION (
110 SELECT biblionumber
111 FROM biblioitems
112 LEFT JOIN deleteditems USING(biblionumber)
113 WHERE biblioitems.timestamp >= ?
114 OR deleteditems.timestamp >= ?
115 ) |, { Slice => {} }, ( $timestamp ) x 4 );
117 } else {
118 my $conditions = {
119 ( $starting_biblionumber or $ending_biblionumber )
121 "me.biblionumber" => {
122 ( $starting_biblionumber ? ( '>=' => $starting_biblionumber ) : () ),
123 ( $ending_biblionumber ? ( '<=' => $ending_biblionumber ) : () ),
126 : (),
127 ( $starting_callnumber or $ending_callnumber )
129 callnumber => {
130 ( $starting_callnumber ? ( '>=' => $starting_callnumber ) : () ),
131 ( $ending_callnumber ? ( '<=' => $ending_callnumber ) : () ),
134 : (),
135 ( $start_accession or $end_accession )
137 dateaccessioned => {
138 ( $start_accession ? ( '>=' => $start_accession ) : () ),
139 ( $end_accession ? ( '<=' => $end_accession ) : () ),
142 : (),
143 ( $itemtype
145 C4::Context->preference('item-level_itypes')
146 ? ( 'items.itype' => $itemtype )
147 : ( 'me.itemtype' => $itemtype )
148 : ()
152 my $biblioitems = Koha::Biblioitems->search( $conditions, { join => 'items' } );
153 while ( my $biblioitem = $biblioitems->next ) {
154 push @record_ids, $biblioitem->biblionumber;
158 elsif ( $record_type eq 'auths' ) {
159 my $conditions = {
160 ( $starting_authid or $ending_authid )
162 authid => {
163 ( $starting_authid ? ( '>=' => $starting_authid ) : () ),
164 ( $ending_authid ? ( '<=' => $ending_authid ) : () ),
167 : (),
168 ( $authtype ? ( authtypecode => $authtype ) : () ),
170 # Koha::MetadataRecord::Authority is not a Koha::Object...
171 my $authorities = Koha::Database->new->schema->resultset('AuthHeader')->search( $conditions );
172 @record_ids = map { $_->authid } $authorities->all;
175 @record_ids = uniq @record_ids;
176 if ( @record_ids and $id_list_file ) {
177 open my $fh, '<', $id_list_file or die "Cannot open file $id_list_file ($!)";
178 my @filter_record_ids = <$fh>;
179 @filter_record_ids = map { my $id = $_; $id =~ s/[\r\n]*$//; $id } @filter_record_ids;
180 # intersection
181 my %record_ids = map { $_ => 1 } @record_ids;
182 @record_ids = grep $record_ids{$_}, @filter_record_ids;
185 if ($deleted_barcodes) {
186 for my $record_id ( @record_ids ) {
187 my $barcode = $dbh->selectall_arrayref(q|
188 SELECT DISTINCT barcode
189 FROM deleteditems
190 WHERE deleteditems.biblionumber = ?
191 |, { Slice => {} }, $record_id );
192 say $_->{barcode} for @$barcode;
195 else {
196 unless ( $csv_profile_id ) {
197 # FIXME export_format.profile should be a unique key
198 my $default_csv_profiles = Koha::CsvProfiles->search({ profile => C4::Context->preference('ExportWithCsvProfile') });
199 $csv_profile_id = $default_csv_profiles->count ? $default_csv_profiles->next->export_format_id : undef;
201 Koha::Exporter::Record::export(
202 { record_type => $record_type,
203 record_ids => \@record_ids,
204 format => $output_format,
205 csv_profile_id => $csv_profile_id,
206 export_items => (not $dont_export_items),
207 clean => $clean || 0,
211 exit;
214 =head1 NAME
216 export records - This script exports record (biblios or authorities)
218 =head1 SYNOPSIS
220 export_records.pl [-h|--help] [--format=format] [--date=datetime] [--record-type=TYPE] [--dont_export_items] [--deleted_barcodes] [--clean] [--id_list_file=PATH] --filename=outputfile
222 =head1 OPTIONS
224 =over
226 =item B<-h|--help>
228 Print a brief help message.
230 =item B<--format>
232 --format=FORMAT FORMAT is either 'xml', 'csv' (biblio records only) or 'marc' (default).
234 =item B<--date>
236 --date=DATETIME DATETIME should be entered as the 'dateformat' syspref is
237 set (dd/mm/yyyy[ hh:mm:ss] for metric, yyyy-mm-dd[ hh:mm:ss] for iso,
238 mm/dd/yyyy[ hh:mm:ss] for us) records exported are the ones that
239 have been modified since DATETIME.
241 =item B<--record-type>
243 --record-type=TYPE TYPE is 'bibs' or 'auths'.
245 =item B<--dont_export_items>
247 --dont_export_items If enabled, the item infos won't be exported.
249 =item B<--csv_profile_id>
251 --csv_profile_id=ID Generate a CSV file with the given CSV profile id (see tools/csv-profiles.pl)
252 Unless provided, the one defined in the system preference 'ExportWithCsvProfile' will be used.
253 This can only be used to export biblio records.
255 =item B<--deleted_barcodes>
257 --deleted_barcodes If used, a list of barcodes of items deleted since DATE
258 is produced (or from all deleted items if no date is
259 specified). Used only if TYPE is 'bibs'.
261 =item B<--clean>
263 --clean removes NSE/NSB.
265 =item B<--id_list_file>
267 --id_list_file=PATH PATH is a path to a file containing a list of
268 IDs (biblionumber or authid) with one ID per line.
269 This list works as a filter; it is compatible with
270 other parameters for selecting records.
272 =item B<--filename>
274 --filename=FILENAME FILENAME used to export the data.
276 =item B<--starting_authid>
278 --starting_authid=ID Export authorities with authid >= ID
280 =item B<--ending_authid>
282 --ending_authid=ID Export authorities with authid <= ID
284 =item B<--authtype>
286 --authtype=AUTHTYPE Export authorities from the given AUTHTYPE
288 =item B<--starting_biblionumber>
290 --starting_biblionumber=ID Export biblio with biblionumber >= ID
292 =item B<--ending_biblionumber>
294 --ending_biblionumber=ID Export biblio with biblionumber <= ID
296 =item B<--itemtype>
298 --itemtype=ITEMTYPE Export biblio from the given ITEMTYPE
300 =item B<--starting_callnumber>
302 --starting_callnumber=CALLNUMBER Export biblio with callnumber >=CALLNUMBER
304 =item B<--ending_callnumber>
306 --ending_callnumber=CALLNUMBER Export biblio with callnumber <=CALLNUMBER
308 =item B<--start_accession>
310 --starting_accession=DATE Export biblio with an item accessionned after DATE
312 =item B<--end_accession>
314 --end_accession=DATE Export biblio with an item accessionned after DATE
316 =back
318 =head1 AUTHOR
320 Koha Development Team
322 =head1 COPYRIGHT
324 Copyright Koha Team
326 =head1 LICENSE
328 This file is part of Koha.
330 Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
331 Foundation; either version 3 of the License, or (at your option) any later version.
333 You should have received a copy of the GNU General Public License along
334 with Koha; if not, write to the Free Software Foundation, Inc.,
335 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
337 =cut