/[BackupPC]/trunk/lib/BackupPC/SearchLib.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/BackupPC/SearchLib.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 9 by dpavlin, Thu Jun 23 12:36:22 2005 UTC revision 142 by ravilov, Fri Oct 7 08:58:45 2005 UTC
# Line 4  package BackupPC::SearchLib; Line 4  package BackupPC::SearchLib;
4  use strict;  use strict;
5  use BackupPC::CGI::Lib qw(:all);  use BackupPC::CGI::Lib qw(:all);
6  use BackupPC::Attrib qw(:all);  use BackupPC::Attrib qw(:all);
 use Data::Dumper;  
7  use DBI;  use DBI;
8    use DateTime;
9    use vars qw(%In $MyURL);
10    use Time::HiRes qw/time/;
11    
12    my $on_page = 100;
13    my $pager_pages = 10;
14    
15    my $dsn = $Conf{SearchDSN};
16    my $db_user = $Conf{SearchUser} || '';
17    
18    my $hest_index_path = $Conf{HyperEstraierIndex};
19    
20    my $dbh;
21    
22    sub get_dbh {
23            $dbh ||= DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } );
24            return $dbh;
25    }
26    
27  sub getUnits() {  sub getUnits() {
28      my @ret = ();          my @ret;
29      my $tmp;  
30      my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}",          my $dbh = get_dbh();
31          "", "", { RaiseError => 1, AutoCommit => 1 } );          my $sth = $dbh->prepare(qq{
32      my $st =                  SELECT
33        $dbh->prepare(                          shares.id       as id,
34          " SELECT shares.ID AS ID, shares.share AS name FROM shares;");                          hosts.name || ':' || shares.name as share
35      $st->execute();                  FROM shares
36      push (@ret, { 'ID' => '', 'name' => '-'});                  JOIN hosts on hostid = hosts.id
37      while ( $tmp = $st->fetchrow_hashref() ) {                  ORDER BY share
38          push( @ret, { 'ID' => $tmp->{'ID'}, 'name' => $tmp->{'name'} } );          } );
39      }          $sth->execute();
40      $dbh->disconnect();          push @ret, { 'id' => '', 'share' => '-'};       # dummy any
41      return @ret;  
42            while ( my $row = $sth->fetchrow_hashref() ) {
43                    push @ret, $row;
44            }
45            return @ret;
46    }
47    
48    sub epoch_to_iso {
49            my $t = shift || return;
50            my $iso = BackupPC::Lib::timeStamp(undef, $t);
51            $iso =~ s/\s/ /g;
52            return $iso;
53  }  }
54    
55    sub dates_from_form($) {
56            my $param = shift || return;
57    
58            sub mk_epoch_date($$) {
59                    my ($name,$suffix) = @_;
60    
61                    my $yyyy = $param->{ $name . '_year_' . $suffix} || return undef;
62                    my $mm .= $param->{ $name . '_month_' . $suffix} ||
63                            ( $suffix eq 'from' ? 1 : 12);
64                    my $dd .= $param->{ $name . '_day_' . $suffix} ||
65                            ( $suffix eq 'from' ? 1 : 31);
66    
67                    $yyyy =~ s/\D//g;
68                    $mm =~ s/\D//g;
69                    $dd =~ s/\D//g;
70    
71                    my $dt = new DateTime(
72                            year => $yyyy,
73                            month => $mm,
74                            day => $dd
75                    );
76                    print STDERR "mk_epoch_date($name,$suffix) [$yyyy-$mm-$dd] = " . $dt->ymd . " " . $dt->hms . "\n";
77                    return $dt->epoch || 'NULL';
78            }
79    
80            my @ret = (
81                    mk_epoch_date('search_backup', 'from'),
82                    mk_epoch_date('search_backup', 'to'),
83                    mk_epoch_date('search', 'from'),
84                    mk_epoch_date('search', 'to'),
85            );
86    
87            return @ret;
88    
89    }
90    
91    
92  sub getWhere($) {  sub getWhere($) {
93      my ($param)    = @_;          my $param = shift || return;
94      my $retSQL     = "";  
95      my @conditions = ();          my ($backup_from, $backup_to, $files_from, $files_to) = dates_from_form($param);
96      my $cond;  
97            my @conditions;
98                push @conditions, qq{ backups.date >= $backup_from } if ($backup_from);
99              push @conditions, qq{ backups.date <= $backup_to } if ($backup_to);
100                push @conditions, qq{ files.date >= $files_from } if ($files_from);
101      if ( defined( $param->{'search_backup_day_from'} ) && $param->{'search_backup_day_from'} ne "") {          push @conditions, qq{ files.date <= $files_to } if ($files_to);
102          push( @conditions,  
103              ' strftime("%d", datetime(backups.date, "unixepoch","localtime")) >= "'          print STDERR "backup: $backup_from - $backup_to files: $files_from - $files_to cond:" . join(" | ",@conditions);
104                . $param->{'search_backup_day_from'} ."\"");  
105      }          push( @conditions, ' files.shareid = ' . $param->{'search_share'} ) if ($param->{'search_share'});
106      if ( defined( $param->{'search_backup_day_to'} ) && $param->{'search_backup_day_to'} ne "") {          push (@conditions, " upper(files.path) LIKE upper('%".$param->{'search_filename'}."%')") if ($param->{'search_filename'});
107          push( @conditions,  
108              ' strftime("%d", datetime(backups.date, "unixepoch","localtime")) <= "'          return join(" and ", @conditions);
109                . $param->{'search_backup_day_from'}  ."\"");  }
110      }  
111      if ( defined( $param->{'search_backup_month_from'} ) && $param->{'search_backup_month_from'} ne "") {  
112          push( @conditions,  sub getFiles($) {
113              ' strftime("%m", datetime(backups.date, "unixepoch","localtime")) >= "'          my ($param) = @_;
114                . $param->{'search_backup_month_from'}  ."\"");  
115      }          my $offset = $param->{'offset'} || 0;
116      if ( defined( $param->{'search_backup_month_to'} ) && $param->{'search_backup_month_to'} ne "") {          $offset *= $on_page;
117          push( @conditions,  
118              ' strftime("%m", datetime(backups.date, "unixepoch","localtime")) <= "'          my $dbh = get_dbh();
119                . $param->{'search_backup_month_to'}  ."\"");  
120      }          my $sql_cols = qq{
121      if ( defined( $param->{'search_backup_year_from'} ) && $param->{'search_backup_year_from'} ne "") {                  files.id                        AS fid,
122          push( @conditions,                  hosts.name                      AS hname,
123              ' strftime("%Y", datetime(backups.date, "unixepoch","localtime")) >= "'                  shares.name                     AS sname,
124                . $param->{'search_backup_year_from'}  ."\"");                  files.backupnum                 AS backupnum,
125      }                  files.path                      AS filepath,
126      if ( defined( $param->{'search_backup_year_to'} ) && $param->{'search_backup_year_to'} ne "") {                  files.date                      AS date,
127          push( @conditions,                  files.type                      AS type,
128              ' strftime("%Y", datetime(backups.date, "unixepoch","localtime")) <= "'                  files.size                      AS size
               . $param->{'search_backup_year_to'}  ."\"");  
     }  
   
     if ( defined( $param->{'search_day_from'} )   && $param->{'search_day_from'} ne "" ) {  
         push( @conditions,  
             ' strftime("%d", datetime(files.date, "unixepoch","localtime")) >= "'  
               . $param->{'search_day_from'}  ."\"");  
     }  
     if ( defined( $param->{'search_month_from'} ) && $param->{'search_month_from'} ne "") {  
         push( @conditions,  
             ' strftime("%m", datetime(files.date, "unixepoch","localtime")) >= "'  
               . $param->{'search_month_from'}  ."\"");  
     }  
     if ( defined( $param->{'search_year_from'} ) && $param->{'search_year_from'} ne "") {  
         push( @conditions,  
             ' strftime("%Y", datetime(files.date, "unixepoch","localtime")) >= "'  
               . $param->{'search_year_from'}  ."\"");  
     }  
     if ( defined( $param->{'search_day_to'} )   && $param->{'search_day_to'} ne "" ) {  
         push( @conditions,  
             ' strftime("%d", datetime(files.date, "unixepoch","localtime")) <= "'  
               . $param->{'search_day_to'}  ."\"");  
     }  
     if ( defined( $param->{'search_month_to'} ) && $param->{'search_month_to'} ne "" ) {  
         push( @conditions,  
             ' strftime("%m", datetime(files.date, "unixepoch","localtime")) <= "'  
               . $param->{'search_month_to'} ."\"" );  
     }  
     if ( defined( $param->{'search_year_to'} )&& $param->{'search_year_to'} ne "" )  {  
         push( @conditions,  
             ' strftime("%Y", datetime(files.date, "unixepoch","localtime")) <= "'  
               . $param->{'search_year_to'} ."\"");  
     }  
   
     if ( defined( $param->{'search_host'} ) && $param->{'search_host'} ne "") {  
       push( @conditions, ' backups.hostID = ' . $param->{'search_host'} );  
     }  
   
     if ( defined ($param->{'search_filename'}) && $param->{'search_filename'} ne "") {  
         push (@conditions, " files.name LIKE '".$param->{'search_filename'}."%'");  
         }  
       
     $retSQL = "";  
     foreach $cond(@conditions)  
       {  
           if ($retSQL ne "")  
             {  
                 $retSQL .= " AND ";  
             }  
           $retSQL .= $cond;  
       }        
   
       
     return $retSQL;  
 }  
   
 sub getFiles($$)  
   {  
       my ($where, $offset) = @_;  
         
         
       my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}",  
         "", "", { RaiseError => 1, AutoCommit => 1 } );  
       my $sql =            
         q{    
               SELECT files.id                       AS fid,  
                      hosts.name                     AS hname,  
                      shares.name                    AS sname,  
                      shares.share                   AS sharename,  
                      backups.num                    AS backupNum,  
                      files.name                     AS filename,  
                      files.path                     AS filepath,  
                      shares.share||files.fullpath AS networkPath,  
                      date(files.date, 'unixepoch', 'localtime') AS date,  
                      files.type                     AS filetype,  
                      files.size                     AS size,  
                      dvds.name                      AS dvd  
                   FROM  
                      files  
                         INNER JOIN shares  ON files.shareID=shares.ID  
                         INNER JOIN hosts   ON hosts.ID = shares.hostID  
                         INNER JOIN backups ON backups.hostID = hosts.ID  
                         LEFT  JOIN dvds    ON dvds.ID = files.dvdid  
               
           };  
   
       if (defined($where) && $where ne "")  
         {  
             $sql .= " WHERE ". $where;        
         }  
   
       $sql .=  
         q{            
             ORDER BY files.id  
               LIMIT 100  
               OFFSET ? * 100 + 1  
129          };          };
130          
131                  my $sql_from = qq{
132                          FROM files
133        my $st = $dbh->prepare(                          INNER JOIN shares       ON files.shareID=shares.ID
134            $sql                          INNER JOIN hosts        ON hosts.ID = shares.hostID
135            );                              INNER JOIN backups      ON backups.num = files.backupnum and backups.hostID = hosts.ID AND backups.shareID = files.shareID
136        if (!defined($offset) && $offset ne "")          };
137        {  
138          $st->bind_param(1, $offset);          my $sql_where;
139        }          my $where = getWhere($param);
140        else          $sql_where = " WHERE ". $where if ($where);
141        {  
142          $st->bind_param(1,0);          my $sql_order = qq{
143        }                  ORDER BY files.date
144        $st->execute;                  LIMIT $on_page
145                          OFFSET ?
146        my @ret = ();          };
147        my $tmp;  
148                  my $sql_count = qq{ select count(files.id) $sql_from $sql_where };
149        while ($tmp = $st->fetchrow_hashref())          my $sql_results = qq{ select $sql_cols $sql_from $sql_where $sql_order };
150          {  
151              push(@ret, {          my $sth = $dbh->prepare($sql_count);
152                             'hname'       => $tmp->{'hname'},          $sth->execute();
153                             'sname'       => $tmp->{'sname'},          my ($results) = $sth->fetchrow_array();
154                             'sharename'   => $tmp->{'sharename'},  
155                             'backupno'    => $tmp->{'backupNum'},          $sth = $dbh->prepare($sql_results);
156                             'fname'       => $tmp->{'filename'},          $sth->execute( $offset );
157                             'fpath'       => $tmp->{'filepath'},  
158                             'networkpath' => $tmp->{'networkPath'},          if ($sth->rows != $results) {
159                             'date'        => $tmp->{'date'},                  my $bug = "$0 BUG: [[ $sql_count ]] = $results while [[ $sql_results ]] = " . $sth->rows;
160                             'type'        => $tmp->{'filetype'},                  $bug =~ s/\s+/ /gs;
161                             'size'        => $tmp->{'size'},                  print STDERR "$bug\n";
                            'id'          => $tmp->{'fid'},  
                            'dvd'         => $tmp->{'dvd'}  
                        }  
             );  
                                   
162          }          }
163    
164            my @ret;
165                
166        $st->finish();          while (my $row = $sth->fetchrow_hashref()) {
167        $dbh->disconnect();                  push @ret, $row;
       return @ret;  
   }  
   
 sub getBackupsNotBurned()  
   {  
       my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}",  
         "", "", { RaiseError => 1, AutoCommit => 1 } );        
       my $sql = q{  
           SELECT  
             hosts.ID         AS hostID,  
             hosts.name       AS host,  
             backups.num      AS backupno,  
             backups.type     AS type,  
             backups.date     AS date  
           FROM backups, shares, files, hosts  
           WHERE  
             backups.num    = files.backupNum  AND  
             shares.ID      = files.shareID    AND            
             backups.hostID = shares.hostID    AND  
             hosts.ID       = backups.hostID   AND  
             files.dvdid    IS NULL  
           GROUP BY  
             backups.hostID, backups.num  
       };  
       my $st = $dbh -> prepare( $sql );  
       my @ret = ();  
       $st -> execute();  
   
       while ( my $tmp = $st -> fetchrow_hashref() )  
         {            
             push(@ret, {  
                          'host'     => $tmp->{'host'},  
                          'hostid'   => $tmp->{'hostID'},  
                          'backupno' => $tmp->{'backupno'},  
                          'type'     => $tmp->{'type'},  
                          'date'     => $tmp->{'date'}  
                        }  
             );  
168          }          }
169              
170        return @ret;                $sth->finish();
171    }          return ($results, \@ret);
172    }
173    
174    sub getHyperEstraier_url($) {
175            my ($use_hest) = @_;
176    
177            return unless $use_hest;
178    
179            use HyperEstraier;
180            my ($index_path, $index_node_url);
181    
182  sub displayBackupsGrid()          if ($use_hest =~ m#^http://#) {
183    {                  $index_node_url = $use_hest;
184        my $retHTML = "";          } else {
185        my $addForm = 1;                  $index_path = $TopDir . '/' . $index_path;
186                    $index_path =~ s#//#/#g;
187            }
188            return ($index_path, $index_node_url);
189    }
190    
191    sub getFilesHyperEstraier($) {
192            my ($param) = @_;
193    
194            my $offset = $param->{'offset'} || 0;
195            $offset *= $on_page;
196    
197            die "no index_path?" unless ($hest_index_path);
198    
199            use HyperEstraier;
200    
201            my ($index_path, $index_node_url) = getHyperEstraier_url($hest_index_path);
202    
203            # open the database
204            my $db;
205            if ($index_path) {
206                    $db = HyperEstraier::Database->new();
207                    $db->open($index_path, $HyperEstraier::ESTDBREADER);
208            } elsif ($index_node_url) {
209                    $db ||= HyperEstraier::Node->new($index_node_url);
210                    $db->set_auth('admin', 'admin');
211            } else {
212                    die "BUG: unimplemented";
213            }
214    
215            # create a search condition object
216            my $cond = HyperEstraier::Condition->new();
217    
218            my $q = $param->{'search_filename'};
219            my $shareid = $param->{'search_share'};
220    
221            if (length($q) > 0) {
222                    # exact match
223                    $cond->add_attr("filepath ISTRINC $q");
224    
225                    $q =~ s/(.)/$1 /g;
226                    # set the search phrase to the search condition object
227                    $cond->set_phrase($q);
228            }
229    
230            my ($backup_from, $backup_to, $files_from, $files_to) = dates_from_form($param);
231    
232            $cond->add_attr("backup_date NUMGE $backup_from") if ($backup_from);
233            $cond->add_attr("backup_date NUMLE $backup_to") if ($backup_to);
234    
235            $cond->add_attr("date NUMGE $files_from") if ($files_from);
236            $cond->add_attr("date NUMLE $files_to") if ($files_to);
237    
238            $cond->add_attr("shareid NUMEQ $shareid") if ($shareid);
239    
240    #       $cond->set_max( $offset + $on_page );
241            $cond->set_options( $HyperEstraier::Condition::SURE );
242            $cond->set_order( 'date NUMA' );
243    
244            # get the result of search
245            my @res;
246            my ($result, $hits);
247    
248            if ($index_path) {
249                    $result = $db->search($cond, 0);
250                    $hits = $result->size;
251            } elsif ($index_node_url) {
252                    $result = $db->search($cond, 0);
253                    $hits = $result->doc_num;
254            } else {
255                    die "BUG: unimplemented";
256            }
257    
258            # for each document in result
259            for my $i ($offset .. ($offset + $on_page - 1)) {
260                    last if ($i >= $hits);
261    
262                    my $doc;
263                    if ($index_path) {
264                            my $id = $result->get($i);
265                            $doc = $db->get_doc($id, 0);
266                    } elsif ($index_node_url) {
267                            $doc = $result->get_doc($i);
268                    } else {
269                            die "BUG: unimplemented";
270                    }
271    
272                    my $row;
273                    foreach my $c (qw/fid hname sname backupnum fiilename filepath date type size/) {
274                            $row->{$c} = $doc->attr($c);
275                    }
276                    push @res, $row;
277            }
278    
279            return ($hits, \@res);
280    }
281    
282    sub getGzipName($$$)
283    {
284            my ($host, $share, $backupnum) = @_;
285            my $ret = $Conf{GzipSchema};
286            
287            $share =~ s/\//_/g;
288            $ret =~ s/\\h/$host/ge;
289            $ret =~ s/\\s/$share/ge;
290            $ret =~ s/\\n/$backupnum/ge;
291            
292            return $ret;
293            
294    }
295    
296    sub getBackupsNotBurned() {
297    
298            my $dbh = get_dbh();
299    
300            my $sql = q{
301                    SELECT
302                            backups.hostID AS hostID,
303                            hosts.name AS host,
304                            shares.name AS share,
305                            backups.id AS backupnum,
306                            backups.type AS type,
307                            backups.date AS date,
308                            backups.size AS size
309                    FROM backups
310                    INNER JOIN shares       ON backups.shareID=shares.ID
311                    INNER JOIN hosts        ON backups.hostID = hosts.ID
312                    LEFT OUTER JOIN archive_backup ON archive_backup.backup_id = backups.id
313                    WHERE backups.size > 0 AND archive_backup.backup_id IS NULL
314                    GROUP BY
315                            backups.hostID,
316                            hosts.name,
317                            shares.name,
318                            backups.num,
319                            backups.shareid,
320                            backups.id,
321                            backups.type,
322                            backups.date,
323                            backups.size
324                    ORDER BY backups.date
325            };
326            my $sth = $dbh->prepare( $sql );
327            my @ret;
328            $sth->execute();
329    
330            while ( my $row = $sth->fetchrow_hashref() ) {
331                    $row->{'age'} = sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) );
332                    $row->{'size'} = sprintf("%0.2f", $row->{'size'} / 1024 / 1024);
333                    my (undef,undef,undef,undef,undef,undef,undef,$fs_size,undef,undef,undef,undef,undef) =
334                            stat( $Conf{InstallDir}.'/'.$Conf{GzipTempDir}.'/'.
335                                    getGzipName($row->{'host'}, $row->{share}, $row->{'backupnum'}));
336                    $row->{'fs_size'} = $fs_size;
337                    push @ret, $row;
338            }
339                
340        if ($addForm)          return @ret;      
341          {  }
342    
343    sub displayBackupsGrid() {
344    
345            my $retHTML .= q{
346                    <form id="forma" method="POST" action="}.$MyURL.q{?action=burn">
347            };
348    
349              $retHTML .= <<EOF3;          $retHTML .= <<'EOF3';
350  <script language="javascript" type="text/javascript">  <style type="text/css">
351  <!--  <!--
352    DIV#fixedBox {
353            position: absolute;
354            top: 50em;
355            left: -24%;
356            padding: 0.5em;
357            width: 20%;
358            background-color: #E0F0E0;
359            border: 1px solid #00C000;
360    }
361    
362    DIV#fixedBox, DIV#fixedBox INPUT, DIV#fixedBox TEXTAREA {
363            font-size: 10pt;
364    }
365    
366    FORM>DIV#fixedBox {
367            position: fixed !important;
368            left: 0.5em !important;
369            top: auto !important;
370            bottom: 1em !important;
371            width: 15% !important;
372    }
373    
374    DIV#fixedBox INPUT[type=text], DIV#fixedBox TEXTAREA {
375            border: 1px solid #00C000;
376    }
377    
378    DIV#fixedBox #note {
379            display: block;
380            width: 100%;
381    }
382    
383    DIV#fixedBox #submitBurner {
384            display: block;
385            width: 100%;
386            margin-top: 0.5em;
387            cursor: pointer;
388    }
389    
390    * HTML {
391            overflow-y: hidden;
392    }
393    
394    * HTML BODY {
395            overflow-y: auto;
396            height: 100%;
397            font-size: 100%;
398    }
399    
400      function checkAll(location)  * HTML DIV#fixedBox {
401      {          position: absolute;
402        for (var i=0;i<document.forma.elements.length;i++)  }
403        {  
404          var e = document.forma.elements[i];  #mContainer, #gradient, #mask, #progressIndicator {
405          if ((e.checked || !e.checked) && e.name != \'all\') {          display: block;
406              if (eval("document.forma."+location+".checked")) {          width: 100%;
407                  e.checked = true;          font-size: 10pt;
408              } else {          font-weight: bold;
409                  e.checked = false;          text-align: center;
410              }          vertical-align: middle;
411          }          padding: 1px;
412        }  }
413      }  
414  //-->  #gradient, #mask, #progressIndicator {
415  </script>                left: 0;
416            border-width: 1px;
417            border-style: solid;
418            border-color: #000000;
419            color: #404040;
420            margin: 0.4em;
421            position: absolute;
422            margin-left: -1px;
423            margin-top: -1px;
424            margin-bottom: -1px;
425            overflow: hidden;
426    }
427    
428    #mContainer {
429            display: block;
430            position: relative;
431            padding: 0px;
432            margin-top: 0.4em;
433            margin-bottom: 0.5em;
434    }
435    
436    #gradient {
437            z-index: 1;
438            background-color: #FFFF00;
439    }
440    
441    #mask {
442            z-index: 2;
443            background-color: #FFFFFF;
444    }
445    
446    #progressIndicator {
447            z-index: 3;
448            background-color: transparent;
449    }
450    -->
451    </style>
452    <script type="text/javascript">
453    <!--
454    
455    var debug_div = null;
456    var media_size = 4400 * 1024;
457    
458    function debug(msg) {
459    //      return; // Disable debugging
460    
461            if (! debug_div) debug_div = document.getElementById('debug');
462    
463            // this will create debug div if it doesn't exist.
464            if (! debug_div) {
465                    debug_div = document.createElement('div');
466                    if (document.body) document.body.appendChild(debug_div);
467                    else debug_div = null;
468            }
469            if (debug_div) {
470                    debug_div.appendChild(document.createTextNode(msg));
471                    debug_div.appendChild(document.createElement("br"));
472            }
473    }
474    
475    
476    var element_id_cache = Array();
477    
478    function element_id(name,element) {
479            if (! element_id_cache[name]) {
480                    element_id_cache[name] = self.document.getElementById(name);
481            }
482            return element_id_cache[name];
483    }
484    
485    function checkAll(location) {
486            var f = element_id('forma') || null;
487            if (!f) return false;
488    
489            var len = f.elements.length;
490            var check_all = element_id('allFiles');
491            var suma = check_all.checked ? (parseInt(f.elements['totalsize'].value) || 0) : 0;
492    
493            for (var i = 0; i < len; i++) {
494                    var e = f.elements[i];
495                    if (e.name != 'all' && e.name.substr(0, 3) == 'fcb') {
496                            if (check_all.checked) {
497                                    if (e.checked) continue;
498                                    var el = element_id("fss" + e.name.substr(3));
499                                    var size = parseInt(el.value) || 0;
500                                    debug('suma: '+suma+' size: '+size);
501                                    if ((suma + size) < media_size) {
502                                            suma += size;
503                                            e.checked = true;
504                                    } else {
505                                            break;
506                                    }
507                            } else {
508                                    e.checked = false;
509                            }
510                    }
511            }
512            update_sum(suma);
513    }
514    
515    function update_sum(suma) {
516            element_id('forma').elements['totalsize'].value = suma;
517            pbar_set(suma, media_size);
518            debug('total size: ' + suma);
519    }
520    
521    function sumiraj(e) {
522            var suma = parseInt(element_id('forma').elements['totalsize'].value) || 0;
523            var len = element_id('forma').elements.length;
524            if (e) {
525                    var size = parseInt( element_id("fss" + e.name.substr(3)).value);
526                    if (e.checked) {
527                            suma += size;
528                    } else {
529                            suma -= size;
530                    }
531            } else {
532                    suma = 0;
533                    for (var i = 0; i < len; i++) {
534                            var e = element_id('forma').elements[i];
535                            if (e.name != 'all' && e.checked && e.name.substr(0,3) == 'fcb') {
536                                    var el = element_id("fss" + e.name.substr(3));
537                                    if (el && el.value) suma += parseInt(el.value) || 0;
538                            }
539                    }
540            }
541            update_sum(suma);
542            return suma;
543    }
544    
545    /* progress bar */
546    
547    var _pbar_width = null;
548    var _pbar_warn = 10;    // change color in last 10%
549    
550    function pbar_reset() {
551            element_id("mask").style.left = "0px";
552            _pbar_width = element_id("mContainer").offsetWidth - 2;
553            element_id("mask").style.width = _pbar_width + "px";
554            element_id("mask").style.display = "block";
555            element_id("progressIndicator").style.zIndex  = 10;
556            element_id("progressIndicator").innerHTML = "0";
557    }
558    
559    function dec2hex(d) {
560            var hch = '0123456789ABCDEF';
561            var a = d % 16;
562            var q = (d - a) / 16;
563            return hch.charAt(q) + hch.charAt(a);
564    }
565    
566    function pbar_set(amount, max) {
567            debug('pbar_set('+amount+', '+max+')');
568    
569            if (_pbar_width == null) {
570                    var _mc = element_id("mContainer");
571                    if (_pbar_width == null) _pbar_width = parseInt(_mc.offsetWidth ? (_mc.offsetWidth - 2) : 0) || null;
572                    if (_pbar_width == null) _pbar_width = parseInt(_mc.clientWidth ? (_mc.clientWidth + 2) : 0) || null;
573                    if (_pbar_width == null) _pbar_width = 0;
574            }
575    
576            var pcnt = Math.floor(amount * 100 / max);
577            var p90 = 100 - _pbar_warn;
578            var pcol = pcnt - p90;
579            if (Math.round(pcnt) <= 100) {
580                    if (pcol < 0) pcol = 0;
581                    var e = element_id("submitBurner");
582                    debug('enable_button');
583                    e.disabled = false;
584                    var a = e.getAttributeNode('disabled') || null;
585                    if (a) e.removeAttributeNode(a);
586            } else {
587                    debug('disable button');
588                    pcol = _pbar_warn;
589                    var e = element_id("submitBurner");
590                    if (!e.disabled) e.disabled = true;
591            }
592            var col_g = Math.floor((_pbar_warn - pcol) * 255 / _pbar_warn);
593            var col = '#FF' + dec2hex(col_g) + '00';
594    
595            //debug('pcol: '+pcol+' g:'+col_g+' _pbar_warn:'+ _pbar_warn + ' color: '+col);
596            element_id("gradient").style.backgroundColor = col;
597    
598            element_id("progressIndicator").innerHTML = pcnt + '%';
599            //element_id("progressIndicator").innerHTML = amount;
600    
601            element_id("mask").style.clip = 'rect(' + Array(
602                    '0px',
603                    element_id("mask").offsetWidth + 'px',
604                    element_id("mask").offsetHeight + 'px',
605                    Math.round(_pbar_width * amount / max) + 'px'
606            ).join(' ') + ')';
607    }
608    
609    if (!self.body) self.body = new Object();
610    self.onload = self.document.onload = self.body.onload = function() {
611            //pbar_reset();
612            sumiraj();
613    };
614    
615    // -->
616    </script>
617    <div id="fixedBox">
618    
619    Size: <input type="text" name="totalsize" size="7" readonly="readonly" style="text-align:right;" value="0" /> kB
620    
621    <div id="mContainer">
622            <div id="gradient">&nbsp;</div>
623            <div id="mask">&nbsp;</div>
624            <div id="progressIndicator">0%</div>
625    </div>
626    <br/>
627    
628    Note:
629    <textarea name="note" cols="10" rows="5" id="note"></textarea>
630    
631    <input type="submit" id="submitBurner" value="Burn selected" name="submitBurner" />
632    
633    </div>
634    <!--
635    <div id="debug" style="float: right; width: 10em; border: 1px #ff0000 solid; background-color: #ffe0e0; -moz-opacity: 0.7;">
636    no debug output yet
637    </div>
638    -->
639  EOF3  EOF3
640                $retHTML .= q{<form name="forma" method="POST" action="}."$MyURL"."?action=burn\"";          $retHTML .= q{
641                $retHTML.= q{<input type="hidden" value="burn" name="action">};                          <input type="hidden" value="burn" name="action">
642                $retHTML .= q{<input type="hidden" value="results" name="search_results">};                          <input type="hidden" value="results" name="search_results">
643          }                          <table style="fview" border="0" cellspacing="0" cellpadding="2">
644        $retHTML .= "<table style=\"fview\">";                          <tr class="tableheader">
645        $retHTML .= "<tr> ";                          <td class="tableheader">
646        if ($addForm)                                  <input type="checkbox" name="allFiles" id="allFiles" onClick="checkAll('allFiles');">
647          {                          </td>
648              $retHTML .= "<td class=\"tableheader\"><input type=\"checkbox\" name=\"allFiles\" onClick=\"checkAll('allFiles');\"></td>";                          <td align="center">Share</td>
649          }                          <td align="center">Backup no</td>
650        $retHTML .=  "<td class=\"tableheader\">Host</td> <td class=\"tableheader\">Backup no</td> <td class=\"tableheader\">Type</td> <td class=\"tableheader\">date</td></tr>";                          <td align="center">Type</td>
651        my @backups = getBackupsNotBurned();                          <td align="center">date</td>
652        my $backup;                          <td align="center">age/days</td>
653                            <td align="center">size/MB</td>
654        if ($addForm)                          <td align="center">gzip size</td>
655          {                          </tr>
656              $retHTML .= "<tr>";  
657              $retHTML .= "<td colspan=7 style=\"tableheader\">";          };
658              $retHTML .= "<input type=\"submit\" value=\"Burn selected backups on medium\" name=\"submitBurner\">";  
659              $retHTML .= "</td>";          my @color = (' bgcolor="#e0e0e0"', '');
660              $retHTML .= "</tr>";  
661                        my $i = 0;
662          }          my $host = '';
663        foreach $backup(@backups)  
664          {          foreach my $backup ( getBackupsNotBurned() ) {
665              my $ftype = "";  
666                                if ($host ne $backup->{'host'}) {
667              $retHTML .= "<tr>";                          $i++;
668              if ($addForm)                          $host = $backup->{'host'};
669                {                  }
670                    $retHTML .= "<td class=\"fview\"> <input type=\"checkbox\" name=\"fcb"                  my $ftype = "";
671                      .$backup->{'hostid'}."_".$backup->{'backupno'}  
672                    ."\" value=\"".$backup->{'hostid'}."_".$backup->{'backupno'}."\"> </td>";                  $retHTML .=
673                }                              '<tr' . $color[$i %2 ] . '>
674                                        <td class="fview">';
675              $retHTML .= "<td class=\"fviewborder\">" . $backup->{'host'} . "</td>";                  # FIXME
676              $retHTML .= "<td class=\"fviewborder\">" . $backup->{'backupno'} . "</td>";                  $backup->{'fs_size'} = int($backup->{'size'} * 1024);
677              $retHTML .= "<td class=\"fviewborder\">" . $backup->{'type'} . "</td>";                  if (($backup->{'fs_size'} || 0) > 0) {
678              $retHTML .= "<td class=\"fviewborder\">" . $backup->{'date'} . "<td>";                          $retHTML .= '
679              $retHTML .= "</tr>";                          <input type="checkbox" name="fcb' .
680          }                          $backup->{'hostid'}.'_'.$backup->{'backupnum'} .
681        $retHTML .= "</table>";                          '" value="' . $backup->{'hostid'}.'_'.$backup->{'backupnum'} .
682        if ($addForm)                          '" onClick="sumiraj(this);">';
683         {                  }
684             $retHTML .= "</form>";                  $retHTML .=
685         }                          '</td>' .
686                                  '<td align="right">' . $backup->{'host'} . ':' . $backup->{'share'} . '</td>' .
687        return $retHTML;                          '<td align="center">' . $backup->{'backupnum'} . '</td>' .
688                              '<td align="center">' . $backup->{'type'} . '</td>' .
689                              '<td align="center">' . epoch_to_iso( $backup->{'date'} ) . '</td>' .
690    }                                '<td align="center">' . $backup->{'age'} . '</td>' .
691                            '<td align="right">' . $backup->{'size'} . '</td>' .
692  sub displayGrid($$$)                          '<td align="right">' . $backup->{'fs_size'} .
693    {                          '<input type="hidden" iD="fss'.$backup->{'hostid'}.'_'.$backup->{'backupnum'} . '" value="'. $backup->{'fs_size'} .'"></td>' .
694        my ($where, $addForm, $offset) = @_;  
695        my $retHTML = "";                          "</tr>\n";
696                  }
697        if ($addForm)  
698          {          $retHTML .= "</table>";
699                $retHTML .= q{<form name="forma" method="POST" action="}."$MyURL"."?action=search\"";          $retHTML .= "</form>";
               $retHTML.= q{<input type="hidden" value="search" name="action">};  
               $retHTML .= q{<input type="hidden" value="results" name="search_results">};  
         }  
       $retHTML .= "<table style=\"fview\">";  
       $retHTML .= "<tr> ";  
       $retHTML .=  "<td class=\"tableheader\">Host</td> <td class=\"tableheader\">Name</td> <td class=\"tableheader\">Type</td> <td class=\"tableheader\">backup no.</td> <td class=\"tableheader\">size</td> <td class=\"tableheader\">date</td>  <td class=\"tableheader\">Media</td></tr>";  
       my @files = getFiles($where, $offset);  
       my $file;  
   
       foreach $file(@files)  
         {  
             my $ftype = "";  
               
             if ($file->{'type'} == BPC_FTYPE_DIR)  
               {  
                   $ftype = "dir";  
               }  
             else  
               {  
                   $ftype = "file";  
               }  
             $retHTML .= "<tr>";  
             $retHTML .= "<td class=\"fviewborder\">" . $file->{'hname'} ."</td>";  
             $retHTML .= "<td class=\"fviewborder\">" . $file->{'fname'} . "</td>";  
             $retHTML .= "<td class=\"fviewborder\">" . $ftype . "</td>";  
             $retHTML .= "<td class=\"fviewborder\">" . $file->{'backupno'} . "</td>";  
             $retHTML .= "<td class=\"fviewborder\">" . $file->{'size'} . "</td>";  
             $retHTML .= "<td class=\"fviewborder\">" . $file->{'date'} . "</td>";  
             $retHTML .= "<td class=\"fviewborder\">" . $file->{'dvd'} . "</td>";  
             $retHTML .= "</tr>";  
         }  
       $retHTML .= "</table>";  
   
         
   
       $retHTML .= "<INPUT TYPE=\"hidden\" VALUE=\"\" NAME=\"offset\">";  
       for (my $ii = 1; $ii <= $#files; $ii++)  
       {  
           $retHTML .= "<a href = \"#\" onclick=\"document.forma.offset.value=$ii;document.forma.submit();\">$ii</a>";  
           if ($ii < $#files)  
             {  
                 $retHTML .= " | ";  
             }  
       }  
   
   
        if ($addForm)  
        {  
            $retHTML .= "</form>";  
        }  
700                
701        return $retHTML;          return $retHTML;
702    }  }      
703    
704    sub displayGrid($) {
705            my ($param) = @_;
706    
707            my $offset = $param->{'offset'};
708            my $hilite = $param->{'search_filename'};
709    
710            my $retHTML = "";
711    
712            my $start_t = time();
713    
714            my ($results, $files);
715            if ($param->{'use_hest'} && length($hilite) > 0) {
716                    ($results, $files) = getFilesHyperEstraier($param);
717            } else {
718                    ($results, $files) = getFiles($param);
719            }
720    
721            my $dur_t = time() - $start_t;
722            my $dur = sprintf("%0.4fs", $dur_t);
723    
724            my ($from, $to) = (($offset * $on_page) + 1, ($offset * $on_page) + $on_page);
725    
726            if ($results <= 0) {
727                    $retHTML .= qq{
728                            <p style="color: red;">No results found...</p>
729                    };
730                    return $retHTML;
731            } else {
732                    # DEBUG
733                    #use Data::Dumper;
734                    #$retHTML .= '<pre>' . Dumper($files) . '</pre>';
735            }
736    
737    
738            $retHTML .= qq{
739            <div>
740            Found <b>$results files</b> showing <b>$from - $to</b> (took $dur)
741            </div>
742            <table style="fview" width="100%" border="0" cellpadding="2" cellspacing="0">
743                    <tr class="fviewheader">
744                    <td></td>
745                    <td align="center">Share</td>
746                    <td align="center">Type and Name</td>
747                    <td align="center">#</td>
748                    <td align="center">Size</td>
749                    <td align="center">Date</td>
750                    <td align="center">Media</td>
751                    </tr>
752            };
753    
754            my $file;
755    
756            sub hilite_html($$) {
757                    my ($html, $search) = @_;
758                    $html =~ s#($search)#<b>$1</b>#gis;
759                    return $html;
760            }
761    
762            sub restore_link($$$$$$) {
763                    my $type = shift;
764                    my $action = 'RestoreFile';
765                    $action = 'browse' if (lc($type) eq 'dir');
766                    return sprintf(qq{<a href="?action=%s&host=%s&num=%d&share=%s&dir=%s">%s</a>}, $action, @_);
767            }
768    
769            my $i = $offset * $on_page;
770    
771            foreach $file (@{ $files }) {
772                    $i++;
773    
774                    my $typeStr  = BackupPC::Attrib::fileType2Text(undef, $file->{'type'});
775                    $retHTML .= qq{<tr class="fviewborder">};
776    
777                    $retHTML .= qq{<td class="fviewborder">$i</td>};
778    
779                    $retHTML .=
780                            qq{<td class="fviewborder" align="right">} . $file->{'hname'} . ':' . $file->{'sname'} . qq{</td>} .
781                            qq{<td class="fviewborder"><img src="$Conf{CgiImageDirURL}/icon-$typeStr.gif" alt="$typeStr" align="middle">&nbsp;} . hilite_html( $file->{'filepath'}, $hilite ) . qq{</td>} .
782                            qq{<td class="fviewborder" align="center">} . restore_link( $typeStr, ${EscURI( $file->{'hname'} )}, $file->{'backupnum'}, ${EscURI( $file->{'sname'})}, ${EscURI( $file->{'filepath'} )}, $file->{'backupnum'} ) . qq{</td>} .
783                            qq{<td class="fviewborder" align="right">} . $file->{'size'} . qq{</td>} .
784                            qq{<td class="fviewborder">} . epoch_to_iso( $file->{'date'} ) . qq{</td>} .
785                            qq{<td class="fviewborder">} . '?' . qq{</td>};
786    
787                    $retHTML .= "</tr>";
788            }
789            $retHTML .= "</table>";
790    
791            # all variables which has to be transfered
792            foreach my $n (qw/search_day_from search_month_from search_year_from search_day_to search_month_to search_year_to search_backup_day_from search_backup_month_from search_backup_year_from search_backup_day_to search_backup_month_to search_backup_year_to search_filename offset/) {
793                    $retHTML .= qq{<INPUT TYPE="hidden" NAME="$n" VALUE="$In{$n}">\n};
794            }
795    
796            my $del = '';
797            my $max_page = int( $results / $on_page );
798            my $page = 0;
799    
800            sub page_link($$$) {
801                    my ($param,$page,$display) = @_;
802    
803                    $param->{'offset'} = $page;
804    
805                    my $html = '<a href = "' . $MyURL;
806                    my $del = '?';
807                    foreach my $k (keys %{ $param }) {
808                            if ($param->{$k}) {
809                                    $html .= $del . $k . '=' . ${EscURI( $param->{$k} )};
810                                    $del = '&';
811                            }
812                    }
813                    $html .= '">' . $display . '</a>';
814            }
815    
816            $retHTML .= '<div style="text-align: center;">';
817    
818            if ($offset > 0) {
819                    $retHTML .= page_link($param, $offset - 1, '&lt;&lt;') . ' ';
820            }
821    
822            while ($page <= $max_page) {
823                    if ($page == $offset) {
824                            $retHTML .= $del . '<b>' . ($page + 1) . '</b>';
825                    } else {
826                            $retHTML .= $del . page_link($param, $page, $page + 1);
827                    }
828    
829                    if ($page < $offset - $pager_pages && $page != 0) {
830                            $retHTML .= " ... ";
831                            $page = $offset - $pager_pages;
832                            $del = '';
833                    } elsif ($page > $offset + $pager_pages && $page != $max_page) {
834                            $retHTML .= " ... ";
835                            $page = $max_page;
836                            $del = '';
837                    } else {
838                            $del = ' | ';
839                            $page++;
840                    }
841            }
842    
843            if ($offset < $max_page) {
844                    $retHTML .= ' ' . page_link($param, $offset + 1, '&gt;&gt;');
845            }
846    
847            $retHTML .= "</div>";
848    
849            return $retHTML;
850    }
851    
852  1;  1;

Legend:
Removed from v.9  
changed lines
  Added in v.142

  ViewVC Help
Powered by ViewVC 1.1.26