/[dehs]/trunk/dehs_pg.php
ViewVC logotype

Diff of /trunk/dehs_pg.php

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

revision 23 by evaso-guest, Mon Jul 19 16:39:33 2004 UTC revision 112 by atomo64-guest, Tue Feb 26 23:07:04 2008 UTC
# Line 1  Line 1 
1  #!/usr/bin/php4 -q  #!/usr/bin/php4 -q
2  <?  <?php
3  /*  /*
4    
5   Originally written by Stefano Fabri <bluefuture@nospam@email.it>  Originally written by Stefano Fabri <bluefuture@nospam@email.it>
6   Copyright 2004, Stefano Fabri  Copyright 2004, Stefano Fabri
7    Copyright 2007, 2008 by Raphael Geissert <atomo64@gmail.com>
8   This program is free software; you can redistribute it and/or modify  
9   it under the terms of the GNU General Public License as published by  This program is free software; you can redistribute it and/or modify
10   the Free Software Foundation; either version 2 of the License, or  it under the terms of the GNU General Public License as published by
11   (at your option) any later version.  the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13   This program is distributed in the hope that it will be useful,  
14   but WITHOUT ANY WARRANTY; without even the implied warranty of  This program is distributed in the hope that it will be useful,
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  but WITHOUT ANY WARRANTY; without even the implied warranty of
16   GNU General Public License for more details.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18   You should have received a copy of the GNU General Public License  
19   along with this program; if not, write to the Free Software  You should have received a copy of the GNU General Public License
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */  */
23    
24  // stabilire una connessione  // stabilire una connessione
25  #ini_set ( "error_reporting", "E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR");  #ini_set ( "error_reporting", "E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR");
26  ini_set ("html_errors", "off");  ini_set ("html_errors", "off");
27  set_time_limit(0);  set_time_limit(0);
28  ini_set ( "memory_limit", "15M");  ini_set ( "memory_limit", "40M");
29  #print_R($_SERVER);  #print_R($_SERVER);
30  require "dehs.config.inc";  require "dehs.config.inc";
31  dir_ctrl($dirs);  dir_ctrl($dirs);
32  if (!function_exists('file_get_contents')) {  if (!function_exists('file_get_contents')) {
33          function file_get_contents($filename) {      function file_get_contents($filename) {
34         if (!($file = fopen($filename,"r"))) return false;          if (!($file = fopen($filename,"rb"))) return false;
35             else {          else {
36                  $contents = "";              $contents = "";
37                          do {              do {
38                                  $data = fread($file, 4096);                  $data = fread($file, 4096);
39                                  if (strlen($data) == 0) {                  if (strlen($data) == 0) {
40                          break;                      break;
41                                  }                  }
42                                  $contents .= $data;                  $contents .= $data;
43                          } while (true);              } while (true);
44                  fclose($file);              fclose($file);
45                  return $contents;              return $contents;
46                  }          }
47     }      }
48  }  }
49  if (in_array("-db_add",$argv))  db_add($argv[2]);  
50          elseif (in_array("-db_query",$argv))  db_query($argv[2],$argv[3] );  if (!function_exists('file_put_contents')) {
51          elseif (in_array("-keep_diff",$argv))  keep_diff($argv[2]);      function file_put_contents($filename, $content, $flags = 0) {
52                  elseif (in_array("-keep_sources",$argv))  keep_sources();          if (!($file = fopen($filename,$flags))) return false;
53                  elseif (in_array("-db_popcon",$argv))  db_popcon($argv[2]);          else {
54                  elseif (in_array("-keep_popcon",$argv))  keep_popcon($argv[2]);              $n = fwrite($file, $content);
55                  elseif (in_array("-db_upstream",$argv))  db_upstream($argv[2]);              fclose($file);
56                  elseif (in_array("-update_all",$argv))  update_all();              return true;
57                  elseif (in_array("-check_db",$argv))  check_db();          }
58                  elseif (in_array("-db_up_error",$argv))  db_up_error();      }
59                  elseif (in_array("-up_changes",$argv))  up_changes($argv[2]);  }
60                  elseif (in_array("-watchf",$argv))  watch_fill($argv[2]);  
61          elseif (in_array("-kb_total",$argv))  print "Diff  if (in_array("-db_add",$argv))  db_add((!empty($argv[2])?$argv[2]:''), false);
62        elseif (in_array("-db_query",$argv))  db_query($argv[2],(!empty($argv[3])?$argv[3]:'%') );
63        elseif (in_array("-dl_diffs",$argv) || in_array("-keep_diff",$argv))  dl_diffs($argv[2]);
64        elseif (in_array("-keep_sources",$argv) || in_array("-download_sources",$argv))  download_sources();
65        elseif (in_array("-db_popcon",$argv))  db_popcon($argv[2]);
66        elseif (in_array("-dl_popcon",$argv) || in_array("-keep_popcon",$argv))  dl_popcon($argv[2]);
67        elseif (in_array("-db_upstream",$argv))  db_upstream((!empty($argv[2])?$argv[2]:'%'));
68        elseif (in_array("-update_all",$argv))  update_all();
69        elseif (in_array("-update_new",$argv))  update_new();
70        elseif (in_array("-update_bogus",$argv))  update_bogus();
71        elseif (in_array("-check_db",$argv))  check_db();
72        elseif (in_array("-dehsqa_db",$argv))  dehsqa_db();
73        elseif (in_array("-clear_db",$argv))  clear_db();
74        elseif (in_array("-db_up_error",$argv))  db_up_error();
75        elseif (in_array("-up_changes",$argv))  up_changes((!empty($argv[2])?$argv[2]:'%'), true);
76        elseif (in_array("-watchf",$argv) || in_array("-wwiz",$argv))  watch_wizard((!empty($argv[2])?$argv[2]:'%'));
77        elseif (in_array("-kb_total",$argv))  print "Diff
78  #stat_up_error();  #stat_up_error();
79  #stat_up_error();to download Kb => " . kb_total($argv[2]) . "\n" ;  #stat_up_error();to download Kb => " . kb_total($argv[2]) . "\n" ;
80                  elseif (in_array("-watch_stat",$argv))  watch_stat();      elseif (in_array("-watch_stat",$argv))  watch_stat();
81          else print "Command $argv[1] not found\n";      else print "Command $argv[1] not found\n";
82  #stat_up_error();  #stat_up_error();
83  function dir_ctrl($dirs) {  function dir_ctrl($dirs) {
84          foreach ($dirs as $dir) {      foreach ($dirs as $dir) {
85                  if (!is_dir($dir)) exec("mkdir -m 0740 -p $dir",$output,$res);          if (!is_dir($dir)) exec("mkdir -m 0740 -p " .  escapeshellarg($dir), $output, $res);
86                                  if ($res!=0) die("Error creating basic dir: $dir\n");          if ($res != 0) die_status("Error creating basic dir: $dir\n");
87          }      }
88  }  }
89  function ext_watch ($file,$pkg,$version){  function ext_watch ($file,$pkg,$version) {
90          if (!file_exists($file)) return "";      if (!file_exists($file)) return "";
91          $zp=gzopen($file,"r") or die("Fail to open $file");      $zp=gzopen($file,"r") or die_status("Fail to open $file");
92          while (!gzeof($zp)) {      while (!gzeof($zp)) {
93                  $line = gzgets ($zp,4096);          $line = gzgets ($zp,4096);
94          if (preg_match("/\+\+\+\s.+\/debian\/watch/",$line)) {          if (preg_match('/\+\+\+\s.+\/debian\/watch$/',$line)) {
95                      $line = gzgets ($zp,4096);              $line = gzgets ($zp,4096);
96                          preg_match ("/.+\+\d+\,(\d+)\s/",$line,$watch_lenght);              preg_match ("/.+\+\d+\,(\d+)\s/",$line,$watch_lenght);
97                          for ($i = 1; $i <= $watch_lenght[1]; $i++)      {              for ($i = 1; $i <= $watch_lenght[1]; $i++)      {
98                                  $line=gzgets ($zp,4096);                  $line=gzgets ($zp,4096);
99                                  $watch .=substr($line,1);                  $watch .=substr($line,1);
                         }  
                 break;  
100              }              }
101                break;
102          }          }
103  if ($watch!=null) return $watch;      }
104  else return NULL;      if ($watch!=null) return $watch;
105        else return NULL;
106  }  }
107  function ftp_cn($login,$password,$ftp_server) {  function ftp_cn($login,$password,$ftp_server) {
108  $conn_id = @ftp_connect($ftp_server) or DIE("Error: Cannot contact ftp server $ftp_server\n");      $conn_id = @ftp_connect($ftp_server) or die_status("Error: Cannot contact ftp server $ftp_server\n");
109  // login con user name e password      // login con user name e password
110  ftp_pasv($conn_id,1);      ftp_pasv($conn_id,1);
111  $login_result = ftp_login($conn_id, "anonymous","bo@bo.org");      $login_result = ftp_login($conn_id, "anonymous","bo@bo.org");
112  // controllo della connessione      // controllo della connessione
113  if ((!$conn_id) || (!$login_result)) {      if ((!$conn_id) || (!$login_result)) {
114          echo "Ftp connection failed!\n";          echo "Ftp connection failed!\n";
115          echo "Failed connection to $ftp_server\n";          echo "Failed connection to $ftp_server\n";
116          die();          die_status();
117      } else {      } else {
118          return $conn_id;          return $conn_id;
119      }      }
120  }  }
121  function keep_diff($initial) {  function dl_diffs($initial = '') {
122          global $dirs,$dbconn,$mirrors;      global $dirs,$dbconn,$mirrors;
123              check_db();      check_db();
124           $conn_normal=ftp_cn("anonymous","bo@bo.org",$mirrors['unstable']);      $conn_normal=ftp_cn("anonymous","bo@bo.org",$mirrors['unstable']);
125           $conn_non_us=ftp_cn("anonymous","bo@bo.org",$mirrors["non-US"]);      #$conn_non_us=ftp_cn("anonymous","bo@bo.org",$mirrors["non-US"]);
126         $db = pg_pconnect($dbconn) ;      $db = pg_pconnect($dbconn) ;
127         $rsql=pg_exec($db, "SELECT * FROM pkgs WHERE name like '$initial%' AND (md5_diff!=md5_atsource or md5_diff is null)") OR DIE("Error in diff query\n");      $rsql=pg_exec($db, "SELECT * FROM pkgs WHERE name like '$initial%' AND (md5_diff!=md5_atsource or md5_diff is null)") OR die_status("Error in diff query\n");
128                  $total=pg_numrows($rsql);      $total=pg_numrows($rsql);
129                          $btotal=byte_total($db,$initial);      $btotal=byte_total($db,$initial);
130                          print "Total Diff to downlaod Kb => " . number_format ($btotal/1024, 2, ",",".") . "\n";      print "Total Diff to downlaod Kb => " . number_format ($btotal/1024, 2, ",",".") . "\n";
131                  while ($res_array=pg_fetch_array($rsql)) {      while ($res_array=pg_fetch_array($rsql)) {
132                          if ($res_array['dist']=='non-US') $conn_id = $conn_non_us;          #if ($res_array['dist']=='non-US') $conn_id = $conn_non_us;
133                          else $conn_id = $conn_normal;          #else
134                                  if   ($localfile=download_diffs($conn_id,$res_array[name],$res_array[version],$res_array['dir'],$res_array[md5_diff],$res_array[md5_atsource],$res_array[bytes],$res_array[dist])) {          $conn_id = $conn_normal;
135                                                  $watch=ext_watch($localfile,$res_array[name],$res_array[version]);          if   ($localfile=download_diff($conn_id,$res_array[name],$res_array[version],$res_array['dir'],$res_array[md5_diff],$res_array[md5_atsource],$res_array[bytes],$res_array[dist])) {
136                                                  unlink($localfile);              $watch=ext_watch($localfile,$res_array[name],$res_array[version]);
137                                                  if ($watch!=NULL) {              unlink($localfile);
138                                                          $watch= addslashes($watch);              if ($watch!=NULL) {
139                                                          pg_exec($db, "UPDATE pkgs SET watch='" . $watch . "', md5_diff='" . $res_array[md5_atsource] . "' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'") or Die("Query error");                  $watch=iconv("ISO-8859-1","UTF-8",$watch);
140                                                  }                  $watch= pg_escape_string($watch);
141                                                  else pg_exec($db, "UPDATE pkgs SET watch=NULL, md5_diff='" . $res_array[md5_atsource] . "' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'") or Die("Errore nella query");                  pg_exec($db, "UPDATE pkgs SET watch='" . $watch . "', md5_diff='" . $res_array[md5_atsource] . "' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'") or die_status("Query error");
142                                          }              }
143                          $btotal=$btotal-$res_array['bytes'];              else pg_exec($db, "UPDATE pkgs SET watch=NULL, md5_diff='" . $res_array[md5_atsource] . "' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'") or die_status("Errore nella query");
144                          number_format ($btotal/1024, 2, ",",".");          }
145                  print "\rDiffs left =>  " . $total-- . " Bytes left => " . number_format ($btotal/1024, 2, ",",".") . " - " . $res_array[name];          $btotal=$btotal-$res_array['bytes'];
146                          }          number_format ($btotal/1024, 2, ",",".");
147          ftp_quit($conn_normal);          print "\rDiffs left =>  " . $total-- . " Bytes left => " . number_format ($btotal/1024, 2, ",",".") . " - " . $res_array[name];
148          ftp_quit($conn_non_us);      }
149          pg_close($db);      ftp_quit($conn_normal);
150          return "Fatto\n";      pg_close($db);
151  }      return "Fatto\n";
152  function download_diffs(&$conn_id,$pkg,$version,$directory,$md5_diff,$md5_atsource,$diff_kb,$dist) {  }
153  global $dirs,$mirrors;  function download_diff(&$conn_id,$pkg,$version,$directory,$md5_diff,$md5_atsource,$diff_kb,$dist) {
154                          $localfile=$dirs[diffs_dir] . "/$pkg" . "_" . "$version.diff.gz";      global $dirs,$mirrors;
155                          if ($dist=="non-US") $remotefile="/debian-non-US/$directory/$pkg" . "_" . "$version.diff.gz";      $localfile=$dirs[diffs_dir] . "/$pkg" . "_" . "$version.diff.gz";
156                          else $remotefile="/debian/$directory/$pkg" . "_" . "$version.diff.gz";      #if ($dist=="non-US") $remotefile="/debian-non-US/$directory/$pkg" . "_" . "$version.diff.gz";
157                          #print "$localfile - $remotefile\n";      #else
158                  if      (ftp_get($conn_id,$localfile,$remotefile,FTP_BINARY)) {      $remotefile="/debian/$directory/$pkg" . "_" . "$version.diff.gz";
159                      #    print "$pkg Diff.gz => Downloaded\n";      #print "$localfile - $remotefile\n";
160                  if(md5_of_file($localfile)==$md5_atsource) {      if      (ftp_get($conn_id,$localfile,$remotefile,FTP_BINARY)) {
161                                  #print "MD5 => OK\n";          #    print "$pkg Diff.gz => Downloaded\n";
162                                  #unlink($localfile);          if(md5_of_file($localfile)==$md5_atsource) {
163                                  return $localfile;              #print "MD5 => OK\n";
164                                  }              #unlink($localfile);
165                          }              return $localfile;
166                  print "Download Diff.gz of package $pkg => Fallito\n";          }
167                          return false;      }
168  }      print "Download Diff.gz of package $pkg => Fallito\n";
169  function db_add($initial=''){      return false;
170  global $dirs,$dists,$dbconn;  }
171  check_db();  function db_add($initial='') {
172  keep_source();      global $dirs,$dists,$dbconn;
173  $db = pg_connect($dbconn)  or  die(pg_errormessage($db));      check_db();
174  pg_exec($db, "CREATE TEMP TABLE pkgs_atsrc (name text,dist text)") or die('Error creating temp table pkgs_atsrc');      download_sources();
175  pg_exec($db, "CREATE UNIQUE INDEX idxdis on pkgs_atsrc (name,dist)") or die('Error creating index on temp table');  
176  pg_exec($db, "CREATE TEMP TABLE bin_atsrc (name text,bin_name text, dist text)") or die('Error creating temp table pkgs_atsrc');      $db = pg_pconnect($dbconn)  or  die_status(pg_last_error($db));
177  pg_exec($db, "CREATE UNIQUE INDEX idxbis on bin_atsrc (name,bin_name,dist)") or die('Error creating index on temp table bin_atsrc');      pg_exec($db, "CREATE TEMP TABLE pkgs_atsrc (name text,dist text)") or die_status('Error creating temp table pkgs_atsrc');
178          foreach ($dists as $dist=>$sections) {      pg_exec($db, "CREATE UNIQUE INDEX idxdis on pkgs_atsrc (name,dist)") or die_status('Error creating index on temp table');
179        pg_exec($db, "CREATE TEMP TABLE bin_atsrc (name text,bin_name text, dist text)") or die_status('Error creating temp table pkgs_atsrc');
180        pg_exec($db, "CREATE UNIQUE INDEX idxbis on bin_atsrc (name,bin_name,dist)") or die_status('Error creating index on temp table bin_atsrc');
181    
182        foreach ($dists as $dist=>$sections) {
183          foreach ($sections as $section) {          foreach ($sections as $section) {
184                  $filename=$dirs[sources_dir] . "/$dist/$section/Sources.gz";              $filename=$dirs['sources_dir'] . "/$dist/$section/Sources.gz";
185                          print "\nI'm parsing $dist/$section => Sources.gz\n";              print "\nI'm parsing $dist/$section => Sources.gz\n";
186          # 1 Pacchetto - 2 versione - 3 np - 4 Directory - 5 md5              # 1 Pacchetto - 2 versione - 3 np - 4 Directory - 5 md5
187                  $regexp="/Package:\s($initial.*)[^a]Binary:\s(.+)[^a]Version:\s(\d+:)?(.+)[^a]Priority.+Maintainer:.+<([^>]+)>[^a].+Directory:\s(.+)[^a]Files:.+\s(\S+)\s(\d+)\s" . '\1\S+' . "\.diff\.gz[^a](Uploaders:\s(.+)[^a])?/Ssi";              $regexp="/Package:\s($initial.*)[^a]Binary:\s(.+)[^a]Version:\s(\d+:)?(.+)[^a]Priority.+Maintainer:.+<([^>]+)>[^a].+Directory:\s(.+)[^a]Files:.+\s(\S+)\s(\d+)\s" . '\1\S+' . "\.diff\.gz[^a](Uploaders:\s(.+)[^a])?/Ssi";
188                  $zp = gzopen($filename, "r") or die("Could not open $filename");              $zp = gzopen($filename, "r") or die_status("Could not open $filename");
189                          $extracted="/tmp/$dist_$section_Sources.gz";              $extracted=tempnam("/tmp/", "$dist_$section_Sources");
190                          exec("gzip -c -d $filename > $extracted");              exec("gzip -c -d $filename > $extracted");
191                          $sourcesize=filesize($extracted);              $sourcesize=filesize($extracted);
192                          unlink($extracted);              unlink($extracted);
193                          if(filesize($filename)>20) {              if(filesize($filename)>20) {
194                  while (!gzeof($zp)) {                  while (!gzeof($zp)) {
195                     $line='';                      $line='';
196                          while ($line!="\n")  {                      while ($line!="\n")  {
197                                  $line = gzgets ($zp,4096) ;                          $line = gzgets ($zp,4096) ;
198                                  $buff1 .= $line;                          $buff1 .= $line;
199                        }
200                        if (preg_match($regexp,$buff1,$matches))  {
201                            if ($matches[5]) {
202                                $matches[5]=iconv("ISO-8859-1","UTF-8",$matches[5]);
203                                $matches[5]=pg_escape_string($matches[5]);
204                            }
205                            if ($matches[10]) {
206                                $matches[10]=iconv("ISO-8859-1","UTF-8",$matches[10]);
207                                $matches[10]=pg_escape_string($matches[10]);
208                            }
209                            @pg_exec($db,"INSERT INTO pkgs_atsrc (name,dist) VALUES ('$matches[1]','$dist')") OR die_status("Temp table pkgs_atsrc query error");
210                            $rst=@pg_exec($db, "INSERT INTO pkgs (name,version,dversionmangled,maint,dir,md5_atsource,bytes,dist,section,uploaders) VALUES ('$matches[1]','$matches[4]','$matches[4]','$matches[5]','$matches[6]','$matches[7]','$matches[8]','$dist','$section','$matches[10]')");
211                            if (!$rst) {
212                                $rsql=pg_exec($db, "SELECT pkgs.version FROM pkgs WHERE name='$matches[1]' AND dist='$dist';");
213                                $version = $matches[4]; // just for safety
214                                while ($res_array=pg_fetch_array($rsql)) {
215                                    $version = $res_array['version'];
216                                }
217                                $extra = '';
218                                if ($version != $matches[4]) {
219                                    // only update the dversionmangled field if the versions differ
220                                    $extra = ",dversionmangled='$matches[4]'";
221                                }
222                                $rst=@pg_exec($db, "UPDATE pkgs SET name='$matches[1]',version='$matches[4]'$extra,maint='$matches[5]',dir='$matches[6]',md5_atsource='$matches[7]',bytes='$matches[8]',dist='$dist',section='$section',uploaders='$matches[10]' WHERE name='$matches[1]' AND dist='$dist'") OR die_status("\nDb adding error =>" . pg_last_error() . "\n");
223                          }                          }
224                          if (preg_match($regexp,$buff1,$matches))  {                          $bin_names=split(",", $matches[2]);
225                                                          if ($matches[10]) {                          foreach ($bin_names as $bin_name) {
226                                                                  $matches[10]=iconv("ISO-8859-1","UTF-8",$matches[10]);                              pg_exec($db,"INSERT INTO bin_atsrc (name,bin_name,dist) VALUES ('$matches[1]','" . trim($bin_name) . "','$dist')") OR die_status("Temp table pkgs_atsrc query error");
227                                                                  $matches[10]=addslashes($matches[10]);                              $rst=@pg_exec($db, "INSERT INTO binpkgs (name,bin_name,dist) VALUES ('$matches[1]','" . trim($bin_name) . "','$dist')") ;
228                                                          }                          }
229                                                          @pg_exec($db,"INSERT INTO pkgs_atsrc (name,dist) VALUES ('$matches[1]','$dist')") OR DIE("Temp table pkgs_atsrc query error");                      }
230                                                          $rst=@pg_exec($db, "INSERT INTO pkgs (name,version,maint,dir,md5_atsource,bytes,dist,section,uploaders) VALUES ('$matches[1]','$matches[4]','$matches[5]','$matches[6]','$matches[7]','$matches[8]','$dist','$section','$matches[10]')") ;                      else {
231                                                          if (!$rst) {                          preg_match("/Package:\s(.+)[^a]Binary/im",$buff1,$matches);
232                                                                  $rst=@pg_exec($db, "UPDATE pkgs SET name='$matches[1]',version='$matches[4]',maint='$matches[5]',dir='$matches[6]',md5_atsource='$matches[7]',bytes='$matches[8]',dist='$dist',section='$section',uploaders='$matches[10]' WHERE name='$matches[1]' AND dist='$dist'") OR DIE ("\nDb adding error =>" . pg_errormessage() . "\n");                          if(strncmp($initial,$matches[1],strlen($initial))<0) break;
233                                                                          }                      }
234                                                          $bin_names=split(",", $matches[2]);                      $left=$sourcesize-gztell($zp);
235                                                          foreach ($bin_names as $bin_name) {                      print "\rSource file Left => $left";
236                                                                  pg_exec($db,"INSERT INTO bin_atsrc (name,bin_name,dist) VALUES ('$matches[1]','" . trim($bin_name) . "','$dist')") OR DIE("Temp table pkgs_atsrc query error");                      $buff1="";
237                                                                  $rst=@pg_exec($db, "INSERT INTO binpkgs (name,bin_name,dist) VALUES ('$matches[1]','" . trim($bin_name) . "','$dist')") ;                      unset($matches);
238                                                          }                  }
                                                 }  
                                         else {  
                                         preg_match("/Package:\s(.+)[^a]Binary/im",$buff1,$matches);  
                                         if(strncmp($initial,$matches[1],strlen($initial))<0) break;  
                                         }  
                                         $left=$sourcesize-gztell($zp);  
                                         print "\rSource file Left => $left";  
                                         $buff1="";  
                                         unset($matches);  
                                 }  
239              }              }
240              gzclose($zp) ;              gzclose($zp) ;
241          }          }
242      }      }
243              clear_db($db);      clear_db($db);
244             pg_close($db);      pg_close($db);
245  }  }
246  function db_query($pkg,$dist) {  function db_query($pkg,$dist='%') {
247          global $dirs,$dbconn;      global $dirs,$dbconn;
248                  $db = pg_pconnect($dbconn )  or  die(pg_errormessage($db));      $db = pg_pconnect($dbconn )  or  die_status(pg_last_error($db));
249                  $rsql=pg_exec($db, "SELECT pkgs.* ,mpop_inst FROM (SELECT name,MAX(binpkgs.pop_inst) AS mpop_inst FROM binpkgs GROUP BY name)      $rsql=pg_exec($db, "SELECT pkgs.* ,mpop_inst FROM (SELECT name,MAX(binpkgs.pop_inst) AS mpop_inst FROM binpkgs GROUP BY name)
250                                                                          AS binpkgs INNER JOIN pkgs ON pkgs.name=binpkgs.name                                                                          AS binpkgs INNER JOIN pkgs ON pkgs.name=binpkgs.name
251                                WHERE pkgs.name='$pkg' AND dist='$dist';");                                WHERE pkgs.name='$pkg' AND dist='$dist';");
252         if (pg_numrows($rsql)==1) {      while ($res_array=pg_fetch_array($rsql)) {
253                          $res_array=pg_fetch_array($rsql);          print "Package=> " . $res_array['name'] . "\n";
254                  print "Package=> " . $res_array['name'] . "\n";          print "Version => " . $res_array['version'] . "\n";
255                  print "Version => " . $res_array['version'] . "\n";          print "Mangled version => " . $res_array['dversionmangled'] . "\n";
256                  print "Diff MD5 Downloaded => " . $res_array['md5_diff'] . "\n";          print "Diff MD5 Downloaded => " . $res_array['md5_diff'] . "\n";
257                                  print "Diff MD5 at Sources => " . $res_array['md5_atsource'] . "\n";          print "Diff MD5 at Sources => " . $res_array['md5_atsource'] . "\n";
258                                  print "Distribution => " . $res_array['dist'] . "\n";          print "Distribution => " . $res_array['dist'] . "\n";
259                                  print "Section => " . $res_array['section'] . "\n";          print "Section => " . $res_array['section'] . "\n";
260                  print "KBytes => " . number_format ( $res_array['bytes']/1024, 2, ",",".") . "\n";          print "KBytes => " . number_format ( $res_array['bytes']/1024, 2, ",",".") . "\n";
261                                  print "Pop Inst=>" . $res_array['mpop_inst'] . "\n";          print "Pop Inst=>" . $res_array['mpop_inst'] . "\n";
262          if ($res_array['watch']) {          if ($res_array['watch']) {
263                         $uscan_res=uscan_foo($res_array['name'] ,$res_array['version'],$res_array['watch']);              $uscan_res=uscan_foo($res_array['name'] ,$res_array['version'],$res_array['watch']);
264                  print "Version Upstream => " . $uscan_res[0] . "\n";              print "Version Upstream => " . $uscan_res[0] . "\n";
265                              print "Watch => " . $res_array['watch'];              print "Watch => " . trim($res_array['watch'])."\n";
266                                  print "Watch Warings => " . $uscan_res[1];              print "Watch Warnings => " . $uscan_res[1];
267          }          }
268          else print "Watch => Not present" . "\n";          else print "Watch => Not present" . "\n";
269            if ($res_array['wwiz_type']) {
270                $uscan_res=uscan_foo($res_array['name'] ,$res_array['version'],$res_array['wwiz']);
271                print "WWiz Upstream => " . $uscan_res[0] . "\n";
272                print "WWiz => " . $res_array['wwiz']."\n";
273                print "WWiz Warings => " . $uscan_res[1];
274          }          }
275       else echo "Package=> " . $pkg . "\nNot found\n!!";          print "\n";
276     pg_close($db);      }
277        if (pg_numrows($rsql)==0) echo "Package=> " . $pkg . "\nNot found\n!!";
278        pg_close($db);
279  }  }
280  function byte_total(&$db,$initial){  function byte_total(&$db,$initial) {
281  global $dirs;      global $dirs;
282  $rsql=pg_exec ($db,"Select sum(bytes) as totbytes FROM pkgs WHERE name like '$initial%' AND (md5_diff!=md5_atsource or md5_diff is null)");      $rsql=pg_exec ($db,"Select sum(bytes) as totbytes FROM pkgs WHERE name like '$initial%' AND (md5_diff!=md5_atsource or md5_diff is null)");
283  $res_array=pg_fetch_array($rsql);      $res_array=pg_fetch_array($rsql);
284  return $res_array[totbytes] ;      return $res_array[totbytes] ;
285  }  }
286  function keep_source (){  function download_sources () {
287                  global $dirs,$dists,$mirrors;      global $dirs,$dists,$mirrors;
288          foreach ($dists as $dist=>$sections) {      foreach ($dists as $dist=>$sections) {
289                                  $server=$mirrors[$dist];          $server=$mirrors[$dist];
290                      $conn_id = ftp_cn("anonymous","bo@bo.org",$server);          $conn_id = ftp_cn("anonymous","bo@bo.org",$server);
291          foreach ($sections as $section) {          foreach ($sections as $section) {
292                  $localfile=$dirs[sources_dir] . "/$dist/$section/Sources.gz";              $localfile=$dirs[sources_dir] . "/$dist/$section/Sources.gz";
293                 $remotefile=remote_dist_dir($dist) . "/$dist/$section/source/Sources.gz";              $remotefile=remote_dist_dir($dist) . "/$dist/$section/source/Sources.gz";
294                  $sources_info=md5_source($section,$dist);              $sources_info=md5_source($section,$dist);
295                  if (!file_exists ($dirs[sources_dir] . "/$dist/")) mkdir ( $dirs[sources_dir] . "/$dist",0740);              if (!file_exists ($dirs[sources_dir] . "/$dist/")) mkdir ( $dirs[sources_dir] . "/$dist",0740);
296                                  if (!file_exists ($dirs[sources_dir] . "/$dist/$section")) mkdir ($dirs[sources_dir] . "/$dist/$section",0740);              if (!file_exists ($dirs[sources_dir] . "/$dist/$section")) mkdir ($dirs[sources_dir] . "/$dist/$section",0740);
297                  if (!file_exists($localfile) OR md5_of_file($localfile)!=$sources_info[md5])  {              if (!file_exists($localfile) OR md5_of_file($localfile)!=$sources_info[md5])  {
298                          print "Downloading $remotefile Bytes $sources_info[bytes]=> ";                  print "Downloading $remotefile Bytes $sources_info[bytes]=> ";
299                          if (ftp_get($conn_id,$localfile,$remotefile,FTP_BINARY)) {                  if (ftp_get($conn_id,$localfile,$remotefile,FTP_BINARY)) {
300                          print "Done\n";                      print "Done\n";
301                          if(md5_of_file($localfile)==$sources_info[md5]) print "MD5 => OK\n";                      if(md5_of_file($localfile)==$sources_info[md5]) print "MD5 => OK\n";
302                          }                      else print "MD5 => no match\n";
303                          else {                  }
304                                  unlink("$localfile");                  else {
305                                  print "Fail\n";                      unlink($localfile);
306                                  die ("Error in source file");                      print "Fail\n";
307                          }                      die_status ("Error in source file");
308                  }                  }
309                }
310          }          }
311                  ftp_quit($conn_id);          ftp_quit($conn_id);
312          }      }
313  }  }
314  function md5_source($section,$dist) {  function md5_source($section,$dist) {
315  global $mirrors;      global $mirrors;
316  $relase=file_get_contents ("ftp://$mirrors[$dist]" . remote_dist_dir($dist) . "/$dist/Release") or die("Failed to check MD5 of sources");      $relase=file_get_contents ("ftp://$mirrors[$dist]" . remote_dist_dir($dist) . "/$dist/Release") or die_status("Failed to check MD5 of sources");
317  preg_match ("/\s([^\s]+)\s+(\d+)\s$section\/source\/Sources\.gz/i", $relase,$matches);      preg_match ("/\s([^\s]+)\s+(\d+)\s$section\/source\/Sources\.gz/i", $relase,$matches);
318  return array("md5"=>$matches[1],"bytes"=>$matches[2]);      return array("md5"=>$matches[1],"bytes"=>$matches[2]);
319   # 183dea9ca3c2e35ffd6cf5eee9501c21          2941637 main/binary-i386/Packages.gz      # 183dea9ca3c2e35ffd6cf5eee9501c21          2941637 main/binary-i386/Packages.gz
320  }  }
321    
322  function vers_conv($debvers) {  function vers_conv($debvers) {
323  preg_match("/(.+)(ds-|-)/",$debvers,$matches);      // Strip off the epoch
324  return $matches[1];      $pos = strpos($debvers, ':');
325        if ( $pos !== FALSE) {
326            $debvers = substr($debvers, $pos+1);
327        }
328    
329        // strip off the Debian revision (look from the back of the string)
330        $pos = strrpos($debvers, '-');
331        if ( $pos !== FALSE) {
332            $debvers = substr($debvers, 0, $pos);
333        }
334    
335        // strip off repacking indicators
336        $debvers = preg_replace("/[-.+~]?(ds|dfsg|debian)(.*)/i", "", $debvers);
337        return $debvers;
338  }  }
339  function check_db() {  function check_db() {
340  global $dirs,$dbconn;      global $dirs,$dbconn;
341  $db = @pg_connect($dbconn) or die(pg_last_notice());      $db = pg_pconnect($dbconn) or die_status(1/*pg_last_notice($db)*/);
342     if (!pg_table_exists($db,"pkgs")){      if (!pg_table_exists($db, "pkgs")) {
343     pg_exec($db, "CREATE TABLE pkgs (id serial PRIMARY KEY,name text,version text,maint text, uploaders text, dir text,watch text,md5_diff text,md5_atsource text,bytes numeric,dist text,section text,up_version text,up_changes text, up_url text,watch_warn text,updated bool, keep_changes bool, wwiz text,wwiz_version text, wwiz_type text)") or die('Error creating table pkgs\n');          pg_exec($db, "CREATE TABLE pkgs (id serial PRIMARY KEY,name text,version text, dversionmangled text,maint text, uploaders text, dir text,watch text,md5_diff text,md5_atsource text,bytes numeric,dist text,section text,up_version text,up_changes text, up_url text,watch_warn text,updated bool, keep_changes bool, wwiz text,wwiz_version text, wwiz_type text, lastcheck timestamp, homepage text, vcs_browser text, lastupvsdebsync timestamp)") or die_status('Error creating table pkgs\n');
344     pg_exec($db, "CREATE UNIQUE INDEX idxname on pkgs (name,dist)") or die('Errore creating index - ' . pg_last_error($db));          pg_exec($db, "CREATE UNIQUE INDEX idxname on pkgs (name,dist)") or die_status('Errore creating index - ' . pg_last_error($db));
345     $sw=true;          pg_exec($db, "CREATE INDEX checkx on pkgs (lastcheck)") or die_status('Errore creating index - ' . pg_last_error($db));
346    }          $sw=true;
347   if (!pg_table_exists($db,"binpkgs")){      }
348     pg_exec($db, "CREATE TABLE binpkgs (id serial PRIMARY KEY, name text,bin_name text,dist text, pop_inst numeric,pop_vote numeric, up_error numeric,avg_error_date numeric)") or die('Error creating table binpkgs\n');      if (!pg_table_exists($db, "binpkgs")) {
349     pg_exec($db, "CREATE UNIQUE INDEX idxbin on binpkgs (name,bin_name,dist)") or die('Errore creating index - ' . pg_last_error($db));          pg_exec($db, "CREATE TABLE binpkgs (id serial PRIMARY KEY, name text,bin_name text,dist text, pop_inst numeric,pop_vote numeric, up_error numeric,avg_error_date numeric)") or die_status('Error creating table binpkgs\n');
350     $sw=true;          pg_exec($db, "CREATE UNIQUE INDEX idxbin on binpkgs (name,bin_name,dist)") or die_status('Errore creating index - ' . pg_last_error($db));
351  }          $sw=true;
352   #if (!pg_table_exists($db,"upstream")){      }
353   #  pg_exec($db, "CREATE TABLE upstream (name text,dist text,up_version text,watch_warn text,updated bool)") or die('Errore nel creare la tabella');      if (!pg_table_exists($db, "events")) {
354   #  pg_exec($db, "CREATE UNIQUE INDEX idxupstrm on upstream (name,dist)") or die('Errore nel creare gli indici ' . pg_last_error($db));          pg_exec($db, "CREATE TABLE events (id serial PRIMARY KEY, name text,type text, dist text, stamp timestamp, content text)") or die_status('Error creating table binpkgs\n');
355   #  $sw=true;          pg_exec($db, "CREATE UNIQUE INDEX idxevents on events (name,dist)") or die_status('Errore creating index - ' . pg_last_error($db));
356  #}          pg_exec($db, "CREATE INDEX pkgxeventtype on events (name,type,dist)") or die_status('Errore creating index - ' . pg_last_error($db));
357  pg_close($db);          $sw=true;
358  if($sw) return $sw;      }
359  else return false;      #if (!pg_table_exists($db,"upstream")){
360  }      #  pg_exec($db, "CREATE TABLE upstream (name text,dist text,up_version text,watch_warn text,updated bool)") or die_status('Errore nel creare la tabella');
361        #  pg_exec($db, "CREATE UNIQUE INDEX idxupstrm on upstream (name,dist)") or die_status('Errore nel creare gli indici ' . pg_last_error($db));
362        #  $sw=true;
363        #}
364        pg_close($db);
365        if($sw) return $sw;
366        else return false;
367    }
368  function pg_table_exists (&$dblink, $table) {  function pg_table_exists (&$dblink, $table) {
369         $sql = "SELECT relname as tbl FROM pg_class WHERE relname = '$table'";      $sql = "SELECT relname as tbl FROM pg_class WHERE relname = '$table'";
370         if ($res = pg_exec ($dblink, $sql)) {      if ($res = pg_exec ($dblink, $sql)) {
371             return pg_numrows($res) > 0;          return pg_numrows($res) > 0;
372         } else {      } else {
373             return false; // or throw exception          return false; // or throw exception
374         }      }
375     }  }
376    
377    
378  function watch_stat() {  function watch_stat() {
379  global $dirs,$dbconn;      global $dirs,$dbconn;
380  $db = pg_pconnect($dbconn) or die(pg_errormessage($db));      $db = pg_pconnect($dbconn) or die_status(pg_last_error($db));
381  $rsql=pg_exec ($db,"Select count(name) as totwatch FROM pkgs WHERE watch!=''");      $rsql=pg_exec ($db,"Select count(name) as totwatch FROM pkgs WHERE watch!=''");
382  $tot_watch=pg_fetch_array($rsql);      $tot_watch=pg_fetch_array($rsql);
383  print "Total packages with Watch =>$tot_watch[0]\n";      print "Total packages with Watch =>$tot_watch[0]\n";
384  $rsql=pg_exec ($db,"Select count(name) as totwatch FROM pkgs WHERE watch IS NULL");      $rsql=pg_exec ($db,"Select count(name) as totwatch FROM pkgs WHERE watch IS NULL");
385  $tot_nowatch=pg_fetch_array($rsql);      $tot_nowatch=pg_fetch_array($rsql);
386  pg_close($db);      pg_close($db);
387  print "Totale packages without Watch => $tot_nowatch[0]\n";      print "Totale packages without Watch => $tot_nowatch[0]\n";
388  print "Watch presence rate => " . number_format ($tot_watch[0]/($tot_nowatch[0]+$tot_watch[0])*100, 2, ",",".")  . "%\n";      print "Watch presence rate => " . number_format ($tot_watch[0]/($tot_nowatch[0]+$tot_watch[0])*100, 2, ",",".")  . "%\n";
389  }  }
390  function remote_dist_dir($dist) {  function remote_dist_dir($dist) {
391  if ($dist=="non-US") return "/debian-non-US/dists/unstable";      #if ($dist=="non-US") return "/debian-non-US/dists/unstable";
392  else return "/debian/dists";      #else
393        return "/debian/dists";
394  }  }
395  function clear_db(&$db) {  function clear_db(&$db) {
396  #global $dbconn;      #global $dbconn;
397  #if (check_db()) die("Il Database era vuoto\n");      #if (check_db()) die_status("Il Database era vuoto\n");
398  #keep_source();      #download_sources();
399  #$db = pg_pconnect($dbconn)  or  die(pg_result_error($db));      #$db = pg_pconnect($dbconn)  or  die_status(pg_result_error($db));
400  $sql1=  "Delete from binpkgs where id NOT IN      $sql1=      "Delete from binpkgs where id NOT IN
401                          (select id FROM binpkgs INNER JOIN bin_atsrc USING (name,bin_name,dist))" ;                          (select id FROM binpkgs INNER JOIN bin_atsrc USING (name,bin_name,dist))" ;
402  $sql2=  "Delete from pkgs where id NOT IN (select id FROM pkgs INNER JOIN pkgs_atsrc USING (name,dist))" ;      $sql2=      "Delete from pkgs where id NOT IN (select id FROM pkgs INNER JOIN pkgs_atsrc USING (name,dist))" ;
403  #$sql3= "Delete from upstream where name NOT IN      $sql3=      "UPDATE pkgs SET wwiz=NULL, wwiz_type=NULL, wwiz_version=NULL WHERE watch IS NOT NULL and watch!=''" ;
404  #                       (select name FROM pkgs_atsrc) AND dist NOT IN (select dist FROM pkgs_atsrc)" ;      $sql4=      "UPDATE pkgs SET up_changes=NULL WHERE updated='1'";
405  if(pg_exec($db,$sql1) AND pg_exec($db,$sql2)) print "Db clear complete\n";      if(pg_exec($db,$sql1) && pg_exec($db,$sql2) && pg_exec($db,$sql3) && pg_exec($db,$sql4)) {
406            print "Db clear complete\n";
407        }
408  }  }
409  function db_popcon($initial='') {  function db_popcon($initial='') {
410  global $dirs,$dbconn;      global $dirs,$dbconn;
411  $db = pg_pconnect($dbconn)  or  die(pg_errormessage($db));      $db = pg_pconnect($dbconn)  or  die_status(pg_last_error($db));
412  $filename=$dirs[popcon_dir] . "/all-popcon-results.txt.gz";      $filename=$dirs[popcon_dir] . "/all-popcon-results.txt.gz";
413  $regexp="/Package:\s($initial\S*)\s+(\d+)\s+(\d+)/iS";      /*                   package name   vote     old    recent  nofiles*/
414                  $zp = gzopen($filename, "r") or die("Could not open $filename");      $regexp="/Package:\s($initial\S*)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/iS";
415                          $tin=time();      $zp = gzopen($filename, "r") or die_status("Could not open $filename");
416                          if(filesize($filename)>20) {      $tin=time();
417                          #pg_exec($db, "BEGIN TRANSACTION") OR DIE("Impossibile iniziare la transazione");      if(filesize($filename)>20) {
418                  while (!gzeof($zp)) {          #pg_exec($db, "BEGIN TRANSACTION") OR die_status("Impossibile iniziare la transazione");
419                                  $line = gzgets ($zp,4096) ;          while (!gzeof($zp)) {
420                                   if (preg_match($regexp,$line,$matches)) {              $line = gzgets ($zp,4096) ;
421                   #print "\rName => $matches[1] Install => $matches[2] Vote => $matches[3]";              if (preg_match($regexp,$line,$matches)) {
422                                   @pg_exec($db, "UPDATE binpkgs SET pop_inst='$matches[2]',pop_vote='$matches[3]' WHERE bin_name='$matches[1]'");                  $pop_inst = $matches[2] + $matches[3] + $matches[4] + $matches[5];
423                                   $processed+=1;                  #print "\rName => $matches[1] Install => $matches[2] Vote => $matches[3]";
424                                   $elapsed_time=time()-$tin;                  @pg_exec($db, "UPDATE binpkgs SET pop_inst='$pop_inst',pop_vote='$matches[2]' WHERE bin_name='$matches[1]'");
425                                   if (($elapsed_time % 20)==0 AND $updtime!=$elapsed_time) {                  /*$processed++;
426                                          $updtime=$elapsed_time;                  $elapsed_time=time()-$tin;
427                                      $wprocessed=$processed-$wprocessed;                  if (($elapsed_time % 20)==0 AND $updtime!=$elapsed_time) {
428                                          $avg_time=$wprocessed/20;                      $updtime=$elapsed_time;
429                                          $wprocessed=$processed;                      $wprocessed=$processed-$wprocessed;
430                                          $countdown=(34701- $processed)/$avg_time;                      $avg_time=$wprocessed/20;
431                                          $last_time=time();                      $wprocessed=$processed;
432                                   }                      $countdown=(34701- $processed)/$avg_time;
433                                   print "\rProcessing Popcon: " .  date("i:s",$countdown=$countdown-(time()-$last_time))   . " =>" . (34701-$processed) . " Media $avg_time";                      $last_time=time();
434                                   $last_time=time();                  }
435                                  }                  print "\rProcessing Popcon: " .  date("i:s",$countdown=$countdown-(time()-$last_time))   . " =>" . (34701-$processed) . " Media $avg_time";
436                                  }                  $last_time=time();*/
437                          #pg_exec($db, "COMMIT") OR DIE("Impossibile commsionare la transazione");              }
438                          }          }
439  @pg_close($db);          #pg_exec($db, "COMMIT") OR die_status("Impossibile commsionare la transazione");
440  }      }
441  function db_popcon_test() {      @pg_close($db);
442  global $dirs,$dbconn;  }
443  $db = pg_pconnect($dbconn)  or  die(pg_errormessage($db));  function dl_popcon() {
444  $filename=$dirs[popcon_dir] . "/all-popcon-results.txt.gz";      global $dirs,$dbconn;
445  $regexp="/Package:\s(\S+)\s+(\d+)\s+(\d+)/";      $localfile=$dirs[popcon_dir] . "/all-popcon-results.txt.gz";
446                  $zp = gzfile($filename, "r") or die("Could not open $filename");      $remotefile="http://popcon.debian.org/all-popcon-results.txt.gz";
447                          if(filesize($filename)>20) {      @file_put_contents($localfile,file_get_contents($remotefile),'w') or die_status("Errore nello scaricare il file popcon\n");
448                  foreach ($zp as $line) {  }
449                                   if (preg_match($regexp,$line,$matches)) {  function is_updated ($up_vers,$debian_vers, $dversionAsIs = false) {
450                  # print "Name => $matches[1] \n Install => $matches[2] \n Vote => $matches[3]\n";      if ($up_vers=='') return 1;
451                                   print "\r" . $i++;  
452                                   #pg_unbuffered_query($db, "UPDATE binpkgs SET pop_inst='$matches[2]',pop_vote='$matches[3]' WHERE bin_name='$matches[1]'");      if ($up_vers === $debian_vers) $dversionAsIs = true;
453                                  }  
454                                  }      passthru("dpkg --compare-versions '".escapeshellarg($up_vers)."' le '" . escapeshellarg($dversionAsIs?$debian_vers:vers_conv($debian_vers)) . "'",$great);
455                          }  
456  @pg_close($db);      return (int)!($great);
457  }  }
458  function file_put_contents($filename, $content, $flags = 0) {  function db_upstream($initial='%', $checkStamps = false, $limit = null) {
459         if (!($file = fopen($filename,$flags))) return false;      global $dirs, $dbconn, $minimumAge;
460             else {  
461          $n = fwrite($file, $content);      check_db();
462          fclose($file);      $db = pg_pconnect($dbconn) ;
463          return true;      events::DB($db);
464             }  
465     }      $postWHERE = '';
466  function keep_popcon() {      if ($checkStamps) {
467  global $dirs,$dbconn;          $postWHERE = "AND (lastcheck<='".sqldate(time() - ($minimumAge*24*3600))."' OR lastcheck IS NULL)";
468  $localfile=$dirs[popcon_dir] . "/all-popcon-results.txt.gz";      }
469  $remotefile="http://popcon.debian.org/all-popcon-results.txt.gz";  
470  @file_put_contents($localfile,file_get_contents($remotefile),'w') or die("Errore nello scaricare il file popcon\n");      $postORDER = '';
471  }      if ($limit !== null && is_int($limit)) {
472  function is_updated ($up_vers,$debian_vers) {          $postORDER = 'LIMIT ' . $limit;
473  if ($up_vers=='') return 0;      }
474  passthru("dpkg --compare-versions '$up_vers' gt '" . vers_conv($debian_vers) . "'",$great);  
475  if ($great==0) return 1;      $rsql=pg_exec($db, "SELECT id, name,dist,up_version,version,watch,lastupvsdebsync FROM pkgs WHERE watch is not null AND name LIKE '$initial' $postWHERE ORDER BY lastcheck DESC $postORDER");
476  else return 0;      $tot=pg_numrows($rsql);
477  }  
478  function db_upstream($initial='%') {      while ($res_array=pg_fetch_array($rsql)) {
479         global $dirs,$dbconn;  
480              check_db();          $uscan_res=uscan_foo($res_array['name'],$res_array['version'],$res_array['watch']);
481         $db = pg_connect($dbconn) ;  
482         $rsql=pg_exec($db, "SELECT pkgs.name,pkgs.dist,up_version,version,watch FROM pkgs WHERE watch is not null AND name LIKE '$initial%' ORDER BY name DESC");          #print $uscan_res[0] . "\n Name => $res_array[name]\n";
483             $tot=pg_numrows($rsql);          $version         = $res_array['version'];
484                  while ($res_array=pg_fetch_array($rsql)) {          $dversionmangled = $res_array['version'];
485                                  $uscan_res=uscan_foo($res_array['name'],$res_array['version'],$res_array['watch']);          $lastupvsdebsync = $res_array['lastupvsdebsync'];
486                                  if($uscan_res[1] !=null) $uscan_res[1]=addslashes($uscan_res[1]);          $mangle = true;
487                                  #print $uscan_res[0] . "\n Name => $res_array[name]\n";  
488                                  $updated=is_updated($uscan_res[0],$res_array[version]);          print "\rVersion=>" . $uscan_res[3];
489                                  if ($uscan_res[0]!=$res_array[up_version]) ($keep_changes=1);          if ($uscan_res[3]!=null) $version=$uscan_res[3];
490                                  else $keep_changes=0;          if ($uscan_res[5]!=null && $uscan_res[5] != vers_conv($res_array['version'])) {
491                                  pg_exec($db, "UPDATE pkgs SET up_version='" . $uscan_res[0] . "',up_url='" . $uscan_res[2] . "', watch_warn='" . $uscan_res[1] . "',keep_changes='$keep_changes', updated='$updated' WHERE name='$res_array[name]' AND dist='$res_array[dist]'") or Die("Error in update pkgs upstream");              $dversionmangled=$uscan_res[5];
492                                  #print "\rUpdated " . $res_array['name'] .  " => $updated Up Version => ". $uscan_res[0];              $mangle = false;
493                                  print "\rUpstream left => " . $tot --;          }
494  #                               }  
495  #                                                       else print "Added ". $res_array['name'] ."\n";          $updated = $uscan_res[4];
496                          #       if ($great==0) {  
497            /*if (!$updated)*/ {
498                          #               print "Package=> " . $res_array['name'] . "\n";              $updated=is_updated($uscan_res[0], $dversionmangled, !$mangle);
499              #           print "Version => " . $res_array['version'] . "\n";          }
500              #           print "Upstream Ver=> " . $uscan_res[0] . "\n";  
501                                          #print "Watch warings=> " . $uscan_res[1]  . "\n";          if (!$updated) {
502                          #       }              events::newVersion($res_array['name'], $res_array['version'], $uscan_res[0], $res_array['dist']);
503                                  }          } else {
504          print "\n";              $lastupvsdebsync = sqldate();
505          pg_close($db);          }
506    
507            if ($lastupvsdebsync === null) {
508                $lastupvsdebsync = sqldate(0);
509            }
510    
511            if ($uscan_res[0]!=$res_array[up_version]) ($keep_changes=1);
512            else $keep_changes=0;
513    
514            $up_url=iconv("ISO-8859-1","UTF-8",$uscan_res[2]);
515            $up_version=iconv("ISO-8859-1","UTF-8",$uscan_res[0]);
516    
517            $watch_warn='';
518            if($uscan_res[1] != null) {
519                $watch_warn=pg_escape_string(iconv("ISO-8859-1","UTF-8", $uscan_res[1]));
520            }
521    
522            pg_exec($db, "UPDATE pkgs SET up_version='" . pg_escape_string($up_version) . "', dversionmangled='".pg_escape_string($dversionmangled)."',up_url='" . pg_escape_string($up_url) . "', watch_warn='" . $watch_warn . "',keep_changes='$keep_changes', wwiz_version=NULL, wwiz_type=NULL, wwiz=NULL, updated='$updated', lastcheck=now(), lastupvsdebsync='$lastupvsdebsync'  WHERE id=$res_array[id]") or die_status("Error in update pkgs upstream");
523            #print "\rUpdated " . $res_array['name'] .  " => $updated Up Version => ". $uscan_res[0];
524            print "\t Upstream left => " . $tot --;
525            #                               }
526            #                                                       else print "Added ". $res_array['name'] ."\n";
527            #       if ($great==0) {
528    
529            #               print "Package=> " . $res_array['name'] . "\n";
530            #               print "Version => " . $res_array['version'] . "\n";
531            #               print "Upstream Ver=> " . $uscan_res[0] . "\n";
532            #print "Watch warings=> " . $uscan_res[1]  . "\n";
533            #       }
534        }
535        print "\n";
536        pg_close($db);
537  }  }
538  function check_url ($url) {  function check_url ($url) {
539     $url_p = parse_url ($url);      $url_p = parse_url ($url);
540     if (isset ($url_p["host"])) $host = $url_p["host"];      if (isset ($url_p["host"])) $host = $url_p["host"];
541     else return false;      else return false;
542     preg_match("/(http|ftp)/",$host,$matches);      preg_match("/(http|ftp)/",$host,$matches);
543     if ($matches[1]=='http') $fp = fsockopen ($host, 80, $errno, $errstr, 10);      if ($matches[1]=='http') $fp = fsockopen ($host, 80, $errno, $errstr, 5);
544     elseif ($matches[1]=='ftp') $fp2 = fsockopen ($host, 21, $errno, $errstr, 10);      elseif ($matches[1]=='ftp') $fp2 = fsockopen ($host, 21, $errno, $errstr, 5);
545     if (!$fp AND !$fp2) return false;      if (!$fp AND !$fp2) return false;
546     else return true;      else return true;
547  }  }
548  function keep_remote ($localfile,$remotefile) {  function keep_remote ($localfile,$remotefile) {
549          #if (!check_url($remotefile)) return false;      if (!check_url($remotefile)) return false;
550          $rfp = @fopen ($remotefile, "rb");      #   $rfp = @fopen ($remotefile, "r");
551          if (!$rfp) {      #   if (!$rfp) {
552                  return false;      #           return false;
553                  }      #           }
554          $lfp = fopen ($localfile, "w");      #     fclose($rfp);
555          ini_set ( "memory_limit", "100M");      #   $lfp = fopen ($localfile, "w");
556          while ($data = fread($rfp, 1024*8)) {      #   stream_set_blocking($rfp,0);
557          $bytes+=1024*8;      #   ini_set ( "memory_limit", "100M");
558          #print "Downlaoding $remotefile: $bytes\r";      #   while (!feof($rfp)) {
559          fwrite($lfp, $data);      #           $data = fread($rfp, 8192);
560          }      #           $bytes+=8192;
561          ini_set ( "memory_limit", "15M");      #           print "Downlaoding $remotefile: $bytes\r";
562          fclose($lfp);      #           fwrite($lfp, $data);
563          fclose($rfp);      #   } while (true);
564        #   ini_set ( "memory_limit", "15M");
565          return true;  
566        if (!copy($remotefile, $localfile)) {
567            echo "Copia di $file non riuscita ...\n";
568            return false;
569        }
570        #   fclose($lfp);
571        #   fclose($rfp);
572    
573        return true;
574  }  }
575  function up_changes($src_name='%') {  function up_changes($src_name='%', $force = false) {
576      global $dbconn;      global $dbconn, $fetchUpstreamChanges;
577          $changes_files=array("CHANGES","NEWS","Changelog","ChangeLog","changelog");  
578      $db = pg_connect($dbconn) ;      if (!$fetchUpstreamChanges && !$force) {
579      $rsql=pg_exec($db, "SELECT id,up_url FROM pkgs WHERE up_url!=''  AND keep_changes='t' AND name LIKE '$src_name%'");          //print "\nSkipping download of upstream sources to grab changelogs\n";
580          $tot=pg_numrows($rsql);          return;
581          while ($res_array=pg_fetch_array($rsql)) {      }
582                      $localfile="/tmp/dehs-" . $res_array[id] . ".tar.gz";  
583                          #print "I'm downloading $res_array[up_url]\r";      $changes_files=array("CHANGES","NEWS","Changelog","ChangeLog","changelog");
584                          $res=keep_remote($localfile,$res_array['up_url']);      $db = pg_pconnect($dbconn) ;
585                          #print "UPDATE pkgs SET up_changes='', keep_changes='f' WHERE id=$res_array[id]";      $rsql=pg_exec($db, "SELECT id,up_url FROM pkgs WHERE up_url!=''  AND keep_changes='t' AND name LIKE '$src_name'");
586                          if (!$res) {      $tot=pg_numrows($rsql);
587                                  pg_exec($db, "UPDATE pkgs SET up_changes='', keep_changes='f' WHERE id=$res_array[id]");      while ($res_array=pg_fetch_array($rsql)) {
588                                  $notdown++;          $localfile=tempnam("/tmp/", "dehs-" . $res_array[id] . ".tar.gz");
589                                  $tot--;          print "I'm downloading $res_array[up_url]\r";
590                                  print "Packages Left: " . $tot-- . " Found: $found Not found: $notfound Not Down: $notdown\r";          $res=keep_remote($localfile,$res_array['up_url']);
591                                  #print "\n Not Downlaoded: $res_array[up_url]\n";          print "UPDATE pkgs SET up_changes='', keep_changes='f' WHERE id=$res_array[id]";
592                                  continue;          if (!$res) {
593                          }              pg_exec($db, "UPDATE pkgs SET up_changes='', keep_changes='f' WHERE id=$res_array[id]");
594                          foreach($changes_files as $filename) {              $notdown++;
595                              $command="tar -zxOf " . $localfile . " *$filename 2>/dev/null";              $tot--;
596                                  exec($command,$up_changes,$cmdres);              print "Packages Left: " . $tot-- . " Found: $found Not found: $notfound Not Down: $notdown\r";
597                                  $up_changes=rtrim(implode("\n",$up_changes));              #print "\n Not Downlaoded: $res_array[up_url]\n";
598                                  if ($cmdres==0 AND strlen($up_changes)>20) {              unlink($localfile);
599                                          $sw=true;              continue;
600                                          $up_changes=addslashes($up_changes);          }
601                                          $conv_res=iconv("ISO-8859-1","UTF-8",$up_changes);          foreach($changes_files as $filename) {
602                                          if ($conv_res) $up_changes=$conv_res;              $command="tar --wildcards -zxOf " . $localfile . " *$filename 2>/dev/null";
603                                          $up_changes_sql="UPDATE pkgs SET up_changes='$up_changes', keep_changes='f' WHERE id=$res_array[id]";              exec($command,$up_changes,$cmdres);
604                                          pg_exec($db,$up_changes_sql);              $up_changes=rtrim(implode("\n",$up_changes));
605                                          #print "\nFounded $filename on $res_array[up_url]\n";              if ($cmdres==0 AND strlen($up_changes)>20) {
606                                          $found++;                  $sw=true;
607                                          break;                  $conv_res=iconv("ISO-8859-1","UTF-8",$up_changes);
608                                  }                  if ($conv_res) $up_changes=$conv_res;
609                          }                  $up_changes=pg_escape_string($up_changes);
610                          if (!$sw) {                  $up_changes_sql="UPDATE pkgs SET up_changes='$up_changes', keep_changes='f' WHERE id=$res_array[id]";
611                          pg_exec($db, "UPDATE pkgs SET up_changes='', keep_changes='f' WHERE id=$res_array[id]");                  pg_exec($db,$up_changes_sql);
612                          #print "\n Not Founded any on $res_array[up_url]\n";                  #print "\nFound $filename on $res_array[up_url]\n";
613                          $notfound++;                  $found++;
614                          }                  break;
615                          unlink($localfile);              }
616                          print "Packages Left: " . $tot-- . " Found: $found Not found: $notfound Not Down: $notdown\r";          }
617                  }          if (!$sw) {
618          pg_close($db);              pg_exec($db, "UPDATE pkgs SET up_changes='', keep_changes='f' WHERE id=$res_array[id]");
619                #print "\n Not Found any on $res_array[up_url]\n";
620                $notfound++;
621            }
622            unlink($localfile);
623            print "Packages Left: " . $tot-- . " Found: $found Not found: $notfound Not Down: $notdown\r";
624        }
625        pg_close($db);
626  }  }
627  function uscan_foo($name,$version,$watch){  function uscan_foo($name,$version,$watch){
628          $fw=fopen("/tmp/$name" . "_watch","w");      global $base, $uscan;
629          fwrite($fw,$watch);  
630          fclose($fw);      $tmpnam=tempnam('/tmp/', $name.'_watch');
631          $uscan_res=shell_exec("perl uscan3.pl $name " . vers_conv($version) . " /tmp/$name" . "_watch 2>/dev/null 2>&1");      $fw=fopen($tmpnam,"w");
632          unlink("/tmp/$name" . "_watch");      fwrite($fw,$watch);
633          preg_match("/\+\+(.+)\+\+\s\+-(.+)-\+/",$uscan_res, $matches);      fclose($fw);
634          if ($matches[1]!='') $upstream=$matches[1];  
635          else $upstream=null;      $uscan_res=shell_exec("$uscan --dehs --package $name --upstream-version " . escapeshellarg(vers_conv($version)) . " --watchfile $tmpnam 2>/dev/null 2>&1");
636          if ($matches[2]!='')    $up_url=$matches[2];  
637          else $up_url=null;      unlink($tmpnam);
638          $uscan_res=preg_replace("/(\+\+.+-\+)/", "", $uscan_res);  
639          if ($uscan_res=='') $uscan_res=null;      $p = xml_parser_create();
640          return array($upstream,$uscan_res,$up_url);      xml_parse_into_struct($p, $uscan_res, $vals, $index);
641  }      xml_parser_free($p);
642    
643        $upstream=$vals[$index['UPSTREAM-VERSION'][0]][value];
644        $up_url=$vals[$index['UPSTREAM-URL'][0]][value];
645        $deb_vers=$vals[$index['DEBIAN-UVERSION'][0]][value];
646        $deb_mangled_vers=$vals[$index['DEBIAN-MANGLED-UVERSION'][0]][value];
647        $warnings=$vals[$index['WARNINGS'][0]][value];
648        $updated=$vals[$index['STATUS'][0]][value];
649    
650        if ($updated=="up to date")  $updated=1;
651        else $updated=0;
652    
653        if ($upstream=='') $upstream=null;
654        if ($up_url=='')  $up_url=null;
655        if ($deb_vers=='') $deb_vers=null;
656        if ($warnings=='') $warnings=null;
657        if ($deb_mangled_vers=='') $deb_mangled_vers=null;
658    
659        return array($upstream, $warnings, $up_url, $deb_vers, $updated, $deb_mangled_vers);
660    }
661  function db_up_error() {  function db_up_error() {
662      global $dbconn;      global $dbconn;
663          check_db();      check_db();
664          $db = pg_connect($dbconn) ;      $db = pg_pconnect($dbconn) ;
665          $bugurl="http://bugs.debian.org/cgi-bin/pkgreport.cgi?which=tag&data=upstream&archive=no&show_list_header=no&pend-exc=done";      $bugurl="http://bugs.debian.org/cgi-bin/pkgreport.cgi?which=tag&data=upstream&archive=no&show_list_header=no&pend-exc=done";
666          print "Downloading bugs list =>";      print "\nDownloading bugs list =>";
667          $ferrors=file_get_contents($bugurl) or die ("I can't keep error page from bugs.debian.org");      $ferrors=file_get_contents($bugurl) or die_status ("I can't keep error page from bugs.debian.org");
668          print "Done\n";      print "Done\n";
669          $ferrors=explode("<li>",$ferrors);      $ferrors=explode("<li>",$ferrors);
670          $totline=count($ferrors);      $totline=count($ferrors);
671          #$errors[]=array();      #$errors[]=array();
672          #array_pop($errors);      #array_pop($errors);
673          print "Total line => $totline\n";      print "Total lines => $totline\n";
674          foreach ($ferrors as $line) {      foreach ($ferrors as $line) {
675             # print $line . "\n";          # print $line . "\n";
676                  $founded=preg_match("/Package:.+<strong>([^<]+)<\/strong><\/a>.+<strong>((\d+)\syear\sand\s|)(\d+)\sdays/sS",$line,$matches);          if (preg_match("/Package:.+?>([^<]+)<\/a>.+<strong>((\d+)\syear\sand\s|)(\d+)\sdays/sS",$line,$matches)) {
677                  if ($founded) {              $errors[$matches[1]][err_tot]++;
678                          $errors[$matches[1]][err_tot]++;              $errors[$matches[1]][days]+=$matches[3]*365+$matches[4];
679                          $errors[$matches[1]][days]+=$matches[3]*365+$matches[4];              #if ($matches[3]) print "Anni $matches[3]";
680                          #if ($matches[3]) print "Anni $matches[3]";              #print "Giorni $matches[4]\n";
681                          #print "Giorni $matches[4]\n";          }
682                  }          print "\rLines left => " . $totline--;
683                  print "\rLine left => " . $totline--;      }
684          }      #arsort($errors);
685          #arsort($errors);      $total=count($errors);
686          $total=count($errors);      foreach ($errors as $pkg_name=>$value) {
687          foreach ($errors as $error=>$value) {          pg_exec($db, "UPDATE binpkgs SET up_error=" . $value[err_tot] . ",avg_error_date=" . $value[days]/$value[err_tot] . " WHERE bin_name='$pkg_name'") or die_status("Error in upstream bug query");
688                  pg_exec($db, "UPDATE binpkgs SET up_error=" . $value[err_tot] . ",avg_error_date=" . $value[days]/$value[err_tot] . " WHERE bin_name='$error'") or Die("Error in upstream error query");          #print "Pkg: $error Errori: $value[err_tot] Average Days: " . $value[days]/$value[err_tot]  . "\n";
689                  #print "Pkg: $error Errori: $value[err_tot] Average Days: " . $value[days]/$value[err_tot]  . "\n";          print "\rBugs left => " . $total--;
690                  print "\rErrors left => " . $total--;      }
691          }      pg_close($db);
         pg_close($db);  
 }  
 function watch_fill($src_name='%'){  
         global $dirs,$dbconn;  
         $db = pg_pconnect($dbconn) or DIE ("Db error");  
         $checked=$errors=$notfound=$verok=$notmatch=0;  
         $sqlstring="SELECT mpop_inst,name,pkgs.version,pkgs.section,pkgs.dist,tot_up_error, tot_avg_error_date, wwiz_version FROM pkgs INNER JOIN  
         (Select max(pop_inst) as mpop_inst,SUM(up_error) as tot_up_error,AVG(avg_error_date) as tot_avg_error_date,name,dist FROM binpkgs GROUP BY name,dist HAVING max(pop_inst) IS NOT NULL) as binpkgs using(name,dist)  
         WHERE (watch IS NULL OR watch='') AND name LIKE '$src_name%' ORDER BY mpop_inst DESC";  
         $rsql=pg_exec($db, $sqlstring);  
         while ($res_array=pg_fetch_array($rsql)) {  
                 if (substr($res_array["name"],0,3)=='lib') $initial=substr($res_array["name"],0,4);  
                 else $initial=substr($res_array["name"],0,1);  
                 $url="http://packages.debian.org/changelogs/pool/" .  
                 $res_array["section"] . "/" .  
                 $initial . "/" .  
                 $res_array["name"] ."/" .  
                 $res_array["name"] . "_" .  
                 $res_array["version"] . "/copyright";  
                 $contents=@file_get_contents($url) or '';  
                 $space='';  
                 if (!$contents) {  
                         $notfound++;  
                         pg_exec($db, "UPDATE pkgs SET wwiz_type='no_cright' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'");  
                 }  
                 else {  
                         $res=preg_match ('/(sf\.net|sourceforge\.net)/',$contents,$matches);  
                         if ($res) {  
                                 preg_match("/([^\d]+)/",$res_array[name],$dmatches);  
                                 $depured_debian_name=$dmatches[1];  
                                 $matches[1]="ftp://ftp2.sf.net/pub/sourceforge/" . substr($depured_debian_name,0,1) . "/" . substr($depured_debian_name,0,2) . "/" . $depured_debian_name . "/";  
                         }  
                         else {  
                                 unset($matches);  
                                 $res=preg_match ('/((ftp:\/\/|http:\/\/)\S+(html|htm|\/(?(?=.+(tar|tgz))|[^>\s\)\a]+)))/',$contents,$matches);  
                                 $final=substr($matches[1],-1);  
                                 if ($final=='.') {  
                                         $matches[1]=substr($matches[1],0,-1);  
                                         $final=substr($matches[1],-1);  
                                 }  
                                 if ($matches[2]=="http://") $space=' ' . "(?:.*/)?";  
                         elseif ($final!='/') $matches[1].='/';  
                                 $matches[1]=preg_replace("/((http|ftp):\/\/.+):(.*)/",'$1' . '$3', $matches[1],1);  
                                 }  
                         if (!$res) {  
                                 $notmatch++;  
                                 $contents=iconv("ISO-8859-1","UTF-8",$contents);  
                                 pg_exec($db, "UPDATE pkgs SET wwiz='" . addslashes($contents) . "', wwiz_type='notmatch', up_url='', up_changes='',keep_changes='f' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'");  
                         }  
                         else {  
                                 if (!preg_match("/$res_array[name]/",$matches[1])) {  
                                 preg_match("/([^\d]+)/",$res_array[name],$dmatches);  
                                 $depured_debian_name=$dmatches[1];  
                                 }  
                                 else $depured_debian_name=$res_array[name];  
                                 $watch="version=2\n" .  
                                 $matches[1]  . $space . $depured_debian_name . "-([\d+\.]+|\d+)(\.tar|\.tgz)(\.gz|\.bz2|) debian uupdate";  
                                 $uscan_res=uscan_foo($res_array['name'] ,$res_array['version'],$watch);  
                         if ($uscan_res[1] AND !$uscan_res[0]) {  
                                 pg_exec($db, "UPDATE pkgs SET wwiz='" . addslashes($uscan_res[1]) . "', wwiz_type='error', up_url='', up_changes='', keep_changes='f' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'");  
                                 $errors++;  
                         }  
                         else {  
                                 $verok++;  
                                 if ($uscan_res[0]!=$res_array[wwiz_version]) ($keep_changes=1);  
                                 else $keep_changes=0;  
                                 pg_exec($db, "UPDATE pkgs SET wwiz='" . addslashes($watch) ."', wwiz_type='watch', wwiz_version='" . $uscan_res[0] . "', up_url='" . $uscan_res[2] . "', keep_changes='$keep_changes' where name='" . $res_array[name] . "' AND dist='" . $res_array[dist] ."'");  
                         }  
                 }  
                 }  
                 print "Package checked: " . $checked++ .  
                       " OK: $verok Error: $errors Not Matched: $notmatch\r";  
         }  
         print "\nCopyright files not founded: $notfound\n";  
         print "Automatic genereted watch file failure: $errors\n";  
         pg_close($db);  
692  }  }
693  function update_all() {  function watch_wizard($src_name='%', $checkStamps = false, $limit = null){
694  global $email;      global $dirs,$dbconn,$sfIgnore,$watchFileExts,$watchFileDversionMangle,$watchFormat,$minimumAge;
695  $intime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);      $db = pg_pconnect($dbconn) or die_status ("Db error");
696  db_add();      $checked=$errors=$notfound=$verok=$notmatch=0;
697  keep_popcon();  
698  db_popcon();      // prepare some stuff that needs to be done once per run
699  keep_diff();      $opts       = array();
700  db_up_error();      $exts       = '';
701  db_upstream();      $dvmanglers = '';
702  watch_fill();  
703  up_changes();      foreach ($watchFileExts as $ext) {
704  $endtime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);          $exts .= $ext . '|';
705  $messaggio="Dehs executed successful\nBegin: $intime\nEnd: $endtime";      }
706  mail($email, "DEHS report", $messaggio,      unset($ext);
707       "From: dehs-noreply@{$_SERVER['SERVER_NAME']}\r\n" .  
708       "Reply-To: dehs-noreply@{$_SERVER['SERVER_NAME']}\r\n" .      if ($exts != '') {
709       "X-Mailer: PHP/" . phpversion());          $opts[] = 'uversionmangle=s/\.(' . substr($exts, 0, -1) . ')$//i';
710            $exts = '(' . $exts . ')';
711        }
712    
713        foreach ($watchFileDversionMangle as $dvmangler) {
714            $dvmanglers .= $dvmangler . '|';
715        }
716        unset($dvmangler);
717    
718        if ($dvmanglers != '') {
719            $opts[] = 'dversionmangle=s/[-.+~]?(' . substr($dvmanglers, 0, -1) . ')(.*)$//i';
720        }
721    
722        $opts[] = 'pasv';
723    
724        if (is_array($opts)) {
725            $_opts = '';
726            foreach ($opts as $opt) {
727                $_opts .= $opt . ',';
728            }
729            $opts = '';
730            if ($_opts != ',') {
731                $opts = 'opts=' . substr($_opts, 0, -1) . ' \\';
732            }
733        }
734    
735        $postWHERE = '';
736        if ($checkStamps) {
737            $postWHERE = "AND (lastcheck<='".sqldate(time() - ($minimumAge*24*3600))."' OR lastcheck IS NULL)";
738        }
739    
740        $postORDER = '';
741        if ($limit !== null && is_int($limit)) {
742            $postORDER = 'LIMIT ' . $limit;
743        }
744    
745        $sqlstring="SELECT mpop_inst,name,pkgs.version,pkgs.dversionmangled,pkgs.section,pkgs.dist,tot_up_error, tot_avg_error_date, wwiz_version FROM pkgs INNER JOIN
746            (Select max(pop_inst) as mpop_inst,SUM(up_error) as tot_up_error,AVG(avg_error_date) as tot_avg_error_date,name,dist FROM binpkgs GROUP BY name,dist HAVING max(pop_inst) IS NOT NULL) as binpkgs using(name,dist)
747            WHERE (watch IS NULL OR watch='') AND name LIKE '$src_name' $postWHERE ORDER BY lastcheck DESC $postORDER";
748        $rsql=pg_exec($db, $sqlstring);
749    
750        while ($res_array=pg_fetch_array($rsql)) {
751    
752            if (substr($res_array["name"],0,3)=='lib')
753                $initial=substr($res_array["name"],0,4);
754            else
755                $initial=substr($res_array["name"],0,1);
756    
757            $url="http://packages.debian.org/changelogs/pool/" .
758            $res_array["section"] . "/" .
759            $initial . "/" .
760            $res_array["name"] ."/" .
761            $res_array["name"] . "_" .
762            $res_array["version"] . "/copyright";
763            $contents=@file_get_contents($url) or '';
764            $generatedWatches = array();
765    
766            if (!$contents) {
767                $url="http://packages.debian.org/changelogs/pool/" .
768                $res_array["section"] . "/" .
769                $initial . "/" .
770                $res_array["name"] ."/" .
771                $res_array["name"] . "_" .
772                $res_array["version"] . "/" . $res_array["name"] . ".copyright";
773                $contents=@file_get_contents($url) or '';
774            }
775    
776            if (!$contents) {
777                $url="http://packages.debian.org/changelogs/pool/" .
778                $res_array["section"] . "/" .
779                $initial . "/" .
780                $res_array["name"] ."/" .
781                "current/copyright";
782                $contents=@file_get_contents($url) or '';
783            }
784    
785            if ($contents) {
786    
787                if (preg_match ('/(sf\.net|sourceforge\.net)/i', $contents, $matches) ||
788                    (preg_match ('/([\w\-]+)\.(sf\.net|sourceforge\.net)/i', $contents, $matches)
789                     && !in_array($matches[1], $sfIgnore))) {
790                    preg_match("/([^\d]+)/",$res_array['name'],$dmatches);
791                    $depured_debian_name=$dmatches[1];
792                    /*$matches[1]="ftp://download.sf.net/pub/sourceforge/" . substr($depured_debian_name,0,1) . "/" . substr($depured_debian_name,0,2) . "/" . $depured_debian_name . "/";*/
793                    $generatedWatches[]=array('url'=>"http://sf.net/" . $depured_debian_name . "/");
794                    $generatedWatches[]=array('url'=>"http://sf.net/" . $depured_debian_name . "/",'name'=>$depured_debian_name);
795    
796                }
797    
798                if (preg_match ('#((ftp://|http://|https://)\S+(html|htm|/(?(?=.+(tar|tgz|zip|gz|bz2))|[^>\s\)\a]+)))#i',$contents,$matches)) {
799                    $final=substr($matches[1],-1);
800                    if ($final=='.') {
801                        $matches[1]=substr($matches[1],0,-1);
802                        $final=substr($matches[1],-1);
803                    }
804                    if ($matches[2]=="http://" || $matches[2]=="https://") {
805                        $space=' ' . "(?:.*/)?";
806                    } elseif ($final!='/') {
807                        $matches[1].='/';
808                    }
809                    $generatedWatches[]=array('url' => preg_replace("#((http|https|ftp)://.+):(.*)#i",'$1' . '$3', $matches[1],1), 'space' => $space);
810                }
811    
812                if (preg_match ('#((ftp|http)\.\S+(html|htm|/(?(?=.+(tar|tgz|zip|gz|bz2))|[^>\s\)\a]+)))#i',$contents,$matches)) {
813                    $final=substr($matches[1],-1);
814                    if ($final=='.') {
815                        $matches[1]=substr($matches[1],0,-1);
816                        $final=substr($matches[1],-1);
817                    }
818                    $space = '';
819                    if ($matches[2]=="http") {
820                        $space=' ' . "(?:.*/)?";
821                    } elseif ($final!='/') {
822                        $matches[1].='/';
823                    }
824                    $generatedWatches[]=array('url'=>preg_replace('#(http|ftp)\.(.+)#i','$1://$0', $matches[1],1), 'space'=>$space);
825                }
826    
827                if (empty($generatedWatches)) {
828                    $notmatch++;
829                    $contents=iconv("ISO-8859-1","UTF-8",$contents);
830                    pg_exec($db, "UPDATE pkgs SET wwiz_version=NULL, wwiz='" . pg_escape_string($contents) . "', wwiz_type='notmatch', up_url='', up_changes='',keep_changes='f' where name='" . pg_escape_string($res_array[name]) . "' AND dist='" . $res_array[dist] ."'");
831    
832                } else {
833    
834                    $defs = array('name' => $res_array['name'], 'url' => '', 'space' => '');
835    
836                    foreach ($generatedWatches as $tWatch) {
837    
838                        if (!is_array($tWatch)) {
839                            $tWatch = array($tWatch);
840                        }
841    
842                        $tWatch = array_merge($defs, $tWatch);
843    
844                        $watch = sprintf($watchFormat, $tWatch['url'], $tWatch['space'], $tWatch['name'], $exts, $opts);
845    
846                        $uscan_res=uscan_foo($res_array['name'] ,$res_array['version'],$watch);
847    
848                        if ($uscan_res[0] && levenshtein($uscan_res[0], $res_array['version']) < strlen($res_array['version'])) {
849                            break;
850                        }
851                    }
852    
853                    if ($uscan_res[1] && !$uscan_res[0]) {
854                        pg_exec($db, "UPDATE pkgs SET wwiz_version=NULL, wwiz='" . pg_escape_string($uscan_res[1]) . "', wwiz_type='error', up_url='', up_changes='', keep_changes='f' where name='" . pg_escape_string($res_array[name]) . "' AND dist='" . $res_array[dist] ."'");
855                        $errors++;
856                    } else {
857                        $verok++;
858                        if ($uscan_res[0] != $res_array['wwiz_version']) {
859                            $keep_changes = 1;
860                        } else {
861                            $keep_changes = 0;
862                        }
863    
864                        $dversionmangled = $res_array[version];
865                        if ($uscan_res[5]!=null && $uscan_res[5] != vers_conv($res_array['version'])) $dversionmangled=$uscan_res[5];
866    
867                        $updated = $uscan_res[4];
868                        /*if ($updated)*/ {
869                            $updated = is_updated($uscan_res[0], $dversionmangled, $dversionmangled != $uscan_res[5]);
870                        }
871    
872                        /*if (!$updated) {
873                            events::newVersion($res_array['name'], $res_array['version'], $uscan_res[0], $res_array['dist']);
874                        }*/
875    
876                        pg_exec($db, "UPDATE pkgs SET wwiz='" . pg_escape_string($watch) ."', wwiz_type='watch', wwiz_version='" . pg_escape_string($uscan_res[0]) . "',dversionmangled='".pg_escape_string($dversionmangled)."', up_url='" . pg_escape_string($uscan_res[2]) . "', keep_changes='$keep_changes', updated='$updated', lastcheck=now() where name='" . pg_escape_string($res_array[name]) . "' AND dist='" . $res_array[dist] ."'");
877                    }
878                }
879            } else {
880                $notfound++;
881                pg_exec($db, "UPDATE pkgs SET wwiz_version=NULL, wwiz_type='no_cright' where name='" . pg_escape_string($res_array['name']) . "' AND dist='" . $res_array['dist'] ."'");
882            }
883            print "Package checked: " . $checked++ .
884            " OK: $verok Error: $errors Not Matched: $notmatch\r";
885        }
886        print "\nCopyright files not found: $notfound\n";
887        print "Automatic generated watch file failures: $errors\n";
888        pg_close($db);
889    }
890    function dbqa_conn ($dbname,$mode) {
891        $id = dba_open($dbname, $mode, "db4");
892        if (!$id) {
893            die_status("dba_open failed\n");
894        }
895        return $id;
896    }
897    function dehsqa_db () {
898        global $dirs,$dbconn;
899        $dir1='/org/alioth.debian.org/chroot/home/groups/dehs/htdocs/';
900        $dir2="/org/qa.debian.org/data/dehs/";
901        $dists=array("unstable","experimental");
902        foreach ($dists as $dist) {
903            $id=dbqa_conn($dir1 . "dehs_qa_" . $dist . ".db",'n');
904            $db = pg_pconnect($dbconn) or die_status ("Db error");
905            $sql="SELECT name, id, up_version, wwiz_version, watch, wwiz_type, watch_warn, version, dversionmangled, updated FROM pkgs WHERE dist='$dist'";
906            $rsql=pg_exec($db, $sql);
907            while($res_array=pg_fetch_array($rsql)) {
908    
909                if (!$res_array[watch]) { if ($res_array['wwiz_type']==null) $version="N/A"; else $version='-';}
910                elseif (!$res_array[up_version]) $version="Error";
911                else $version=$res_array[up_version];
912    
913                if ($res_array['wwiz_type']=="watch") $wwiz=$res_array['wwiz_version'];
914                else $wwiz=$res_array[wwiz_type];
915    
916                $updated = $res_array['updated']? 'yes' : 'no';
917    
918                $xml="<data><id>$res_array[id]</id>" .
919                "<up_version>$version</up_version>" .
920                "<dversion>$res_array[version]</dversion>" .
921                "<dversionmangled>$res_array[dversionmangled]</dversionmangled>" .
922                "<updated>$updated</updated>" .
923                "<wwiz>$wwiz</wwiz>" .
924                "</data>";
925                $value=addslashes($xml);
926                dba_replace($res_array[name],$xml,$id);
927            }
928            dba_optimize($id);
929            dba_close($id);
930            pg_close($db);
931        }
932  }  }
933  function md5_of_file($inFile) {  function md5_of_file($inFile) {
934     if (file_exists($inFile)) {      if (file_exists($inFile)) {
935         $fd = fopen($inFile, 'r');          return md5_file($inFile);
936         $fileContents = fread($fd, filesize($inFile));      } else {
937         fclose ($fd);          return false;
938         return md5($fileContents);      }
    } else {  
        return false;  
    }  
939  }  }
940  ?>  function update_all() {
941        global $email, $timeStamp_basedCheck;
942        $intime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);
943        echo __FUNCTION__ . ' started the ' . $intime . "\n";
944        db_add();
945        dl_popcon();
946        db_popcon();
947        dl_diffs();
948        db_up_error();
949        db_upstream('%', in_array('all', $timeStamp_basedCheck));
950        watch_wizard('%', in_array('all', $timeStamp_basedCheck));
951        up_changes();
952        dehsqa_db();
953        $endtime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);
954        $messaggio="Dehs executed successful\nBegin: $intime\nEnd: $endtime";
955        mail($email, "DEHS report", $messaggio,
956        "From: dehs-noreply@{$_SERVER['SERVER_NAME']}\r\n" .
957        "Reply-To: dehs-noreply@{$_SERVER['SERVER_NAME']}\r\n" .
958        "X-Mailer: PHP/" . phpversion());
959    }
960    function update_new() {
961        global $email_all;
962        $intime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);
963        echo __FUNCTION__ . ' started the ' . $intime . "\n";
964        db_add();
965        dl_popcon();
966        db_popcon();
967    
968        global $dbconn;
969        check_db();
970        $db = pg_pconnect($dbconn);
971        $sql="SELECT name FROM pkgs WHERE (md5_diff!=md5_atsource or md5_diff is null) or lastcheck is null";
972        $rsql=pg_exec($db, $sql);
973        $res_array=pg_fetch_all($rsql);
974        pg_close($db);
975    
976        dl_diffs();
977        db_up_error();
978        if ($res_array !== false) {
979            foreach ($res_array as $entry) {
980                db_upstream($entry['name']);
981                up_changes($entry['name']);
982                watch_wizard($entry['name']);
983            }
984            dehsqa_db();
985        }
986        $endtime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);
987        $messaggio="Dehs executed successful\nBegin: $intime\nPackages processed: ".((!is_array($res_array))?'none':count($res_array))."\nEnd: $endtime";
988        mail($email_all, "DEHS update_new report", $messaggio,
989        "From: dehs-noreply@{$_SERVER['SERVER_NAME']}\r\n" .
990        "Reply-To: dehs-noreply@{$_SERVER['SERVER_NAME']}\r\n" .
991        "X-Mailer: PHP/" . phpversion());
992    }
993    function update_bogus() {
994        global $email_all,$dbconn;
995        $intime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);
996        echo __FUNCTION__ . ' started the ' . $intime . "\n";
997        check_db();
998        $db = pg_pconnect($dbconn);
999        $sql="SELECT name FROM pkgs WHERE up_version='' AND watch!=''";
1000        $rsql=pg_exec($db, $sql);
1001        $res_array=pg_fetch_all($rsql);
1002        $orig_count = ((!is_array($res_array))?0:count($res_array));
1003        if ($res_array !== false) {
1004            foreach ($res_array as $entry) {
1005                db_upstream($entry['name']);
1006                up_changes($entry['name']);
1007            }
1008            dehsqa_db();
1009        }
1010        $sql="SELECT COUNT(name) FROM pkgs WHERE up_version='' AND watch!=''";
1011        $rsql=pg_exec($db, $sql);
1012        $res_array=pg_fetch_array($rsql);
1013        $new_count = (int)$res_array[0];
1014        pg_close($db);
1015        $endtime=gmdate('D, d M Y H:i:s \C\E\S\T',time()+3600*2);
1016        $messaggio="Dehs executed successful\nBegin: $intime\n".
1017        "Packages processed: $orig_count\n" .
1018        "New bogus watches count: $new_count\nEnd: $endtime";
1019        mail($email_all, "DEHS update_bogus report", $messaggio,
1020        "From: dehs-noreply@{$_SERVER['SERVER_NAME']}\r\n" .
1021        "Reply-To: dehs-noreply@{$_SERVER['SERVER_NAME']}\r\n" .
1022        "X-Mailer: PHP/" . phpversion());
1023    }
1024    function die_status($msg = '', $status = 1) {
1025        if (empty($msg)) {
1026            die($status);
1027        } else if (is_int($msg) && $status == 1 /* default */) {
1028            die($msg);
1029        } else {
1030            echo $msg;
1031            die($status);
1032        }
1033    }
1034    
1035    class events
1036    {
1037    
1038        function DB($db = null)
1039        {
1040            static $_db;
1041    
1042            if ($db === null) {
1043                return $_db;
1044            }
1045            $_db = $db;
1046        }
1047    
1048        function newVersion($sourcePackage, $currentVersion, $newVersion, $distribution, $downloadLink = '')
1049        {
1050            global $base, $sendNotifications;
1051    
1052            if (!$sendNotifications) {
1053                return null;
1054            }
1055    
1056            $eventsId = events::_searchEvent(__FUNCTION__, $sourcePackage, $newVersion);
1057    
1058            if (!empty($eventsId)) {
1059                echo "\nSkipping ".__FUNCTION__." event for v$newVersion of $sourcePackage; already sent";
1060                return null;
1061            }
1062    
1063            $tpl = file_get_contents($base . '/tpls/new_version.mail.tpl');
1064    
1065            $search  = array();
1066            $replace = array();
1067    
1068            $search [] = '#PACKAGE#';
1069            $replace[] = $sourcePackage;
1070            $search [] = '#DVERSION#';
1071            $replace[] = $currentVersion;
1072            $search [] = '#UVERSION#';
1073            $replace[] = $newVersion;
1074            $search [] = '#DISTRO#';
1075            $replace[] = $distribution;
1076            $search [] = '#LINK#';
1077            $replace[] = $downloadLink;
1078    
1079            $message = str_replace($search, $replace, $tpl);
1080            $subject = events::_extractHeader('subject', $message);
1081            $to      = events::_extractHeader('to', $message);
1082            $bcc     = events::_extractHeader('bcc', $message);
1083    
1084            $extraHeaders = events::_getDefaultExtraHeaders();
1085            $extraHeaders[] = 'X-DEHS-UVERSION: ' . $newVersion;
1086            $extraHeaders[] = 'BCC: ' . $bcc;
1087    
1088            if (!mail($to, $subject, $message, implode("\r\n", $extraHeaders), escapeshellarg($to)))
1089            return false;
1090    
1091            events::_recordEvent(__FUNCTION__, $sourcePackage, $distribution, $newVersion);
1092        }
1093    
1094        function _extractHeader($header, &$message)
1095        {
1096            $regex  = '/' . preg_quote($header, '/') . ':([ \t]*)(.*)/i';
1097    
1098            $matches = array();
1099    
1100            if (preg_match($regex, $message, $matches)) {
1101    
1102                $spacer = $matches[1];
1103                $value  = $matches[2];
1104    
1105                $message = trim(preg_replace($regex, '', $message));
1106    
1107                return $value;
1108    
1109            }
1110            return false;
1111        }
1112    
1113        function _getDefaultExtraHeaders()
1114        {
1115            global $notificationsMailFrom;
1116            $headers = array();
1117    
1118            $headers[] = 'From: ' . $notificationsMailFrom;
1119    //        $headers[] = 'X-PTS-Approved: sure';
1120            $headers[] = 'X-BY-DEHS: sure';
1121    
1122            return $headers;
1123        }
1124    
1125        function _recordEvent($eventType, $packageName, $distribution, $content)
1126        {
1127            $db = events::DB();
1128    
1129            $eventType    = pg_escape_string($eventType);
1130            $packageName  = pg_escape_string($packageName);
1131            $distribution = pg_escape_string($distribution);
1132            $content      = pg_escape_string($content);
1133    
1134            pg_exec($db, "INSERT INTO events (name,type,dist,content,stamp) VALUES ('$packageName','$eventType','$distribution','$content',now())") OR die_status("Failed to record event: $packageName|$eventType|$distribution|$content");
1135        }
1136    
1137        function _searchEvent($eventType, $packageName, $contentSearch, $distribution = '')
1138        {
1139            $db = events::DB();
1140    
1141            $eventType    = pg_escape_string($eventType);
1142            $packageName  = pg_escape_string($packageName);
1143    
1144            $result = pg_exec($db, "SELECT id FROM events WHERE name='$packageName' and type='$eventType' ".($distribution? "and dist='$distribution'":'')." and content LIKE '$contentSearch'");
1145            return pg_fetch_all($result);
1146        }
1147    
1148    }
1149    
1150    function sqldate($timestamp=null) {
1151            if ($timestamp===null) $timestamp=time();
1152            return gmdate("Y-m-d H:i:s", $timestamp);
1153    }
1154    
1155    ?>

Legend:
Removed from v.23  
changed lines
  Added in v.112

  ViewVC Help
Powered by ViewVC 1.1.5