Bug 22887: Remove dup in fr-FR av.sql
[koha.git] / tools / export.pl
blob97ff255da00dfa53698312164ca5d19e62e7f571
1 #!/usr/bin/perl
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>.
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::Output;
26 use Koha::Authority::Types;
27 use Koha::Biblioitems;
28 use Koha::CsvProfiles;
29 use Koha::Database;
30 use Koha::DateUtils qw( dt_from_string output_pref );
31 use Koha::Exporter::Record;
32 use Koha::ItemTypes;
33 use Koha::Libraries;
35 my $query = new CGI;
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');
42 my $filename;
43 if ( $record_type eq 'auths' ) {
44 $filename = $query->param("filename_auth") || ( $output_format eq 'xml' ? 'koha.xml' : 'koha.mrc' );
45 } else {
46 $filename = $query->param("filename") || ( $output_format eq 'csv' ? 'koha.csv' : 'koha.mrc' );
48 $filename =~ s/(\r|\n)//;
50 my $dbh = C4::Context->dbh;
52 my @record_ids;
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",
67 query => $query,
68 type => "intranet",
69 authnotrequired => 0,
70 flagsrequired => { tools => 'export_catalog' },
71 debug => 1,
75 my @branch = $query->multi_param("branch");
77 my @messages;
78 if ( $op eq 'export' ) {
79 my $filename = $query->param('id_list_file');
80 if ( $filename ) {
81 my $mimetype = $query->uploadInfo($filename)->{'Content-Type'};
82 my @valid_mimetypes = qw( application/octet-stream text/csv text/plain application/vnd.ms-excel );
83 unless ( grep { $_ eq $mimetype } @valid_mimetypes ) {
84 push @messages, { type => 'alert', code => 'invalid_mimetype' };
85 $op = '';
90 if ( $op eq "export" ) {
92 my $export_remove_fields = $query->param("export_remove_fields") || q||;
93 my @biblionumbers = $query->multi_param("biblionumbers");
94 my @itemnumbers = $query->multi_param("itemnumbers");
95 my $strip_items_not_from_libraries = $query->param('strip_items_not_from_libraries');
96 my @sql_params;
97 my $sql_query;
99 my $libraries = Koha::Libraries->search_filtered->unblessed;
100 my $only_export_items_for_branches = $strip_items_not_from_libraries ? \@branch : undef;
101 my @branchcodes;
102 for my $branchcode ( @branch ) {
103 if ( grep { $_->{branchcode} eq $branchcode } @$libraries ) {
104 push @branchcodes, $branchcode;
108 if ( $record_type eq 'bibs' or $record_type eq 'auths' ) {
109 # No need to retrieve the record_ids if we already get them
110 unless ( @record_ids ) {
111 if ( $record_type eq 'bibs' ) {
112 my $starting_biblionumber = $query->param("StartingBiblionumber");
113 my $ending_biblionumber = $query->param("EndingBiblionumber");
114 my $itemtype = $query->param("itemtype");
115 my $start_callnumber = $query->param("start_callnumber");
116 my $end_callnumber = $query->param("end_callnumber");
117 my $start_accession =
118 ( $query->param("start_accession") )
119 ? dt_from_string( scalar $query->param("start_accession") )
120 : '';
121 my $end_accession =
122 ( $query->param("end_accession") )
123 ? dt_from_string( scalar $query->param("end_accession") )
124 : '';
127 my $conditions = {
128 ( $starting_biblionumber or $ending_biblionumber )
130 "me.biblionumber" => {
131 ( $starting_biblionumber ? ( '>=' => $starting_biblionumber ) : () ),
132 ( $ending_biblionumber ? ( '<=' => $ending_biblionumber ) : () ),
135 : (),
137 ( $start_callnumber or $end_callnumber )
139 'items.itemcallnumber' => {
140 ( $start_callnumber ? ( '>=' => $start_callnumber ) : () ),
141 ( $end_callnumber ? ( '<=' => $end_callnumber ) : () ),
144 : (),
146 ( $start_accession or $end_accession )
148 'items.dateaccessioned' => {
149 ( $start_accession ? ( '>=' => $start_accession ) : () ),
150 ( $end_accession ? ( '<=' => $end_accession ) : () ),
153 : (),
154 ( @branchcodes ? ( 'items.homebranch' => { in => \@branchcodes } ) : () ),
155 ( $itemtype
157 C4::Context->preference('item-level_itypes')
158 ? ( 'items.itype' => $itemtype )
159 : ( 'me.itemtype' => $itemtype )
160 : ()
164 my $biblioitems = Koha::Biblioitems->search( $conditions, { join => 'items', columns => 'biblionumber' } );
165 while ( my $biblioitem = $biblioitems->next ) {
166 push @record_ids, $biblioitem->biblionumber;
169 elsif ( $record_type eq 'auths' ) {
170 my $starting_authid = $query->param('starting_authid');
171 my $ending_authid = $query->param('ending_authid');
172 my $authtype = $query->param('authtype');
174 my $conditions = {
175 ( $starting_authid or $ending_authid )
177 authid => {
178 ( $starting_authid ? ( '>=' => $starting_authid ) : () ),
179 ( $ending_authid ? ( '<=' => $ending_authid ) : () ),
182 : (),
183 ( $authtype ? ( authtypecode => $authtype ) : () ),
185 # Koha::MetadataRecord::Authority is not a Koha::Object...
186 my $authorities = Koha::Database->new->schema->resultset('AuthHeader')->search( $conditions );
187 @record_ids = map { $_->authid } $authorities->all;
191 @record_ids = uniq @record_ids;
192 if ( @record_ids and my $filefh = $query->upload("id_list_file") ) {
193 my @filter_record_ids = <$filefh>;
194 @filter_record_ids = map { my $id = $_; $id =~ s/[\r\n]*$//; $id } @filter_record_ids;
195 # intersection
196 my %record_ids = map { $_ => 1 } @record_ids;
197 @record_ids = grep $record_ids{$_}, @filter_record_ids;
200 print CGI->new->header(
201 -type => 'application/octet-stream',
202 -charset => 'utf-8',
203 -attachment => $filename,
206 my $csv_profile_id = $query->param('csv_profile_id');
207 Koha::Exporter::Record::export(
208 { record_type => $record_type,
209 record_ids => \@record_ids,
210 format => $output_format,
211 filename => $filename,
212 itemnumbers => \@itemnumbers,
213 dont_export_fields => $export_remove_fields,
214 csv_profile_id => $csv_profile_id,
215 export_items => (not $dont_export_items),
216 only_export_items_for_branches => $only_export_items_for_branches,
220 elsif ( $record_type eq 'db' or $record_type eq 'conf' ) {
221 my $successful_export;
223 if ( $flags->{superlibrarian}
224 and (
225 $record_type eq 'db' and C4::Context->config('backup_db_via_tools')
227 $record_type eq 'conf' and C4::Context->config('backup_conf_via_tools')
230 binmode STDOUT, ':encoding(UTF-8)';
232 my $charset = 'utf-8';
233 my $mimetype = 'application/octet-stream';
234 if ( $filename =~ m/\.gz$/ ) {
235 $mimetype = 'application/x-gzip';
236 $charset = '';
237 binmode STDOUT;
239 elsif ( $filename =~ m/\.bz2$/ ) {
240 $mimetype = 'application/x-bzip2';
241 binmode STDOUT;
242 $charset = '';
244 print $query->header(
245 -type => $mimetype,
246 -charset => $charset,
247 -attachment => $filename,
250 my $extension = $record_type eq 'db' ? 'sql' : 'tar';
252 $successful_export = download_backup(
254 directory => $backupdir,
255 extension => $extension,
256 filename => $filename,
259 unless ($successful_export) {
260 my $remotehost = $query->remote_host();
261 $remotehost =~ s/(\n|\r)//;
262 warn
263 "A suspicious attempt was made to download the " . ( $record_type eq 'db' ? 'db' : 'configuration' ) . "at '$filename' by someone at "
264 . $remotehost . "\n";
269 exit;
272 else {
274 my $itemtypes = Koha::ItemTypes->search_with_localization;
276 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
278 my $libraries = Koha::Libraries->search_filtered({}, { order_by => ['branchname'] })->unblessed;
279 for my $library ( @$libraries ) {
280 $library->{selected} = 1 if grep { $library->{branchcode} eq $_ } @branch;
283 if ( $flags->{superlibrarian}
284 && C4::Context->config('backup_db_via_tools')
285 && $backupdir
286 && -d $backupdir )
288 $template->{VARS}->{'allow_db_export'} = 1;
289 $template->{VARS}->{'dbfiles'} = getbackupfilelist(
290 { directory => "$backupdir", extension => 'sql' } );
293 if ( $flags->{superlibrarian}
294 && C4::Context->config('backup_conf_via_tools')
295 && $backupdir
296 && -d $backupdir )
298 $template->{VARS}->{'allow_conf_export'} = 1;
299 $template->{VARS}->{'conffiles'} = getbackupfilelist(
300 { directory => "$backupdir", extension => 'tar' } );
303 $template->param(
304 libraries => $libraries,
305 itemtypes => $itemtypes,
306 authority_types => $authority_types,
307 export_remove_fields => C4::Context->preference("ExportRemoveFields"),
308 csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' }) ],
309 messages => \@messages,
312 output_html_with_http_headers $query, $cookie, $template->output;
315 sub getbackupfilelist {
316 my $args = shift;
317 my $directory = $args->{directory};
318 my $extension = $args->{extension};
319 my @files;
321 if ( opendir( my $dir, $directory ) ) {
322 while ( my $file = readdir($dir) ) {
323 next unless ( $file =~ m/\.$extension(\.(gz|bz2|xz))?/ );
324 push @files, $file
325 if ( -f "$directory/$file" && -r "$directory/$file" );
327 closedir($dir);
329 return \@files;
332 sub download_backup {
333 my $args = shift;
334 my $directory = $args->{directory};
335 my $extension = $args->{extension};
336 my $filename = $args->{filename};
338 return unless ( $directory && -d $directory );
339 return unless ( $filename =~ m/\.$extension(\.(gz|bz2|xz))?$/ );
340 return if ( $filename =~ m#/# );
341 $filename = "$directory/$filename";
342 return unless ( -f $filename && -r $filename );
343 return unless ( open( my $dump, '<', $filename ) );
344 binmode $dump;
346 while ( read( $dump, my $data, 64 * 1024 ) ) {
347 print $data;
349 close($dump);
350 return 1;