From eadf625a51b23a8f62505949df863bdc8b2d0546 Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Sun, 6 Jun 2010 18:08:06 +0200 Subject: create a class folder and move FedoraClient --- FedoraClient.php | 267 ------------------------------------------------- class/FedoraClient.php | 267 +++++++++++++++++++++++++++++++++++++++++++++++++ fedcli.php | 43 ++++---- main.inc.php | 2 +- 4 files changed, 290 insertions(+), 289 deletions(-) delete mode 100644 FedoraClient.php create mode 100644 class/FedoraClient.php diff --git a/FedoraClient.php b/FedoraClient.php deleted file mode 100644 index e003a03..0000000 --- a/FedoraClient.php +++ /dev/null @@ -1,267 +0,0 @@ - - * - * @category Main - * @package FedoraClient - * - * @author Remi Collet - * @author Johan Cwiklinski - * @copyright 2010 Remi Collet - * @license http://www.gnu.org/licenses/lgpl-2.1.txt LGPL License 2.1 or (at your option) any later version - * @link http://github.com/remicollet/rpmphp/ - * @since The begining of times. - */ - -define('FEDORACLIENT_VERSION', '0.1.0-dev'); - -if (!function_exists('curl_version')) { - die("curl extension required\n"); -} -require_once 'Cache/Lite.php'; - -abstract class FedoraClient -{ - private $url; - private $agent; - private $debug = 0; - protected $cache; - - function __construct ($url, array $options) - { - $dir = "/tmp/cachelite-".posix_getlogin()."/"; - @mkdir($dir); - $this->cache = new Cache_Lite( - array( - 'memoryCaching' => true, - 'cacheDir' => $dir, - 'automaticSerialization' => true - ) - ); - - $this->url = $url; - if (isset($options['agent']) && !empty($options['agent'])) { - $this->agent = $options['agent']; - } else { - $this->agent = 'Fedora PHPClient/'.FEDORACLIENT_VERSION; - } - if (isset($options['debug']) && intval($options['debug'])>0) { - $this->debug = intval($options['debug']); - } - $this->logDebug( - 3, - __CLASS__."::".__FUNCTION__.": url='$url', agent='".$this->agent."'" - ); - } - - function logDebug($level, $msg) - { - if ($this->debug>=$level) { - echo "[debug][$level] $msg\n"; - } - } - - function sendRequest($method, array $options=array()) - { - $curl = curl_init(); - // And join to make our url. - $url = $this->url.$method; - curl_setopt($curl, CURLOPT_URL, $url); - // Boilerplate so pycurl processes cookies - curl_setopt($curl, CURLOPT_COOKIEFILE, '/dev/null'); - - //# Associate with the response to accumulate data - $this->data=''; - curl_setopt($curl, CURLOPT_WRITEFUNCTION, array($this, 'receive')); - - // Follow redirect - curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($curl, CURLOPT_MAXREDIRS, 5); - - // Set standard headers - curl_setopt( - $curl, - CURLOPT_HTTPHEADER, - array('User-agent: '.$this->agent, 'Accept: application/json') - ); - - // run the request - $this->logDebug( - 2, - __CLASS__."::".__FUNCTION__.": call '$url'" - ); - curl_exec($curl); - - - // Check for auth failures - // Note: old TG apps returned 403 Forbidden on authentication failures. - // Updated apps return 401 Unauthorized - // We need to accept both until all apps are updated to return 401. - $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE); - if ($http_status==401 || $http_status==403) { - $this->logDebug( - 1, - __CLASS__."::".__FUNCTION__. - ": http_status '$http_status' Authentication failed logging in" - ); - return array(); - } else if ($http_status>=400) { - $this->logDebug( - 1, - __CLASS__."::".__FUNCTION__. - ": http_status '$http_status' Unknown HTTP Server Response" - ); - return array(); - } else { - $this->logDebug( - 2, - __CLASS__."::".__FUNCTION__.": http_status '$http_status'" - ); - } - - $this->logDebug( - 2, - __CLASS__."::".__FUNCTION__.": close connexion" - ); - - curl_close($curl); - return json_decode($this->data, true); - } - - function receive($curl, $data) - { - $this->logDebug( - 9, - __CLASS__."::".__FUNCTION__.": $data" - ); - $this->data .= $data; - return strlen($data); - } -} - - -class FedoraPkgdb extends FedoraClient -{ - - function __construct (array $options=array()) - { - parent::__construct('https://admin.fedoraproject.org/pkgdb/', $options); - $this->logDebug( - 3, - __CLASS__."::".__FUNCTION__ - ); - } - - function getBranches($refresh=false) - { - $rep = ($refresh ? false : $this->cache->get(__FUNCTION__, __CLASS__)); - if ($rep) { - $this->logDebug( - 2, - __CLASS__."::".__FUNCTION__."() get from cache" - ); - } else { - $rep =$this->sendRequest('collections'); - $this->cache->save($rep, __FUNCTION__, __CLASS__); - $this->logDebug( - 2, - __CLASS__."::".__FUNCTION__."() save to cache" - ); - } - - $branches = array(); - if (isset($rep['collections'])) { - foreach ($rep['collections'] as $coll) { - if (isset($coll[0]['branchname'])) { - $branches[$coll[0]['branchname']] = $coll[0]; - } - } - } - return $branches; - } - - function getPackageInfo($name, $refresh=false) - { - $url="acls/name/$name"; - $rep = ($refresh ? false : $this->cache->get($url, __CLASS__)); - if ($rep) { - $this->logDebug( - 2, - __CLASS__."::".__FUNCTION__."($name) get from cache" - ); - } else { - $rep =$this->sendRequest($url); - $this->cache->save($rep, $url, __CLASS__); - $this->logDebug( - 2, - __CLASS__."::".__FUNCTION__."($name) save to cache" - ); - } - - if (isset($rep['status']) && !$rep['status']) { - $this->logDebug( - 1, - __CLASS__."::".__FUNCTION__."($name) ".$rep['message'] - ); - return false; - } - $branches = array(); - foreach ($rep['packageListings'] as $pack) { - $branches[$pack['collection']['branchname']] = $pack; - } - return $branches; - } - - function getBranch($name, $refresh=false) - { - $branches = $this->getBranches($refresh); - - if (isset($branches[$name])) { - return $branches[$name]; - } - return false; - } - - function getCritPath($refresh=false) - { - $url="lists/critpath"; - $rep = ($refresh ? false : $this->cache->get($url, __CLASS__)); - if ($rep) { - $this->logDebug( - 2, - __CLASS__."::".__FUNCTION__." get from cache" - ); - } else { - $rep =$this->sendRequest($url); - $this->cache->save($rep, $url, __CLASS__); - $this->logDebug( - 2, - __CLASS__."::".__FUNCTION__." save to cache" - ); - } - return $rep['pkgs']; - } -} -?> diff --git a/class/FedoraClient.php b/class/FedoraClient.php new file mode 100644 index 0000000..e003a03 --- /dev/null +++ b/class/FedoraClient.php @@ -0,0 +1,267 @@ + + * + * @category Main + * @package FedoraClient + * + * @author Remi Collet + * @author Johan Cwiklinski + * @copyright 2010 Remi Collet + * @license http://www.gnu.org/licenses/lgpl-2.1.txt LGPL License 2.1 or (at your option) any later version + * @link http://github.com/remicollet/rpmphp/ + * @since The begining of times. + */ + +define('FEDORACLIENT_VERSION', '0.1.0-dev'); + +if (!function_exists('curl_version')) { + die("curl extension required\n"); +} +require_once 'Cache/Lite.php'; + +abstract class FedoraClient +{ + private $url; + private $agent; + private $debug = 0; + protected $cache; + + function __construct ($url, array $options) + { + $dir = "/tmp/cachelite-".posix_getlogin()."/"; + @mkdir($dir); + $this->cache = new Cache_Lite( + array( + 'memoryCaching' => true, + 'cacheDir' => $dir, + 'automaticSerialization' => true + ) + ); + + $this->url = $url; + if (isset($options['agent']) && !empty($options['agent'])) { + $this->agent = $options['agent']; + } else { + $this->agent = 'Fedora PHPClient/'.FEDORACLIENT_VERSION; + } + if (isset($options['debug']) && intval($options['debug'])>0) { + $this->debug = intval($options['debug']); + } + $this->logDebug( + 3, + __CLASS__."::".__FUNCTION__.": url='$url', agent='".$this->agent."'" + ); + } + + function logDebug($level, $msg) + { + if ($this->debug>=$level) { + echo "[debug][$level] $msg\n"; + } + } + + function sendRequest($method, array $options=array()) + { + $curl = curl_init(); + // And join to make our url. + $url = $this->url.$method; + curl_setopt($curl, CURLOPT_URL, $url); + // Boilerplate so pycurl processes cookies + curl_setopt($curl, CURLOPT_COOKIEFILE, '/dev/null'); + + //# Associate with the response to accumulate data + $this->data=''; + curl_setopt($curl, CURLOPT_WRITEFUNCTION, array($this, 'receive')); + + // Follow redirect + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($curl, CURLOPT_MAXREDIRS, 5); + + // Set standard headers + curl_setopt( + $curl, + CURLOPT_HTTPHEADER, + array('User-agent: '.$this->agent, 'Accept: application/json') + ); + + // run the request + $this->logDebug( + 2, + __CLASS__."::".__FUNCTION__.": call '$url'" + ); + curl_exec($curl); + + + // Check for auth failures + // Note: old TG apps returned 403 Forbidden on authentication failures. + // Updated apps return 401 Unauthorized + // We need to accept both until all apps are updated to return 401. + $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE); + if ($http_status==401 || $http_status==403) { + $this->logDebug( + 1, + __CLASS__."::".__FUNCTION__. + ": http_status '$http_status' Authentication failed logging in" + ); + return array(); + } else if ($http_status>=400) { + $this->logDebug( + 1, + __CLASS__."::".__FUNCTION__. + ": http_status '$http_status' Unknown HTTP Server Response" + ); + return array(); + } else { + $this->logDebug( + 2, + __CLASS__."::".__FUNCTION__.": http_status '$http_status'" + ); + } + + $this->logDebug( + 2, + __CLASS__."::".__FUNCTION__.": close connexion" + ); + + curl_close($curl); + return json_decode($this->data, true); + } + + function receive($curl, $data) + { + $this->logDebug( + 9, + __CLASS__."::".__FUNCTION__.": $data" + ); + $this->data .= $data; + return strlen($data); + } +} + + +class FedoraPkgdb extends FedoraClient +{ + + function __construct (array $options=array()) + { + parent::__construct('https://admin.fedoraproject.org/pkgdb/', $options); + $this->logDebug( + 3, + __CLASS__."::".__FUNCTION__ + ); + } + + function getBranches($refresh=false) + { + $rep = ($refresh ? false : $this->cache->get(__FUNCTION__, __CLASS__)); + if ($rep) { + $this->logDebug( + 2, + __CLASS__."::".__FUNCTION__."() get from cache" + ); + } else { + $rep =$this->sendRequest('collections'); + $this->cache->save($rep, __FUNCTION__, __CLASS__); + $this->logDebug( + 2, + __CLASS__."::".__FUNCTION__."() save to cache" + ); + } + + $branches = array(); + if (isset($rep['collections'])) { + foreach ($rep['collections'] as $coll) { + if (isset($coll[0]['branchname'])) { + $branches[$coll[0]['branchname']] = $coll[0]; + } + } + } + return $branches; + } + + function getPackageInfo($name, $refresh=false) + { + $url="acls/name/$name"; + $rep = ($refresh ? false : $this->cache->get($url, __CLASS__)); + if ($rep) { + $this->logDebug( + 2, + __CLASS__."::".__FUNCTION__."($name) get from cache" + ); + } else { + $rep =$this->sendRequest($url); + $this->cache->save($rep, $url, __CLASS__); + $this->logDebug( + 2, + __CLASS__."::".__FUNCTION__."($name) save to cache" + ); + } + + if (isset($rep['status']) && !$rep['status']) { + $this->logDebug( + 1, + __CLASS__."::".__FUNCTION__."($name) ".$rep['message'] + ); + return false; + } + $branches = array(); + foreach ($rep['packageListings'] as $pack) { + $branches[$pack['collection']['branchname']] = $pack; + } + return $branches; + } + + function getBranch($name, $refresh=false) + { + $branches = $this->getBranches($refresh); + + if (isset($branches[$name])) { + return $branches[$name]; + } + return false; + } + + function getCritPath($refresh=false) + { + $url="lists/critpath"; + $rep = ($refresh ? false : $this->cache->get($url, __CLASS__)); + if ($rep) { + $this->logDebug( + 2, + __CLASS__."::".__FUNCTION__." get from cache" + ); + } else { + $rep =$this->sendRequest($url); + $this->cache->save($rep, $url, __CLASS__); + $this->logDebug( + 2, + __CLASS__."::".__FUNCTION__." save to cache" + ); + } + return $rep['pkgs']; + } +} +?> diff --git a/fedcli.php b/fedcli.php index ce9c31a..eb897c4 100755 --- a/fedcli.php +++ b/fedcli.php @@ -2,33 +2,34 @@ */ - + + require 'Console/Getargs.php'; -require 'FedoraClient.php'; +require 'class/FedoraClient.php'; function Help() { echo "\nFedora Client Command Line usage\n\n"; - + echo "fdcli command options\n"; echo "\tbranch: branch info\n"; echo "\tbranches: list branches\n"; @@ -43,11 +44,11 @@ function Branches() { "debug" => array('short' => 'd', 'max' => 1, 'min' => 1, 'desc' => "debug level", 'default' => "0") ); $args =& Console_Getargs::factory($config); - + if (PEAR::isError($args)) { die (Console_Getargs::getHelp($config)); } - + $client = new FedoraPkgdb(array('debug' => intval($args->getValue('debug')))); $branches = $client->getBranches(); foreach ($branches as $name => $branch) { @@ -62,11 +63,11 @@ function Branch() { "debug" => array('short' => 'd', 'max' => 1, 'min' => 1, 'desc' => "debug level", 'default' => "0") ); $args =& Console_Getargs::factory($config); - + if (PEAR::isError($args)) { die (Console_Getargs::getHelp($config)); } - + $client = new FedoraPkgdb(array('debug' => intval($args->getValue('debug')))); $branch = $client->getBranch($args->getValue('branch')); print_r($branch); @@ -78,11 +79,11 @@ function CritPath() { "debug" => array('short' => 'd', 'max' => 1, 'min' => 1, 'desc' => "debug level", 'default' => "0") ); $args =& Console_Getargs::factory($config); - + if (PEAR::isError($args)) { die (Console_Getargs::getHelp($config)); } - + $client = new FedoraPkgdb(array('debug' => intval($args->getValue('debug')))); $branch = $client->getCritPath(); print_r($branch); @@ -94,7 +95,7 @@ function Package() { "debug" => array('short' => 'd', 'max' => 1, 'min' => 1, 'desc' => "debug level", 'default' => "0") ); $args =& Console_Getargs::factory($config); - + if (PEAR::isError($args)) { die (Console_Getargs::getHelp($config)); } @@ -104,7 +105,7 @@ function Package() { if (!$rep) { die("Package not found\n"); } - + $first = true; foreach ($rep as $branch => $pack) { if ($first) { @@ -114,7 +115,7 @@ function Package() { echo "Description:\n".$pack['package']['description']."\n"; } echo $branch.":\t".$pack['owner']; - + $i=0; foreach ($pack['people'] as $user) { if (isset($user['aclOrder']['commit']['statuscode']) && $user['aclOrder']['commit']['statuscode']==3) { @@ -130,11 +131,11 @@ function Version() { "debug" => array('short' => 'd', 'max' => 1, 'min' => 1, 'desc' => "debug level", 'default' => "0") ); $args =& Console_Getargs::factory($config); - + if (PEAR::isError($args)) { die (Console_Getargs::getHelp($config)."\n"); } - + echo "PHP Fedora Client class version ".FEDORACLIENT_VERSION."\n"; } @@ -147,6 +148,6 @@ switch ($cmd) { case 'critpath': CritPath(); break; case 'package': Package(); break; case 'version': Version(); break; - default: Help(); + default: Help(); } ?> diff --git a/main.inc.php b/main.inc.php index a846e16..c9ec889 100644 --- a/main.inc.php +++ b/main.inc.php @@ -37,7 +37,7 @@ define('RPMPHP_VERSION', '1.0.0-dev'); require 'config.inc.php'; require '/usr/share/php/Smarty/Smarty.class.php'; -require 'FedoraClient.php'; +require 'class/FedoraClient.php'; $smarty = new Smarty(); -- cgit From 5330ba05cafe0c33ea7f6d0e0a0b7117117f246f Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Sun, 6 Jun 2010 20:35:36 +0200 Subject: Introduce CommonTable, TableIterator, TablePearRepo classes (and use it in refresh) --- class/CommonTable.php | 368 +++++++++++++++++++++++++++++++++++++++++++++++++ class/FedoraClient.php | 2 + refresh.php | 15 +- 3 files changed, 373 insertions(+), 12 deletions(-) create mode 100644 class/CommonTable.php diff --git a/class/CommonTable.php b/class/CommonTable.php new file mode 100644 index 0000000..721a224 --- /dev/null +++ b/class/CommonTable.php @@ -0,0 +1,368 @@ +. + * + * @category Main + * @package RPMPHP + * + * @author Remi Collet + * @author Johan Cwiklinski + * @copyright 2010 Remi Collet + * @license http://www.gnu.org/licenses/agpl-3.0-standalone.html AGPL License 3.0 or (at your option) any later version + * @link http://github.com/remicollet/rpmphp/ + * @since The begining of times. +*/ + +abstract class CommonTable { + + protected $db; + protected $table; + + function __construct(PDO $db, $table) { + + $this->db = $db; + $this->table = $table; + + if (!$this->existsTable($table)) { + $this->createTable($table); + } + } + + public function existsTable($table) { + $req = new TableIterator($this->db, "SHOW TABLES LIKE '$table'"); + foreach ($req as $data) { + return true; + } + return false; + } + + protected function exec($sql) { + $res = $this->db->exec($sql); + if ($res===false) { + $err = $this->db->errorInfo(); + throw new Exception($err[2]); + } + } + + protected function add(array $fields) { + $col = array(); + $val = array(); + foreach ($fields as $name => $value) { + $col[] = "`$name`"; + if (is_null($value)) { + $val[] = 'NULL'; + } else if (is_numeric($value)) { + $val[] = $value; + } else { + $val[] = "'$value'"; + } + } + $sql = "INSERT INTO `".$this->table."` (".implode(',',$col).") + VALUE (".implode(',',$val).")"; + $this->exec($sql); + } + abstract protected function createTable(); + + /** + * Instanciate a Simple DBIterator + * + * Examples = + * foreach ($DB->request("select * from glpi_states") as $data) { ... } + * foreach ($DB->request("glpi_states") as $ID => $data) { ... } + * foreach ($DB->request("glpi_states", "ID=1") as $ID => $data) { ... } + * foreach ($DB->request("glpi_states", "", "name") as $ID => $data) { ... } + * foreach ($DB->request("glpi_computers",array("name"=>"SBEI003W","entities_id"=>1),array("serial","otherserial")) { ... } + * + * @param $tableorsql table name, array of names or SQL query + * @param $crit string or array of field/values, ex array("id"=>1), if empty => all rows + * + * Examples = + * array("id"=>NULL) + * array("OR"=>array("id"=>1, "NOT"=>array("state"=>3))); + * array("AND"=>array("id"=>1, array("NOT"=>array("state"=>array(3,4,5),"toto"=>2)))) + * + * param 'FIELDS' name or array of field names + * param 'ORDER' filed name or array of field names + * param 'LIMIT' max of row to retrieve + * param 'START' first row to retrieve + * + * @return DBIterator + **/ + public function request ($crit='') { + return new TableIterator ($this->db, $this->table, $crit); + } + + public function getAllArray($fieldkey, $fieldvalue) { + $crit = array('FIELDS' => array($fieldkey, $fieldvalue), + 'ORDER' => $fieldkey); + $tab = array(); + foreach ($this->request($crit) as $data) { + $tab[$data[$fieldkey]] = $data[$fieldvalue]; + } + return $tab; + } +} + +class TablePearRepo extends CommonTable { + + function __construct($db) { + parent::__construct($db, 'pearrepo'); + } + + protected function createTable() { + + // Table schema + $sql = "CREATE TABLE `pearrepo` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `alias` varchar(30) NOT NULL, + `url` varchar(255) NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `alias` (`alias`) + ) DEFAULT CHARSET=utf8"; + + $this->exec($sql); + + // Some known repo, other could be add manually + // no reply from "phpdb" => "pear.phpdb.org" + $channels = array( + "pear" => "pear.php.net" + ,"doctrine" => "pear.phpdoctrine.org" + ,"ezc" => "components.ez.no" + ,"pdepend" => "pear.pdepend.org" + ,"phing" => "pear.phing.info" + ,"phpmd" => "pear.phpmd.org" + ,"phpunit" => "pear.phpunit.de" + ,"swift" => "pear.swiftmailer.org" + ,"symphony" => "pear.symfony-project.com" + ); + + foreach ($channels as $alias => $url) { + $this->add(array('alias'=>$alias, 'url'=>$url)); + } + } + + function getAllRepo() { + return $this->getAllArray('alias', 'url'); + } +} + +/* + * Helper for simple query => see $DBmysql->requete + */ +class TableIterator implements Iterator { + + private $con; + private $sql; + private $res = false; + private $row; + private $pos; + + /** + * Constructor + * + * @param $dbconnexion Database Connnexion (must be a CommonDBTM object) + * @param $table table name + * @param $crit string or array of filed/values, ex array("id"=>1), if empty => all rows + * + **/ + function __construct (PDO $dbconnexion, $table, $crit='') { + + $this->conn = $dbconnexion; + if (is_string($table) && strpos($table, " ")) { + $this->sql = $table; + } else { + // check field, orderby, limit, start in criterias + $field=""; + $orderby=""; + $limit=0; + $start=0; + + if (is_array($crit) && count($crit)) { + foreach ($crit as $key => $val) { + if ($key==="FIELDS") { + $field = $val; + unset($crit[$key]); + } else if ($key==="ORDER") { + $orderby = $val; + unset($crit[$key]); + } else if ($key==="LIMIT") { + $limit = $val; + unset($crit[$key]); + } else if ($key==="START") { + $start = $val; + unset($crit[$key]); + } + } + } + + // SELECT field list + if (is_array($field)) { + $this->sql = ""; + foreach ($field as $t => $f) { + if (is_numeric($t)) { + $this->sql .= (empty($this->sql) ? "SELECT " : ",") . $f; + } else if (is_array($f)) { + $this->sql .= (empty($this->sql) ? "SELECT $t." : ",$t.") . implode(",$t.",$f); + } else { + $this->sql .= (empty($this->sql) ? "SELECT " : ",") . "$t.$f"; + } + } + } else if (empty($field)) { + $this->sql = "SELECT *"; + } else { + $this->sql = "SELECT `$field`"; + } + // FROM table list + if (is_array($table)) { + $this->sql .= " FROM `".implode("`, `",$table)."`"; + } else { + $this->sql .= " FROM `$table`"; + } + // WHERE criteria list + if (!empty($crit)) { + print_r($crit); + $this->sql .= " WHERE ".$this->analyseCrit($crit); + } + // ORDER BY + if (is_array($orderby)) { + $this->sql .= " ORDER BY `".implode("`, `",$orderby)."`"; + } else if (!empty($orderby)) { + $this->sql .= " ORDER BY `$orderby`"; + } + if (is_numeric($limit) && $limit>0) { + $this->sql .= " LIMIT $limit"; + if (is_numeric($start) && $start>0) { + $this->sql .= " OFFSET $start"; + } + } + } + //echo "SQL: ".$this->sql."\n"; + $this->res = $this->conn->prepare($this->sql); + if ($this->res===false) { + $err = $this->db->errorInfo(); + throw new Exception($err[2]); + } + + $this->pos = -1; + } + + function __destruct () { + + if ($this->res) { + $this->res->closeCursor(); + } + } + + private function analyseCrit ($crit, $bool="AND") { + + if (!is_array($crit)) { + return $crit; + } + $ret = ""; + foreach ($crit as $name => $value) { + if (!empty($ret)) { + $ret .= " $bool "; + } + if (is_numeric($name)) { + // No Key case => recurse. + $ret .= "(" . $this->analyseCrit($value, $bool) . ")"; + } else if ($name==="OR" || $name==="AND") { + // Binary logical operator + $ret .= "(" . $this->analyseCrit($value, $name) . ")"; + } else if ($name==="NOT") { + // Uninary logicial operator + $ret .= " NOT (" . $this->analyseCrit($value, "AND") . ")"; + } else if ($name==="FKEY") { + // Foreign Key condition + if (is_array($value) && count($value)==2) { + reset($value); + list($t1,$f1)=each($value); + list($t2,$f2)=each($value); + $ret .= (is_numeric($t1) ? "$f1" : "$t1.$f1") . "=" . + (is_numeric($t2) ? "$f2" : "$t2.$f2"); + } else { + trigger_error("BAD FOREIGN KEY", E_USER_ERROR); + } + } else if (is_array($value)) { + // Array of Value + $ret .= "$name IN ('". implode("','",$value)."')"; + } else if (is_null($value)) { + // NULL condition + $ret .= "$name IS NULL"; + } else if (is_numeric($value)) { + // Integer + $ret .= "$name=$value"; + } else { + // String + $ret .= "$name='$value'"; + } + } + return $ret; + } + + public function rewind () { + + if ($this->res && $this->pos>=0) { + $this->res->closeCursor(); + $this->pos = -1; + } + + if ($this->res && $this->pos<0) { + if (!$this->res->execute()) { + $err = $this->res->errorInfo(); + throw new Exception($err[2]); + } + } + return $this->next(); + } + + public function current() { + + return $this->row; + } + + public function key() { + + return (isset($this->row["id"]) ? $this->row["id"] : $this->pos); + } + + public function next() { + + if (!$this->res) { + return false; + } + $this->row = $this->res->fetch(PDO::FETCH_ASSOC); + $this->pos++; + return $this->row; + } + + public function valid() { + + return $this->res && $this->row; + } + + public function numrows() { + + return ($this->res ? $this->res->rowCount() : 0); + } +} +?> diff --git a/class/FedoraClient.php b/class/FedoraClient.php index e003a03..43d9dce 100644 --- a/class/FedoraClient.php +++ b/class/FedoraClient.php @@ -227,10 +227,12 @@ class FedoraPkgdb extends FedoraClient ); return false; } + $this->logDebug(8,print_r($rep,true)); $branches = array(); foreach ($rep['packageListings'] as $pack) { $branches[$pack['collection']['branchname']] = $pack; } + $this->logDebug(7,print_r($branches,true)); return $branches; } diff --git a/refresh.php b/refresh.php index 3be354f..cc56e84 100644 --- a/refresh.php +++ b/refresh.php @@ -44,6 +44,7 @@ if (isset($_SERVER["SERVER_NAME"])) { die("This script is launched twice a day, be patient"); } require "config.inc.php"; +require "class/CommonTable.php"; /** * To document @@ -306,18 +307,8 @@ try { // ------------------------------------------------------------------- if ($_SERVER['argc']==1 || in_array('pear', $_SERVER['argv'])) { echo date("r : ") . "PEAR reading channels\n"; - $channels=array( - "pear" => "pear.php.net" - ,"doctrine" => "pear.phpdoctrine.org" - ,"ezc" => "components.ez.no" - ,"pdepend" => "pear.pdepend.org" - ,"phing" => "pear.phing.info" - ,"phpdb" => "pear.phpdb.org" - ,"phpmd" => "pear.phpmd.org" - ,"phpunit" => "pear.phpunit.de" - ,"swift" => "pear.swiftmailer.org" - ,"symphony" => "pear.symfony-project.com" - ); + $pear = new TablePearRepo($db); + $channels = $pear->getAllRepo(); try { $nbtot=0; -- cgit From 358fc123a50e23389b0f48f3afa0468bc9fae51a Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Sun, 6 Jun 2010 20:57:48 +0200 Subject: add some docs --- class/CommonTable.php | 63 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/class/CommonTable.php b/class/CommonTable.php index 721a224..66e1c27 100644 --- a/class/CommonTable.php +++ b/class/CommonTable.php @@ -37,6 +37,12 @@ abstract class CommonTable { protected $db; protected $table; + /** + * Instanciate a CommonTable + * + * @param $db object PDO instance of the DB connection + * @param $table string with table name + */ function __construct(PDO $db, $table) { $this->db = $db; @@ -47,6 +53,13 @@ abstract class CommonTable { } } + /** + * Check if the table already exists + * + * @param $table string with table name + * + * @return boolean + */ public function existsTable($table) { $req = new TableIterator($this->db, "SHOW TABLES LIKE '$table'"); foreach ($req as $data) { @@ -55,6 +68,11 @@ abstract class CommonTable { return false; } + /** + * Execute an SQL statement (INSERT, DELETE, ...) + * + * @param $sql string + */ protected function exec($sql) { $res = $this->db->exec($sql); if ($res===false) { @@ -63,6 +81,11 @@ abstract class CommonTable { } } + /** + * Add a new row in the table + * + * @param fields hashtable of fieldname => value + */ protected function add(array $fields) { $col = array(); $val = array(); @@ -80,19 +103,21 @@ abstract class CommonTable { VALUE (".implode(',',$val).")"; $this->exec($sql); } + + /** + * Create the table + */ abstract protected function createTable(); /** - * Instanciate a Simple DBIterator + * Instanciate a Simple TableIterator on the current table * * Examples = - * foreach ($DB->request("select * from glpi_states") as $data) { ... } - * foreach ($DB->request("glpi_states") as $ID => $data) { ... } - * foreach ($DB->request("glpi_states", "ID=1") as $ID => $data) { ... } - * foreach ($DB->request("glpi_states", "", "name") as $ID => $data) { ... } - * foreach ($DB->request("glpi_computers",array("name"=>"SBEI003W","entities_id"=>1),array("serial","otherserial")) { ... } + * foreach ($DB->request() as $ID => $data) { ... } + * foreach ($DB->request("ID=1") as $ID => $data) { ... } + * foreach ($DB->request("", "name") as $ID => $data) { ... } + * foreach ($DB->request(array("name"=>"SBEI003W","entities_id"=>1),array("serial","otherserial")) { ... } * - * @param $tableorsql table name, array of names or SQL query * @param $crit string or array of field/values, ex array("id"=>1), if empty => all rows * * Examples = @@ -111,6 +136,14 @@ abstract class CommonTable { return new TableIterator ($this->db, $this->table, $crit); } + /** + * Retrieve 2 columns of all the table's row in a hashtable + * + * @param $fieldkey string name of the field to use as index + * @param $fieldvalue string name of the field to use as value + * + * @return hashtable + */ public function getAllArray($fieldkey, $fieldvalue) { $crit = array('FIELDS' => array($fieldkey, $fieldvalue), 'ORDER' => $fieldkey); @@ -124,10 +157,16 @@ abstract class CommonTable { class TablePearRepo extends CommonTable { + /** + * Instanciate a TablePearRepo to manage pearrepo table + */ function __construct($db) { parent::__construct($db, 'pearrepo'); } + /** + * Create the table and populate it with known repo + */ protected function createTable() { // Table schema @@ -160,13 +199,18 @@ class TablePearRepo extends CommonTable { } } + /** + * Retrieve all the known repository + * + * @return hastable of alias => url + */ function getAllRepo() { return $this->getAllArray('alias', 'url'); } } /* - * Helper for simple query => see $DBmysql->requete + * Helper for simple query => use directly or through CommonTable::request() */ class TableIterator implements Iterator { @@ -272,6 +316,9 @@ class TableIterator implements Iterator { } } + /** + * Build WHERE clause + */ private function analyseCrit ($crit, $bool="AND") { if (!is_array($crit)) { -- cgit From a6b39fd500d6419dc9e268efc52474e06a11458f Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Sun, 6 Jun 2010 21:21:06 +0200 Subject: missing ref to GLPI --- class/CommonTable.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/class/CommonTable.php b/class/CommonTable.php index 66e1c27..a913edd 100644 --- a/class/CommonTable.php +++ b/class/CommonTable.php @@ -211,6 +211,10 @@ class TablePearRepo extends CommonTable { /* * Helper for simple query => use directly or through CommonTable::request() + * + * Freely inspired from DBmysqlIterator class from GLPI + * (already written by Remi, and ported to PDO) + * See http://www.glpi-project.org/ */ class TableIterator implements Iterator { -- cgit From a6282ca0e8a13fd53b4bea6eeff7a69f1312eef3 Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Tue, 8 Jun 2010 18:39:20 +0200 Subject: autoload --- class/CommonTable.php | 316 +----------------------------------------------- class/FedoraClient.php | 107 +--------------- class/FedoraPkgdb.php | 142 ++++++++++++++++++++++ class/TableIterator.php | 287 +++++++++++++++++++++++++++++++++++++++++++ class/TablePearRepo.php | 97 +++++++++++++++ include/main.php | 13 +- 6 files changed, 540 insertions(+), 422 deletions(-) create mode 100644 class/FedoraPkgdb.php create mode 100644 class/TableIterator.php create mode 100644 class/TablePearRepo.php diff --git a/class/CommonTable.php b/class/CommonTable.php index 1ed47f8..57c701b 100644 --- a/class/CommonTable.php +++ b/class/CommonTable.php @@ -170,318 +170,4 @@ abstract class CommonTable } } -class TablePearRepo extends CommonTable -{ - - /** - * Instanciate a TablePearRepo to manage pearrepo table - * - * @param object $db PDO instance of the DB connection - */ - function __construct($db) - { - parent::__construct($db, 'pearrepo'); - } - - /** - * Create the table and populate it with known repo - * - * @return void - */ - protected function createTable() - { - - // Table schema - $sql = "CREATE TABLE `pearrepo` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `alias` varchar(30) NOT NULL, - `url` varchar(255) NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `alias` (`alias`) - ) DEFAULT CHARSET=utf8"; - - $this->exec($sql); - - // Some known repo, other could be add manually - // no reply from "phpdb" => "pear.phpdb.org" - $channels = array( - "pear" => "pear.php.net" - ,"doctrine" => "pear.phpdoctrine.org" - ,"ezc" => "components.ez.no" - ,"pdepend" => "pear.pdepend.org" - ,"phing" => "pear.phing.info" - ,"phpmd" => "pear.phpmd.org" - ,"phpunit" => "pear.phpunit.de" - ,"swift" => "pear.swiftmailer.org" - ,"symphony" => "pear.symfony-project.com" - ); - - foreach ($channels as $alias => $url) { - $this->add(array('alias'=>$alias, 'url'=>$url)); - } - } - - /** - * Retrieve all the known repository - * - * @return hastable of alias => url - */ - function getAllRepo() - { - return $this->getAllArray('alias', 'url'); - } -} - -/** - * Helper for simple query => use directly or through CommonTable::request() - * - * Freely inspired from DBmysqlIterator class from GLPI - * (already written by Remi, and ported to PDO) - * See http://www.glpi-project.org/ - */ -class TableIterator implements Iterator -{ - private $con; - private $sql; - private $res = false; - private $row; - private $pos; - - /** - * Constructor - * - * @param CommonDBTM $dbconnexion Database Connnexion - * (must be a CommonDBTM object) - * @param string $table table name - * @param string|array $crit string or array of filed/values, - * ex array("id"=>1), if empty => all rows - */ - function __construct (PDO $dbconnexion, $table, $crit='') - { - - $this->conn = $dbconnexion; - if (is_string($table) && strpos($table, " ")) { - $this->sql = $table; - } else { - // check field, orderby, limit, start in criterias - $field=""; - $orderby=""; - $limit=0; - $start=0; - - if (is_array($crit) && count($crit)) { - foreach ($crit as $key => $val) { - if ($key==="FIELDS") { - $field = $val; - unset($crit[$key]); - } else if ($key==="ORDER") { - $orderby = $val; - unset($crit[$key]); - } else if ($key==="LIMIT") { - $limit = $val; - unset($crit[$key]); - } else if ($key==="START") { - $start = $val; - unset($crit[$key]); - } - } - } - - // SELECT field list - if (is_array($field)) { - $this->sql = ""; - foreach ($field as $t => $f) { - if (is_numeric($t)) { - $this->sql .= (empty($this->sql) - ? "SELECT " : ",") . $f; - } else if (is_array($f)) { - $this->sql .= (empty($this->sql) - ? "SELECT $t." : ",$t.") . implode(",$t.", $f); - } else { - $this->sql .= (empty($this->sql) - ? "SELECT " : ",") . "$t.$f"; - } - } - } else if (empty($field)) { - $this->sql = "SELECT *"; - } else { - $this->sql = "SELECT `$field`"; - } - // FROM table list - if (is_array($table)) { - $this->sql .= " FROM `".implode("`, `", $table)."`"; - } else { - $this->sql .= " FROM `$table`"; - } - // WHERE criteria list - if (!empty($crit)) { - print_r($crit); - $this->sql .= " WHERE ".$this->_analyseCrit($crit); - } - // ORDER BY - if (is_array($orderby)) { - $this->sql .= " ORDER BY `".implode("`, `", $orderby)."`"; - } else if (!empty($orderby)) { - $this->sql .= " ORDER BY `$orderby`"; - } - if (is_numeric($limit) && $limit>0) { - $this->sql .= " LIMIT $limit"; - if (is_numeric($start) && $start>0) { - $this->sql .= " OFFSET $start"; - } - } - } - //echo "SQL: ".$this->sql."\n"; - $this->res = $this->conn->prepare($this->sql); - if ($this->res===false) { - $err = $this->db->errorInfo(); - throw new Exception($err[2]); - } - - $this->pos = -1; - } - - /** - * Class destructor - */ - function __destruct () - { - if ($this->res) { - $this->res->closeCursor(); - } - } - - /** - * Build WHERE clause - * - * @param TODO $crit To document - * @param TODO $bool To document - * - * @return To document - */ - private function _analyseCrit ($crit, $bool="AND") - { - - if (!is_array($crit)) { - return $crit; - } - $ret = ""; - foreach ($crit as $name => $value) { - if (!empty($ret)) { - $ret .= " $bool "; - } - if (is_numeric($name)) { - // No Key case => recurse. - $ret .= "(" . $this->_analyseCrit($value, $bool) . ")"; - } else if ($name==="OR" || $name==="AND") { - // Binary logical operator - $ret .= "(" . $this->_analyseCrit($value, $name) . ")"; - } else if ($name==="NOT") { - // Uninary logicial operator - $ret .= " NOT (" . $this->_analyseCrit($value, "AND") . ")"; - } else if ($name==="FKEY") { - // Foreign Key condition - if (is_array($value) && count($value)==2) { - reset($value); - list($t1,$f1)=each($value); - list($t2,$f2)=each($value); - $ret .= (is_numeric($t1) ? "$f1" : "$t1.$f1") . "=" . - (is_numeric($t2) ? "$f2" : "$t2.$f2"); - } else { - trigger_error("BAD FOREIGN KEY", E_USER_ERROR); - } - } else if (is_array($value)) { - // Array of Value - $ret .= "$name IN ('". implode("','", $value)."')"; - } else if (is_null($value)) { - // NULL condition - $ret .= "$name IS NULL"; - } else if (is_numeric($value)) { - // Integer - $ret .= "$name=$value"; - } else { - // String - $ret .= "$name='$value'"; - } - } - return $ret; - } - - /** - * To document - * - * @return To document - */ - public function rewind () - { - - if ($this->res && $this->pos>=0) { - $this->res->closeCursor(); - $this->pos = -1; - } - - if ($this->res && $this->pos<0) { - if (!$this->res->execute()) { - $err = $this->res->errorInfo(); - throw new Exception($err[2]); - } - } - return $this->next(); - } - - /** - * To document - * - * @return To document - */ - public function current() - { - return $this->row; - } - - /** - * To document - * - * @return To document - */ - public function key() - { - return (isset($this->row["id"]) ? $this->row["id"] : $this->pos); - } - - /** - * To document - * - * @return To document - */ - public function next() - { - if (!$this->res) { - return false; - } - $this->row = $this->res->fetch(PDO::FETCH_ASSOC); - $this->pos++; - return $this->row; - } - - /** - * To document - * - * @return To document - */ - public function valid() - { - return $this->res && $this->row; - } - - /** - * To document - * - * @return To document - */ - public function numrows() - { - return ($this->res ? $this->res->rowCount() : 0); - } -} -?> +?> \ No newline at end of file diff --git a/class/FedoraClient.php b/class/FedoraClient.php index 43d9dce..ed29f54 100644 --- a/class/FedoraClient.php +++ b/class/FedoraClient.php @@ -161,109 +161,4 @@ abstract class FedoraClient } } - -class FedoraPkgdb extends FedoraClient -{ - - function __construct (array $options=array()) - { - parent::__construct('https://admin.fedoraproject.org/pkgdb/', $options); - $this->logDebug( - 3, - __CLASS__."::".__FUNCTION__ - ); - } - - function getBranches($refresh=false) - { - $rep = ($refresh ? false : $this->cache->get(__FUNCTION__, __CLASS__)); - if ($rep) { - $this->logDebug( - 2, - __CLASS__."::".__FUNCTION__."() get from cache" - ); - } else { - $rep =$this->sendRequest('collections'); - $this->cache->save($rep, __FUNCTION__, __CLASS__); - $this->logDebug( - 2, - __CLASS__."::".__FUNCTION__."() save to cache" - ); - } - - $branches = array(); - if (isset($rep['collections'])) { - foreach ($rep['collections'] as $coll) { - if (isset($coll[0]['branchname'])) { - $branches[$coll[0]['branchname']] = $coll[0]; - } - } - } - return $branches; - } - - function getPackageInfo($name, $refresh=false) - { - $url="acls/name/$name"; - $rep = ($refresh ? false : $this->cache->get($url, __CLASS__)); - if ($rep) { - $this->logDebug( - 2, - __CLASS__."::".__FUNCTION__."($name) get from cache" - ); - } else { - $rep =$this->sendRequest($url); - $this->cache->save($rep, $url, __CLASS__); - $this->logDebug( - 2, - __CLASS__."::".__FUNCTION__."($name) save to cache" - ); - } - - if (isset($rep['status']) && !$rep['status']) { - $this->logDebug( - 1, - __CLASS__."::".__FUNCTION__."($name) ".$rep['message'] - ); - return false; - } - $this->logDebug(8,print_r($rep,true)); - $branches = array(); - foreach ($rep['packageListings'] as $pack) { - $branches[$pack['collection']['branchname']] = $pack; - } - $this->logDebug(7,print_r($branches,true)); - return $branches; - } - - function getBranch($name, $refresh=false) - { - $branches = $this->getBranches($refresh); - - if (isset($branches[$name])) { - return $branches[$name]; - } - return false; - } - - function getCritPath($refresh=false) - { - $url="lists/critpath"; - $rep = ($refresh ? false : $this->cache->get($url, __CLASS__)); - if ($rep) { - $this->logDebug( - 2, - __CLASS__."::".__FUNCTION__." get from cache" - ); - } else { - $rep =$this->sendRequest($url); - $this->cache->save($rep, $url, __CLASS__); - $this->logDebug( - 2, - __CLASS__."::".__FUNCTION__." save to cache" - ); - } - return $rep['pkgs']; - } -} -?> +?> \ No newline at end of file diff --git a/class/FedoraPkgdb.php b/class/FedoraPkgdb.php new file mode 100644 index 0000000..c2085b0 --- /dev/null +++ b/class/FedoraPkgdb.php @@ -0,0 +1,142 @@ + + * + * @category Main + * @package FedoraClient + * + * @author Remi Collet + * @author Johan Cwiklinski + * @copyright 2010 Remi Collet + * @license http://www.gnu.org/licenses/lgpl-2.1.txt LGPL License 2.1 or (at your option) any later version + * @link http://github.com/remicollet/rpmphp/ + * @since The begining of times. + */ + +class FedoraPkgdb extends FedoraClient +{ + + function __construct (array $options=array()) + { + parent::__construct('https://admin.fedoraproject.org/pkgdb/', $options); + $this->logDebug( + 3, + __CLASS__."::".__FUNCTION__ + ); + } + + function getBranches($refresh=false) + { + $rep = ($refresh ? false : $this->cache->get(__FUNCTION__, __CLASS__)); + if ($rep) { + $this->logDebug( + 2, + __CLASS__."::".__FUNCTION__."() get from cache" + ); + } else { + $rep =$this->sendRequest('collections'); + $this->cache->save($rep, __FUNCTION__, __CLASS__); + $this->logDebug( + 2, + __CLASS__."::".__FUNCTION__."() save to cache" + ); + } + + $branches = array(); + if (isset($rep['collections'])) { + foreach ($rep['collections'] as $coll) { + if (isset($coll[0]['branchname'])) { + $branches[$coll[0]['branchname']] = $coll[0]; + } + } + } + return $branches; + } + + function getPackageInfo($name, $refresh=false) + { + $url="acls/name/$name"; + $rep = ($refresh ? false : $this->cache->get($url, __CLASS__)); + if ($rep) { + $this->logDebug( + 2, + __CLASS__."::".__FUNCTION__."($name) get from cache" + ); + } else { + $rep =$this->sendRequest($url); + $this->cache->save($rep, $url, __CLASS__); + $this->logDebug( + 2, + __CLASS__."::".__FUNCTION__."($name) save to cache" + ); + } + + if (isset($rep['status']) && !$rep['status']) { + $this->logDebug( + 1, + __CLASS__."::".__FUNCTION__."($name) ".$rep['message'] + ); + return false; + } + $this->logDebug(8,print_r($rep,true)); + $branches = array(); + foreach ($rep['packageListings'] as $pack) { + $branches[$pack['collection']['branchname']] = $pack; + } + $this->logDebug(7,print_r($branches,true)); + return $branches; + } + + function getBranch($name, $refresh=false) + { + $branches = $this->getBranches($refresh); + + if (isset($branches[$name])) { + return $branches[$name]; + } + return false; + } + + function getCritPath($refresh=false) + { + $url="lists/critpath"; + $rep = ($refresh ? false : $this->cache->get($url, __CLASS__)); + if ($rep) { + $this->logDebug( + 2, + __CLASS__."::".__FUNCTION__." get from cache" + ); + } else { + $rep =$this->sendRequest($url); + $this->cache->save($rep, $url, __CLASS__); + $this->logDebug( + 2, + __CLASS__."::".__FUNCTION__." save to cache" + ); + } + return $rep['pkgs']; + } +} +?> \ No newline at end of file diff --git a/class/TableIterator.php b/class/TableIterator.php new file mode 100644 index 0000000..9f5889b --- /dev/null +++ b/class/TableIterator.php @@ -0,0 +1,287 @@ +. + * + * @category Main + * @package RPMPHP + * + * @author Remi Collet + * @author Johan Cwiklinski + * @copyright 2010 Remi Collet + * @license http://www.gnu.org/licenses/agpl-3.0-standalone.html AGPL License 3.0 or (at your option) any later version + * @link http://github.com/remicollet/rpmphp/ + * @since The begining of times. +*/ + +/** + * Helper for simple query => use directly or through CommonTable::request() + * + * Freely inspired from DBmysqlIterator class from GLPI + * (already written by Remi, and ported to PDO) + * See http://www.glpi-project.org/ + */ +class TableIterator implements Iterator +{ + private $con; + private $sql; + private $res = false; + private $row; + private $pos; + + /** + * Constructor + * + * @param CommonDBTM $dbconnexion Database Connnexion + * (must be a CommonDBTM object) + * @param string $table table name + * @param string|array $crit string or array of filed/values, + * ex array("id"=>1), if empty => all rows + */ + function __construct (PDO $dbconnexion, $table, $crit='') + { + + $this->conn = $dbconnexion; + if (is_string($table) && strpos($table, " ")) { + $this->sql = $table; + } else { + // check field, orderby, limit, start in criterias + $field=""; + $orderby=""; + $limit=0; + $start=0; + + if (is_array($crit) && count($crit)) { + foreach ($crit as $key => $val) { + if ($key==="FIELDS") { + $field = $val; + unset($crit[$key]); + } else if ($key==="ORDER") { + $orderby = $val; + unset($crit[$key]); + } else if ($key==="LIMIT") { + $limit = $val; + unset($crit[$key]); + } else if ($key==="START") { + $start = $val; + unset($crit[$key]); + } + } + } + + // SELECT field list + if (is_array($field)) { + $this->sql = ""; + foreach ($field as $t => $f) { + if (is_numeric($t)) { + $this->sql .= (empty($this->sql) + ? "SELECT " : ",") . $f; + } else if (is_array($f)) { + $this->sql .= (empty($this->sql) + ? "SELECT $t." : ",$t.") . implode(",$t.", $f); + } else { + $this->sql .= (empty($this->sql) + ? "SELECT " : ",") . "$t.$f"; + } + } + } else if (empty($field)) { + $this->sql = "SELECT *"; + } else { + $this->sql = "SELECT `$field`"; + } + // FROM table list + if (is_array($table)) { + $this->sql .= " FROM `".implode("`, `", $table)."`"; + } else { + $this->sql .= " FROM `$table`"; + } + // WHERE criteria list + if (!empty($crit)) { + print_r($crit); + $this->sql .= " WHERE ".$this->_analyseCrit($crit); + } + // ORDER BY + if (is_array($orderby)) { + $this->sql .= " ORDER BY `".implode("`, `", $orderby)."`"; + } else if (!empty($orderby)) { + $this->sql .= " ORDER BY `$orderby`"; + } + if (is_numeric($limit) && $limit>0) { + $this->sql .= " LIMIT $limit"; + if (is_numeric($start) && $start>0) { + $this->sql .= " OFFSET $start"; + } + } + } + //echo "SQL: ".$this->sql."\n"; + $this->res = $this->conn->prepare($this->sql); + if ($this->res===false) { + $err = $this->db->errorInfo(); + throw new Exception($err[2]); + } + + $this->pos = -1; + } + + /** + * Class destructor + */ + function __destruct () + { + if ($this->res) { + $this->res->closeCursor(); + } + } + + /** + * Build WHERE clause + * + * @param TODO $crit To document + * @param TODO $bool To document + * + * @return To document + */ + private function _analyseCrit ($crit, $bool="AND") + { + + if (!is_array($crit)) { + return $crit; + } + $ret = ""; + foreach ($crit as $name => $value) { + if (!empty($ret)) { + $ret .= " $bool "; + } + if (is_numeric($name)) { + // No Key case => recurse. + $ret .= "(" . $this->_analyseCrit($value, $bool) . ")"; + } else if ($name==="OR" || $name==="AND") { + // Binary logical operator + $ret .= "(" . $this->_analyseCrit($value, $name) . ")"; + } else if ($name==="NOT") { + // Uninary logicial operator + $ret .= " NOT (" . $this->_analyseCrit($value, "AND") . ")"; + } else if ($name==="FKEY") { + // Foreign Key condition + if (is_array($value) && count($value)==2) { + reset($value); + list($t1,$f1)=each($value); + list($t2,$f2)=each($value); + $ret .= (is_numeric($t1) ? "$f1" : "$t1.$f1") . "=" . + (is_numeric($t2) ? "$f2" : "$t2.$f2"); + } else { + trigger_error("BAD FOREIGN KEY", E_USER_ERROR); + } + } else if (is_array($value)) { + // Array of Value + $ret .= "$name IN ('". implode("','", $value)."')"; + } else if (is_null($value)) { + // NULL condition + $ret .= "$name IS NULL"; + } else if (is_numeric($value)) { + // Integer + $ret .= "$name=$value"; + } else { + // String + $ret .= "$name='$value'"; + } + } + return $ret; + } + + /** + * To document + * + * @return To document + */ + public function rewind () + { + + if ($this->res && $this->pos>=0) { + $this->res->closeCursor(); + $this->pos = -1; + } + + if ($this->res && $this->pos<0) { + if (!$this->res->execute()) { + $err = $this->res->errorInfo(); + throw new Exception($err[2]); + } + } + return $this->next(); + } + + /** + * To document + * + * @return To document + */ + public function current() + { + return $this->row; + } + + /** + * To document + * + * @return To document + */ + public function key() + { + return (isset($this->row["id"]) ? $this->row["id"] : $this->pos); + } + + /** + * To document + * + * @return To document + */ + public function next() + { + if (!$this->res) { + return false; + } + $this->row = $this->res->fetch(PDO::FETCH_ASSOC); + $this->pos++; + return $this->row; + } + + /** + * To document + * + * @return To document + */ + public function valid() + { + return $this->res && $this->row; + } + + /** + * To document + * + * @return To document + */ + public function numrows() + { + return ($this->res ? $this->res->rowCount() : 0); + } +} +?> \ No newline at end of file diff --git a/class/TablePearRepo.php b/class/TablePearRepo.php new file mode 100644 index 0000000..b0b6465 --- /dev/null +++ b/class/TablePearRepo.php @@ -0,0 +1,97 @@ +. + * + * @category Main + * @package RPMPHP + * + * @author Remi Collet + * @author Johan Cwiklinski + * @copyright 2010 Remi Collet + * @license http://www.gnu.org/licenses/agpl-3.0-standalone.html AGPL License 3.0 or (at your option) any later version + * @link http://github.com/remicollet/rpmphp/ + * @since The begining of times. +*/ + +class TablePearRepo extends CommonTable +{ + + /** + * Instanciate a TablePearRepo to manage pearrepo table + * + * @param object $db PDO instance of the DB connection + */ + function __construct($db) + { + parent::__construct($db, 'pearrepo'); + } + + /** + * Create the table and populate it with known repo + * + * @return void + */ + protected function createTable() + { + + // Table schema + $sql = "CREATE TABLE `pearrepo` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `alias` varchar(30) NOT NULL, + `url` varchar(255) NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `alias` (`alias`) + ) DEFAULT CHARSET=utf8"; + + $this->exec($sql); + + // Some known repo, other could be add manually + // no reply from "phpdb" => "pear.phpdb.org" + $channels = array( + "pear" => "pear.php.net" + ,"doctrine" => "pear.phpdoctrine.org" + ,"ezc" => "components.ez.no" + ,"pdepend" => "pear.pdepend.org" + ,"phing" => "pear.phing.info" + ,"phpmd" => "pear.phpmd.org" + ,"phpunit" => "pear.phpunit.de" + ,"swift" => "pear.swiftmailer.org" + ,"symphony" => "pear.symfony-project.com" + ); + + foreach ($channels as $alias => $url) { + $this->add(array('alias'=>$alias, 'url'=>$url)); + } + } + + /** + * Retrieve all the known repository + * + * @return hastable of alias => url + */ + function getAllRepo() + { + return $this->getAllArray('alias', 'url'); + } +} + +?> \ No newline at end of file diff --git a/include/main.php b/include/main.php index b97db02..265d20c 100644 --- a/include/main.php +++ b/include/main.php @@ -37,7 +37,6 @@ define('RPMPHP_VERSION', '1.0.0-dev'); require 'config.php'; require 'Smarty/Smarty.class.php'; -require dirname(__FILE__).'/../class/FedoraClient.php'; $smarty = new Smarty(); @@ -51,6 +50,18 @@ $ariane[] = array ( 'text' => 'Reports home' ); +/** + * Manage class autoload + * + * @param string $classname + * + * @return void + */ +function __autoload($classname) +{ + require dirname(__FILE__).'/../class/'.$classname.'.php'; +} + /** * Get repositories list * -- cgit From e671ebe6e140b4fd2bf671c07b0b59aa070ea23b Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Tue, 8 Jun 2010 19:24:25 +0200 Subject: add CommonTable->delete() method --- class/CommonTable.php | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/class/CommonTable.php b/class/CommonTable.php index 57c701b..f08b29e 100644 --- a/class/CommonTable.php +++ b/class/CommonTable.php @@ -73,6 +73,8 @@ abstract class CommonTable * Execute an SQL statement (INSERT, DELETE, ...) * * @param string $sql The SQL clause + * + * @param integer number of affected rows */ protected function exec($sql) { @@ -81,14 +83,17 @@ abstract class CommonTable $err = $this->db->errorInfo(); throw new Exception($err[2]); } + return $res; } /** * Add a new row in the table * * @param hashtable $fields hashtable of fieldname => value + * + * @return integer primary key of inserted row */ - protected function add(array $fields) + public function add(array $fields) { $col = array(); $val = array(); @@ -99,14 +104,47 @@ abstract class CommonTable } else if (is_numeric($value)) { $val[] = $value; } else { - $val[] = "'$value'"; + $val[] = "'".addslashes($value)."'"; } } $sql = "INSERT INTO `".$this->table."` (".implode(',', $col).") VALUE (".implode(',', $val).")"; $this->exec($sql); + + $id = $this->db->lastInsertId(); + + return $id; } + /** + * Delete a row in the table + * + * @param hashtable $crit of key => value + * + * @return integer : number of row deleted + */ + public function delete(array $crit) + { + $sql = "DELETE FROM `".$this->table."` "; + + $link="WHERE"; + foreach ($crit as $key => $value) { + $sql .= " $link `$key`"; + + if (is_null($value)) { + $sql .= 'IS NULL'; + } else if (is_numeric($value)) { + $sql .= '='.$value; + } else { + $sql .= "='".addslashes($value)."'"; + } + + $link = "AND"; + } + $nb = $this->exec($sql); + + return $nb; + } /** * Create the table */ -- cgit From 53454761ed4d619b328d7cb5f628daafe127efb7 Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Tue, 8 Jun 2010 19:34:49 +0200 Subject: some docs --- class/CommonTable.php | 1 + class/TableIterator.php | 30 +++++++++++++++--------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/class/CommonTable.php b/class/CommonTable.php index f08b29e..1102c4d 100644 --- a/class/CommonTable.php +++ b/class/CommonTable.php @@ -145,6 +145,7 @@ abstract class CommonTable return $nb; } + /** * Create the table */ diff --git a/class/TableIterator.php b/class/TableIterator.php index 9f5889b..5c42d24 100644 --- a/class/TableIterator.php +++ b/class/TableIterator.php @@ -52,7 +52,7 @@ class TableIterator implements Iterator * * @param CommonDBTM $dbconnexion Database Connnexion * (must be a CommonDBTM object) - * @param string $table table name + * @param string $table table name or complete SQL request * @param string|array $crit string or array of filed/values, * ex array("id"=>1), if empty => all rows */ @@ -154,8 +154,8 @@ class TableIterator implements Iterator /** * Build WHERE clause * - * @param TODO $crit To document - * @param TODO $bool To document + * @param string|array $crit To document + * @param string $bool logical operator between criteria * * @return To document */ @@ -208,9 +208,9 @@ class TableIterator implements Iterator } /** - * To document + * Go to the begin of the request (launch it) * - * @return To document + * @return hastable|false : next row */ public function rewind () { @@ -230,9 +230,9 @@ class TableIterator implements Iterator } /** - * To document + * Retrieve current row * - * @return To document + * @return hastable|false */ public function current() { @@ -240,9 +240,9 @@ class TableIterator implements Iterator } /** - * To document + * Retrieve key of current row * - * @return To document + * @return mixed */ public function key() { @@ -250,9 +250,9 @@ class TableIterator implements Iterator } /** - * To document + * Retrieve next row * - * @return To document + * @return hastable|false */ public function next() { @@ -265,9 +265,9 @@ class TableIterator implements Iterator } /** - * To document + * Is current row defined * - * @return To document + * @return boolean */ public function valid() { @@ -275,9 +275,9 @@ class TableIterator implements Iterator } /** - * To document + * Compute number of rows * - * @return To document + * @return integer */ public function numrows() { -- cgit From 3c22a10c40837b1695ae04d120bbfc68da01059c Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Tue, 8 Jun 2010 20:04:48 +0200 Subject: add TableRRepo class and use it --- class/CommonTable.php | 20 +++++++- class/TablePearRepo.php | 2 +- class/TableRRepo.php | 120 ++++++++++++++++++++++++++++++++++++++++++++++++ refresh.php | 43 ++--------------- 4 files changed, 142 insertions(+), 43 deletions(-) create mode 100644 class/TableRRepo.php diff --git a/class/CommonTable.php b/class/CommonTable.php index 1102c4d..e049188 100644 --- a/class/CommonTable.php +++ b/class/CommonTable.php @@ -81,7 +81,7 @@ abstract class CommonTable $res = $this->db->exec($sql); if ($res===false) { $err = $this->db->errorInfo(); - throw new Exception($err[2]); + throw new Exception("\nSQL: $sql\nERROR: ".$err[2]."\nCODE:"); } return $res; } @@ -197,7 +197,7 @@ abstract class CommonTable * * @return hashtable */ - public function getAllArray($fieldkey, $fieldvalue) + public function getHashtable($fieldkey, $fieldvalue) { $crit = array('FIELDS' => array($fieldkey, $fieldvalue), 'ORDER' => $fieldkey); @@ -207,6 +207,22 @@ abstract class CommonTable } return $tab; } + + /** + * Retrieve a big array with all date from the table + * + * @param array|string $crit for the request + * + * @return array, index is rowid, value is a hastable + */ + public function getArray($crit='') + { + $tab = array(); + foreach ($this->request($crit) as $id => $data) { + $tab[$id] = $data; + } + return $tab; + } } ?> \ No newline at end of file diff --git a/class/TablePearRepo.php b/class/TablePearRepo.php index b0b6465..6ff3eaa 100644 --- a/class/TablePearRepo.php +++ b/class/TablePearRepo.php @@ -90,7 +90,7 @@ class TablePearRepo extends CommonTable */ function getAllRepo() { - return $this->getAllArray('alias', 'url'); + return $this->getHashtable('alias', 'url'); } } diff --git a/class/TableRRepo.php b/class/TableRRepo.php new file mode 100644 index 0000000..75570c3 --- /dev/null +++ b/class/TableRRepo.php @@ -0,0 +1,120 @@ +. + * + * @category Main + * @package RPMPHP + * + * @author Remi Collet + * @author Johan Cwiklinski + * @copyright 2010 Remi Collet + * @license http://www.gnu.org/licenses/agpl-3.0-standalone.html AGPL License 3.0 or (at your option) any later version + * @link http://github.com/remicollet/rpmphp/ + * @since The begining of times. +*/ + +class TableRRepo extends CommonTable +{ + + /** + * Instanciate a TablePearRepo to manage pearrepo table + * + * @param object $db PDO instance of the DB connection + */ + function __construct($db) + { + parent::__construct($db, 'rrepo'); + } + + /** + * Create the table and populate it with known repo + * + * @return void + */ + protected function createTable() + { + + // Table schema + $sql = "CREATE TABLE `rrepo` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(30) NOT NULL, + `state` varchar(30) NOT NULL default 'stable', + `url` varchar(255) NOT NULL, + `active` tinyint NOT NULL default 1, + PRIMARY KEY (`id`), + UNIQUE KEY `name_state` (`name`,`state`) + ) DEFAULT CHARSET=utf8"; + + $this->exec($sql); + + // Some known repo, other could be add manually + $repos = array( + array( + "name" => "R", + "url" => "http://cran.at.r-project.org/src/contrib/PACKAGES" + ), + array( + "name" => "biocp", + "url" => "http://www.bioconductor.org/packages/release/bioc/src/contrib/PACKAGES" + ), + array( + "name" => "biocp", + "state" => "unstable", + "url" => "http://www.bioconductor.org/packages/devel/bioc/src/contrib/PACKAGES" + ), + array( + "name" => "bioca", + "url" => "http://www.bioconductor.org/packages/release/data/annotation/src/contrib/PACKAGES" + ), + array( + "name" => "bioca", + "state" => "unstable", + "url" => "http://www.bioconductor.org/packages/devel/data/annotation/src/contrib/PACKAGES" + ), + array( + "name" => "bioce", + "url" => "http://www.bioconductor.org/packages/release/data/experiment/src/contrib/PACKAGES" + ), + array( + "name" => "bioce", + "state" => "unstable", + "url" => "http://www.bioconductor.org/packages/devel/data/experiment/src/contrib/PACKAGES" + ) + ); + + foreach ($repos as $repo) { + $this->add($repo); + } + } + + /** + * Retrieve all the known repository + * + * @return hastable of alias => url + */ + function getAllRepo() + { + return $this->getArray(array('active'=>1)); + } +} + +?> \ No newline at end of file diff --git a/refresh.php b/refresh.php index 53aef84..7547883 100644 --- a/refresh.php +++ b/refresh.php @@ -508,47 +508,10 @@ try { // ------------------------------------------------------------------- if ($_SERVER['argc']==1 || in_array('R', $_SERVER['argv'])) { - - $repos = array( - array( - "name" => "R", - "state" => "stable", - "url" => "http://cran.at.r-project.org/src/contrib/PACKAGES" - ), - //array("name"=>"R-forge","state"=>"unstable", "url" => "http://r-forge.r-project.org/src/contrib/PACKAGES"), - array( - "name" => "biocp", - "state" => "stable", - "url" => "http://www.bioconductor.org/packages/release/bioc/src/contrib/PACKAGES" - ), - array( - "name" => "biocp", - "state" => "unstable", - "url" => "http://www.bioconductor.org/packages/devel/bioc/src/contrib/PACKAGES" - ), - array( - "name" => "bioca", - "state" => "stable", - "url" => "http://www.bioconductor.org/packages/release/data/annotation/src/contrib/PACKAGES" - ), - array( - "name" => "bioca", - "state" => "unstable", - "url" => "http://www.bioconductor.org/packages/devel/data/annotation/src/contrib/PACKAGES" - ), - array( - "name" => "bioce", - "state" => "stable", - "url" => "http://www.bioconductor.org/packages/release/data/experiment/src/contrib/PACKAGES" - ), - array( - "name" => "bioce", - "state" => "unstable", - "url" => "http://www.bioconductor.org/packages/devel/data/experiment/src/contrib/PACKAGES" - ) - ); +// $tottot=0; - foreach ($repos as $repo) { + $pear = new TableRRepo($db); + foreach ($pear->request() as $repo) { echo date("r : ") . "Reading " . $repo["name"] . " (" . $repo["state"] . ")\n"; $index = @file_get_contents($repo["url"]); -- cgit From 825c4679551854f48c6c6b67e5c5b7f7322d24d2 Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Tue, 8 Jun 2010 20:25:59 +0200 Subject: improves previous, add active field to pearrepo table --- class/CommonTable.php | 8 +++--- class/TableIterator.php | 1 - class/TablePearRepo.php | 67 ++++++++++++++++++++++++++++++++++++++----------- class/TableRRepo.php | 9 ++++--- refresh.php | 4 +-- 5 files changed, 66 insertions(+), 23 deletions(-) diff --git a/class/CommonTable.php b/class/CommonTable.php index e049188..8fd0471 100644 --- a/class/CommonTable.php +++ b/class/CommonTable.php @@ -194,13 +194,15 @@ abstract class CommonTable * * @param string $fieldkey name of the field to use as index * @param string $fieldvalue name of the field to use as value + * @param array $crit for request * * @return hashtable */ - public function getHashtable($fieldkey, $fieldvalue) + public function getHashtable($fieldkey, $fieldvalue, array $crit=array()) { - $crit = array('FIELDS' => array($fieldkey, $fieldvalue), - 'ORDER' => $fieldkey); + $crit['FIELDS'] = array($fieldkey, $fieldvalue); + $crit['ORDER'] = $fieldkey; + $tab = array(); foreach ($this->request($crit) as $data) { $tab[$data[$fieldkey]] = $data[$fieldvalue]; diff --git a/class/TableIterator.php b/class/TableIterator.php index 5c42d24..0903f37 100644 --- a/class/TableIterator.php +++ b/class/TableIterator.php @@ -115,7 +115,6 @@ class TableIterator implements Iterator } // WHERE criteria list if (!empty($crit)) { - print_r($crit); $this->sql .= " WHERE ".$this->_analyseCrit($crit); } // ORDER BY diff --git a/class/TablePearRepo.php b/class/TablePearRepo.php index 6ff3eaa..34a0c17 100644 --- a/class/TablePearRepo.php +++ b/class/TablePearRepo.php @@ -58,38 +58,77 @@ class TablePearRepo extends CommonTable `id` int(11) NOT NULL AUTO_INCREMENT, `alias` varchar(30) NOT NULL, `url` varchar(255) NOT NULL, + `active` tinyint NOT NULL default 1, PRIMARY KEY (`id`), - UNIQUE KEY `alias` (`alias`) + UNIQUE KEY `alias` (`alias`), + KEY `active` (`active`) ) DEFAULT CHARSET=utf8"; $this->exec($sql); // Some known repo, other could be add manually - // no reply from "phpdb" => "pear.phpdb.org" + // no reply from "" => "" $channels = array( - "pear" => "pear.php.net" - ,"doctrine" => "pear.phpdoctrine.org" - ,"ezc" => "components.ez.no" - ,"pdepend" => "pear.pdepend.org" - ,"phing" => "pear.phing.info" - ,"phpmd" => "pear.phpmd.org" - ,"phpunit" => "pear.phpunit.de" - ,"swift" => "pear.swiftmailer.org" - ,"symphony" => "pear.symfony-project.com" + array( + 'alias' => 'pear', + 'url' => 'pear.php.net' + ), + array( + 'alias' => 'doctrine', + 'url' => 'pear.phpdoctrine.org' + ), + array( + 'alias' => 'ezc', + 'url' => 'components.ez.no' + ), + array( + 'alias' => 'pdepend', + 'url' => 'pear.pdepend.org' + ), + array( + 'alias' => 'phing', + 'url' => 'pear.phing.info' + ), + array( + 'alias' => 'phpmd', + 'url' => 'pear.phpmd.org' + ), + array( + 'alias' => 'phpunit', + 'url' => 'pear.phpunit.de' + ), + array( + 'alias' => 'swift', + 'url' => 'pear.swiftmailer.org' + ), + array( + 'alias' => 'symphony', + 'url' => 'pear.symfony-project.com' + ), + array( + 'alias' => 'phpdb', + 'active' => 0, + 'url' => 'pear.phpdb.org' + ) ); - foreach ($channels as $alias => $url) { - $this->add(array('alias'=>$alias, 'url'=>$url)); + foreach ($channels as $channel) { + $this->add($channel); } } /** * Retrieve all the known repository * + * @param boolean $active true for only active repo (false for all) + * * @return hastable of alias => url */ - function getAllRepo() + function getAllRepo($active=true) { + if ($active) { + return $this->getHashtable('alias', 'url', array('active'=>1)); + } return $this->getHashtable('alias', 'url'); } } diff --git a/class/TableRRepo.php b/class/TableRRepo.php index 75570c3..884b4e3 100644 --- a/class/TableRRepo.php +++ b/class/TableRRepo.php @@ -61,7 +61,8 @@ class TableRRepo extends CommonTable `url` varchar(255) NOT NULL, `active` tinyint NOT NULL default 1, PRIMARY KEY (`id`), - UNIQUE KEY `name_state` (`name`,`state`) + UNIQUE KEY `name_state` (`name`,`state`), + KEY `active` (`active`) ) DEFAULT CHARSET=utf8"; $this->exec($sql); @@ -109,11 +110,13 @@ class TableRRepo extends CommonTable /** * Retrieve all the known repository * + * @param boolean $active true for only active repo (false for all) + * * @return hastable of alias => url */ - function getAllRepo() + function getAllRepo($active=true) { - return $this->getArray(array('active'=>1)); + return $this->getArray($active ? array('active'=>1) : ''); } } diff --git a/refresh.php b/refresh.php index 7547883..e839e93 100644 --- a/refresh.php +++ b/refresh.php @@ -510,8 +510,8 @@ try { if ($_SERVER['argc']==1 || in_array('R', $_SERVER['argv'])) { // $tottot=0; - $pear = new TableRRepo($db); - foreach ($pear->request() as $repo) { + $rrepo = new TableRRepo($db); + foreach ($rrepo->request() as $repo) { echo date("r : ") . "Reading " . $repo["name"] . " (" . $repo["state"] . ")\n"; $index = @file_get_contents($repo["url"]); -- cgit From e7493ab34e9db2226a5dd1d99938c04193ed88af Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Sun, 11 Jul 2010 18:23:16 +0200 Subject: urlencode package name, fixed #53 --- class/FedoraPkgdb.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/class/FedoraPkgdb.php b/class/FedoraPkgdb.php index c2085b0..ee54119 100644 --- a/class/FedoraPkgdb.php +++ b/class/FedoraPkgdb.php @@ -77,7 +77,7 @@ class FedoraPkgdb extends FedoraClient function getPackageInfo($name, $refresh=false) { - $url="acls/name/$name"; + $url="acls/name/".urlencode($name); $rep = ($refresh ? false : $this->cache->get($url, __CLASS__)); if ($rep) { $this->logDebug( -- cgit From 50f2de59ed8ea3f41530fc4f06c12ae1613416ab Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Sun, 11 Jul 2010 18:23:35 +0200 Subject: fix fedcli --- fedcli.php | 3 ++- include/main.php | 24 ++++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/fedcli.php b/fedcli.php index eb897c4..1d5f485 100755 --- a/fedcli.php +++ b/fedcli.php @@ -23,9 +23,10 @@ * See */ +define ('CLIONLY', true); +require 'include/main.php'; require 'Console/Getargs.php'; -require 'class/FedoraClient.php'; function Help() { echo "\nFedora Client Command Line usage\n\n"; diff --git a/include/main.php b/include/main.php index 265d20c..286cf4e 100644 --- a/include/main.php +++ b/include/main.php @@ -36,19 +36,23 @@ define('RPMPHP_VERSION', '1.0.0-dev'); require 'config.php'; -require 'Smarty/Smarty.class.php'; -$smarty = new Smarty(); +if (!defined('CLIONLY')) +{ + require 'Smarty/Smarty.class.php'; + + $smarty = new Smarty(); -$smarty->template_dir = 'smarty/templates/rpmphp'; -$smarty->compile_dir = 'smarty/templates_c'; -$smarty->cache_dir = 'smarty/cache'; -$smarty->config_dir = 'smarty/configs'; + $smarty->template_dir = 'smarty/templates/rpmphp'; + $smarty->compile_dir = 'smarty/templates_c'; + $smarty->cache_dir = 'smarty/cache'; + $smarty->config_dir = 'smarty/configs'; -$ariane[] = array ( - 'url' => './', - 'text' => 'Reports home' -); + $ariane[] = array ( + 'url' => './', + 'text' => 'Reports home' + ); +} /** * Manage class autoload -- cgit From e6b48f0d645a72b2b4898f3ddc90fc8437f9b1e2 Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Sun, 11 Jul 2010 18:43:12 +0200 Subject: add PkdDb link on zoom page, solves #58 --- class/FedoraClient.php | 2 +- class/FedoraPkgdb.php | 10 +++++++++- smarty/templates/rpmphp/zoom.tpl | 2 +- zoom.php | 2 ++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/class/FedoraClient.php b/class/FedoraClient.php index ed29f54..c1a10c5 100644 --- a/class/FedoraClient.php +++ b/class/FedoraClient.php @@ -44,7 +44,7 @@ require_once 'Cache/Lite.php'; abstract class FedoraClient { - private $url; + protected $url; private $agent; private $debug = 0; protected $cache; diff --git a/class/FedoraPkgdb.php b/class/FedoraPkgdb.php index ee54119..9f2a094 100644 --- a/class/FedoraPkgdb.php +++ b/class/FedoraPkgdb.php @@ -37,10 +37,13 @@ class FedoraPkgdb extends FedoraClient { + private $suburl; function __construct (array $options=array()) { parent::__construct('https://admin.fedoraproject.org/pkgdb/', $options); + $this->suburl = 'acls/name/'; + $this->logDebug( 3, __CLASS__."::".__FUNCTION__ @@ -75,9 +78,14 @@ class FedoraPkgdb extends FedoraClient return $branches; } + function getPackageURL($name) + { + return $this->url.$this->suburl.urlencode($name); + } + function getPackageInfo($name, $refresh=false) { - $url="acls/name/".urlencode($name); + $url=$this->suburl.urlencode($name); $rep = ($refresh ? false : $this->cache->get($url, __CLASS__)); if ($rep) { $this->logDebug( diff --git a/smarty/templates/rpmphp/zoom.tpl b/smarty/templates/rpmphp/zoom.tpl index 704c6b7..bcbb222 100644 --- a/smarty/templates/rpmphp/zoom.tpl +++ b/smarty/templates/rpmphp/zoom.tpl @@ -43,7 +43,7 @@ {if $k eq 'Bugzilla'} Active bugs - {elseif $k eq 'URL' or $k eq 'ViewVC'} + {elseif $k eq 'URL' or $k eq 'ViewVC' or $k eq 'PkgDB'} {$v} {else} {$v} diff --git a/zoom.php b/zoom.php index 45a4830..a7ec090 100644 --- a/zoom.php +++ b/zoom.php @@ -167,6 +167,8 @@ if ( !isset($name) || !$name ) { $summary['ViewVC'] = 'http://cvs.fedoraproject.org/viewvc/rpms/' . $name . '/'; + $summary['PkgDB'] = $fedcli->getPackageURL($name); + if (isset($_GET['pkgdb'])) { $fedpkg = $fedcli->getPackageInfo($name); } else { -- cgit From a5fdd45528a85457821ef4739efcd3fc778affde Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Sun, 11 Jul 2010 18:56:05 +0200 Subject: fix issue with using name containing special char. --- class/FedoraPkgdb.php | 2 +- pkgdb-ajax.php | 2 +- scripts/rpmphp.js | 2 +- smarty/templates/rpmphp/zoom.tpl | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/class/FedoraPkgdb.php b/class/FedoraPkgdb.php index 9f2a094..d170911 100644 --- a/class/FedoraPkgdb.php +++ b/class/FedoraPkgdb.php @@ -80,7 +80,7 @@ class FedoraPkgdb extends FedoraClient function getPackageURL($name) { - return $this->url.$this->suburl.urlencode($name); + return $this->url.$this->suburl.$name; } function getPackageInfo($name, $refresh=false) diff --git a/pkgdb-ajax.php b/pkgdb-ajax.php index f23b4cf..24d07b8 100644 --- a/pkgdb-ajax.php +++ b/pkgdb-ajax.php @@ -80,6 +80,6 @@ if ( !isset($name) || !$name ) { } } } - +error_log("RETURN:".print_r($fedpkg,true)); echo json_encode($fedpkg); ?> \ No newline at end of file diff --git a/scripts/rpmphp.js b/scripts/rpmphp.js index 18dfa32..6723438 100644 --- a/scripts/rpmphp.js +++ b/scripts/rpmphp.js @@ -30,7 +30,7 @@ function initZoomJS(name) if ( data.error ) { alert(data.error); } else { - $('#' + name + '_desc').empty().html(data.devel.package.description); + $('#' + 'pkgdb_desc').empty().html(data.devel.package.description); $.each( data, function(k, v){ diff --git a/smarty/templates/rpmphp/zoom.tpl b/smarty/templates/rpmphp/zoom.tpl index bcbb222..5841478 100644 --- a/smarty/templates/rpmphp/zoom.tpl +++ b/smarty/templates/rpmphp/zoom.tpl @@ -40,7 +40,7 @@ {foreach from=$summary key=k item=v} {$k}: - + {if $k eq 'Bugzilla'} Active bugs {elseif $k eq 'URL' or $k eq 'ViewVC' or $k eq 'PkgDB'} -- cgit From 6e6d59bf0677c6b2d5b92a6abdab15a7ecaf9947 Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Sun, 11 Jul 2010 19:10:36 +0200 Subject: move FEDORACLIENT_VERSION to class, so FedoraClient::VERSION --- class/FedoraClient.php | 5 ++--- fedcli.php | 2 +- index.php | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/class/FedoraClient.php b/class/FedoraClient.php index c1a10c5..f1ce8f6 100644 --- a/class/FedoraClient.php +++ b/class/FedoraClient.php @@ -35,8 +35,6 @@ * @since The begining of times. */ -define('FEDORACLIENT_VERSION', '0.1.0-dev'); - if (!function_exists('curl_version')) { die("curl extension required\n"); } @@ -44,6 +42,7 @@ require_once 'Cache/Lite.php'; abstract class FedoraClient { + const VERSION='0.1.0-dev'; protected $url; private $agent; private $debug = 0; @@ -65,7 +64,7 @@ abstract class FedoraClient if (isset($options['agent']) && !empty($options['agent'])) { $this->agent = $options['agent']; } else { - $this->agent = 'Fedora PHPClient/'.FEDORACLIENT_VERSION; + $this->agent = 'Fedora PHPClient/'.self::VERSION; } if (isset($options['debug']) && intval($options['debug'])>0) { $this->debug = intval($options['debug']); diff --git a/fedcli.php b/fedcli.php index 1d5f485..40b5b0c 100755 --- a/fedcli.php +++ b/fedcli.php @@ -137,7 +137,7 @@ function Version() { die (Console_Getargs::getHelp($config)."\n"); } - echo "PHP Fedora Client class version ".FEDORACLIENT_VERSION."\n"; + echo "PHP Fedora Client class version ".FedoraClient::VERSION."\n"; } $cmd = array_shift($_SERVER['argv']); diff --git a/index.php b/index.php index c4ddb24..0c163b1 100644 --- a/index.php +++ b/index.php @@ -70,7 +70,7 @@ try { ); } $smarty->assign('rpmphp_version', RPMPHP_VERSION); -$smarty->assign('fedcli_version', FEDORACLIENT_VERSION); +$smarty->assign('fedcli_version', FedoraClient::VERSION); $page_content = $smarty->fetch('index.tpl'); $smarty->assign('page_content', $page_content); -- cgit