Bug 19661: (follow-up) Use Basic auth in tests
[koha.git] / misc / export_records.pl
1 #!/usr/bin/perl
2
3 #
4 # This file is part of Koha.
5 #
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.
10 #
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.
15 #
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>.
18
19 use Modern::Perl;
20 use MARC::File::XML;
21 use List::MoreUtils qw(uniq);
22 use Getopt::Long;
23 use Pod::Usage;
24
25 use C4::Auth;
26 use C4::Context;
27 use C4::Record;
28
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 );
34
35 my (
36     $output_format,
37     $timestamp,
38     $dont_export_items,
39     $csv_profile_id,
40     $deleted_barcodes,
41     $clean,
42     $filename,
43     $record_type,
44     $id_list_file,
45     $starting_authid,
46     $ending_authid,
47     $authtype,
48     $starting_biblionumber,
49     $ending_biblionumber,
50     $itemtype,
51     $starting_callnumber,
52     $ending_callnumber,
53     $start_accession,
54     $end_accession,
55     $marc_conditions,
56     $help
57 );
58
59 GetOptions(
60     'format=s'                => \$output_format,
61     'date=s'                  => \$timestamp,
62     'dont_export_items'       => \$dont_export_items,
63     'csv_profile_id=s'        => \$csv_profile_id,
64     'deleted_barcodes'        => \$deleted_barcodes,
65     'clean'                   => \$clean,
66     'filename=s'              => \$filename,
67     'record-type=s'           => \$record_type,
68     'id_list_file=s'          => \$id_list_file,
69     'starting_authid=s'       => \$starting_authid,
70     'ending_authid=s'         => \$ending_authid,
71     'authtype=s'              => \$authtype,
72     'starting_biblionumber=s' => \$starting_biblionumber,
73     'ending_biblionumber=s'   => \$ending_biblionumber,
74     'itemtype=s'              => \$itemtype,
75     'starting_callnumber=s'   => \$starting_callnumber,
76     'ending_callnumber=s'     => \$ending_callnumber,
77     'start_accession=s'       => \$start_accession,
78     'end_accession=s'         => \$end_accession,
79     'marc_conditions=s'       => \$marc_conditions,
80     'h|help|?'                => \$help
81 ) || pod2usage(1);
82
83 if ($help) {
84     pod2usage(1);
85 }
86
87 $filename ||= 'koha.mrc';
88 $output_format ||= 'iso2709';
89 $record_type ||= 'bibs';
90
91 # Retrocompatibility for the format parameter
92 $output_format = 'iso2709' if $output_format eq 'marc';
93
94 if ( $output_format eq 'csv' and $record_type eq 'auths' ) {
95     pod2usage(q|CSV output is only available for biblio records|);
96 }
97
98 if ( $output_format eq 'csv' and not $csv_profile_id ) {
99     pod2usage(q|Define a csv profile to export in CSV|);
100 }
101
102 if ( $timestamp and $record_type ne 'bibs' ) {
103     pod2usage(q|--timestamp can only be used with biblios|);
104 }
105
106 if ( $record_type ne 'bibs' and $record_type ne 'auths' ) {
107     pod2usage(q|--record_type is not valid|);
108 }
109
110 if ( $deleted_barcodes and $record_type ne 'bibs' ) {
111     pod2usage(q|--deleted_barcodes can only be used with biblios|);
112 }
113
114 $start_accession = dt_from_string( $start_accession ) if $start_accession;
115 $end_accession   = dt_from_string( $end_accession )   if $end_accession;
116
117 # Parse marc conditions
118 my @marc_conditions;
119 if ($marc_conditions) {
120     foreach my $condition (split(/,\s*/, $marc_conditions)) {
121         if ($condition =~ /^(\d{3})([\w\d]?)(=|(?:!=)|>|<)([^,]+)$/) {
122             push @marc_conditions, [$1, $2, $3, $4];
123         }
124         elsif ($condition =~ /^(exists|not_exists)\((\d{3})([\w\d]?)\)$/) {
125             push @marc_conditions, [$2, $3, $1 eq 'exists' ? '?' : '!?'];
126         }
127         else {
128             die("Invalid condititon: $condition");
129         }
130     }
131 }
132
133 my $dbh = C4::Context->dbh;
134
135 # Redirect stdout
136 open STDOUT, '>', $filename if $filename;
137
138
139 my @record_ids;
140
141 $timestamp = ($timestamp) ? output_pref({ dt => dt_from_string($timestamp), dateformat => 'iso', dateonly => 0, }): '';
142
143 if ( $record_type eq 'bibs' ) {
144     if ( $timestamp ) {
145         push @record_ids, $_->{biblionumber} for @{
146             $dbh->selectall_arrayref(q| (
147                 SELECT biblio_metadata.biblionumber
148                 FROM biblio_metadata
149                   LEFT JOIN items USING(biblionumber)
150                 WHERE biblio_metadata.timestamp >= ?
151                   OR items.timestamp >= ?
152             ) UNION (
153                 SELECT biblio_metadata.biblionumber
154                 FROM biblio_metadata
155                   LEFT JOIN deleteditems USING(biblionumber)
156                 WHERE biblio_metadata.timestamp >= ?
157                   OR deleteditems.timestamp >= ?
158             ) |, { Slice => {} }, ( $timestamp ) x 4 );
159         };
160     } else {
161         my $conditions = {
162             ( $starting_biblionumber or $ending_biblionumber )
163                 ? (
164                     "me.biblionumber" => {
165                         ( $starting_biblionumber ? ( '>=' => $starting_biblionumber ) : () ),
166                         ( $ending_biblionumber   ? ( '<=' => $ending_biblionumber   ) : () ),
167                     }
168                 )
169                 : (),
170             ( $starting_callnumber or $ending_callnumber )
171                 ? (
172                     callnumber => {
173                         ( $starting_callnumber ? ( '>=' => $starting_callnumber ) : () ),
174                         ( $ending_callnumber   ? ( '<=' => $ending_callnumber   ) : () ),
175                     }
176                 )
177                 : (),
178             ( $start_accession or $end_accession )
179                 ? (
180                     dateaccessioned => {
181                         ( $start_accession ? ( '>=' => $start_accession ) : () ),
182                         ( $end_accession   ? ( '<=' => $end_accession   ) : () ),
183                     }
184                 )
185                 : (),
186             ( $itemtype
187                 ?
188                   C4::Context->preference('item-level_itypes')
189                     ? ( 'items.itype' => $itemtype )
190                     : ( 'me.itemtype' => $itemtype )
191                 : ()
192             ),
193
194         };
195         my $biblioitems = Koha::Biblioitems->search( $conditions, { join => 'items' } );
196         while ( my $biblioitem = $biblioitems->next ) {
197             push @record_ids, $biblioitem->biblionumber;
198         }
199     }
200 }
201 elsif ( $record_type eq 'auths' ) {
202     my $conditions = {
203         ( $starting_authid or $ending_authid )
204             ? (
205                 authid => {
206                     ( $starting_authid ? ( '>=' => $starting_authid ) : () ),
207                     ( $ending_authid   ? ( '<=' => $ending_authid   ) : () ),
208                 }
209             )
210             : (),
211         ( $authtype ? ( authtypecode => $authtype ) : () ),
212     };
213     # Koha::MetadataRecord::Authority is not a Koha::Object...
214     my $authorities = Koha::Database->new->schema->resultset('AuthHeader')->search( $conditions );
215     @record_ids = map { $_->authid } $authorities->all;
216 }
217
218 @record_ids = uniq @record_ids;
219 if ( @record_ids and $id_list_file ) {
220     open my $fh, '<', $id_list_file or die "Cannot open file $id_list_file ($!)";
221     my @filter_record_ids = <$fh>;
222     @filter_record_ids = map { my $id = $_; $id =~ s/[\r\n]*$//; $id } @filter_record_ids;
223     # intersection
224     my %record_ids = map { $_ => 1 } @record_ids;
225     @record_ids = grep $record_ids{$_}, @filter_record_ids;
226 }
227
228 if ($deleted_barcodes) {
229     for my $record_id ( @record_ids ) {
230         my $barcode = $dbh->selectall_arrayref(q|
231             SELECT DISTINCT barcode
232             FROM deleteditems
233             WHERE deleteditems.biblionumber = ?
234         |, { Slice => {} }, $record_id );
235         say $_->{barcode} for @$barcode;
236     }
237 }
238 else {
239     Koha::Exporter::Record::export(
240         {   record_type        => $record_type,
241             record_ids         => \@record_ids,
242             record_conditions  => @marc_conditions ? \@marc_conditions : undef,
243             format             => $output_format,
244             csv_profile_id     => $csv_profile_id,
245             export_items       => (not $dont_export_items),
246             clean              => $clean || 0,
247         }
248     );
249 }
250 exit;
251
252
253 =head1 NAME
254
255 export records - This script exports record (biblios or authorities)
256
257 =head1 SYNOPSIS
258
259 export_records.pl [-h|--help] [--format=format] [--date=datetime] [--record-type=TYPE] [--dont_export_items] [--deleted_barcodes] [--clean] [--id_list_file=PATH] --filename=outputfile
260
261 =head1 OPTIONS
262
263 =over
264
265 =item B<-h|--help>
266
267 Print a brief help message.
268
269 =item B<--format>
270
271  --format=FORMAT        FORMAT is either 'xml', 'csv' (biblio records only) or 'marc' (default).
272
273 =item B<--date>
274
275  --date=DATETIME        DATETIME should be entered as the 'dateformat' syspref is
276                         set (dd/mm/yyyy[ hh:mm:ss] for metric, yyyy-mm-dd[ hh:mm:ss] for iso,
277                         mm/dd/yyyy[ hh:mm:ss] for us) records exported are the ones that
278                         have been modified since DATETIME.
279
280 =item B<--record-type>
281
282  --record-type=TYPE     TYPE is 'bibs' or 'auths'.
283
284 =item B<--dont_export_items>
285
286  --dont_export_items    If enabled, the item infos won't be exported.
287
288 =item B<--csv_profile_id>
289
290  --csv_profile_id=ID    Generate a CSV file with the given CSV profile id (see tools/csv-profiles.pl)
291                         This can only be used to export biblio records.
292
293 =item B<--deleted_barcodes>
294
295  --deleted_barcodes     If used, a list of barcodes of items deleted since DATE
296                         is produced (or from all deleted items if no date is
297                         specified). Used only if TYPE is 'bibs'.
298
299 =item B<--clean>
300
301  --clean                removes NSE/NSB.
302
303 =item B<--id_list_file>
304
305  --id_list_file=PATH    PATH is a path to a file containing a list of
306                         IDs (biblionumber or authid) with one ID per line.
307                         This list works as a filter; it is compatible with
308                         other parameters for selecting records.
309
310 =item B<--filename>
311
312  --filename=FILENAME   FILENAME used to export the data.
313
314 =item B<--starting_authid>
315
316  --starting_authid=ID  Export authorities with authid >= ID
317
318 =item B<--ending_authid>
319
320  --ending_authid=ID    Export authorities with authid <= ID
321
322 =item B<--authtype>
323
324  --authtype=AUTHTYPE   Export authorities from the given AUTHTYPE
325
326 =item B<--starting_biblionumber>
327
328  --starting_biblionumber=ID  Export biblio with biblionumber >= ID
329
330 =item B<--ending_biblionumber>
331
332  --ending_biblionumber=ID    Export biblio with biblionumber <= ID
333
334 =item B<--itemtype>
335
336  --itemtype=ITEMTYPE         Export biblio from the given ITEMTYPE
337
338 =item B<--starting_callnumber>
339
340  --starting_callnumber=CALLNUMBER Export biblio with callnumber >=CALLNUMBER
341
342 =item B<--ending_callnumber>
343
344  --ending_callnumber=CALLNUMBER Export biblio with callnumber <=CALLNUMBER
345
346 =item B<--start_accession>
347
348  --starting_accession=DATE      Export biblio with an item accessionned after DATE
349
350 =item B<--end_accession>
351
352  --end_accession=DATE           Export biblio with an item accessionned after DATE
353
354 =item B<--marc_conditions>
355
356  --marc_conditions=CONDITIONS   Only include biblios with MARC data matching CONDITIONS.
357                                 CONDITIONS is on the format: <marc_target><binary_operator><value>,
358                                 or <unary_operation>(<marc_target>).
359                                 with multiple conditions separated by commas (,).
360                                 For example: --marc_conditions="035a!=(EXAMPLE)123,041a=swe".
361                                 Multiple conditions are all required to match.
362                                 If <marc_target> has multiple values all values
363                                 are also required to match.
364                                 Valid operators are: = (equal to), != (not equal to),
365                                 > (great than) and < (less than).
366
367                                 Two unary operations are also supported:
368                                 exists(<marc_target>) and not_exists(<marc_target>).
369                                 For example: --marc_conditions="exists(035a)".
370
371                                 "exists(<marc_target)" will include marc records where
372                                 <marc_target> exists regardless of target value, and
373                                 "exists(<marc_target>)" will include marc records where
374                                 no <marc_target> exists.
375
376 =back
377
378 =head1 AUTHOR
379
380 Koha Development Team
381
382 =head1 COPYRIGHT
383
384 Copyright Koha Team
385
386 =head1 LICENSE
387
388 This file is part of Koha.
389
390 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
391 Foundation; either version 3 of the License, or (at your option) any later version.
392
393 You should have received a copy of the GNU General Public License along
394 with Koha; if not, write to the Free Software Foundation, Inc.,
395 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
396
397 =cut