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
20 use List
::MoreUtils
qw(uniq);
24 use C4
::AuthoritiesMarc
; # GetAuthority
25 use C4
::Biblio
; # GetMarcBiblio
26 use C4
::Branch
; # GetBranches
28 use C4
::Koha
; # GetItemTypes
36 my $dont_export_items;
41 my $op = $query->param("op") || '';
42 my $filename = $query->param("filename") || 'koha.mrc';
43 my $dbh = C4
::Context
->dbh;
44 my $marcflavour = C4
::Context
->preference("marcflavour");
45 my $format = $query->param("format") || 'iso2709';
47 # Checks if the script is called from commandline
48 my $commandline = not defined $ENV{GATEWAY_INTERFACE
};
55 'format=s' => \
$output_format,
56 'date=s' => \
$timestamp,
57 'dont_export_items' => \
$dont_export_items,
58 'deleted_barcodes' => \
$deleted_barcodes,
60 'filename=s' => \
$filename,
61 'record-type=s' => \
$record_type,
67 export
.pl
[--format
=format
] [--date
=date
] [--record
-type
=TYPE
] [--dont_export_items
] [--deleted_barcodes
] [--clean
] --filename
=outputfile
70 --format
=FORMAT FORMAT is either
'xml' or 'marc' (default)
72 --date
=DATE DATE should be entered as the
'dateformat' syspref is
73 set
(dd
/mm/yyyy
for metric
, yyyy
-mm
-dd
for iso
,
74 mm
/dd
/yyyy
for us
) records exported are the ones that
75 have been modified since DATE
77 --record
-type
=TYPE TYPE is
'bibs' or 'auths'
79 --deleted_barcodes If used
, a list of barcodes of items deleted since DATE
80 is produced
(or from all deleted items
if no date is
81 specified
). Used only
if TYPE is
'bibs'
83 --clean removes NSE
/NSB
88 # Default parameters values :
89 $output_format ||= 'marc';
91 $dont_export_items ||= 0;
92 $deleted_barcodes ||= 0;
94 $record_type ||= "bibs";
97 open STDOUT
, '>', $filename if $filename;
102 $op = $query->param("op") || '';
103 $filename = $query->param("filename") || 'koha.mrc';
104 $filename =~ s/(\r|\n)//;
108 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user
(
110 template_name
=> "tools/export.tmpl",
113 authnotrequired
=> $commandline,
114 flagsrequired
=> { tools
=> 'export_catalog' },
119 my $limit_ind_branch =
120 ( C4
::Context
->preference('IndependantBranches')
121 && C4
::Context
->userenv
122 && !( C4
::Context
->userenv->{flags
} & 1 )
123 && C4
::Context
->userenv->{branch
} ) ?
1 : 0;
125 my $branch = $query->param("branch") || '';
126 if ( C4
::Context
->preference("IndependantBranches")
127 && C4
::Context
->userenv
128 && !( C4
::Context
->userenv->{flags
} & 1 ) )
130 $branch = C4
::Context
->userenv->{'branch'};
133 my $backupdir = C4
::Context
->config('backupdir');
135 if ( $op eq "export" ) {
136 if ( $format eq "iso2709" or $format eq "xml" ) {
137 my $charset = 'utf-8';
138 my $mimetype = 'application/octet-stream';
139 binmode STDOUT
, ':encoding(UTF-8)';
140 if ( $filename =~ m/\.gz$/ ) {
141 $mimetype = 'application/x-gzip';
145 elsif ( $filename =~ m/\.bz2$/ ) {
146 $mimetype = 'application/x-bzip2';
150 print $query->header(
152 -charset
=> $charset,
153 -attachment
=> $filename
154 ) unless ($commandline);
156 $record_type = $query->param("record_type") unless ($commandline);
157 $output_format = $query->param("output_format") || 'marc'
158 unless ($commandline);
159 my $export_remove_fields = $query->param("export_remove_fields");
160 my @biblionumbers = $query->param("biblionumbers");
161 my @itemnumbers = $query->param("itemnumbers");
166 my $StartingBiblionumber = $query->param("StartingBiblionumber");
167 my $EndingBiblionumber = $query->param("EndingBiblionumber");
168 my $itemtype = $query->param("itemtype");
169 my $start_callnumber = $query->param("start_callnumber");
170 my $end_callnumber = $query->param("end_callnumber");
171 $timestamp = ($timestamp) ? C4
::Dates
->new($timestamp) : ''
173 my $start_accession =
174 ( $query->param("start_accession") )
175 ? C4
::Dates
->new( $query->param("start_accession") )
178 ( $query->param("end_accession") )
179 ? C4
::Dates
->new( $query->param("end_accession") )
181 $dont_export_items = $query->param("dont_export_item")
182 unless ($commandline);
184 my $strip_nonlocal_items = $query->param("strip_nonlocal_items");
186 my $biblioitemstable =
187 ( $commandline and $deleted_barcodes )
188 ?
'deletedbiblioitems'
191 ( $commandline and $deleted_barcodes )
195 my $starting_authid = $query->param('starting_authid');
196 my $ending_authid = $query->param('ending_authid');
197 my $authtype = $query->param('authtype');
199 if ( $record_type eq 'bibs' and not @biblionumbers ) {
202 # Specific query when timestamp is used
203 # Actually it's used only with CLI and so all previous filters
205 # If one day timestamp is used via the web interface, this part will
206 # certainly have to be rewrited
207 my ( $query, $params ) = construct_query
(
209 recordtype
=> $record_type,
210 timestamp
=> $timestamp,
211 biblioitemstable
=> $biblioitemstable,
215 @sql_params = @
$params;
219 my ( $query, $params ) = construct_query
(
221 recordtype
=> $record_type,
222 biblioitemstable
=> $biblioitemstable,
223 itemstable
=> $itemstable,
224 StartingBiblionumber
=> $StartingBiblionumber,
225 EndingBiblionumber
=> $EndingBiblionumber,
227 start_callnumber
=> $start_callnumber,
228 end_callnumber
=> $end_callnumber,
229 start_accession
=> $start_accession,
230 end_accession
=> $end_accession,
231 itemtype
=> $itemtype,
235 @sql_params = @
$params;
238 elsif ( $record_type eq 'auths' ) {
239 my ( $query, $params ) = construct_query
(
241 recordtype
=> $record_type,
242 starting_authid
=> $starting_authid,
243 ending_authid
=> $ending_authid,
244 authtype
=> $authtype,
248 @sql_params = @
$params;
251 elsif ( $record_type eq 'db' ) {
252 my $successful_export;
253 if ( $flags->{superlibrarian
}
254 && C4
::Context
->config('backup_db_via_tools') )
256 $successful_export = download_backup
(
258 directory
=> "$backupdir",
260 filename
=> "$filename"
264 unless ($successful_export) {
265 my $remotehost = $query->remote_host();
266 $remotehost =~ s/(\n|\r)//;
268 "A suspicious attempt was made to download the db at '$filename' by someone at "
269 . $remotehost . "\n";
273 elsif ( $record_type eq 'conf' ) {
274 my $successful_export;
275 if ( $flags->{superlibrarian
}
276 && C4
::Context
->config('backup_conf_via_tools') )
278 $successful_export = download_backup
(
280 directory
=> "$backupdir",
282 filename
=> "$filename"
286 unless ($successful_export) {
287 my $remotehost = $query->remote_host();
288 $remotehost =~ s/(\n|\r)//;
290 "A suspicious attempt was made to download the configuration at '$filename' by someone at "
291 . $remotehost . "\n";
295 elsif (@biblionumbers) {
296 push @recordids, (@biblionumbers);
300 # Someone is trying to mess us up
304 unless (@biblionumbers) {
305 my $sth = $dbh->prepare($sql_query);
306 $sth->execute(@sql_params);
307 push @recordids, map {
309 } @
{ $sth->fetchall_arrayref };
312 for my $recordid ( uniq
@recordids ) {
313 if ($deleted_barcodes) {
315 SELECT DISTINCT barcode
317 WHERE deleteditems.biblionumber = ?
319 my $sth = $dbh->prepare($q);
320 $sth->execute($recordid);
321 while ( my $row = $sth->fetchrow_array ) {
327 if ( $record_type eq 'bibs' ) {
328 $record = eval { GetMarcBiblio
($recordid); };
331 next if not defined $record;
332 C4
::Biblio
::EmbedItemsInMarcBiblio
( $record, $recordid,
334 unless $dont_export_items;
335 if ( $strip_nonlocal_items
337 || $dont_export_items )
339 my ( $homebranchfield, $homebranchsubfield ) =
340 GetMarcFromKohaField
( 'items.homebranch', '' );
341 for my $itemfield ( $record->field($homebranchfield) ) {
343 # if stripping nonlocal items, use loggedinuser's branch if they didn't select one
344 $branch = C4
::Context
->userenv->{'branch'}
346 $record->delete_field($itemfield)
347 if ( $dont_export_items
348 || $itemfield->subfield($homebranchsubfield) ne
353 elsif ( $record_type eq 'auths' ) {
354 $record = C4
::AuthoritiesMarc
::GetAuthority
($recordid);
355 next if not defined $record;
358 if ($export_remove_fields) {
359 my @fields = split " ", $export_remove_fields;
365 # skip if this record doesn't have this field
366 next if not defined $record->field($field);
368 $record->field($field)->delete_subfields($subfield);
371 $record->delete_field( $record->field($field) );
375 RemoveAllNsb
($record) if ($clean);
376 if ( $output_format eq "xml" ) {
377 if ( $marcflavour eq 'UNIMARC' && $record_type eq 'auths' )
379 print $record->as_xml_record('UNIMARCAUTH');
382 print $record->as_xml_record($marcflavour);
386 print $record->as_usmarc();
392 elsif ( $format eq "csv" ) {
393 my @biblionumbers = uniq
$query->param("biblionumbers");
394 my @itemnumbers = $query->param("itemnumbers");
396 marc2csv
( \
@biblionumbers,
397 GetCsvProfileId
( C4
::Context
->preference('ExportWithCsvProfile') ),
399 print $query->header(
400 -type
=> 'application/octet-stream',
401 -'Content-Transfer-Encoding' => 'binary',
402 -attachment
=> "export.csv"
411 my $itemtypes = GetItemTypes
;
413 foreach my $thisitemtype ( sort keys %$itemtypes ) {
415 value
=> $thisitemtype,
416 description
=> $itemtypes->{$thisitemtype}->{'description'},
418 push @itemtypesloop, \
%row;
420 my $branches = GetBranches
($limit_ind_branch);
423 sort { $branches->{$a}->{branchname
} cmp $branches->{$b}->{branchname
} }
429 value
=> $thisbranch,
430 selected
=> $thisbranch eq $branch,
431 branchname
=> $branches->{$thisbranch}->{'branchname'},
435 my $authtypes = getauthtypes
;
437 foreach my $thisauthtype ( sort keys %$authtypes ) {
438 next unless $thisauthtype;
440 value
=> $thisauthtype,
441 description
=> $authtypes->{$thisauthtype}->{'authtypetext'},
443 push @authtypesloop, \
%row;
446 if ( $flags->{superlibrarian
}
447 && C4
::Context
->config('backup_db_via_tools')
451 $template->{VARS
}->{'allow_db_export'} = 1;
452 $template->{VARS
}->{'dbfiles'} = getbackupfilelist
(
453 { directory
=> "$backupdir", extension
=> 'sql' } );
456 if ( $flags->{superlibrarian
}
457 && C4
::Context
->config('backup_conf_via_tools')
461 $template->{VARS
}->{'allow_conf_export'} = 1;
462 $template->{VARS
}->{'conffiles'} = getbackupfilelist
(
463 { directory
=> "$backupdir", extension
=> 'tar' } );
467 branchloop
=> \
@branchloop,
468 itemtypeloop
=> \
@itemtypesloop,
469 authtypeloop
=> \
@authtypesloop,
470 export_remove_fields
=> C4
::Context
->preference("ExportRemoveFields"),
473 output_html_with_http_headers
$query, $cookie, $template->output;
476 sub construct_query
{
479 my ( $sql_query, @sql_params );
481 if ( $params->{recordtype
} eq "bibs" ) {
482 if ( $params->{timestamp
} ) {
483 my $biblioitemstable = $params->{biblioitemstable
};
486 FROM $biblioitemstable
487 LEFT JOIN items USING(biblionumber)
488 WHERE $biblioitemstable.timestamp >= ?
489 OR items.timestamp >= ?
492 FROM $biblioitemstable
493 LEFT JOIN deleteditems USING(biblionumber)
494 WHERE $biblioitemstable.timestamp >= ?
495 OR deleteditems.timestamp >= ?
497 my $ts = $timestamp->output('iso');
498 @sql_params = ( $ts, $ts, $ts, $ts );
501 my $biblioitemstable = $params->{biblioitemstable
};
502 my $itemstable = $params->{itemstable
};
503 my $StartingBiblionumber = $params->{StartingBiblionumber
};
504 my $EndingBiblionumber = $params->{EndingBiblionumber
};
505 my $branch = $params->{branch
};
506 my $start_callnumber = $params->{start_callnumber
};
507 my $end_callnumber = $params->{end_callnumber
};
508 my $start_accession = $params->{star_accession
};
509 my $end_accession = $params->{end_accession
};
510 my $itemtype = $params->{itemtype
};
517 || ( $itemtype && C4
::Context
->preference('item-level_itypes') );
518 $sql_query = $items_filter
519 ?
"SELECT DISTINCT $biblioitemstable.biblionumber
520 FROM $biblioitemstable JOIN $itemstable
521 USING (biblionumber) WHERE 1"
522 : "SELECT $biblioitemstable.biblionumber FROM $biblioitemstable WHERE biblionumber >0 ";
524 if ($StartingBiblionumber) {
525 $sql_query .= " AND $biblioitemstable.biblionumber >= ? ";
526 push @sql_params, $StartingBiblionumber;
529 if ($EndingBiblionumber) {
530 $sql_query .= " AND $biblioitemstable.biblionumber <= ? ";
531 push @sql_params, $EndingBiblionumber;
535 $sql_query .= " AND homebranch = ? ";
536 push @sql_params, $branch;
539 if ($start_callnumber) {
540 $sql_query .= " AND itemcallnumber >= ? ";
541 push @sql_params, $start_callnumber;
544 if ($end_callnumber) {
545 $sql_query .= " AND itemcallnumber <= ? ";
546 push @sql_params, $end_callnumber;
548 if ($start_accession) {
549 $sql_query .= " AND dateaccessioned >= ? ";
550 push @sql_params, $start_accession->output('iso');
553 if ($end_accession) {
554 $sql_query .= " AND dateaccessioned <= ? ";
555 push @sql_params, $end_accession->output('iso');
560 ( C4
::Context
->preference('item-level_itypes') )
561 ?
" AND items.itype = ? "
562 : " AND biblioitems.itemtype = ?";
563 push @sql_params, $itemtype;
567 elsif ( $params->{recordtype
} eq "auths" ) {
568 if ( $params->{timestamp
} ) {
573 my $starting_authid = $params->{starting_authid
};
574 my $ending_authid = $params->{ending_authid
};
575 my $authtype = $params->{authtype
};
577 "SELECT DISTINCT auth_header.authid FROM auth_header WHERE 1";
579 if ($starting_authid) {
580 $sql_query .= " AND auth_header.authid >= ? ";
581 push @sql_params, $starting_authid;
584 if ($ending_authid) {
585 $sql_query .= " AND auth_header.authid <= ? ";
586 push @sql_params, $ending_authid;
590 $sql_query .= " AND auth_header.authtypecode = ? ";
591 push @sql_params, $authtype;
595 return ( $sql_query, \
@sql_params );
598 sub getbackupfilelist
{
600 my $directory = $args->{directory
};
601 my $extension = $args->{extension
};
604 if ( opendir( my $dir, $directory ) ) {
605 while ( my $file = readdir($dir) ) {
606 next unless ( $file =~ m/\.$extension(\.(gz|bz2|xz))?/ );
608 if ( -f
"$directory/$file" && -r
"$directory/$file" );
615 sub download_backup
{
617 my $directory = $args->{directory
};
618 my $extension = $args->{extension
};
619 my $filename = $args->{filename
};
621 return unless ( $directory && -d
$directory );
622 return unless ( $filename =~ m/\.$extension(\.(gz|bz2|xz))?$/ );
623 return if ( $filename =~ m
#/# );
624 $filename = "$directory/$filename";
625 return unless ( -f
$filename && -r
$filename );
626 return unless ( open( my $dump, '<', $filename ) );
629 while ( read( $dump, my $data, 64 * 1024 ) ) {