Bug 15194 - Drop-down menu 'Actions' has problem in 'Saved reports' page with languag...
[koha.git] / tools / export.pl
blob2176ccbf0f152848d1bf3e9df6859a6859e073e9
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::Branch; # GetBranches
25 use C4::Csv;
26 use C4::Koha; # GetItemTypes
27 use C4::Output;
29 use Koha::Biblioitems;
30 use Koha::Database;
31 use Koha::DateUtils qw( dt_from_string output_pref );
32 use Koha::Exporter::Record;
34 my $query = new CGI;
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") || 'koha.mrc';
42 $filename =~ s/(\r|\n)//;
44 my $dbh = C4::Context->dbh;
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->param("biblionumbers");
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';
58 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
60 template_name => "tools/export.tt",
61 query => $query,
62 type => "intranet",
63 authnotrequired => 0,
64 flagsrequired => { tools => 'export_catalog' },
65 debug => 1,
69 my @branch = $query->param("branch");
70 my $only_my_branch;
71 # Limit to local branch if IndependentBranches and not superlibrarian
72 if (
74 C4::Context->preference('IndependentBranches')
75 && C4::Context->userenv
76 && !C4::Context->IsSuperLibrarian()
77 && C4::Context->userenv->{branch}
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'} );
86 my %branchmap = map { $_ => 1 } @branch; # for quick lookups
88 if ( $op eq "export" ) {
90 my $export_remove_fields = $query->param("export_remove_fields") || q||;
91 my @biblionumbers = $query->param("biblionumbers");
92 my @itemnumbers = $query->param("itemnumbers");
93 my @sql_params;
94 my $sql_query;
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( $query->param("start_accession") )
108 : '';
109 my $end_accession =
110 ( $query->param("end_accession") )
111 ? dt_from_string( $query->param("end_accession") )
112 : '';
115 my $conditions = {
116 ( $starting_biblionumber or $ending_biblionumber )
118 "me.biblionumber" => {
119 ( $starting_biblionumber ? ( '>=' => $starting_biblionumber ) : () ),
120 ( $ending_biblionumber ? ( '<=' => $ending_biblionumber ) : () ),
123 : (),
124 ( $start_callnumber or $end_callnumber )
126 callnumber => {
127 ( $start_callnumber ? ( '>=' => $start_callnumber ) : () ),
128 ( $end_callnumber ? ( '<=' => $end_callnumber ) : () ),
131 : (),
132 ( $start_accession or $end_accession )
134 dateaccessioned => {
135 ( $start_accession ? ( '>=' => $start_accession ) : () ),
136 ( $end_accession ? ( '<=' => $end_accession ) : () ),
139 : (),
140 ( @branch ? ( 'items.homebranch' => { in => \@branch } ) : () ),
141 ( $itemtype
143 C4::Context->preference('item-level_itypes')
144 ? ( 'items.itype' => $itemtype )
145 : ( 'biblioitems.itemtype' => $itemtype )
146 : ()
150 my $biblioitems = Koha::Biblioitems->search( $conditions, { join => 'items' } );
151 while ( my $biblioitem = $biblioitems->next ) {
152 push @record_ids, $biblioitem->biblionumber;
155 elsif ( $record_type eq 'auths' ) {
156 my $starting_authid = $query->param('starting_authid');
157 my $ending_authid = $query->param('ending_authid');
158 my $authtype = $query->param('authtype');
160 my $conditions = {
161 ( $starting_authid or $ending_authid )
163 authid => {
164 ( $starting_authid ? ( '>=' => $starting_authid ) : () ),
165 ( $ending_authid ? ( '<=' => $ending_authid ) : () ),
168 : (),
169 ( $authtype ? ( authtypecode => $authtype ) : () ),
171 # Koha::Authority is not a Koha::Object...
172 my $authorities = Koha::Database->new->schema->resultset('AuthHeader')->search( $conditions );
173 @record_ids = map { $_->authid } $authorities->all;
177 @record_ids = uniq @record_ids;
178 if ( @record_ids and my $filefh = $query->upload("id_list_file") ) {
179 my @filter_record_ids = <$filefh>;
180 @filter_record_ids = map { my $id = $_; $id =~ s/[\r\n]*$//; $id } @filter_record_ids;
181 # intersection
182 my %record_ids = map { $_ => 1 } @record_ids;
183 @record_ids = grep $record_ids{$_}, @filter_record_ids;
186 print CGI->new->header(
187 -type => 'application/octet-stream',
188 -charset => 'utf-8',
189 -attachment => $filename,
192 Koha::Exporter::Record::export(
193 { record_type => $record_type,
194 record_ids => \@record_ids,
195 format => $output_format,
196 filename => $filename,
197 itemnumbers => \@itemnumbers,
198 dont_export_fields => $export_remove_fields,
199 csv_profile_id => ( $query->param('csv_profile_id') || GetCsvProfileId( C4::Context->preference('ExportWithCsvProfile') ) || undef ),
200 export_items => (not $dont_export_items),
204 elsif ( $record_type eq 'db' or $record_type eq 'conf' ) {
205 my $successful_export;
207 if ( $flags->{superlibrarian}
208 and (
209 $record_type eq 'db' and C4::Context->config('backup_db_via_tools')
211 $record_type eq 'conf' and C4::Context->config('backup_conf_via_tools')
214 binmode STDOUT, ':encoding(UTF-8)';
216 my $charset = 'utf-8';
217 my $mimetype = 'application/octet-stream';
218 if ( $filename =~ m/\.gz$/ ) {
219 $mimetype = 'application/x-gzip';
220 $charset = '';
221 binmode STDOUT;
223 elsif ( $filename =~ m/\.bz2$/ ) {
224 $mimetype = 'application/x-bzip2';
225 binmode STDOUT;
226 $charset = '';
228 print $query->header(
229 -type => $mimetype,
230 -charset => $charset,
231 -attachment => $filename,
234 my $extension = $record_type eq 'db' ? 'sql' : 'tar';
236 $successful_export = download_backup(
238 directory => $backupdir,
239 extension => $extension,
240 filename => $filename,
243 unless ($successful_export) {
244 my $remotehost = $query->remote_host();
245 $remotehost =~ s/(\n|\r)//;
246 warn
247 "A suspicious attempt was made to download the " . ( $record_type eq 'db' ? 'db' : 'configuration' ) . "at '$filename' by someone at "
248 . $remotehost . "\n";
253 exit;
256 else {
258 my $itemtypes = GetItemTypes;
259 my @itemtypesloop;
260 foreach my $thisitemtype ( sort keys %$itemtypes ) {
261 my %row = (
262 value => $thisitemtype,
263 description => $itemtypes->{$thisitemtype}->{translated_description},
265 push @itemtypesloop, \%row;
267 my $branches = GetBranches($only_my_branch);
268 my @branchloop;
269 for my $thisbranch (
270 sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} }
271 keys %{$branches}
274 push @branchloop,
276 value => $thisbranch,
277 selected => %branchmap ? $branchmap{$thisbranch} : 1,
278 branchname => $branches->{$thisbranch}->{'branchname'},
282 my $authtypes = getauthtypes;
283 my @authtypesloop;
284 foreach my $thisauthtype ( sort keys %$authtypes ) {
285 next unless $thisauthtype;
286 my %row = (
287 value => $thisauthtype,
288 description => $authtypes->{$thisauthtype}->{'authtypetext'},
290 push @authtypesloop, \%row;
293 if ( $flags->{superlibrarian}
294 && C4::Context->config('backup_db_via_tools')
295 && $backupdir
296 && -d $backupdir )
298 $template->{VARS}->{'allow_db_export'} = 1;
299 $template->{VARS}->{'dbfiles'} = getbackupfilelist(
300 { directory => "$backupdir", extension => 'sql' } );
303 if ( $flags->{superlibrarian}
304 && C4::Context->config('backup_conf_via_tools')
305 && $backupdir
306 && -d $backupdir )
308 $template->{VARS}->{'allow_conf_export'} = 1;
309 $template->{VARS}->{'conffiles'} = getbackupfilelist(
310 { directory => "$backupdir", extension => 'tar' } );
313 $template->param(
314 branchloop => \@branchloop,
315 itemtypeloop => \@itemtypesloop,
316 authtypeloop => \@authtypesloop,
317 export_remove_fields => C4::Context->preference("ExportRemoveFields"),
318 csv_profiles => C4::Csv::GetCsvProfiles('marc'),
321 output_html_with_http_headers $query, $cookie, $template->output;
324 sub getbackupfilelist {
325 my $args = shift;
326 my $directory = $args->{directory};
327 my $extension = $args->{extension};
328 my @files;
330 if ( opendir( my $dir, $directory ) ) {
331 while ( my $file = readdir($dir) ) {
332 next unless ( $file =~ m/\.$extension(\.(gz|bz2|xz))?/ );
333 push @files, $file
334 if ( -f "$directory/$file" && -r "$directory/$file" );
336 closedir($dir);
338 return \@files;
341 sub download_backup {
342 my $args = shift;
343 my $directory = $args->{directory};
344 my $extension = $args->{extension};
345 my $filename = $args->{filename};
347 return unless ( $directory && -d $directory );
348 return unless ( $filename =~ m/\.$extension(\.(gz|bz2|xz))?$/ );
349 return if ( $filename =~ m#/# );
350 $filename = "$directory/$filename";
351 return unless ( -f $filename && -r $filename );
352 return unless ( open( my $dump, '<', $filename ) );
353 binmode $dump;
355 while ( read( $dump, my $data, 64 * 1024 ) ) {
356 print $data;
358 close($dump);
359 return 1;