|
Line 0
Link Here
|
|
|
1 |
package Koha::Staff::Controller::Tools::Export; |
| 2 |
|
| 3 |
use Mojo::Base 'Mojolicious::Controller'; |
| 4 |
|
| 5 |
use Koha::Exporter::Record; |
| 6 |
use C4::Biblio qw//; |
| 7 |
use Encode; |
| 8 |
use Carp; |
| 9 |
|
| 10 |
use Koha::ItemTypes; |
| 11 |
use Koha::Authority::Types; |
| 12 |
use Koha::Libraries; |
| 13 |
|
| 14 |
use Koha::Auth; |
| 15 |
|
| 16 |
sub index { |
| 17 |
my $c = shift; |
| 18 |
my ($flags,$loggedinuser) = $c->staff_authorize({ flagsrequired => { 'tools' => 'export_catalog' } }); |
| 19 |
my $template = $c->prepare_template({ |
| 20 |
template_filename => 'tools/export.tt', |
| 21 |
interface => 'intranet', |
| 22 |
}); |
| 23 |
|
| 24 |
#FIXME: This is for showing messages about the download... |
| 25 |
my @messages = (); |
| 26 |
$template->{VARS}->{messages} = \@messages; |
| 27 |
|
| 28 |
my $itemtypes = Koha::ItemTypes->search_with_localization; |
| 29 |
$template->{VARS}->{itemtypes} = $itemtypes; |
| 30 |
|
| 31 |
my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } ); |
| 32 |
$template->{VARS}->{authority_types} = $authority_types; |
| 33 |
|
| 34 |
my $branch = $c->every_param('branch'); |
| 35 |
my $libraries = Koha::Libraries->search_filtered({}, { order_by => ['branchname'] })->unblessed; |
| 36 |
#NOTE: This will only work if we return to this action... |
| 37 |
for my $library ( @$libraries ) { |
| 38 |
$library->{selected} = 1 if grep { $library->{branchcode} eq $_ } @$branch; |
| 39 |
} |
| 40 |
$template->{VARS}->{libraries} = $libraries; |
| 41 |
|
| 42 |
my $backupdir = C4::Context->config('backupdir'); |
| 43 |
if ( $flags->{superlibrarian} |
| 44 |
&& C4::Context->config('backup_db_via_tools') |
| 45 |
&& $backupdir |
| 46 |
&& -d $backupdir ) |
| 47 |
{ |
| 48 |
$template->{VARS}->{'allow_db_export'} = 1; |
| 49 |
$template->{VARS}->{'dbfiles'} = _getbackupfilelist( |
| 50 |
{ directory => "$backupdir", extension => 'sql' } ); |
| 51 |
} |
| 52 |
|
| 53 |
if ( $flags->{superlibrarian} |
| 54 |
&& C4::Context->config('backup_conf_via_tools') |
| 55 |
&& $backupdir |
| 56 |
&& -d $backupdir ) |
| 57 |
{ |
| 58 |
$template->{VARS}->{'allow_conf_export'} = 1; |
| 59 |
$template->{VARS}->{'conffiles'} = _getbackupfilelist( |
| 60 |
{ directory => "$backupdir", extension => 'tar' } ); |
| 61 |
} |
| 62 |
|
| 63 |
$c->render( text => $template->output ); |
| 64 |
} |
| 65 |
|
| 66 |
sub _getbackupfilelist { |
| 67 |
my $args = shift; |
| 68 |
my $directory = $args->{directory}; |
| 69 |
my $extension = $args->{extension}; |
| 70 |
my @files; |
| 71 |
|
| 72 |
if ( opendir( my $dir, $directory ) ) { |
| 73 |
while ( my $file = readdir($dir) ) { |
| 74 |
next unless ( $file =~ m/\.$extension(\.(gz|bz2|xz))?/ ); |
| 75 |
push @files, $file |
| 76 |
if ( -f "$directory/$file" && -r "$directory/$file" ); |
| 77 |
} |
| 78 |
closedir($dir); |
| 79 |
} |
| 80 |
use Data::Dumper; |
| 81 |
warn Dumper(\@files); |
| 82 |
return \@files; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
sub download { |
| 87 |
my $c = shift; |
| 88 |
my ($flags,$loggedinuser) = $c->staff_authorize({ flagsrequired => { 'tools' => 'export_catalog' } }); |
| 89 |
|
| 90 |
my $record_type = $c->param('record_type') || 'bibs'; #FIXME: Remove bibs? |
| 91 |
my $backupdir = C4::Context->config('backupdir'); |
| 92 |
|
| 93 |
if ( $record_type eq 'bibs' or $record_type eq 'auths' ){ |
| 94 |
#TODO: Refactor code from export.pl |
| 95 |
} |
| 96 |
elsif ( $record_type eq 'db' or $record_type eq 'conf' ){ |
| 97 |
#TODO: Most of this code should go in a model module |
| 98 |
if ( $flags->{superlibrarian} |
| 99 |
and ( |
| 100 |
$record_type eq 'db' and C4::Context->config('backup_db_via_tools') |
| 101 |
or |
| 102 |
$record_type eq 'conf' and C4::Context->config('backup_conf_via_tools') |
| 103 |
) |
| 104 |
) { |
| 105 |
my $extension = $record_type eq 'db' ? 'sql' : 'tar'; |
| 106 |
my $filename = $c->param('filename'); |
| 107 |
$filename =~ s/(\r|\n)//; |
| 108 |
my $fh = _get_backup_filehandle({ |
| 109 |
directory => $backupdir, |
| 110 |
extension => $extension, |
| 111 |
filename => $filename, |
| 112 |
}); |
| 113 |
my $content_type = 'application/octet-stream'; |
| 114 |
if ( $filename =~ m/\.gz$/ ){ |
| 115 |
$content_type = 'application/x-gzip'; |
| 116 |
} |
| 117 |
elsif ( $filename =~ m/\.bz2$/ ){ |
| 118 |
$content_type = 'application/x-bzip2'; |
| 119 |
} |
| 120 |
#FIXME: Add error handling |
| 121 |
$c->res->headers->content_type($content_type); |
| 122 |
$c->res->headers->content_disposition("attachment; filename=$filename;"); |
| 123 |
my $drain; |
| 124 |
$drain = sub { |
| 125 |
my $c = shift; |
| 126 |
read($fh,my $chunk, 64 * 1024); |
| 127 |
if ($chunk){ |
| 128 |
$c->write($chunk,$drain); |
| 129 |
} |
| 130 |
else { |
| 131 |
$c->write(''); |
| 132 |
} |
| 133 |
}; |
| 134 |
$c->$drain; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
#FIXME: Move this into a Koha::Exporter module |
| 140 |
sub _get_backup_filehandle { |
| 141 |
my ($args) = @_; |
| 142 |
my $directory = $args->{directory}; |
| 143 |
my $extension = $args->{extension}; |
| 144 |
my $filename = $args->{filename}; |
| 145 |
|
| 146 |
return unless ( $directory && -d $directory ); |
| 147 |
return unless ( $filename =~ m/\.$extension(\.(gz|bz2|xz))?$/ ); |
| 148 |
return if ( $filename =~ m#/# ); |
| 149 |
$filename = "$directory/$filename"; |
| 150 |
return unless ( -f $filename && -r $filename ); |
| 151 |
return unless ( open( my $fh, '<', $filename ) ); |
| 152 |
return $fh; |
| 153 |
} |
| 154 |
|
| 155 |
1; |