foo
[BackupPC.git] / lib / BackupPC / SearchLib.pm
index e3f44e8..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) {
+       if ($hest_node_url) {
                $result = $db->search($cond, 0);
-               $hits = $result->size;
-       } elsif ($index_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,18 +381,24 @@ 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 { !/^\./ && !/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;
@@ -476,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,
@@ -654,7 +667,7 @@ var max_file_size = $max_archive_file_size;
        $retHTML .= <<'EOF3';
 
 function debug(msg) {
-//     return; // Disable debugging
+       return; // Disable debugging
 
        if (! debug_div) debug_div = document.getElementById('debug');