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

Legend:
Removed from v.13  
changed lines
  Added in v.150

  ViewVC Help
Powered by ViewVC 1.1.5