| blib/lib/WebEditor/OldFeatures/AdminExport.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % | 
| statement | 12 | 64 | 18.7 | 
| branch | 0 | 26 | 0.0 | 
| condition | 0 | 6 | 0.0 | 
| subroutine | 4 | 7 | 57.1 | 
| pod | 0 | 3 | 0.0 | 
| total | 16 | 106 | 15.0 | 
| line | stmt | bran | cond | sub | pod | time | code | 
|---|---|---|---|---|---|---|---|
| 1 | package WebEditor::OldFeatures::AdminExport; | ||||||
| 2 | |||||||
| 3 | 1 | 1 | 116727 | use strict; | |||
| 1 | 3 | ||||||
| 1 | 38 | ||||||
| 4 | 1 | 1 | 6 | use vars qw($VERSION); | |||
| 1 | 2 | ||||||
| 1 | 67 | ||||||
| 5 | $VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/); | ||||||
| 6 | |||||||
| 7 | 1 | 1 | 5 | use CGI qw(param); | |||
| 1 | 2 | ||||||
| 1 | 9 | ||||||
| 8 | 1 | 1 | 129 | use Archive::Tar; | |||
| 1 | 2 | ||||||
| 1 | 843 | ||||||
| 9 | |||||||
| 10 | sub do_export { | ||||||
| 11 | 0 | 0 | 0 | my $self = shift; | |||
| 12 | 0 | my $c = $self->C; | |||||
| 13 | |||||||
| 14 | 0 | my $tarfilename = "backup_".get_date().".tgz"; | |||||
| 15 | 0 | my $tarfile = $c->paths->cgidir."/backup/".$tarfilename; | |||||
| 16 | 0 | my @files; | |||||
| 17 | my $file; | ||||||
| 18 | |||||||
| 19 | 0 | 0 | opendir DIR, $c->paths->database | ||||
| 20 | or die "Can't open directory " . $c->paths->database . ": $!"; | ||||||
| 21 | 0 | while (defined($file = readdir(DIR))) { | |||||
| 22 | 0 | 0 | next if $file =~ /^\.|CVS/i; # ignore hidden files | ||||
| 23 | 0 | 0 | push @files, $file if not -d $c->paths->database."/".$file; | ||||
| 24 | } | ||||||
| 25 | 0 | closedir DIR; | |||||
| 26 | |||||||
| 27 | 0 | 0 | opendir DIR, $c->paths->database."/content" | ||||
| 28 | or die "Can't open directory " . $c->paths->database . "/content: $!"; | ||||||
| 29 | 0 | while (defined($file = readdir(DIR))) { | |||||
| 30 | 0 | 0 | next if $file =~ /^\.|CVS/i; # ignore hidden files | ||||
| 31 | 0 | 0 | push @files, "content/".$file if not -d $c->paths->database."/content/".$file; | ||||
| 32 | } | ||||||
| 33 | 0 | closedir DIR; | |||||
| 34 | |||||||
| 35 | 0 | my $tar = Archive::Tar->new(); | |||||
| 36 | 0 | 0 | chdir $c->paths->database or die "Can't chdir: $!"; | ||||
| 37 | 0 | my $ok = $tar->add_files(@files); | |||||
| 38 | 0 | 0 | $ok = $ok && $tar->add_data(".theanswer","42"); | ||||
| 39 | 0 | 0 | 0 | if (!$tar->write($tarfile,9) || !$ok) { | |||
| 40 | 0 | print "kann Archivdatei nicht erzeugen: ".$tar->error; | |||||
| 41 | } else { | ||||||
| 42 | 0 | print "Archivdatei \"$tarfilename\" erzeugt. \n"; | |||||
| 43 | #XXX kann man überhaupt auf dieses Verzeichnis zugreifen??? | ||||||
| 44 | 0 | print "Bitte hier herunterladen und gut aufbewahren. \n"; | |||||
| 45 | } | ||||||
| 46 | 0 | chdir $c->paths->cgidir; | |||||
| 47 | } | ||||||
| 48 | |||||||
| 49 | sub do_import { | ||||||
| 50 | 0 | 0 | 0 | my $self = shift; | |||
| 51 | 0 | my $c = $self->C; | |||||
| 52 | |||||||
| 53 | 0 | 0 | if (!param('tarfile')) { | ||||
| 54 | 0 | print qq~ | |||||
| 55 | 0 | ~; | |||||
| 62 | } else { | ||||||
| 63 | 0 | require Archive::Tar; | |||||
| 64 | 0 | my $uploadfile = param('tarfile'); | |||||
| 65 | 0 | my $tarfile = $c->paths->cgidir."/backup/upload.tgz"; | |||||
| 66 | 0 | 0 | open(BACKUP,">$tarfile") or die "Can't write to $tarfile: $!"; | ||||
| 67 | 0 | binmode BACKUP; | |||||
| 68 | 0 | while (<$uploadfile>) { | |||||
| 69 | 0 | print BACKUP $_; | |||||
| 70 | } | ||||||
| 71 | 0 | close BACKUP; | |||||
| 72 | 0 | print "File-upload abgeschlossen. $tarfile "; | |||||
| 73 | 0 | my $tar = Archive::Tar->new(); | |||||
| 74 | 0 | $tar->read($tarfile); | |||||
| 75 | 0 | 0 | if ($tar->get_content(".theanswer") =~ "42") { | ||||
| 76 | 0 | $tar->remove(".theanswer"); | |||||
| 77 | 0 | 0 | chdir $c->paths->database | ||||
| 78 | or die "Can't chdir to " . $c->paths->database . ": $!"; | ||||||
| 79 | 0 | 0 | if (!$tar->extract_archive($tarfile)) { | ||||
| 80 | 0 | print "geht nich: " . $tar->error(); | |||||
| 81 | } else { | ||||||
| 82 | 0 | print "Datenfiles extrahiert. "; | |||||
| 83 | }; | ||||||
| 84 | } else { | ||||||
| 85 | 0 | print "Dies scheint keine gültige Backup-datei zu sein! \n"; | |||||
| 86 | 0 | print "Es wurden keine Dateien extrahiert. \n"; | |||||
| 87 | } | ||||||
| 88 | 0 | chdir $c->paths->cgidir; | |||||
| 89 | } | ||||||
| 90 | } | ||||||
| 91 | |||||||
| 92 | #################################################################### | ||||||
| 93 | # | ||||||
| 94 | # general subs | ||||||
| 95 | # | ||||||
| 96 | sub get_date { | ||||||
| 97 | # Return time as ISO 8601 date from given time in days | ||||||
| 98 | 0 | 0 | 0 | my $time = time; | |||
| 99 | 0 | my @l = localtime $time; | |||||
| 100 | 0 | sprintf("%04d-%02d-%02d-%02d%02d", | |||||
| 101 | $l[5]+1900, $l[4]+1, $l[3], | ||||||
| 102 | $l[2], $l[1], $l[0]); | ||||||
| 103 | } | ||||||
| 104 | |||||||
| 105 | 1; |