]> git.sur5r.net Git - bacula/bacula/blobdiff - gui/bweb/cgi/bresto.pl
ebl Add option to fix bar graphics
[bacula/bacula] / gui / bweb / cgi / bresto.pl
index aa29574811b5d0d0219f9e1a1d9301994542834e..836696d534967e9772418556dc51d34268f9733c 100755 (executable)
@@ -380,7 +380,8 @@ sub ls_dirs
     my $jobclause = $self->{curjobids};
 
     # Let's retrieve the list of the visible dirs in this dir ...
-    # First, I need the empty filenameid to locate efficiently the dirs in the file table
+    # First, I need the empty filenameid to locate efficiently
+    # the dirs in the file table
     my $query = "SELECT FilenameId FROM Filename WHERE Name = ''";
     my $sth = $self->dbh_prepare($query);
     $sth->execute();
@@ -395,20 +396,20 @@ SELECT PathId, Path, JobId, Lstat FROM (
     SELECT Path1.PathId, Path1.Path, lower(Path1.Path),
            listfile1.JobId, listfile1.Lstat
     FROM (
-        SELECT DISTINCT brestore_pathhierarchy1.PathId
-        FROM brestore_pathhierarchy AS brestore_pathhierarchy1
-        JOIN Path AS Path2
-            ON (brestore_pathhierarchy1.PathId = Path2.PathId)
-        JOIN brestore_pathvisibility AS brestore_pathvisibility1
-            ON (brestore_pathhierarchy1.PathId = brestore_pathvisibility1.PathId)
-        WHERE brestore_pathhierarchy1.PPathId = $pathid
-        AND brestore_pathvisibility1.jobid IN ($jobclause)) AS listpath1
-    JOIN Path AS Path1 ON (listpath1.PathId = Path1.PathId)
-    LEFT JOIN (
-        SELECT File1.PathId, File1.JobId, File1.Lstat FROM File AS File1
-        WHERE File1.FilenameId = $dir_filenameid
-        AND File1.JobId IN ($jobclause)) AS listfile1
-        ON (listpath1.PathId = listfile1.PathId)
+       SELECT DISTINCT brestore_pathhierarchy1.PathId
+       FROM brestore_pathhierarchy AS brestore_pathhierarchy1
+       JOIN Path AS Path2
+           ON (brestore_pathhierarchy1.PathId = Path2.PathId)
+       JOIN brestore_pathvisibility AS brestore_pathvisibility1
+           ON (brestore_pathhierarchy1.PathId = brestore_pathvisibility1.PathId)
+       WHERE brestore_pathhierarchy1.PPathId = $pathid
+       AND brestore_pathvisibility1.jobid IN ($jobclause)) AS listpath1
+   JOIN Path AS Path1 ON (listpath1.PathId = Path1.PathId)
+   LEFT JOIN (
+       SELECT File1.PathId, File1.JobId, File1.Lstat FROM File AS File1
+       WHERE File1.FilenameId = $dir_filenameid
+       AND File1.JobId IN ($jobclause)) AS listfile1
+       ON (listpath1.PathId = listfile1.PathId)
      ) AS A ORDER BY 2,3 DESC
 ";
     $self->debug($query);
@@ -739,6 +740,7 @@ SELECT JobId
 
 package main;
 use strict;
+use POSIX qw/strftime/;
 use Bweb;
 
 my $conf = new Bweb::Config(config_file => $Bweb::config_file);
