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>.
22 use List::MoreUtils qw( uniq );
23 use C4::Auth qw( get_template_and_user );
24 use C4::Output qw( output_html_with_http_headers );
26 use Koha::Authority::Types;
27 use Koha::Biblioitems;
28 use Koha::CsvProfiles;
30 use Koha::DateUtils qw( dt_from_string );
31 use Koha::Exporter::Record;
37 my $dont_export_items = $query->param("dont_export_item") || 0;
38 my $record_type = $query->param("record_type");
39 my $op = $query->param("op") || '';
40 my $output_format = $query->param("format") || $query->param("output_format") || 'iso2709';
41 my $backupdir = C4::Context->config('backupdir');
43 if ( $record_type && $record_type eq 'auths' ) {
44 $filename = $query->param("filename_auth") || ( $output_format eq 'xml' ? 'koha.xml' : 'koha.mrc' );
46 $filename = $query->param("filename") || ( $output_format eq 'csv' ? 'koha.csv' : 'koha.mrc' );
48 $filename =~ s/(\r|\n)//;
50 my $dbh = C4::Context->dbh;
53 # biblionumbers is sent from circulation.pl only
54 if ( $query->param("biblionumbers") ) {
55 $record_type = 'bibs';
56 @record_ids = $query->multi_param("biblionumbers");
59 # Default value for output_format is 'iso2709'
60 $output_format ||= 'iso2709';
61 # Retrocompatibility for the format parameter
62 $output_format = 'iso2709' if $output_format eq 'marc';
64 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
66 template_name => "tools/export.tt",
69 flagsrequired => { tools => 'export_catalog' },
73 my @branch = $query->multi_param("branch");
76 if ( $op eq 'export' ) {
77 my $filename = $query->param('id_list_file');
79 my $mimetype = $query->uploadInfo($filename)->{'Content-Type'};
80 my @valid_mimetypes = qw( application/octet-stream text/csv text/plain application/vnd.ms-excel );
81 unless ( grep { $_ eq $mimetype } @valid_mimetypes ) {
82 push @messages, { type => 'alert', code => 'invalid_mimetype' };
88 if ( $op eq "export" ) {
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 $strip_items_not_from_libraries = $query->param('strip_items_not_from_libraries');
95 my $libraries = Koha::Libraries->search_filtered->unblessed;
96 my $only_export_items_for_branches = $strip_items_not_from_libraries ? \@branch : undef;
98 for my $branchcode ( @branch ) {
99 if ( grep { $_->{branchcode} eq $branchcode } @$libraries ) {
100 push @branchcodes, $branchcode;
104 if ( $record_type eq 'bibs' or $record_type eq 'auths' ) {
105 # No need to retrieve the record_ids if we already get them
106 unless ( @record_ids ) {
107 if ( $record_type eq 'bibs' ) {
109 my %it_map = map { $_->itemtype => 1 } Koha::ItemTypes->search->as_list;
110 my @itemtypes = map { $it_map{$_} ? $_ : () } $query->multi_param('itemtype'); #Validate inputs against map
112 my $starting_biblionumber = $query->param("StartingBiblionumber");
113 my $ending_biblionumber = $query->param("EndingBiblionumber");
114 my $start_callnumber = $query->param("start_callnumber");
115 my $end_callnumber = $query->param("end_callnumber");
116 my $start_accession =
117 ( $query->param("start_accession") )
118 ? dt_from_string( scalar $query->param("start_accession") )
121 ( $query->param("end_accession") )
122 ? dt_from_string( scalar $query->param("end_accession") )
127 ( $starting_biblionumber or $ending_biblionumber )
129 "me.biblionumber" => {
130 ( $starting_biblionumber ? ( '>=' => $starting_biblionumber ) : () ),
131 ( $ending_biblionumber ? ( '<=' => $ending_biblionumber ) : () ),
136 ( $start_callnumber or $end_callnumber )
138 'items.itemcallnumber' => {
139 ( $start_callnumber ? ( '>=' => $start_callnumber ) : () ),
140 ( $end_callnumber ? ( '<=' => $end_callnumber ) : () ),
145 ( $start_accession or $end_accession )
147 'items.dateaccessioned' => {
148 ( $start_accession ? ( '>=' => $start_accession ) : () ),
149 ( $end_accession ? ( '<=' => $end_accession ) : () ),
153 ( @branchcodes ? ( 'items.homebranch' => { in => \@branchcodes } ) : () ),
156 C4::Context->preference('item-level_itypes')
157 ? ( 'items.itype' => { in => \@itemtypes } )
158 : ( 'me.itemtype' => { in => \@itemtypes } )
163 my $biblioitems = Koha::Biblioitems->search( $conditions, { join => 'items', columns => 'biblionumber' } );
164 while ( my $biblioitem = $biblioitems->next ) {
165 push @record_ids, $biblioitem->biblionumber;
168 elsif ( $record_type eq 'auths' ) {
169 my $starting_authid = $query->param('starting_authid');
170 my $ending_authid = $query->param('ending_authid');
171 my $authtype = $query->param('authtype');
174 ( $starting_authid or $ending_authid )
177 ( $starting_authid ? ( '>=' => $starting_authid ) : () ),
178 ( $ending_authid ? ( '<=' => $ending_authid ) : () ),
182 ( $authtype ? ( authtypecode => $authtype ) : () ),
184 # Koha::MetadataRecord::Authority is not a Koha::Object...
185 my $authorities = Koha::Database->new->schema->resultset('AuthHeader')->search( $conditions );
186 @record_ids = map { $_->authid } $authorities->all;
190 @record_ids = uniq @record_ids;
191 if ( @record_ids and my $filefh = $query->upload("id_list_file") ) {
192 my @filter_record_ids = <$filefh>;
193 @filter_record_ids = map { my $id = $_; $id =~ s/[\r\n]*$//; $id } @filter_record_ids;
195 my %record_ids = map { $_ => 1 } @record_ids;
196 @record_ids = grep $record_ids{$_}, @filter_record_ids;
199 my $export_items_bundle_contents = $query->param('export_items_bundle_contents');
200 if ($export_items_bundle_contents and $record_type eq 'bibs') {
201 my $schema = Koha::Database->new->schema;
202 my $items_bundle_rs = $schema->resultset('ItemBundle');
203 foreach my $itemnumber (@itemnumbers) {
204 my @item_bundle_items = $items_bundle_rs->search({ host => $itemnumber });
205 foreach my $item_bundle_item (@item_bundle_items) {
206 my $biblionumber = $item_bundle_item->item->get_column('biblionumber');
207 my $itemnumber = $item_bundle_item->get_column('item');
208 push @record_ids, $biblionumber;
209 push @itemnumbers, $itemnumber;
212 @record_ids = uniq @record_ids;
215 print CGI->new->header(
216 -type => 'application/octet-stream',
218 -attachment => $filename,
221 my $csv_profile_id = $query->param('csv_profile_id');
222 Koha::Exporter::Record::export(
223 { record_type => $record_type,
224 record_ids => \@record_ids,
225 format => $output_format,
226 filename => $filename,
227 itemnumbers => \@itemnumbers,
228 dont_export_fields => $export_remove_fields,
229 csv_profile_id => $csv_profile_id,
230 export_items => (not $dont_export_items),
231 only_export_items_for_branches => $only_export_items_for_branches,
235 elsif ( $record_type eq 'db' or $record_type eq 'conf' ) {
236 my $successful_export;
238 if ( $flags->{superlibrarian}
240 $record_type eq 'db' and C4::Context->config('backup_db_via_tools')
242 $record_type eq 'conf' and C4::Context->config('backup_conf_via_tools')
245 binmode STDOUT, ':encoding(UTF-8)';
247 my $charset = 'utf-8';
248 my $mimetype = 'application/octet-stream';
249 if ( $filename =~ m/\.gz$/ ) {
250 $mimetype = 'application/x-gzip';
254 elsif ( $filename =~ m/\.bz2$/ ) {
255 $mimetype = 'application/x-bzip2';
259 print $query->header(
261 -charset => $charset,
262 -attachment => $filename,
265 my $extension = $record_type eq 'db' ? 'sql' : 'tar';
267 $successful_export = download_backup(
269 directory => $backupdir,
270 extension => $extension,
271 filename => $filename,
274 unless ($successful_export) {
275 my $remotehost = $query->remote_host();
276 $remotehost =~ s/(\n|\r)//;
278 "A suspicious attempt was made to download the " . ( $record_type eq 'db' ? 'db' : 'configuration' ) . "at '$filename' by someone at "
279 . $remotehost . "\n";
289 my $itemtypes = Koha::ItemTypes->search_with_localization;
291 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
293 my $libraries = Koha::Libraries->search_filtered({}, { order_by => ['branchname'] })->unblessed;
294 for my $library ( @$libraries ) {
295 $library->{selected} = 1 if grep { $library->{branchcode} eq $_ } @branch;
298 if ( $flags->{superlibrarian}
299 && C4::Context->config('backup_db_via_tools')
303 $template->{VARS}->{'allow_db_export'} = 1;
304 $template->{VARS}->{'dbfiles'} = getbackupfilelist(
305 { directory => "$backupdir", extension => 'sql' } );
308 if ( $flags->{superlibrarian}
309 && C4::Context->config('backup_conf_via_tools')
313 $template->{VARS}->{'allow_conf_export'} = 1;
314 $template->{VARS}->{'conffiles'} = getbackupfilelist(
315 { directory => "$backupdir", extension => 'tar' } );
319 libraries => $libraries,
320 itemtypes => $itemtypes,
321 authority_types => $authority_types,
322 export_remove_fields => C4::Context->preference("ExportRemoveFields"),
323 csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' })->as_list ],
324 messages => \@messages,
327 output_html_with_http_headers $query, $cookie, $template->output;
330 sub getbackupfilelist {
332 my $directory = $args->{directory};
333 my $extension = $args->{extension};
336 if ( opendir( my $dir, $directory ) ) {
337 while ( my $file = readdir($dir) ) {
338 next unless ( $file =~ m/\.$extension(\.(gz|bz2|xz))?/ );
340 if ( -f "$directory/$file" && -r "$directory/$file" );
347 sub download_backup {
349 my $directory = $args->{directory};
350 my $extension = $args->{extension};
351 my $filename = $args->{filename};
353 return unless ( $directory && -d $directory );
354 return unless ( $filename =~ m/\.$extension(\.(gz|bz2|xz))?$/ );
355 return if ( $filename =~ m#/# );
356 $filename = "$directory/$filename";
357 return unless ( -f $filename && -r $filename );
358 return unless ( open( my $dump, '<', $filename ) );
361 while ( read( $dump, my $data, 64 * 1024 ) ) {