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

Contents of /trunk/dehs_pg.php

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.5