/[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 6 by dpavlin, Thu Jun 23 09:47:59 2005 UTC revision 24 by dpavlin, Mon Jul 11 16:48:33 2005 UTC
# Line 25  sub getUnits() { Line 25  sub getUnits() {
25  }  }
26    
27  sub getWhere($) {  sub getWhere($) {
28      my ($param)    = @_;          my ($param)    = @_;
29      my $retSQL     = "";          my @conditions;
     my @conditions = ();  
     my $cond;  
30    
31                sub mk_iso_date($$) {
32                      my ($name,$suffix) = @_;
       
     if ( defined( $param->{'search_backup_day_from'} ) && $param->{'search_backup_day_from'} ne "") {  
         push( @conditions,  
             ' strftime("%d", datetime(backups.date, "unixepoch","localtime")) >= "'  
               . $param->{'search_backup_day_from'} ."\"");  
     }  
     if ( defined( $param->{'search_backup_day_to'} ) && $param->{'search_backup_day_to'} ne "") {  
         push( @conditions,  
             ' strftime("%d", datetime(backups.date, "unixepoch","localtime")) <= "'  
               . $param->{'search_backup_day_from'}  ."\"");  
     }  
     if ( defined( $param->{'search_backup_month_from'} ) && $param->{'search_backup_month_from'} ne "") {  
         push( @conditions,  
             ' strftime("%m", datetime(backups.date, "unixepoch","localtime")) >= "'  
               . $param->{'search_backup_month_from'}  ."\"");  
     }  
     if ( defined( $param->{'search_backup_month_to'} ) && $param->{'search_backup_month_to'} ne "") {  
         push( @conditions,  
             ' strftime("%m", datetime(backups.date, "unixepoch","localtime")) <= "'  
               . $param->{'search_backup_month_to'}  ."\"");  
     }  
     if ( defined( $param->{'search_backup_year_from'} ) && $param->{'search_backup_year_from'} ne "") {  
         push( @conditions,  
             ' strftime("%Y", datetime(backups.date, "unixepoch","localtime")) >= "'  
               . $param->{'search_backup_year_from'}  ."\"");  
     }  
     if ( defined( $param->{'search_backup_year_to'} ) && $param->{'search_backup_year_to'} ne "") {  
         push( @conditions,  
             ' strftime("%Y", datetime(backups.date, "unixepoch","localtime")) <= "'  
               . $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'} ."\"");  
     }  
33    
34      if ( defined( $param->{'search_host'} ) && $param->{'search_host'} ne "") {                  my $yyyy = $param->{ $name . '_year_' . $suffix} || return;
35        push( @conditions, ' backups.hostID = ' . $param->{'search_host'} );                  my $mm .= $param->{ $name . '_month_' . $suffix} ||
36      }                          ( $suffix eq 'from' ? 1 : 12);
37                    my $dd .= $param->{ $name . '_day_' . $suffix} ||
38                            ( $suffix eq 'from' ? 1 : 31);
39                    return sprintf("%04d-%02d-%02d", $yyyy, $mm, $dd);
40            }
41    
42            my $backup_from = mk_iso_date('search_backup', 'from');
43            push @conditions, qq{ date(backups.date, 'unixepoch','localtime') >= '$backup_from' } if ($backup_from);
44            my $backup_to = mk_iso_date('search_backup', 'to');
45            push @conditions, qq{ date(backups.date, 'unixepoch','localtime') <= '$backup_to' } if ($backup_to);
46    
47            my $files_from = mk_iso_date('search', 'from');
48            push @conditions, qq{ date(files.date, 'unixepoch','localtime') >= '$files_from' } if ($files_from);
49            my $files_to = mk_iso_date('search', 'to');
50            push @conditions, qq{ date(files.date, 'unixepoch','localtime') <= '$files_to' } if ($files_to);
51    
52      if ( defined ($param->{'search_filename'}) && $param->{'search_filename'} ne "") {          print STDERR "backup: $backup_from - $backup_to files: $files_from - $files_to cond:",join(" | ",@conditions);
         push (@conditions, " files.name LIKE '".$param->{'search_filename'}."%'");  
         }  
53            
54      $retSQL = "";          push( @conditions, ' backups.hostID = ' . $param->{'search_host'} ) if ($param->{'search_host'});
     foreach $cond(@conditions)  
       {  
           if ($retSQL ne "")  
             {  
                 $retSQL .= " AND ";  
             }  
           $retSQL .= $cond;  
       }        
55    
56                push (@conditions, " files.name LIKE '".$param->{'search_filename'}."%'") if ($param->{'search_filename'});
57      return $retSQL;  
58            return (
59                    join(" and ", @conditions),
60                    $files_from, $files_to,
61                    $backup_from, $backup_to
62            );
63  }  }
64    
65  sub getFiles($)  
66    sub getFiles($$)
67    {    {
68        my ($where) = @_;        my ($where, $offset) = @_;
69          
70                
71        my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}",        my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}",
72          "", "", { RaiseError => 1, AutoCommit => 1 } );          "", "", { RaiseError => 1, AutoCommit => 1 } );
73        my $sql =                  my $sql =          
74          q{            q{  
75                SELECT files.id                       AS fid,                  SELECT  files.id                        AS fid,
76                       hosts.name                     AS hname,                          hosts.name                      AS hname,
77                       shares.name                    AS sname,                          shares.name                     AS sname,
78                       shares.share                   AS sharename,                          shares.share                    AS sharename,
79                       backups.num                    AS backupNum,                          files.backupNum                 AS backupNum,
80                       files.name                     AS filename,                          files.name                      AS filename,
81                       files.path                     AS filepath,                          files.path                      AS filepath,
82                       shares.share||files.fullpath AS networkPath,                          shares.share||files.fullpath    AS networkPath,
83                       date(files.date, 'unixepoch', 'localtime') AS date,                          date(files.date, 'unixepoch', 'localtime') AS date,
84                       files.type                     AS filetype,                          files.type                      AS filetype,
85                       files.size                     AS size,                          files.size                      AS size,
86                       dvds.name                      AS dvd                          dvds.name                       AS dvd
87                    FROM                  FROM files
88                       files                          INNER JOIN shares       ON files.shareID=shares.ID
89                          INNER JOIN shares  ON files.shareID=shares.ID                          INNER JOIN hosts        ON hosts.ID = shares.hostID
90                          INNER JOIN hosts   ON hosts.ID = shares.hostID                          INNER JOIN backups      ON backups.num = files.backupNum and backups.hostID = hosts.ID
91                          INNER JOIN backups ON backups.hostID = hosts.ID                          LEFT  JOIN dvds         ON dvds.ID = files.dvdid
                         LEFT  JOIN dvds    ON dvds.ID = files.dvdid  
                       
92            };            };
93    
94        if (defined($where) && $where ne "")        if (defined($where) && $where ne "")
# Line 151  sub getFiles($) Line 96  sub getFiles($)
96              $sql .= " WHERE ". $where;                    $sql .= " WHERE ". $where;      
97          }          }
98    
99          $sql .=
100            q{          
101                ORDER BY files.id
102                  LIMIT 100
103                  OFFSET ? * 100 + 1
104            };
105          
106          
107                
108        my $st = $dbh->prepare(        my $st = $dbh->prepare(
109            $sql            $sql
110            );                );    
111          if (!defined($offset) && $offset ne "")
112          {
113            $st->bind_param(1, $offset);
114          }
115          else
116          {
117            $st->bind_param(1,0);
118          }
119        $st->execute;        $st->execute;
120                
121        my @ret = ();        my @ret = ();
# Line 307  EOF3 Line 267  EOF3
267        
268    }          }      
269    
270  sub displayGrid($$)  sub displayGrid($$$$) {
271    {          my ($where, $addForm, $offset, $hilite) = @_;
272        my ($where, $addForm) = @_;          my $retHTML = "";
273        my $retHTML = "";  
274                  if ($addForm) {
275        if ($addForm)                  $retHTML .= qq{<form name="forma" method="POST" action="}.$MyURL.qq{?action=search">};
276          {                  $retHTML.= qq{<input type="hidden" value="search" name="action">};
277        $retHTML .= <<EOF3;                  $retHTML .= qq{<input type="hidden" value="results" name="search_results">};
278  <script language="javascript" type="text/javascript">          }
279  <!--          $retHTML .= qq{
280            <table style="fview" width="100%">
281      function checkAll(location)                  <tr>
282      {                  <td class="tableheader">Host</td>
283        for (var i=0;i<document.forma.elements.length;i++)                  <td class="tableheader">Name</td>
284        {                  <td class="tableheader">Type</td>
285          var e = document.forma.elements[i];                  <td class="tableheader">#</td>
286          if ((e.checked || !e.checked) && e.name != \'all\') {                  <td class="tableheader">Size</td>
287              if (eval("document.forma."+location+".checked")) {                  <td class="tableheader">Date</td>
288                  e.checked = true;                  <td class="tableheader">Media</td>
289              } else {                  </tr>
290                  e.checked = false;          };
291              }          my @files = getFiles($where, $offset);
292          }          my $file;
293        }  
294      }          sub hilite_html($$) {
295                    my ($html, $search) = @_;
296                    $html =~ s#($search)#<b>$1</b>#gis;
297  //-->                  return $html;
298  </script>                }
299  EOF3  
300                $retHTML .= q{<form name="forma" method="POST" action="}."$MyURL"."?action=burn\"";          foreach $file (@files) {
301                $retHTML.= q{<input type="hidden" value="burn" name="action">};                  my $typeStr  = BackupPC::Attrib::fileType2Text(undef, $file->{'type'});
302                $retHTML .= q{<input type="hidden" value="results" name="search_results">};                  $retHTML .= "<tr>";
303    
304                    foreach my $v ((
305                            $file->{'hname'},
306                            qq{<img src="$Conf{CgiImageDirURL}/icon-$typeStr.gif" align="center">&nbsp;} . hilite_html( $file->{'fpath'}, $hilite ),
307                            $typeStr,
308                            $file->{'backupno'},
309                            $file->{'size'},
310                            $file->{'date'},
311                            $file->{'dvd'}
312                    )) {
313                            $retHTML .= qq{<td class="fviewborder">$v</td>};
314                    }
315    
316                    $retHTML .= "</tr>";
317            }
318            $retHTML .= "</table>";
319    
320            # skip pager
321            return $retHTML;
322    
323            $retHTML .= "<INPUT TYPE=\"hidden\" VALUE=\"\" NAME=\"offset\">";
324            for (my $ii = 1; $ii <= $#files; $ii++) {
325                    $retHTML .= "<a href = \"#\" onclick=\"document.forma.offset.value=$ii;document.forma.submit();\">$ii</a>";
326                    if ($ii < $#files) {
327                            $retHTML .= " | ";
328                    }
329          }          }
       $retHTML .= "<table style=\"fview\">";  
       $retHTML .= "<tr> ";  
       if ($addForm)  
         {  
             $retHTML .= "<td class=\"tableheader\"><input type=\"checkbox\" name=\"allFiles\" onClick=\"checkAll('allFiles');\"></td>";  
         }  
       $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);  
       my $file;  
330    
331        if ($addForm)          $retHTML .= "</form>" if ($addForm);
         {  
             $retHTML .= "<tr>";  
             $retHTML .= "<td colspan=7 style=\"tableheader\">";  
             $retHTML .= "<input type=\"submit\" value=\"Burn selected files on medium\" name=\"submitBurner\">";  
             $retHTML .= "</td>";  
             $retHTML .= "</tr>";  
               
         }  
       foreach $file(@files)  
         {  
             my $ftype = "";  
               
             if ($file->{'type'} == BPC_FTYPE_DIR)  
               {  
                   $ftype = "dir";  
               }  
             else  
               {  
                   $ftype = "file";  
               }  
             $retHTML .= "<tr>";  
             if ($addForm)  
               {  
                   $retHTML .= "<td class=\"fview\"> <input type=\"checkbox\" name=\"fcb"  
                     .$file->{'id'}  
                   ."\" value=\"".$file->{'id'}."\"> </td>";  
               }      
               
             $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>";  
       if ($addForm)  
        {  
            $retHTML .= "</form>";  
        }  
332                
333        return $retHTML;          return $retHTML;
334    }  }
335    
336  1;  1;

Legend:
Removed from v.6  
changed lines
  Added in v.24

  ViewVC Help
Powered by ViewVC 1.1.26