Bug 7143: [QA Follow-up] Correcting dates for first patch pushed
[koha.git] / tools / export.pl
blobb1ce09cc51f703ba6a0d918f72c416e12df88a6e
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::Koha; # GetItemTypes
25 use C4::Output;
27 use Koha::Authority::Types;
28 use Koha::Biblioitems;
29 use Koha::CsvProfiles;
30 use Koha::Database;
31 use Koha::DateUtils qw( dt_from_string output_pref );
32 use Koha::Exporter::Record;
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 = $query->param("filename") || ( $output_format eq 'csv' ? 'koha.csv' : 'koha.mrc' );
43 $filename =~ s/(\r|\n)//;
45 my $dbh = C4::Context->dbh;
47 my @record_ids;
48 # biblionumbers is sent from circulation.pl only
49 if ( $query->param("biblionumbers") ) {
50 $record_type = 'bibs';
51 @record_ids = $query->multi_param("biblionumbers");
54 # Default value for output_format is 'iso2709'
55 $output_format ||= 'iso2709';
56 # Retrocompatibility for the format parameter
57 $output_format = 'iso2709' if $output_format eq 'marc';
59 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
61 template_name => "tools/export.tt",
62 query => $query,
63 type => "intranet",
64 authnotrequired => 0,
65 flagsrequired => { tools => 'export_catalog' },
66 debug => 1,
70 my @branch = $query->multi_param("branch");
72 if ( $op eq "export" ) {
74 my $export_remove_fields = $query->param("export_remove_fields") || q||;
75 my @biblionumbers = $query->multi_param("biblionumbers");
76 my @itemnumbers = $query->multi_param("itemnumbers");
77 my $strip_nonlocal_items = $query->param('strip_nonlocal_items');
78 my @sql_params;
79 my $sql_query;
81 my $libraries = $strip_nonlocal_items
82 ? [ Koha::Libraries->find(C4::Context->userenv->{branch})->unblessed ]
83 : Koha::Libraries->search_filtered->unblessed;
84 my @branchcodes;
85 for my $branchcode ( @branch ) {
86 if ( grep { $_->{branchcode} eq $branchcode } @$libraries ) {
87 push @branchcodes, $branchcode;
91 if ( $record_type eq 'bibs' or $record_type eq 'auths' ) {
92 # No need to retrieve the record_ids if we already get them
93 unless ( @record_ids ) {
94 if ( $record_type eq 'bibs' ) {
95 my $starting_biblionumber = $query->param("StartingBiblionumber");
96 my $ending_biblionumber = $query->param("EndingBiblionumber");
97 my $itemtype = $query->param("itemtype");
98 my $start_callnumber = $query->param("start_callnumber");
99 my $end_callnumber = $query->param("end_callnumber");
100 my $start_accession =
101 ( $query->param("start_accession") )
102 ? dt_from_string( scalar $query->param("start_accession") )
103 : '';
104 my $end_accession =
105 ( $query->param("end_accession") )
106 ? dt_from_string( scalar $query->param("end_accession") )
107 : '';
110 my $conditions = {
111 ( $starting_biblionumber or $ending_biblionumber )
113 "me.biblionumber" => {
114 ( $starting_biblionumber ? ( '>=' => $starting_biblionumber ) : () ),
115 ( $ending_biblionumber ? ( '<=' => $ending_biblionumber ) : () ),
118 : (),
120 ( $start_callnumber or $end_callnumber )
122 'items.itemcallnumber' => {
123 ( $start_callnumber ? ( '>=' => $start_callnumber ) : () ),
124 ( $end_callnumber ? ( '<=' => $end_callnumber ) : () ),
127 : (),
129 ( $start_accession or $end_accession )
131 'items.dateaccessioned' => {
132 ( $start_accession ? ( '>=' => $start_accession ) : () ),
133 ( $end_accession ? ( '<=' => $end_accession ) : () ),
136 : (),
137 ( @branchcodes ? ( 'items.homebranch' => { in => \@branchcodes } ) : () ),
138 ( $itemtype
140 C4::Context->preference('item-level_itypes')
141 ? ( 'items.itype' => $itemtype )
142 : ( 'me.itemtype' => $itemtype )
143 : ()
147 my $biblioitems = Koha::Biblioitems->search( $conditions, { join => 'items', columns => 'biblionumber' } );
148 while ( my $biblioitem = $biblioitems->next ) {
149 push @record_ids, $biblioitem->biblionumber;
152 elsif ( $record_type eq 'auths' ) {
153 my $starting_authid = $query->param('starting_authid');
154 my $ending_authid = $query->param('ending_authid');
155 my $authtype = $query->param('authtype');
157 my $conditions = {
158 ( $starting_authid or $ending_authid )
160 authid => {
161 ( $starting_authid ? ( '>=' => $starting_authid ) : () ),
162 ( $ending_authid ? ( '<=' => $ending_authid ) : () ),
165 : (),
166 ( $authtype ? ( authtypecode => $authtype ) : () ),
168 # Koha::MetadataRecord::Authority is not a Koha::Object...
169 my $authorities = Koha::Database->new->schema->resultset('AuthHeader')->search( $conditions );
170 @record_ids = map { $_->authid } $authorities->all;
174 @record_ids = uniq @record_ids;
175 if ( @record_ids and my $filefh = $query->upload("id_list_file") ) {
176 my @filter_record_ids = <$filefh>;
177 @filter_record_ids = map { my $id = $_; $id =~ s/[\r\n]*$//; $id } @filter_record_ids;
178 # intersection
179 my %record_ids = map { $_ => 1 } @record_ids;
180 @record_ids = grep $record_ids{$_}, @filter_record_ids;
183 print CGI->new->header(
184 -type => 'application/octet-stream',
185 -charset => 'utf-8',
186 -attachment => $filename,
189 my $csv_profile_id = $query->param('csv_profile_id');
190 unless ( $csv_profile_id ) {
191 # FIXME export_format.profile should be a unique key
192 my $default_csv_profiles = Koha::CsvProfiles->search({ profile => C4::Context->preference('ExportWithCsvProfile') });
193 $csv_profile_id = $default_csv_profiles->count ? $default_csv_profiles->next->export_format_id : undef;
196 Koha::Exporter::Record::export(
197 { record_type => $record_type,
198 record_ids => \@record_ids,
199 format => $output_format,
200 filename => $filename,
201 itemnumbers => \@itemnumbers,
202 dont_export_fields => $export_remove_fields,
203 csv_profile_id => $csv_profile_id,
204 export_items => (not $dont_export_items),
208 elsif ( $record_type eq 'db' or $record_type eq 'conf' ) {
209 my $successful_export;
211 if ( $flags->{superlibrarian}
212 and (
213 $record_type eq 'db' and C4::Context->config('backup_db_via_tools')
215 $record_type eq 'conf' and C4::Context->config('backup_conf_via_tools')
218 binmode STDOUT, ':encoding(UTF-8)';
220 my $charset = 'utf-8';
221 my $mimetype = 'application/octet-stream';
222 if ( $filename =~ m/\.gz$/ ) {
223 $mimetype = 'application/x-gzip';
224 $charset = '';
225 binmode STDOUT;
227 elsif ( $filename =~ m/\.bz2$/ ) {
228 $mimetype = 'application/x-bzip2';
229 binmode STDOUT;
230 $charset = '';
232 print $query->header(
233 -type => $mimetype,
234 -charset => $charset,
235 -attachment => $filename,
238 my $extension = $record_type eq 'db' ? 'sql' : 'tar';
240 $successful_export = download_backup(
242 directory => $backupdir,
243 extension => $extension,
244 filename => $filename,
247 unless ($successful_export) {
248 my $remotehost = $query->remote_host();
249 $remotehost =~ s/(\n|\r)//;
250 warn
251 "A suspicious attempt was made to download the " . ( $record_type eq 'db' ? 'db' : 'configuration' ) . "at '$filename' by someone at "
252 . $remotehost . "\n";
257 exit;
260 else {
262 my $itemtypes = GetItemTypes;
263 my @itemtypesloop;
264 foreach my $thisitemtype ( sort keys %$itemtypes ) {
265 my %row = (
266 value => $thisitemtype,
267 description => $itemtypes->{$thisitemtype}->{translated_description},
269 push @itemtypesloop, \%row;
272 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
274 my $libraries = Koha::Libraries->search_filtered({}, { order_by => ['branchname'] })->unblessed;
275 for my $library ( @$libraries ) {
276 $library->{selected} = 1 if grep { $library->{branchcode} eq $_ } @branch;
279 if ( $flags->{superlibrarian}
280 && C4::Context->config('backup_db_via_tools')
281 && $backupdir
282 && -d $backupdir )
284 $template->{VARS}->{'allow_db_export'} = 1;
285 $template->{VARS}->{'dbfiles'} = getbackupfilelist(
286 { directory => "$backupdir", extension => 'sql' } );
289 if ( $flags->{superlibrarian}
290 && C4::Context->config('backup_conf_via_tools')
291 && $backupdir
292 && -d $backupdir )
294 $template->{VARS}->{'allow_conf_export'} = 1;
295 $template->{VARS}->{'conffiles'} = getbackupfilelist(
296 { directory => "$backupdir", extension => 'tar' } );
299 $template->param(
300 libraries => $libraries,
301 itemtypeloop => \@itemtypesloop,
302 authority_types => $authority_types,
303 export_remove_fields => C4::Context->preference("ExportRemoveFields"),
304 csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc' }) ],
307 output_html_with_http_headers $query, $cookie, $template->output;
310 sub getbackupfilelist {
311 my $args = shift;
312 my $directory = $args->{directory};
313 my $extension = $args->{extension};
314 my @files;
316 if ( opendir( my $dir, $directory ) ) {
317 while ( my $file = readdir($dir) ) {
318 next unless ( $file =~ m/\.$extension(\.(gz|bz2|xz))?/ );
319 push @files, $file
320 if ( -f "$directory/$file" && -r "$directory/$file" );
322 closedir($dir);
324 return \@files;
327 sub download_backup {
328 my $args = shift;
329 my $directory = $args->{directory};
330 my $extension = $args->{extension};
331 my $filename = $args->{filename};
333 return unless ( $directory && -d $directory );
334 return unless ( $filename =~ m/\.$extension(\.(gz|bz2|xz))?$/ );
335 return if ( $filename =~ m#/# );
336 $filename = "$directory/$filename";
337 return unless ( -f $filename && -r $filename );
338 return unless ( open( my $dump, '<', $filename ) );
339 binmode $dump;
341 while ( read( $dump, my $data, 64 * 1024 ) ) {
342 print $data;
344 close($dump);
345 return 1;