<?php /*

 Originally written by Stefano Fabri <bluefuture@nospam@email.it>
 Copyright 2004, Stefano Fabri

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
ob_start('ob_gzhandler');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=UTF-8"
 http-equiv="content-type">
<?php

if (isset($_GET['maint']) && !isset($_GET['login'])) $_GET['login'] = $_GET['maint'];
if (isset($_GET['name']) && !isset($_GET['package'])) $_GET['package'] = $_GET['name'];

if (isset($_GET['login']) && !empty($_GET['login'])) {
    $title = 'UEHS report for ' . $_GET['login'];
} else if (isset($_GET['package']) && !empty($_GET['package'])) {
    $title = 'UEHS report for package ' . $_GET['package'];
} else {
    $title = 'Web query';
}
?>
  <title><?=htmlentities($title)?> :: UEHS</title>
</head>
<body>
<? include("header.inc"); ?>
<?php
include("menu.inc");
require("config_www.inc");
$login='%';
$package='%';

if (!isset($_GET['login']) && !isset($_GET['package'])) exit('<span style="color: rgb(255, 0, 0);">No search parameter submitted</span>');
if (isset($_GET['login']) && !$_GET['login']) exit('<span style="color: rgb(255, 0, 0);">Maintainer email not submitted</span>');
elseif ($_GET['login']) $login=$_GET['login'];
if (isset($_GET['package']) AND !$_GET['package']) exit('<span style="color: rgb(255, 0, 0);">Package name not submitted</span>');
elseif ($_GET['package']) $package=$_GET['package'];

$login = pg_escape_string($login);
$package  = pg_escape_string($package);

?>
<?php
    if ($login != '%' || count(explode(',',$package)) > 1) {
?>
<center><table bgcolor="#efe1c3" border="0" cellpadding="0" cellspacing="0" summary="">
  <tr>
        <td colspan="3" valign="top">
          <img src="/images/light-upperleft.png" align="left" border="0" hspace="0" vspace="0" alt="" width="8" height="8">
          <img src="/images/light-upperright.png" align="right" border="0" hspace="0" vspace="0" alt="" width="8" height="8">
        </td>
  </tr>
  <tr>
        <td>&nbsp;</td>
        <td valign="top" class="smallsserif">
<div style="text-align: center;font-family: Arial, Helvetica, sans-serif;font-size: 90%;">
    <div>
        <a href="#no_watch">Packages without a watch file</a> |
        <a href="#no_updated">Packages not in sync with upstream</a>
    </div>
    <div>
        <a href="#no_upstream">Packages with a watch file but no upstream version checkable</a> |
        <a href="#ok">Packages up to date</a>
    </div>
</div></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
        <td colspan="3" valign="bottom">
          <img src="/images/light-lowerleft.png" align="left" border="0" hspace="0" vspace="0" alt="" width="8.5" height="8">
          <img src="/images/light-lowerright.png" align="right" border="0" hspace="0" vspace="0" alt="" width="8" height="8">
        </td>
  </tr>
  </table></center><br/>
<?php } ?>
<?php

$sql=ARRAY(no_updated=>"Select id,pkgs.pop_inst,name,section,version,tot_up_error,tot_avg_error_date,pkgs.dist,up_version,watch_warn, up_changes,up_url,dversionmangled,lastupvsdebsync,lastcheck from pkgs
INNER join (Select SUM(up_error) as tot_up_error,AVG(avg_error_date) as tot_avg_error_date,name,dist FROM binpkgs GROUP BY name,dist) as binpkgs using(name,dist)
where updated='0' AND up_version!='' AND (maint LIKE '$login' OR uploaders LIKE '%$login%') AND name LIKE '$package'
order by pop_inst desc;",
no_upstream=>"select id,pkgs.pop_inst,name,section,version, tot_up_error, tot_avg_error_date, pkgs.dist,watch_warn,up_changes, up_version,up_url,dversionmangled,lastupvsdebsync,lastcheck,failures_count,watch  from pkgs INNER JOIN (Select SUM(up_error) as tot_up_error,AVG(avg_error_date) as tot_avg_error_date,name,dist FROM binpkgs GROUP BY name,dist)
as binpkgs using(name,dist) where up_version='' AND watch!='' AND (maint LIKE '$login' OR uploaders LIKE '%$login%') AND name LIKE '$package' order by pop_inst desc;",
no_watch=>"SELECT id,pkgs.pop_inst,name,pkgs.version,pkgs.dist,tot_up_error, tot_avg_error_date,wwiz,wwiz_type,wwiz_version,maint,section,up_changes,up_url,dversionmangled,lastupvsdebsync,lastcheck FROM pkgs INNER JOIN
(Select SUM(up_error) as tot_up_error,AVG(avg_error_date) as tot_avg_error_date,name,dist FROM binpkgs GROUP BY name,dist) as binpkgs using(name,dist)
WHERE (watch IS NULL OR watch='') AND (maint LIKE '$login' OR uploaders LIKE '%$login%') AND name LIKE '$package' ORDER BY pop_inst DESC;",
ok=>"Select id,pkgs.pop_inst,name,section,version,tot_up_error,tot_avg_error_date,pkgs.dist,up_version,watch_warn, up_changes,up_url,dversionmangled,lastupvsdebsync,lastcheck from pkgs
INNER join (Select SUM(up_error) as tot_up_error,AVG(avg_error_date) as tot_avg_error_date,name,dist FROM binpkgs GROUP BY name,dist) as binpkgs using(name,dist)
where updated='1' AND (maint LIKE '$login' OR uploaders LIKE '%$login%') AND name LIKE '$package' AND (up_version != '' AND up_version IS NOT NULL)
order by pop_inst desc;");
foreach($sql as $key => $value) {
	$rs=pg_exec($db,$value) OR Die("Error on query");
	$qta=pg_exec($db, "SELECT count(name) from pkgs WHERE (maint LIKE '$login' OR uploaders LIKE '%$login%') AND name LIKE '$package' ") OR Die("Errore on query");
	$totrs=pg_numrows($rs);
	$totpkgs=pg_fetch_array($qta,0);
	if ($totrs>0) {
	?><table bgcolor="#efe1c3" border="0" cellpadding="0" cellspacing="0" width="100%" summary="">
  <tr>
        <td colspan="3" valign="top" width="100%">
          <img src="/images/light-upperleft.png" align="left" border="0" hspace="0" vspace="0" alt="" width="8" height="8">
          <img src="/images/light-upperright.png" align="right" border="0" hspace="0" vspace="0" alt="" width="8" height="8">
        </td>
  </tr>
  <tr>
        <td>&nbsp;</td>
        <td valign="top" class="smallsserif">
<p><a name="<?php print $key; ?>"><big>&nbsp;&nbsp;<?php
		switch ($key) {
			case "no_watch":
				print "$totrs <b>packages without a watch file</b>, <b>share: </b> " . number_format ( $totrs/$totpkgs[0] * 100, 2) . "%<br>" ;
				break;
			case "no_upstream":
				print "$totrs <b>packages without a checkable upstream</b>, <b>share: </b> " . number_format ( $totrs/$totpkgs[0] * 100, 2) . "%<br>" ;
				break;
			case "no_updated":
				print "$totrs <b>packages with a newer upstream release</b>, <b>share: </b> " . number_format ( $totrs/$totpkgs[0] * 100, 2) . "%<br>" ;
				break;
			case "ok":
				print "$totrs <b>packages up-to-date</b>, <b>share: </b> " . number_format ( $totrs/$totpkgs[0] * 100, 2) . "%<br>" ;
				break;
			} ?>
</big></a></td>
  <td>&nbsp;</td>
  </tr>
  <tr>
        <td colspan="3" valign="bottom" width="100%">
          <img src="/images/light-lowerleft.png" align="left" border="0" hspace="0" vspace="0" alt="" width="8.5" height="8">
          <img src="/images/light-lowerright.png" align="right" border="0" hspace="0" vspace="0" alt="" width="8" height="8">
        </td>
  </tr>
  </table>
<?php
            $c = 0;
        	while ($res_array=pg_fetch_array($rs)) {

        	    if (1 == $c) {
?>
<table>
<tr>
<?php } ?>
<td width="50%">
<ul>
<li><a name="<? print $res_array["name"]; ?>"><b>Name:</b> <a href="http://packages.qa.debian.org/<?=$res_array['name']?>"><? print $res_array["name"]; ?></a> (<a href="https://launchpad.net/ubuntu/+source/<?=$res_array['name']?>">LP</a>)</a></li>
<ul>
<li><b>PopCon inst:</b> <a href="http://qa.debian.org/popcon.php?package=<?=$res_array['name']?>"><? print $res_array["pop_inst"]; ?></a></li>
<? if ($key=="no_watch") { ?>
<li><b>WWiz upstream version:</b>
		    <? if ($res_array["wwiz_version"]) {?>
			<? if ($res_array[up_changes]!='') { ?>
	   			<a href="<? print "wwiz_detail.php?id=$res_array[id]&amp;type=up_changes";?>">
		  		<? print $res_array["wwiz_version"]; ?></a>
			<?} else print $res_array["wwiz_version"];
			}
			else {
			      print "none";
				  $res_array["wwiz_version"]="N/A";
				 }
			?></li>
<? }?>
<? if ($key!="no_watch") { ?>
<li><b>Upstream version: </b>
		<? if ($res_array["up_version"]) {?>
	  	   <? if ($res_array[up_changes]!='') { ?>
	   			<a href="<? print "wwiz_detail.php?id=$res_array[id]&amp;type=up_changes";?>">
		  		<? print $res_array["up_version"]; ?></a>
			<?} else print $res_array["up_version"];
			}
			else print "none";?></li>
<? }
if (!empty($res_array['up_url'])) {
    echo '<li><b>Source download: </b> <a href="' . htmlentities($res_array['up_url']) . '">' . htmlentities(preg_replace('/(.+?)\?.*/','$1',basename($res_array['up_url']))) . '</a></li>';
}
?>
<li><b>Debian version: </b><? print htmlentities($res_array["version"]);
if ($res_array["dversionmangled"] != $res_array["version"]) {
    echo ' <span style="white-space: nowrap;">(mangled:&nbsp;<i>' . htmlentities($res_array["dversionmangled"]) . '</i>)</span>';
}
?></li>
<li><b>Last time checked: </b><? echo htmlentities(preg_replace('/\..+/','',$res_array['lastcheck'])); ?></li>
<li><b>Last time found up to date: </b><? echo htmlentities($res_array['lastupvsdebsync']); ?></li>
<?php
if (isset($res_array['failures_count'])) {
    ?><li><b>Total continuous failures: </b><? echo $res_array['failures_count']; ?></li><?php
}
?>
<? if ($key=="no_watch") { ?>
<li><b>Watch Wizard:</b>
<?
	if ($res_array["wwiz_type"]!="no_cright") {
?>
<a href="<? print "wwiz_detail.php?id=$res_array[id]&amp;type=watch";?>">
<? print $res_array["wwiz_type"]; ?></a>
	<?}
		else print "N/A";
	?></li>
<?}?>
<? if ($key!="no_watch") { 
$watch_comments = '';
if (!$res_array['up_version'] && isset($res_array['watch'])) {
    $watch_comments = trim(preg_replace('/^(\s*[^#].*$|\s*#)/m','',$res_array['watch']),"\n\r\0\x0B");
}
?>
<li><b>Watch: </b><a href="<? print "wwiz_detail.php?id=$res_array[id]&amp;type=watch";?>">view</a><? if($watch_comments) echo "&nbsp;<small>(comments&nbsp;below)</small>"; ?></li>
<?php
    if ($watch_comments) {
        ?><pre>
<? echo htmlspecialchars($watch_comments); ?>
</pre><?php
    }
}?>
<li><b>Uscan errors: </b><? $res_array["watch_warn"] ? print "<br>" . nl2br(htmlentities($res_array["watch_warn"])) : print "No errors"; ?></li>
<li><b>Copyright: </b><? print "<a href='" . htmlentities(changelog($res_array)) . "'>" . "view</a>"; ?></li>
</ul>
</ul>
</td>
<hr>
<?php
        if (2 == $c) {
?>
</tr>
</table>
<hr>
<?php   }
/* Who uses this? I guess nobody
        print "<!-- Machine-readable: " . $res_array['name'] . " " .
        $res_array["version"] . " " .
        $res_array["up_version"]  . " " .
        $res_array["wwiz_version"] . " -->";
*/
//        $c = (1 == $c)? 2 : 1;

        }
        if (2 == $c) {
            ?>
            </tr>
            </table>
            <?php
        }
    }
}
	pg_close($db);

function changelog($res_array) {
		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://changelogs.ubuntu.com/changelogs/pool/" .
		$res_array["section"] . "/" .
		$initial . "/" .
		$res_array["name"] ."/" .
		$res_array["name"] . "_" .
		$res_array["version"] . "/copyright";
		return $url;
}?>
  </tbody>
</table>
<br>
<br>
</body>
</html>
