foo
[BackupPC.git] / lib / BackupPC / SearchLib.pm
index 8aa0e12..792d5b9 100644 (file)
@@ -17,9 +17,14 @@ my $pager_pages = 10;
 my $dsn = $Conf{SearchDSN};
 my $db_user = $Conf{SearchUser} || '';
 
-my $hest_index_path = $Conf{HyperEstraierIndex};
+my $hest_node_url = $Conf{HyperEstraierIndex};
 
 my $dbh;
+my $Dir = $Conf{InstallDir}."/data/log";
+open(LOG, ">>", "$Dir/LOG")
+select(LOG);
+$|=1;
+
 
 sub get_dbh {
        $dbh ||= DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } );
@@ -117,6 +122,13 @@ sub getWhere($) {
        push( @conditions, ' files.shareid = ' . $param->{'search_share'} ) if ($param->{'search_share'});
        push (@conditions, " upper(files.path) LIKE upper('%".$param->{'search_filename'}."%')") if ($param->{'search_filename'});
 
+       if ( $param->{burned} ) {
+               my $is_what = 'is null';
+               $is_what = '= 1' if ($param->{burned} eq 'burned');
+               push @conditions, "archive_burned.part $is_what";
+               push @conditions, "archive_burned.copy $is_what";
+       }
+
        return join(" and ", @conditions);
 }
 
