4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along with
16 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
17 # Suite 330, Boston, MA 02111-1307 USA
24 use C4::Biblio; # GetMarcBiblio GetXmlBiblio
25 use C4::AuthoritiesMarc; # GetAuthority
27 use C4::Koha; # GetItemTypes
28 use C4::Branch; # GetBranches
36 my $dbh = C4::Context->dbh;
37 my $marcflavour = C4::Context->preference("marcflavour");
40 my $dont_export_items;
46 # Checks if the script is called from commandline
47 my $commandline = not defined $ENV{GATEWAY_INTERFACE};
54 'format=s' => \$output_format,
55 'date=s' => \$timestamp,
56 'dont_export_items' => \$dont_export_items,
57 'deleted_barcodes' => \$deleted_barcodes,
59 'filename=s' => \$filename,
60 'record-type=s' => \$record_type,
66 export.pl [--format=format] [--date=date] [--record-type=TYPE] [--dont_export_items] [--deleted_barcodes] [--clean] --filename=outputfile
69 --format=FORMAT FORMAT is either 'xml' or 'marc' (default)
71 --date=DATE DATE should be entered as the 'dateformat' syspref is
72 set (dd/mm/yyyy for metric, yyyy-mm-dd for iso,
73 mm/dd/yyyy for us) records exported are the ones that
74 have been modified since DATE
76 --record-type=TYPE TYPE is 'bibs' or 'auths'
78 --deleted_barcodes If used, a list of barcodes of items deleted since DATE
79 is produced (or from all deleted items if no date is
80 specified). Used only if TYPE is 'bibs'
82 --clean removes NSE/NSB
87 # Default parameters values :
88 $output_format ||= 'marc';
90 $dont_export_items ||= 0;
91 $deleted_barcodes ||= 0;
93 $record_type ||= "bibs";
96 open STDOUT, '>', $filename if $filename;
100 $op = $query->param("op") || '';
101 $filename = $query->param("filename") || 'koha.mrc';
102 $filename =~ s/(\r|\n)//;
106 my ($template, $loggedinuser, $cookie, $flags) = get_template_and_user(
108 template_name => "tools/export.tmpl",
111 authnotrequired => $commandline,
112 flagsrequired => {tools => 'export_catalog'},
117 my $limit_ind_branch = (
118 C4::Context->preference('IndependantBranches') &&
119 C4::Context->userenv &&
120 !(C4::Context->userenv->{flags} & 1) &&
121 C4::Context->userenv->{branch}
124 my $branch = $query->param("branch") || '';
125 if ( C4::Context->preference("IndependantBranches") &&
126 C4::Context->userenv &&
127 !(C4::Context->userenv->{flags} & 1) ) {
128 $branch = C4::Context->userenv->{'branch'};
131 my $backupdir = C4::Context->config('backupdir');
133 if ($op eq "export") {
134 my $charset = 'utf-8';
135 my $mimetype = 'application/octet-stream';
136 binmode STDOUT, ':encoding(UTF-8)';
137 if ( $filename =~ m/\.gz$/ ) {
138 $mimetype = 'application/x-gzip';
141 } elsif ( $filename =~ m/\.bz2$/ ) {
142 $mimetype = 'application/x-bzip2';
146 print $query->header(
148 -charset => $charset,
149 -attachment => $filename
150 ) unless ($commandline);
152 $record_type = $query->param("record_type") unless ($commandline);
153 $output_format = $query->param("output_format") || 'marc' unless ($commandline);
154 my $dont_export_fields = $query->param("dont_export_fields");
158 my $StartingBiblionumber = $query->param("StartingBiblionumber");
159 my $EndingBiblionumber = $query->param("EndingBiblionumber");
160 my $itemtype = $query->param("itemtype");
161 my $start_callnumber = $query->param("start_callnumber");
162 my $end_callnumber = $query->param("end_callnumber");
163 $timestamp = ($timestamp) ? C4::Dates->new($timestamp) : '' if ($commandline);
164 my $start_accession =
165 ( $query->param("start_accession") )
166 ? C4::Dates->new( $query->param("start_accession") )
169 ( $query->param("end_accession") )
170 ? C4::Dates->new( $query->param("end_accession") )
172 $dont_export_items = $query->param("dont_export_item") unless ($commandline);
173 my $strip_nonlocal_items = $query->param("strip_nonlocal_items");
175 my $biblioitemstable = ($commandline and $deleted_barcodes)
176 ? 'deletedbiblioitems'
178 my $itemstable = ($commandline and $deleted_barcodes)
182 my $starting_authid = $query->param('starting_authid');
183 my $ending_authid = $query->param('ending_authid');
184 my $authtype = $query->param('authtype');
186 if ( $record_type eq 'bibs' ) {
188 # Specific query when timestamp is used
189 # Actually it's used only with CLI and so all previous filters
191 # If one day timestamp is used via the web interface, this part will
192 # certainly have to be rewrited
195 FROM $biblioitemstable
196 LEFT JOIN items USING(biblionumber)
197 WHERE $biblioitemstable.timestamp >= ?
198 OR items.timestamp >= ?
201 FROM $biblioitemstable
202 LEFT JOIN deleteditems USING(biblionumber)
203 WHERE $biblioitemstable.timestamp >= ?
204 OR deleteditems.timestamp >= ?
206 my $ts = $timestamp->output('iso');
207 @sql_params = ($ts, $ts, $ts, $ts);
210 $branch || $start_callnumber || $end_callnumber ||
211 $start_accession || $timestamp || $end_accession ||
212 ($itemtype && C4::Context->preference('item-level_itypes'));
213 $sql_query = $items_filter ?
214 "SELECT DISTINCT $biblioitemstable.biblionumber
215 FROM $biblioitemstable JOIN $itemstable
216 USING (biblionumber) WHERE 1"
218 "SELECT $biblioitemstable.biblionumber FROM $biblioitemstable WHERE biblionumber >0 ";
220 if ( $StartingBiblionumber ) {
221 $sql_query .= " AND $biblioitemstable.biblionumber >= ? ";
222 push @sql_params, $StartingBiblionumber;
225 if ( $EndingBiblionumber ) {
226 $sql_query .= " AND $biblioitemstable.biblionumber <= ? ";
227 push @sql_params, $EndingBiblionumber;
231 $sql_query .= " AND homebranch = ? ";
232 push @sql_params, $branch;
235 if ($start_callnumber) {
236 $sql_query .= " AND itemcallnumber <= ? ";
237 push @sql_params, $start_callnumber;
240 if ($end_callnumber) {
241 $sql_query .= " AND itemcallnumber >= ? ";
242 push @sql_params, $end_callnumber;
244 if ($start_accession) {
245 $sql_query .= " AND dateaccessioned >= ? ";
246 push @sql_params, $start_accession->output('iso');
249 if ($end_accession) {
250 $sql_query .= " AND dateaccessioned <= ? ";
251 push @sql_params, $end_accession->output('iso');
255 $sql_query .= (C4::Context->preference('item-level_itypes')) ? " AND items.itype = ? " : " AND biblioitems.itemtype = ?";
256 push @sql_params, $itemtype;
260 elsif ( $record_type eq 'auths' ) {
262 "SELECT DISTINCT auth_header.authid FROM auth_header WHERE 1";
264 if ($starting_authid) {
265 $sql_query .= " AND auth_header.authid >= ? ";
266 push @sql_params, $starting_authid;
269 if ($ending_authid) {
270 $sql_query .= " AND auth_header.authid <= ? ";
271 push @sql_params, $ending_authid;
275 $sql_query .= " AND auth_header.authtypecode = ? ";
276 push @sql_params, $authtype;
279 elsif ( $record_type eq 'db' ) {
280 my $successful_export;
281 if ( $flags->{superlibrarian} && C4::Context->config('backup_db_via_tools') ) {
282 $successful_export = download_backup( { directory => "$backupdir", extension => 'sql', filename => "$filename" } )
284 unless ( $successful_export ) {
285 my $remotehost = $query->remote_host();
286 $remotehost =~ s/(\n|\r)//;
287 warn "A suspicious attempt was made to download the db at '$filename' by someone at " . $remotehost . "\n";
291 elsif ( $record_type eq 'conf' ) {
292 my $successful_export;
293 if ( $flags->{superlibrarian} && C4::Context->config('backup_conf_via_tools') ) {
294 $successful_export = download_backup( { directory => "$backupdir", extension => 'tar', filename => "$filename" } )
296 unless ( $successful_export ) {
297 my $remotehost = $query->remote_host();
298 $remotehost =~ s/(\n|\r)//;
299 warn "A suspicious attempt was made to download the configuration at '$filename' by someone at " . $remotehost . "\n";
304 # Someone is trying to mess us up
308 my $sth = $dbh->prepare($sql_query);
309 $sth->execute(@sql_params);
311 while ( my ($recordid) = $sth->fetchrow ) {
312 if ( $deleted_barcodes ) {
314 SELECT DISTINCT barcode
316 WHERE deleteditems.biblionumber = ?
318 my $sth = $dbh->prepare($q);
319 $sth->execute($recordid);
320 while (my $row = $sth->fetchrow_array) {
325 if ( $record_type eq 'bibs' ) {
326 $record = eval { GetMarcBiblio($recordid); };
331 next if not defined $record;
332 C4::Biblio::EmbedItemsInMarcBiblio( $record, $recordid )
333 unless $dont_export_items;
334 if ( $strip_nonlocal_items || $limit_ind_branch ) {
335 my ( $homebranchfield, $homebranchsubfield ) =
336 GetMarcFromKohaField( 'items.homebranch', '' );
337 for my $itemfield ( $record->field($homebranchfield) ) {
339 # if stripping nonlocal items, use loggedinuser's branch if they didn't select one
340 $branch = C4::Context->userenv->{'branch'} unless $branch;
341 $record->delete_field($itemfield)
343 $itemfield->subfield($homebranchsubfield) ne $branch );
347 elsif ( $record_type eq 'auths' ) {
348 $record = C4::AuthoritiesMarc::GetAuthority($recordid);
349 next if not defined $record;
352 if ( $dont_export_fields ) {
353 my @fields = split " ", $dont_export_fields;
354 foreach ( @fields ) {
358 # skip if this record doesn't have this field
359 next if not defined $record->field($field);
361 $record->field($field)->delete_subfields($subfield);
364 $record->delete_field($record->field($field));
368 RemoveAllNsb($record) if ($clean);
369 if ( $output_format eq "xml" ) {
370 if ($marcflavour eq 'UNIMARC' && $record_type eq 'auths') {
371 print $record->as_xml_record('UNIMARCAUTH');
373 print $record->as_xml_record($marcflavour);
377 print $record->as_usmarc();
387 my $itemtypes = GetItemTypes;
389 foreach my $thisitemtype (sort keys %$itemtypes) {
392 value => $thisitemtype,
393 description => $itemtypes->{$thisitemtype}->{'description'},
395 push @itemtypesloop, \%row;
397 my $branches = GetBranches($limit_ind_branch);
400 sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} }
404 { value => $thisbranch,
405 selected => $thisbranch eq $branch,
406 branchname => $branches->{$thisbranch}->{'branchname'},
410 my $authtypes = getauthtypes;
412 foreach my $thisauthtype ( sort keys %$authtypes ) {
413 next unless $thisauthtype;
415 value => $thisauthtype,
416 description => $authtypes->{$thisauthtype}->{'authtypetext'},
418 push @authtypesloop, \%row;
421 if ( $flags->{superlibrarian} && C4::Context->config('backup_db_via_tools') && $backupdir && -d $backupdir ) {
422 $template->{VARS}->{'allow_db_export'} = 1;
423 $template->{VARS}->{'dbfiles'} = getbackupfilelist( { directory => "$backupdir", extension => 'sql' } );
426 if ( $flags->{superlibrarian} && C4::Context->config('backup_conf_via_tools') && $backupdir && -d $backupdir ) {
427 $template->{VARS}->{'allow_conf_export'} = 1;
428 $template->{VARS}->{'conffiles'} = getbackupfilelist( { directory => "$backupdir", extension => 'tar' } );
432 branchloop => \@branchloop,
433 itemtypeloop => \@itemtypesloop,
434 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
435 authtypeloop => \@authtypesloop,
436 dont_export_fields => C4::Context->preference("DontExportFields"),
439 output_html_with_http_headers $query, $cookie, $template->output;
442 sub getbackupfilelist {
444 my $directory = $args->{directory};
445 my $extension = $args->{extension};
448 if ( opendir(my $dir, $directory) ) {
449 while (my $file = readdir($dir)) {
450 next unless ( $file =~ m/\.$extension(\.(gz|bz2|xz))?/ );
451 push @files, $file if ( -f "$directory/$file" && -r "$directory/$file" );
458 sub download_backup {
460 my $directory = $args->{directory};
461 my $extension = $args->{extension};
462 my $filename = $args->{filename};
464 return unless ( $directory && -d $directory );
465 return unless ( $filename =~ m/\.$extension(\.(gz|bz2|xz))?$/ );
466 return if ( $filename =~ m#/# );
467 $filename = "$directory/$filename";
468 return unless ( -f $filename && -r $filename );
469 return unless ( open(my $dump, '<', $filename) );
471 while (read($dump, my $data, 64 * 1024)) {