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>.
21 use List::MoreUtils qw( uniq );
22 use Getopt::Long qw( GetOptions );
23 use Pod::Usage qw( pod2usage );
30 use Koha::Biblioitems;
32 use Koha::CsvProfiles;
33 use Koha::Exporter::Record;
34 use Koha::DateUtils qw( dt_from_string output_pref );
49 $starting_biblionumber,
61 'format=s' => \$output_format,
62 'date=s' => \$timestamp,
63 'dont_export_items' => \$dont_export_items,
64 'csv_profile_id=s' => \$csv_profile_id,
65 'deleted_barcodes' => \$deleted_barcodes,
67 'filename=s' => \$filename,
68 'record-type=s' => \$record_type,
69 'id_list_file=s' => \$id_list_file,
70 'starting_authid=s' => \$starting_authid,
71 'ending_authid=s' => \$ending_authid,
72 'authtype=s' => \$authtype,
73 'starting_biblionumber=s' => \$starting_biblionumber,
74 'ending_biblionumber=s' => \$ending_biblionumber,
75 'itemtype=s' => \$itemtype,
76 'starting_callnumber=s' => \$starting_callnumber,
77 'ending_callnumber=s' => \$ending_callnumber,
78 'start_accession=s' => \$start_accession,
79 'end_accession=s' => \$end_accession,
80 'marc_conditions=s' => \$marc_conditions,
88 $filename ||= 'koha.mrc';
89 $output_format ||= 'iso2709';
90 $record_type ||= 'bibs';
92 # Retrocompatibility for the format parameter
93 $output_format = 'iso2709' if $output_format eq 'marc';
95 if ( $output_format eq 'csv' and $record_type eq 'auths' ) {
96 pod2usage(q|CSV output is only available for biblio records|);
99 if ( $output_format eq 'csv' and not $csv_profile_id ) {
100 pod2usage(q|Define a csv profile to export in CSV|);
104 if ( $record_type ne 'bibs' and $record_type ne 'auths' ) {
105 pod2usage(q|--record_type is not valid|);
108 if ( $deleted_barcodes and $record_type ne 'bibs' ) {
109 pod2usage(q|--deleted_barcodes can only be used with biblios|);
112 $start_accession = dt_from_string( $start_accession ) if $start_accession;
113 $end_accession = dt_from_string( $end_accession ) if $end_accession;
115 # Parse marc conditions
117 if ($marc_conditions) {
118 foreach my $condition (split(/,\s*/, $marc_conditions)) {
119 if ($condition =~ /^(\d{3})([\w\d]?)(=|(?:!=)|>|<)([^,]+)$/) {
120 push @marc_conditions, [$1, $2, $3, $4];
122 elsif ($condition =~ /^(exists|not_exists)\((\d{3})([\w\d]?)\)$/) {
123 push @marc_conditions, [$2, $3, $1 eq 'exists' ? '?' : '!?'];
126 die("Invalid condititon: $condition");
131 my $dbh = C4::Context->dbh;
134 open STDOUT, '>', $filename if $filename;
139 $timestamp = ($timestamp) ? output_pref({ dt => dt_from_string($timestamp), dateformat => 'iso', dateonly => 0, }): '';
141 if ( $record_type eq 'bibs' ) {
143 if (!$dont_export_items) {
144 push @record_ids, $_->{biblionumber} for @{
145 $dbh->selectall_arrayref(q| (
146 SELECT biblio_metadata.biblionumber
148 LEFT JOIN items USING(biblionumber)
149 WHERE biblio_metadata.timestamp >= ?
150 OR items.timestamp >= ?
152 SELECT biblio_metadata.biblionumber
154 LEFT JOIN deleteditems USING(biblionumber)
155 WHERE biblio_metadata.timestamp >= ?
156 OR deleteditems.timestamp >= ?
157 ) |, { Slice => {} }, ( $timestamp ) x 4 );
160 push @record_ids, $_->{biblionumber} for @{
161 $dbh->selectall_arrayref(q| (
162 SELECT biblio_metadata.biblionumber
164 WHERE biblio_metadata.timestamp >= ?
165 ) |, { Slice => {} }, $timestamp );
170 ( $starting_biblionumber or $ending_biblionumber )
172 "me.biblionumber" => {
173 ( $starting_biblionumber ? ( '>=' => $starting_biblionumber ) : () ),
174 ( $ending_biblionumber ? ( '<=' => $ending_biblionumber ) : () ),
178 ( $starting_callnumber or $ending_callnumber )
181 ( $starting_callnumber ? ( '>=' => $starting_callnumber ) : () ),
182 ( $ending_callnumber ? ( '<=' => $ending_callnumber ) : () ),
186 ( $start_accession or $end_accession )
189 ( $start_accession ? ( '>=' => $start_accession ) : () ),
190 ( $end_accession ? ( '<=' => $end_accession ) : () ),
196 C4::Context->preference('item-level_itypes')
197 ? ( 'items.itype' => $itemtype )
198 : ( 'me.itemtype' => $itemtype )
203 my $biblioitems = Koha::Biblioitems->search( $conditions, { join => 'items' } );
204 while ( my $biblioitem = $biblioitems->next ) {
205 push @record_ids, $biblioitem->biblionumber;
209 elsif ( $record_type eq 'auths' ) {
211 push @record_ids, $_->{authid} for @{
212 $dbh->selectall_arrayref(
216 WHERE modification_time >= ?
217 ) |, { Slice => {} }, $timestamp
222 ( $starting_authid or $ending_authid )
225 ( $starting_authid ? ( '>=' => $starting_authid ) : () ),
226 ( $ending_authid ? ( '<=' => $ending_authid ) : () ),
230 ( $authtype ? ( authtypecode => $authtype ) : () ),
233 # Koha::MetadataRecord::Authority is not a Koha::Object...
234 my $authorities = Koha::Database->new->schema->resultset('AuthHeader')->search($conditions);
235 @record_ids = map { $_->authid } $authorities->all;
239 @record_ids = uniq @record_ids;
240 if ( @record_ids and $id_list_file ) {
241 open my $fh, '<', $id_list_file or die "Cannot open file $id_list_file ($!)";
242 my @filter_record_ids = <$fh>;
243 @filter_record_ids = map { my $id = $_; $id =~ s/[\r\n]*$//; $id } @filter_record_ids;
245 my %record_ids = map { $_ => 1 } @record_ids;
246 @record_ids = grep $record_ids{$_}, @filter_record_ids;
249 if ($deleted_barcodes) {
250 for my $record_id ( @record_ids ) {
251 my $barcode = $dbh->selectall_arrayref(q|
252 SELECT DISTINCT barcode
254 WHERE deleteditems.biblionumber = ?
255 AND barcode IS NOT NULL AND barcode != ''
256 |, { Slice => {} }, $record_id );
257 say $_->{barcode} for @$barcode;
261 Koha::Exporter::Record::export(
262 { record_type => $record_type,
263 record_ids => \@record_ids,
264 record_conditions => @marc_conditions ? \@marc_conditions : undef,
265 format => $output_format,
266 csv_profile_id => $csv_profile_id,
267 export_items => (not $dont_export_items),
268 clean => $clean || 0,
277 export records - This script exports record (biblios or authorities)
281 export_records.pl [-h|--help] [--format=format] [--date=datetime] [--record-type=TYPE] [--dont_export_items] [--deleted_barcodes] [--clean] [--id_list_file=PATH] --filename=outputfile
289 Print a brief help message.
293 --format=FORMAT FORMAT is either 'xml', 'csv' (biblio records only) or 'marc' (default).
297 --date=DATETIME DATETIME should be entered as the 'dateformat' syspref is
298 set (dd/mm/yyyy[ hh:mm:ss] for metric, yyyy-mm-dd[ hh:mm:ss] for iso,
299 mm/dd/yyyy[ hh:mm:ss] for us) records exported are the ones that
300 have been modified since DATETIME.
302 =item B<--record-type>
304 --record-type=TYPE TYPE is 'bibs' or 'auths'.
306 =item B<--dont_export_items>
308 --dont_export_items If enabled, the item infos won't be exported.
310 =item B<--csv_profile_id>
312 --csv_profile_id=ID Generate a CSV file with the given CSV profile id (see tools/csv-profiles.pl)
313 This can only be used to export biblio records.
315 =item B<--deleted_barcodes>
317 --deleted_barcodes If used, a list of barcodes of items deleted since DATE
318 is produced (or from all deleted items if no date is
319 specified). Used only if TYPE is 'bibs'.
323 --clean removes NSE/NSB.
325 =item B<--id_list_file>
327 --id_list_file=PATH PATH is a path to a file containing a list of
328 IDs (biblionumber or authid) with one ID per line.
329 This list works as a filter; it is compatible with
330 other parameters for selecting records.
334 --filename=FILENAME FILENAME used to export the data.
336 =item B<--starting_authid>
338 --starting_authid=ID Export authorities with authid >= ID
340 =item B<--ending_authid>
342 --ending_authid=ID Export authorities with authid <= ID
346 --authtype=AUTHTYPE Export authorities from the given AUTHTYPE
348 =item B<--starting_biblionumber>
350 --starting_biblionumber=ID Export biblio with biblionumber >= ID
352 =item B<--ending_biblionumber>
354 --ending_biblionumber=ID Export biblio with biblionumber <= ID
358 --itemtype=ITEMTYPE Export biblio from the given ITEMTYPE
360 =item B<--starting_callnumber>
362 --starting_callnumber=CALLNUMBER Export biblio with callnumber >=CALLNUMBER
364 =item B<--ending_callnumber>
366 --ending_callnumber=CALLNUMBER Export biblio with callnumber <=CALLNUMBER
368 =item B<--start_accession>
370 --starting_accession=DATE Export biblio with an item accessionned after DATE
372 =item B<--end_accession>
374 --end_accession=DATE Export biblio with an item accessionned after DATE
376 =item B<--marc_conditions>
378 --marc_conditions=CONDITIONS Only include biblios with MARC data matching CONDITIONS.
379 CONDITIONS is on the format: <marc_target><binary_operator><value>,
380 or <unary_operation>(<marc_target>).
381 with multiple conditions separated by commas (,).
382 For example: --marc_conditions="035a!=(EXAMPLE)123,041a=swe".
383 Multiple conditions are all required to match.
384 If <marc_target> has multiple values all values
385 are also required to match.
386 Valid operators are: = (equal to), != (not equal to),
387 > (great than) and < (less than).
389 Two unary operations are also supported:
390 exists(<marc_target>) and not_exists(<marc_target>).
391 For example: --marc_conditions="exists(035a)".
393 "exists(<marc_target)" will include marc records where
394 <marc_target> exists regardless of target value, and
395 "exists(<marc_target>)" will include marc records where
396 no <marc_target> exists.
402 Koha Development Team
410 This file is part of Koha.
412 # Koha is free software; you can redistribute it and/or modify it
413 # under the terms of the GNU General Public License as published by
414 # the Free Software Foundation; either version 3 of the License, or
415 # (at your option) any later version.
417 # Koha is distributed in the hope that it will be useful, but
418 # WITHOUT ANY WARRANTY; without even the implied warranty of
419 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
420 # GNU General Public License for more details.
422 # You should have received a copy of the GNU General Public License
423 # along with Koha; if not, see <http://www.gnu.org/licenses>.