Increment version for 3.22.21
[koha.git] / tools / export.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 CGI qw ( -utf8 );
21 use MARC::File::XML;
22 use List::MoreUtils qw(uniq);
23 use C4::Auth;
24 use C4::Branch;             # GetBranches
25 use C4::Csv;
26 use C4::Koha;               # GetItemTypes
27 use C4::Output;
28
29 use Koha::Biblioitems;
30 use Koha::Database;
31 use Koha::DateUtils qw( dt_from_string output_pref );
32 use Koha::Exporter::Record;
33
34 my $query = new CGI;
35
36 my $dont_export_items = $query->param("dont_export_item") || 0;
37 my $record_type       = $query->param("record_type");
38 my $op                = $query->param("op") || '';
39 my $output_format     = $query->param("format") || $query->param("output_format") || 'iso2709';
40 my $backupdir         = C4::Context->config('backupdir');
41 my $filename          = $query->param("filename") || ( $output_format eq 'csv' ? 'koha.csv' : 'koha.mrc' );
42 $filename =~ s/(\r|\n)//;
43
44 my $dbh = C4::Context->dbh;
45
46 my @record_ids;
47 # biblionumbers is sent from circulation.pl only
48 if ( $query->param("biblionumbers") ) {
49     $record_type = 'bibs';
50     @record_ids = $query->multi_param("biblionumbers");
51 }
52
53 # Default value for output_format is 'iso2709'
54 $output_format ||= 'iso2709';
55 # Retrocompatibility for the format parameter
56 $output_format = 'iso2709' if $output_format eq 'marc';
57
58 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
59     {
60         template_name   => "tools/export.tt",
61         query           => $query,
62         type            => "intranet",
63         authnotrequired => 0,
64         flagsrequired   => { tools => 'export_catalog' },
65         debug           => 1,
66     }
67 );
68
69 my @branch = $query->multi_param("branch");
70 my $only_my_branch;
71 # Limit to local branch if IndependentBranches and not superlibrarian
72 if (
73     (
74           C4::Context->preference('IndependentBranches')
75         && C4::Context->userenv
76         && !C4::Context->IsSuperLibrarian()
77         && C4::Context->userenv->{branch}
78     )
79     # Limit result to local branch strip_nonlocal_items
80     or $query->param('strip_nonlocal_items')
81 ) {
82     $only_my_branch = 1;
83     @branch = ( C4::Context->userenv->{'branch'} );
84 }
85
86 my %branchmap = map { $_ => 1 } @branch; # for quick lookups
87
88 if ( $op eq "export" ) {
89
90     my $export_remove_fields = $query->param("export_remove_fields") || q||;
91     my @biblionumbers      = $query->multi_param("biblionumbers");
92     my @itemnumbers        = $query->multi_param("itemnumbers");
93     my @sql_params;
94     my $sql_query;
95
96     if ( $record_type eq 'bibs' or $record_type eq 'auths' ) {
97         # No need to retrieve the record_ids if we already get them
98         unless ( @record_ids ) {
99             if ( $record_type eq 'bibs' ) {
100                 my $starting_biblionumber = $query->param("StartingBiblionumber");
101                 my $ending_biblionumber   = $query->param("EndingBiblionumber");
102                 my $itemtype             = $query->param("itemtype");
103                 my $start_callnumber     = $query->param("start_callnumber");
104                 my $end_callnumber       = $query->param("end_callnumber");
105                 my $start_accession =
106                   ( $query->param("start_accession") )
107                   ? dt_from_string( scalar $query->param("start_accession") )
108                   : '';
109                 my $end_accession =
110                   ( $query->param("end_accession") )
111                   ? dt_from_string( scalar $query->param("end_accession") )
112                   : '';
113
114
115                 my $conditions = {
116                     ( $starting_biblionumber or $ending_biblionumber )
117                         ? (
118                             "me.biblionumber" => {
119                                 ( $starting_biblionumber ? ( '>=' => $starting_biblionumber ) : () ),
120                                 ( $ending_biblionumber   ? ( '<=' => $ending_biblionumber   ) : () ),
121                             }
122                         )
123                         : (),
124
125                     ( $start_callnumber or $end_callnumber )
126                         ? (
127                             'items.itemcallnumber' => {
128                                 ( $start_callnumber ? ( '>=' => $start_callnumber ) : () ),
129                                 ( $end_callnumber   ? ( '<=' => $end_callnumber   ) : () ),
130                             }
131                         )
132                         : (),
133
134                     ( $start_accession or $end_accession )
135                         ? (
136                             'items.dateaccessioned' => {
137                                 ( $start_accession ? ( '>=' => $start_accession ) : () ),
138                                 ( $end_accession   ? ( '<=' => $end_accession   ) : () ),
139                             }
140                         )
141                         : (),
142                     ( @branch ? ( 'items.homebranch' => { in => \@branch } ) : () ),
143                     ( $itemtype
144                         ?
145                           C4::Context->preference('item-level_itypes')
146                             ? ( 'items.itype' => $itemtype )
147                             : ( 'me.itemtype' => $itemtype )
148                         : ()
149                     ),
150
151                 };
152                 my $biblioitems = Koha::Biblioitems->search( $conditions, { join => 'items', columns => 'biblionumber' } );
153                 while ( my $biblioitem = $biblioitems->next ) {
154                     push @record_ids, $biblioitem->biblionumber;
155                 }
156             }
157             elsif ( $record_type eq 'auths' ) {
158                 my $starting_authid = $query->param('starting_authid');
159                 my $ending_authid   = $query->param('ending_authid');
160                 my $authtype        = $query->param('authtype');
161
162                 my $conditions = {
163                     ( $starting_authid or $ending_authid )
164                         ? (
165                             authid => {
166                                 ( $starting_authid ? ( '>=' => $starting_authid ) : () ),
167                                 ( $ending_authid   ? ( '<=' => $ending_authid   ) : () ),
168                             }
169                         )
170                         : (),
171                     ( $authtype ? ( authtypecode => $authtype ) : () ),
172                 };
173                 # Koha::Authority is not a Koha::Object...
174                 my $authorities = Koha::Database->new->schema->resultset('AuthHeader')->search( $conditions );
175                 @record_ids = map { $_->authid } $authorities->all;
176             }
177         }
178
179         @record_ids = uniq @record_ids;
180         if ( @record_ids and my $filefh = $query->upload("id_list_file") ) {
181             my @filter_record_ids = <$filefh>;
182             @filter_record_ids = map { my $id = $_; $id =~ s/[\r\n]*$//; $id } @filter_record_ids;
183             # intersection
184             my %record_ids = map { $_ => 1 } @record_ids;
185             @record_ids = grep $record_ids{$_}, @filter_record_ids;
186         }
187
188         print CGI->new->header(
189             -type       => 'application/octet-stream',
190             -charset    => 'utf-8',
191             -attachment => $filename,
192         );
193
194         Koha::Exporter::Record::export(
195             {   record_type        => $record_type,
196                 record_ids         => \@record_ids,
197                 format             => $output_format,
198                 filename           => $filename,
199                 itemnumbers        => \@itemnumbers,
200                 dont_export_fields => $export_remove_fields,
201                 csv_profile_id     => ( $query->param('csv_profile_id') || GetCsvProfileId( C4::Context->preference('ExportWithCsvProfile') ) || undef ),
202                 export_items       => (not $dont_export_items),
203             }
204         );
205     }
206     elsif ( $record_type eq 'db' or $record_type eq 'conf' ) {
207         my $successful_export;
208
209         if ( $flags->{superlibrarian}
210             and (
211                     $record_type eq 'db' and C4::Context->config('backup_db_via_tools')
212                  or
213                     $record_type eq 'conf' and C4::Context->config('backup_conf_via_tools')
214             )
215         ) {
216             binmode STDOUT, ':encoding(UTF-8)';
217
218             my $charset  = 'utf-8';
219             my $mimetype = 'application/octet-stream';
220             if ( $filename =~ m/\.gz$/ ) {
221                 $mimetype = 'application/x-gzip';
222                 $charset  = '';
223                 binmode STDOUT;
224             }
225             elsif ( $filename =~ m/\.bz2$/ ) {
226                 $mimetype = 'application/x-bzip2';
227                 binmode STDOUT;
228                 $charset = '';
229             }
230             print $query->header(
231                 -type       => $mimetype,
232                 -charset    => $charset,
233                 -attachment => $filename,
234             );
235
236             my $extension = $record_type eq 'db' ? 'sql' : 'tar';
237
238             $successful_export = download_backup(
239                 {
240                     directory => $backupdir,
241                     extension => $extension,
242                     filename  => $filename,
243                 }
244             );
245             unless ($successful_export) {
246                 my $remotehost = $query->remote_host();
247                 $remotehost =~ s/(\n|\r)//;
248                 warn
249     "A suspicious attempt was made to download the " . ( $record_type eq 'db' ? 'db' : 'configuration' ) . "at '$filename' by someone at "
250                   . $remotehost . "\n";
251             }
252         }
253     }
254
255     exit;
256 }
257
258 else {
259
260     my $itemtypes = GetItemTypes;
261     my @itemtypesloop;
262     foreach my $thisitemtype ( sort keys %$itemtypes ) {
263         my %row = (
264             value       => $thisitemtype,
265             description => $itemtypes->{$thisitemtype}->{translated_description},
266         );
267         push @itemtypesloop, \%row;
268     }
269     my $branches = GetBranches($only_my_branch);
270     my @branchloop;
271     for my $thisbranch (
272         sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} }
273         keys %{$branches}
274       )
275     {
276         push @branchloop,
277           {
278             value      => $thisbranch,
279             selected   => %branchmap ? $branchmap{$thisbranch} : 1,
280             branchname => $branches->{$thisbranch}->{'branchname'},
281           };
282     }
283
284     my $authtypes = getauthtypes;
285     my @authtypesloop;
286     foreach my $thisauthtype ( sort keys %$authtypes ) {
287         next unless $thisauthtype;
288         my %row = (
289             value       => $thisauthtype,
290             description => $authtypes->{$thisauthtype}->{'authtypetext'},
291         );
292         push @authtypesloop, \%row;
293     }
294
295     if (   $flags->{superlibrarian}
296         && C4::Context->config('backup_db_via_tools')
297         && $backupdir
298         && -d $backupdir )
299     {
300         $template->{VARS}->{'allow_db_export'} = 1;
301         $template->{VARS}->{'dbfiles'}         = getbackupfilelist(
302             { directory => "$backupdir", extension => 'sql' } );
303     }
304
305     if (   $flags->{superlibrarian}
306         && C4::Context->config('backup_conf_via_tools')
307         && $backupdir
308         && -d $backupdir )
309     {
310         $template->{VARS}->{'allow_conf_export'} = 1;
311         $template->{VARS}->{'conffiles'}         = getbackupfilelist(
312             { directory => "$backupdir", extension => 'tar' } );
313     }
314
315     $template->param(
316         branchloop               => \@branchloop,
317         itemtypeloop             => \@itemtypesloop,
318         authtypeloop             => \@authtypesloop,
319         export_remove_fields     => C4::Context->preference("ExportRemoveFields"),
320         csv_profiles             => C4::Csv::GetCsvProfiles('marc'),
321     );
322
323     output_html_with_http_headers $query, $cookie, $template->output;
324 }
325
326 sub getbackupfilelist {
327     my $args      = shift;
328     my $directory = $args->{directory};
329     my $extension = $args->{extension};
330     my @files;
331
332     if ( opendir( my $dir, $directory ) ) {
333         while ( my $file = readdir($dir) ) {
334             next unless ( $file =~ m/\.$extension(\.(gz|bz2|xz))?/ );
335             push @files, $file
336               if ( -f "$directory/$file" && -r "$directory/$file" );
337         }
338         closedir($dir);
339     }
340     return \@files;
341 }
342
343 sub download_backup {
344     my $args      = shift;
345     my $directory = $args->{directory};
346     my $extension = $args->{extension};
347     my $filename  = $args->{filename};
348
349     return unless ( $directory && -d $directory );
350     return unless ( $filename =~ m/\.$extension(\.(gz|bz2|xz))?$/ );
351     return if ( $filename =~ m#/# );
352     $filename = "$directory/$filename";
353     return unless ( -f $filename && -r $filename );
354     return unless ( open( my $dump, '<', $filename ) );
355     binmode $dump;
356
357     while ( read( $dump, my $data, 64 * 1024 ) ) {
358         print $data;
359     }
360     close($dump);
361     return 1;
362 }