@@ -212,6 +224,14 @@ sub getFiles($) {
        my $where = getWhere($param);
        $sql_where = " WHERE ". $where if ($where);
 
+       # do we have to add tables for burned media?
+       if ( $param->{burned} ) {
+               $sql_from .= qq{
+                       LEFT OUTER JOIN archive_backup on archive_backup.backup_id = backups.id
+                       LEFT OUTER JOIN archive_burned on archive_burned.archive_id = archive_backup.archive_id
+               };
+       }
+
        my $order = getSort('search', 'sql', $param->{'sort'});
 
        my $sql_order = qq{
@@ -251,16 +271,11 @@ sub getHyperEstraier_url($) {
 
        return unless $use_hest;
 
-       use HyperEstraier;
-       my ($index_path, $index_node_url);
+       use Search::Estraier 0.04;
+       die "direct access to Hyper Estraier datatase is no longer supported. Please use estmaster\n"
+               unless ($use_hest =~ m#^http://#);
 
-       if ($use_hest =~ m#^http://#) {
-               $index_node_url = $use_hest;
-       } else {
-               $index_path = $TopDir . '/' . $use_hest;
-               $index_path =~ s#//#/#g;
-       }
-       return ($index_path, $index_node_url);
+       return $use_hest;
 }
 
 sub getFilesHyperEstraier($) {
@@ -269,26 +284,19 @@ sub getFilesHyperEstraier($) {
        my $offset = $param->{'offset'} || 0;
        $offset *= $on_page;
 
-       die "no index_path?" unless ($hest_index_path);
-
-       use HyperEstraier;
-
-       my ($index_path, $index_node_url) = getHyperEstraier_url($hest_index_path);
+       die "no Hyper Estraier node URL?" unless ($hest_node_url);
 
        # open the database
        my $db;
-       if ($index_path) {
-               $db = HyperEstraier::Database->new();
-               $db->open($index_path, $HyperEstraier::ESTDBREADER);
-       } elsif ($index_node_url) {
-               $db ||= HyperEstraier::Node->new($index_node_url);
+       if ($hest_node_url) {
+               $db ||= Search::Estraier::Node->new($hest_node_url);
                $db->set_auth('admin', 'admin');
        } else {
                die "BUG: unimplemented";
        }
 
        # create a search condition object
-       my $cond = HyperEstraier::Condition->new();
+       my $cond = Search::Estraier::Condition->new();
 
        my $q = $param->{'search_filename'};
        my $shareid = $param->{'search_share'};
@@ -312,33 +320,32 @@ sub getFilesHyperEstraier($) {
 
        $cond->add_attr("shareid NUMEQ $shareid") if ($shareid);
 
-#      $cond->set_max( $offset + $on_page );
-       $cond->set_options( $HyperEstraier::Condition::SURE );
+       $cond->set_max( $offset + $on_page );
+       $cond->set_options( 'SURE' );
        $cond->set_order( getSort('search', 'est', $param->{'sort'} ) );
 
        # get the result of search
        my @res;
        my ($result, $hits);
 
-       if ($index_path) {
-               $result = $db->search($cond, 0);
-               $hits = $result->size;
-       } elsif ($index_node_url) {
+       if ($hest_node_url) {
                $result = $db->search($cond, 0);
-               $hits = $result->doc_num;
+               if ($result) {
+                       $hits = $result->hits;
+               } else {
+                       $hits = 0;
+                       return ($hits,[]);
+               }
        } else {
                die "BUG: unimplemented";
        }
 
        # for each document in result
        for my $i ($offset .. ($offset + $on_page - 1)) {
-               last if ($i >= $hits);
+               last if ($i >= $result->doc_num);
 
                my $doc;
-               if ($index_path) {
-                       my $id = $result->get($i);
-                       $doc = $db->get_doc($id, 0);
-               } elsif ($index_node_url) {
+               if ($hest_node_url) {
                        $doc = $result->get_doc($i);
                } else {
                        die "BUG: unimplemented";
@@ -374,19 +381,27 @@ sub get_tgz_size_by_name($) {
        my $name = shift;
 
        my $tgz = $Conf{InstallDir}.'/'.$Conf{GzipTempDir}.'/'.$name;
-
        my $size = -1;
 
        if (-f "${tgz}.tar.gz") {
+               print "stating ${tgz}.tar.gz...";
                $size = (stat("${tgz}.tar.gz"))[7];
+               print "size: $size\n";
        } elsif (-d $tgz) {
+               print "$tgz is dir, stating files in it...\n";
                opendir(my $dir, $tgz) || die "can't opendir $tgz: $!";
-               my @parts = grep { !/^\./ && -f "$tgz/$_" } readdir($dir);
+               my @parts = grep { !/^\./ && !/md5/ && -f "$tgz/$_" } readdir($dir);
                $size = 0;
                foreach my $part (@parts) {
+                       my $currSize =  (stat("$tgz/$part"))[7]; 
                        $size += (stat("$tgz/$part"))[7] || die "can't stat $tgz/$part: $!";
+                       print "\t$tgz/$part: $currSize\n";
                }
+               print "\ttotal $size\n";
+
                closedir $dir;
+       } else {
+               return -1;
        }
 
        return $size;
@@ -418,6 +433,36 @@ sub getGzipSize($$)
        );
 }
 
+sub getVolumes($) {
+       my $id = shift;
+
+       my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";
+
+       my $sth = $dbh->prepare(qq{
+               select
+                       size
+               from backup_parts
+               where backup_id = ?
+               order by part_nr asc
+       });
+
+       $sth->execute($id);
+
+       my $cumulative_size = 0;
+       my $volumes = 1;
+
+       while(my ($size) = $sth->fetchrow_array) {
+               if ($cumulative_size + $size > $max_archive_size) {
+                       $volumes++;
+                       $cumulative_size = $size;
+               } else {
+                       $cumulative_size += $size;
+               }
+       }
+
+       return ($volumes,$cumulative_size);
+}
+
 sub getBackupsNotBurned($) {
 
        my $param = shift;
@@ -444,7 +489,7 @@ print STDERR "## sort=". ($param->{'sort'} || 'no sort param') . " burn sql orde
                INNER JOIN shares       ON backups.shareID=shares.ID
                INNER JOIN hosts        ON backups.hostID = hosts.ID
                LEFT OUTER JOIN archive_backup ON archive_backup.backup_id = backups.id 
-               WHERE backups.inc_size > 0 AND backups.inc_deleted is false AND archive_backup.backup_id IS NULL
+               WHERE backups.inc_size > 0 AND backups.size > 0 AND backups.inc_deleted is false AND archive_backup.backup_id IS NULL AND backups.parts > 0
                GROUP BY
                        backups.hostID,
                        hosts.name,
@@ -466,14 +511,21 @@ print STDERR "## sort=". ($param->{'sort'} || 'no sort param') . " burn sql orde
        while ( my $row = $sth->fetchrow_hashref() ) {
                $row->{'age'} = sprintf("%0.1f", ( $row->{'age'} / 86400 ) );
                #$row->{'age'} = sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) );
-               $row->{'size'} = sprintf("%0.2f", $row->{'size'} / 1024 / 1024);
 
-               # do some cluster calculation (approximate) and convert to kB
-               $row->{'inc_size'} = int(($row->{'inc_size'} + 1023 ) / ( 2 * 1024 ) * 2);
+               my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";
+               if ($row->{size} > $max_archive_size) {
+                       ($row->{volumes}, $row->{inc_size_calc}) = getVolumes($row->{id});
+               }
+
+               $row->{size} = sprintf("%0.2f", $row->{size} / 1024 / 1024);
+
+               # do some cluster calculation (approximate)
+               $row->{inc_size} = int(( ($row->{inc_size} + 1023 ) / 2 )  * 2);
+               $row->{inc_size_calc} ||= $row->{inc_size};
                push @ret, $row;
        }
       
-       return @ret;      
+       return @ret;
 }
 
 sub displayBackupsGrid($) {
@@ -589,7 +641,7 @@ DIV#fixedBox #submitBurner {
        background-color: transparent;
 }
 
-#parts {
+#volumes {
        padding: 0.4em;
        display: none;
        width: 100%;
@@ -673,41 +725,51 @@ function checkAll(location) {
 
 function update_sum(suma, suma_disp) {
        if (! suma_disp) suma_disp = suma;
-       element_id('forma').elements['totalsize'].value = suma_disp;
+       suma_disp = Math.floor(suma_disp / 1024);
+       element_id('forma').elements['totalsize_kb'].value = suma_disp;
+       element_id('forma').elements['totalsize'].value = suma;
        pbar_set(suma, media_size);
        debug('total size: ' + suma);
 }
 
-function sumiraj(e) {
-       var suma = parseInt(element_id('forma').elements['totalsize'].value) || 0;
-       var len = element_id('forma').elements.length;
-       if (e) {
-               var size = parseInt( element_id("fss" + e.name.substr(3)).value);
-               if (e.checked) {
-                       suma += size;
+function update_size(name, checked, suma) {
+       var size = parseInt( element_id("fss" + name).value);
+
+       if (checked) {
+               suma += size;
+       } else {
+               suma -= size;
+       }
+
+       var volumes = parseInt( element_id("prt" + name).value);
+       debug('update_size('+name+','+checked+') suma: '+suma+' volumes: '+volumes);
+       if (volumes > 1) {
+               if (checked) {
+                       element_id("volumes").innerHTML = "This will take "+volumes+" mediums!";
+                       element_id("volumes").style.display = 'block';
+                       suma = size;
+                       update_sum(suma);
                } else {
                        suma -= size;
+                       element_id("volumes").style.display = 'none';
                }
+       }
 
-               var parts = parseInt( element_id("prt" + e.name.substr(3)).value);
-               if (suma > max_file_size && suma == size && parts > 1) {
-                       element_id("parts").innerHTML = "This will take "+parts+" mediums!";
-                       element_id("parts").style.display = 'block';
-                       update_sum(media_size, suma);
-                       suma = media_size;
-                       return suma;
-               } else {
-                       element_id("parts").style.display = 'none';
-               }
+       return suma;
+}
 
+function sumiraj(e) {
+       var suma = parseInt(element_id('forma').elements['totalsize'].value) || 0;
+       var len = element_id('forma').elements.length;
+       if (e) {
+               suma = update_size(e.name.substr(3), e.checked, suma);
                if (suma < 0) suma = 0;
        } else {
                suma = 0;
                for (var i = 0; i < len; i++) {
-                       var e = element_id('forma').elements[i];
-                       if (e.name != 'all' && e.checked && e.name.substr(0,3) == 'fcb') {
-                               var el = element_id("fss" + e.name.substr(3));
-                               if (el && el.value) suma += parseInt(el.value) || 0;
+                       var fel = element_id('forma').elements[i];
+                       if (fel.name != 'all' && fel.checked && fel.name.substr(0,3) == 'fcb') {
+                               suma = update_size(fel.name.substr(3), fel.checked, suma);
                        }
                }
        }
@@ -789,7 +851,8 @@ self.onload = self.document.onload = self.body.onload = function() {
 </script>
 <div id="fixedBox">
 
-Size: <input type="text" name="totalsize" size="7" readonly="readonly" style="text-align:right;" value="0" /> kB
+<input type="hidden" name="totalsize"/>
+Size: <input type="text" name="totalsize_kb" size="7" readonly="readonly" style="text-align:right;" value="0" /> kB
 
 <div id="mContainer">
        <div id="gradient">&nbsp;</div>
@@ -798,7 +861,7 @@ Size: <input type="text" name="totalsize" size="7" readonly="readonly" style="te
 </div>
 <br/>
 
-<div id="parts">&nbsp;</div>
+<div id="volumes">&nbsp;</div>
 
 Note:
 <textarea name="note" cols="10" rows="5" id="note"></textarea>
@@ -831,7 +894,7 @@ EOF3
                sort_header($param, 'Size/Mb', 'size', 'center') .
                sort_header($param, 'gzip size/Kb', 'incsize', 'center') .
        qq{
-                       </tr>
+                       <td align="center">medias</td></tr>
        };
 
        my @color = (' bgcolor="#e0e0e0"', '');
@@ -858,6 +921,8 @@ EOF3
                        <input type="checkbox" name="fcb' . $checkbox_key . '" value="' . $checkbox_key . '" onClick="sumiraj(this);">';
                }
 
+               my $img_url = $Conf{CgiImageDirURL};
+
                $retHTML .=
                        '</td>' .
                        '<td align="right">' . $backup->{'host'} . ':' . $backup->{'share'} . '</td>' .
@@ -866,9 +931,10 @@ EOF3
                        '<td align="center">' . epoch_to_iso( $backup->{'date'} ) . '</td>' .
                        '<td align="center">' . $backup->{'age'} . '</td>' .
                        '<td align="right">' . $backup->{'size'} . '</td>' .
-                       '<td align="right">' . $backup->{'inc_size'} .
-                       '<input type="hidden" id="fss'.$checkbox_key .'" value="'. $backup->{'inc_size'} .'"></td>' .
-                       '<input type="hidden" id="prt'.$checkbox_key .'" value="'. $backup->{'parts'} .'"></td>' .
+                       '<td align="right">' . sprintf("%0.1f", $backup->{'inc_size'} / 1024 ) .
+                       '<input type="hidden" id="fss'.$checkbox_key .'" value="'. $backup->{'inc_size_calc'} .'"></td>' .
+                       '<input type="hidden" id="prt'.$checkbox_key .'" value="'. $backup->{'volumes'} .'"></td>' .
+                       '<td align="left">' . ( qq{<img src="$img_url/icon-cd.gif" alt="media">} x $backup->{volumes} ) . '</td>' .
 
                        "</tr>\n";
        }