Bug 14722: rename the script
[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::Csv;
28 use C4::Record;
29
30 use Koha::Biblioitems;
31 use Koha::Database;
32 use Koha::Exporter::Record;
33 use Koha::DateUtils qw( dt_from_string output_pref );
34
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);
58
59 if ($help) {
60     pod2usage(1);
61 }
62
63 $filename ||= 'koha.mrc';
64 $output_format ||= 'iso2709';
65 $record_type ||= 'bibs';
66
67 # Retrocompatibility for the format parameter
68 $output_format = 'iso2709' if $output_format eq 'marc';
69
70
71 if ( $timestamp and $record_type ne 'bibs' ) {
72     pod2usage(q|--timestamp can only be used with biblios|);
73 }
74
75 if ( $record_type ne 'bibs' and $record_type ne 'auths' ) {
76     pod2usage(q|--record_type is not valid|);
77 }
78
79 if ( $deleted_barcodes and $record_type ne 'bibs' ) {
80     pod2usage(q|--deleted_barcodes can only be used with biblios|);
81 }
82
83 $start_accession = dt_from_string( $start_accession ) if $start_accession;
84 $end_accession   = dt_from_string( $end_accession )   if $end_accession;
85
86 my $dbh = C4::Context->dbh;
87
88 # Redirect stdout
89 open STDOUT, '>', $filename if $filename;
90
91
92 my @record_ids;
93
94 $timestamp = ($timestamp) ? output_pref({ dt => dt_from_string($timestamp), dateformat => 'iso', dateonly => 1, }): '';
95
96 if ( $record_type eq 'bibs' ) {
97     if ( $timestamp ) {
98         push @record_ids, $_->{biblionumber} for @{
99             $dbh->selectall_arrayref(q| (
100                 SELECT biblionumber
101                 FROM biblioitems
102                   LEFT JOIN items USING(biblionumber)
103                 WHERE biblioitems.timestamp >= ?
104                   OR items.timestamp >= ?
105             ) UNION (
106                 SELECT biblionumber
107                 FROM biblioitems
108                   LEFT JOIN deleteditems USING(biblionumber)
109                 WHERE biblioitems.timestamp >= ?
110                   OR deleteditems.timestamp >= ?
111             ) |, { Slice => {} }, ( $timestamp ) x 4 );
112         };
113     } else {
114         my $conditions = {
115             ( $starting_biblionumber or $ending_biblionumber )
116                 ? (
117                     "me.biblionumber" => {
118                         ( $starting_biblionumber ? ( '>=' => $starting_biblionumber ) : () ),
119                         ( $ending_biblionumber   ? ( '<=' => $ending_biblionumber   ) : () ),
120                     }
121                 )
122                 : (),
123             ( $starting_callnumber or $ending_callnumber )
124                 ? (
125                     callnumber => {
126                         ( $starting_callnumber ? ( '>=' => $starting_callnumber ) : () ),
127                         ( $ending_callnumber   ? ( '<=' => $ending_callnumber   ) : () ),
128                     }
129                 )
130                 : (),
131             ( $start_accession or $end_accession )
132                 ? (
133                     dateaccessioned => {
134                         ( $start_accession ? ( '>=' => $start_accession ) : () ),
135                         ( $end_accession   ? ( '<=' => $end_accession   ) : () ),
136                     }
137                 )
138                 : (),
139             ( $itemtype
140                 ?
141                   C4::Context->preference('item-level_itypes')
142                     ? ( 'items.itype' => $itemtype )
143                     : ( 'biblioitems.itemtype' => $itemtype )
144                 : ()
145             ),
146
147         };
148         my $biblioitems = Koha::Biblioitems->search( $conditions, { join => 'items' } );
149         while ( my $biblioitem = $biblioitems->next ) {
150             push @record_ids, $biblioitem->biblionumber;
151         }
152     }
153 }
154 elsif ( $record_type eq 'auths' ) {
155     my $conditions = {
156         ( $starting_authid or $ending_authid )
157             ? (
158                 authid => {
159                     ( $starting_authid ? ( '>=' => $starting_authid ) : () ),
160                     ( $ending_authid   ? ( '<=' => $ending_authid   ) : () ),
161                 }
162             )
163             : (),
164         ( $authtype ? ( authtypecode => $authtype ) : () ),
165     };
166     # Koha::Authority is not a Koha::Object...
167     my $authorities = Koha::Database->new->schema->resultset('AuthHeader')->search( $conditions );
168     @record_ids = map { $_->authid } $authorities->all;
169 }
170
171 @record_ids = uniq @record_ids;
172 if ( @record_ids and my $id_list_file ) {
173     my @filter_record_ids = <$id_list_file>;
174     @filter_record_ids = map { my $id = $_; $id =~ s/[\r\n]*$// } @filter_record_ids;
175     # intersection
176     my %record_ids = map { $_ => 1 } @record_ids;
177     @record_ids = grep $record_ids{$_}, @filter_record_ids;
178 }
179
180 if ($deleted_barcodes) {
181     for my $record_id ( @record_ids ) {
182         my $q = q|
183         |;
184         my $barcode = $dbh->selectall_arrayref(q| (
185             SELECT DISTINCT barcode
186             FROM deleteditems
187             WHERE deleteditems.biblionumber = ?
188         |, { Slice => {} }, $record_id );
189         say $_->{barcode} for @$barcode
190     }
191 }
192 else {
193     Koha::Exporter::Record::export(
194         {   record_type        => $record_type,
195             record_ids         => \@record_ids,
196             format             => $output_format,
197             csv_profile_id     => ( $csv_profile_id || GetCsvProfileId( C4::Context->preference('ExportWithCsvProfile') ) || undef ),
198             export_items       => (not $dont_export_items),
199             clean              => $clean || 0,
200         }
201     );
202 }
203 exit;
204
205
206 =head1 NAME
207
208 export records - This script exports record (biblios or authorities)
209
210 =head1 SYNOPSIS
211
212 export_records.pl [-h|--help] [--format=format] [--date=date] [--record-type=TYPE] [--dont_export_items] [--deleted_barcodes] [--clean] [--id_list_file=PATH] --filename=outputfile
213
214 =head1 OPTIONS
215
216 =over
217
218 =item B<-h|--help>
219
220 Print a brief help message.
221
222 =item B<--format>
223
224  --format=FORMAT        FORMAT is either 'xml', 'csv'  or 'marc' (default).
225
226 =item B<--date>
227
228  --date=DATE            DATE should be entered as the 'dateformat' syspref is
229                         set (dd/mm/yyyy for metric, yyyy-mm-dd for iso,
230                         mm/dd/yyyy for us) records exported are the ones that
231                         have been modified since DATE.
232
233 =item B<--record-type>
234
235  --record-type=TYPE     TYPE is 'bibs' or 'auths'.
236
237 =item B<--dont_export_items>
238
239  --dont_export_items    If enabled, the item infos won't be exported.
240
241 =item B<--csv_profile_id>
242
243  --csv_profile_id=ID    Generate a CSV file with the given CSV profile id (see tools/csv-profiles.pl)
244                         Unless provided, the one defined in the system preference 'ExportWithCsvProfile' will be used.
245
246 =item B<--deleted_barcodes>
247
248  --deleted_barcodes     If used, a list of barcodes of items deleted since DATE
249                         is produced (or from all deleted items if no date is
250                         specified). Used only if TYPE is 'bibs'.
251
252 =item B<--clean>
253
254  --clean                removes NSE/NSB.
255
256 =item B<--id_list_file>
257
258  --id_list_file=PATH    PATH is a path to a file containing a list of
259                         IDs (biblionumber or authid) with one ID per line.
260                         This list works as a filter; it is compatible with
261                         other parameters for selecting records.
262
263 =item B<--filename>
264
265  --filename=FILENAME   FILENAME used to export the data.
266
267 =item B<--starting_authid>
268
269 =item B<--ending_authid>
270
271 =item B<--authtype>
272
273 =item B<--starting_biblionumber>
274
275 =item B<--ending_biblionumber>
276
277 =item B<--itemtype>
278
279 =item B<--starting_callnumber>
280
281 =item B<--ending_callnumber>
282
283 =item B<--start_accession>
284
285 =item B<--end_accession>
286
287 =back
288
289 =head1 AUTHOR
290
291 Koha Development Team
292
293 =head1 COPYRIGHT
294
295 Copyright Koha Team
296
297 =head1 LICENSE
298
299 This file is part of Koha.
300
301 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
302 Foundation; either version 3 of the License, or (at your option) any later version.
303
304 You should have received a copy of the GNU General Public License along
305 with Koha; if not, write to the Free Software Foundation, Inc.,
306 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
307
308 =cut