@@ -793,7 +795,7 @@ if ($action eq 'list_client') {     # list all client [ ['c1'],['c2']..]
 
     print "]\n";
     exit 0;
-} elsif ($action eq 'list_storage') { # TODO: use .storage hier
+} elsif ($action eq 'list_storage') { # TODO: use .storage here
     print CGI::header('application/x-javascript');
 
     my $q="SELECT Name FROM Storage";
@@ -831,7 +833,6 @@ if ($pathid =~ /^(\d+)$/) {
 } else {
     $pathid = $bvfs->get_root();
 }
-
 $bvfs->ch_dir($pathid);
 
 if ($action eq 'restore') {
@@ -873,19 +874,37 @@ if ($action eq 'restore') {
 
     my $u = join(" UNION ", @union);
 
-    $bvfs->dbh_do("CREATE TEMPORARY TABLE btemp AS ($u)");
+    $bvfs->dbh_do("CREATE TEMPORARY TABLE btemp AS $u");
     # TODO: remove FilenameId et PathId
-    $bvfs->dbh_do("CREATE TABLE b2$$ AS (
+
+    # now we have to choose the file with the max(jobid)
+    # for each file of btemp
+    if ($bvfs->dbh_is_mysql()) {
+       $bvfs->dbh_do("CREATE TEMPORARY TABLE btemp2 AS (
+SELECT max(JobId) as JobId, PathId, FilenameId
+  FROM btemp
+ GROUP BY PathId, FilenameId
+ HAVING FileIndex > 0
+)");
+       $bvfs->dbh_do("CREATE TABLE b2$$ AS (
 SELECT btemp.JobId, btemp.FileIndex, btemp.FilenameId, btemp.PathId
-  FROM btemp,
-       (SELECT max(JobId) as JobId, PathId, FilenameId
-          FROM btemp
-      GROUP BY PathId, FilenameId
-      ORDER BY JobId DESC) AS a
-  WHERE a.JobId = btemp.JobId
-    AND a.PathId= btemp.PathId
-    AND a.FilenameId = btemp.FilenameId
+  FROM btemp, btemp2
+  WHERE btemp2.JobId = btemp.JobId
+    AND btemp2.PathId= btemp.PathId
+    AND btemp2.FilenameId = btemp.FilenameId
+)");
+   } else { # postgresql have distinct with more than one criteria...
+        $bvfs->dbh_do("CREATE TABLE b2$$ AS (
+SELECT JobId, FileIndex
+FROM (
+ SELECT DISTINCT ON (PathId, FilenameId) JobId, FileIndex
+   FROM btemp
+  ORDER BY PathId, FilenameId, JobId DESC
+ ) AS T
+ WHERE FileIndex > 0
 )");
+    }
+
     my $bconsole = $bvfs->get_bconsole();
     # TODO: pouvoir choisir le replace et le jobname
     my $jobid = $bconsole->run(client    => $arg->{client},
@@ -896,22 +915,46 @@ SELECT btemp.JobId, btemp.FileIndex, btemp.FilenameId, btemp.PathId
                               file      => "?b2$$");
     
     $bvfs->dbh_do("DROP TABLE b2$$");
+
+    if (!$jobid) {
+       print CGI::header('text/html');
+       $bvfs->display_begin();
+       $bvfs->error("Can't start your job:<br/>" . $bconsole->before());
+       $bvfs->display_end();
+       exit 0;
+    }
     sleep(2);
     print CGI::redirect("bweb.pl?action=dsp_cur_job;jobid=$jobid") ;
     exit 0;
 }
 
+sub escape_quote
+{
+    my ($str) = @_;
+    $str =~ s/'/\\'/g;
+    return $str;
+}
+
 print CGI::header('application/x-javascript');
 
 if ($action eq 'list_files') {
-    print "[";
+    print "[[0,0,0,0,'.',4096,'1970-01-01 00:00:00'],";
     my $files = $bvfs->ls_files();
 #      [ 1, 2, 3, "Bill",  10, '2007-01-01 00:00:00'],
 #   File.FilenameId, listfiles.id, listfiles.Name, File.LStat, File.JobId
 
     print join(',',
-              map { "[$_->[1], $_->[0], $pathid, $_->[4], \"$_->[2]\", 10, \"2007-01-01 00:00:00\"]" }
-              @$files);
+              map { my @p=Bvfs::parse_lstat($_->[3]); 
+                    '[' . join(',', 
+                               $_->[1],
+                               $_->[0],
+                               $pathid,
+                               $_->[4],
+                               "'" . escape_quote($_->[2]) . "'",
+                               "'" . $p[7] . "'",
+                               "'" . strftime('%Y-%m-%d %H:%m:%S', localtime($p[11])) .  "'") .
+                   ']'; 
+              } @$files);
     print "]\n";
 
 } elsif ($action eq 'list_dirs') {
@@ -921,9 +964,9 @@ if ($action eq 'list_files') {
     # return ($dirid,$dir_basename,$lstat,$jobid)
 
     print join(',',
-              map { "{ 'jobid': '$bvfs->{curjobids}', 'id': '$_->[0]', 'text': '$_->[1]', 'cls':'folder'}" }
+              map { "{ 'jobid': '$bvfs->{curjobids}', 'id': '$_->[0]'," . 
+                       "'text': '" . escape_quote($_->[1]) . "', 'cls':'folder'}" }
               @$dirs);
-
     print "]\n";
 
 } elsif ($action eq 'list_versions') {
@@ -936,24 +979,24 @@ if ($action eq 'list_files') {
     #($pathid,$fileid,$jobid, $fid, $mtime, $size, $inchanger, $md5, $volname);
     my $files = $bvfs->get_all_file_versions($args->{pathid}, $args->{filenameid}, $args->{client}, $vafv);
     print join(',',
-              map { "[ $_->[3], $_->[1], $_->[0], $_->[2], '$_->[8]', $_->[6], '$_->[7]', $_->[5], $_->[4] ]" }
+              map { "[ $_->[3], $_->[1], $_->[0], $_->[2], '$_->[8]', $_->[6], '$_->[7]', $_->[5],'" . strftime('%Y-%m-%d %H:%m:%S', localtime($_->[4])) . "']" }
               @$files);
     print "]\n";
 
 } elsif ($action eq 'get_media') {
 
     my $jobid = join(',', @jobid);
-    my $fileid = join(',', grep { /^\d+$/ } CGI::param('fileid'));
+    my $fileid = join(',', grep { /^\d+(,\d+)*$/ } CGI::param('fileid'));
 
     my $q="
- SELECT DISTINCT VolumeName, InChanger
+ SELECT DISTINCT VolumeName, Enabled, InChanger
    FROM File,
     ( -- Get all media from this job
       SELECT MIN(FirstIndex) AS FirstIndex, MAX(LastIndex) AS LastIndex,
-             VolumeName, Inchanger
+             VolumeName, Enabled, Inchanger
         FROM JobMedia JOIN Media USING (MediaId)
        WHERE JobId IN ($jobid)
-       GROUP BY VolumeName, InChanger
+       GROUP BY VolumeName,Enabled,InChanger
     ) AS allmedia
   WHERE File.FileId IN ($fileid)
     AND File.FileIndex >= allmedia.FirstIndex
@@ -961,7 +1004,7 @@ if ($action eq 'list_files') {
 ";
     my $lst = $bvfs->dbh_selectall_arrayref($q);
     print "[";
-    print join(',', map { "[ '$_->[0]', $_->[1]]" } @$lst);
+    print join(',', map { "['$_->[0]',$_->[1],$_->[2]]" } @$lst);
     print "]\n";
 
 }