From fe3d37364bbc072aedac951d4b2acc724ba7393a Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Sat, 7 Aug 2010 18:32:09 +0200 Subject: add Class TableRpmRepo and use it everywhere --- all.php | 39 ++++++++--- class/CommonTable.php | 40 +++++++++-- class/TableRpmRepo.php | 113 ++++++++++++++++++++++++++++++ include/main.php | 26 ------- index.php | 44 ++++++------ refresh.php | 184 ++++++++++++++++++++++++------------------------- rpm.php | 15 ++-- testdb.php | 38 ++++++++++ 8 files changed, 339 insertions(+), 160 deletions(-) create mode 100644 class/TableRpmRepo.php create mode 100644 testdb.php diff --git a/all.php b/all.php index 6976dc5..4a5a5ab 100644 --- a/all.php +++ b/all.php @@ -57,14 +57,27 @@ function report ($db) { global $what, $smarty; $packages = null; - $repos = listRepos($db); + $rpmrepo = new TableRpmRepo($db); + $repos = $rpmrepo->getAllRepoHash(); $smarty->assign('repos', $repos); if (substr($what, 0, 1)=='%') { - $sql=sprintf("SELECT DISTINCT name FROM rpm WHERE SUBSTRING(name,1,1)='%s' ORDER BY name", substr($what, 1, 1)); + $sql = sprintf( + "SELECT DISTINCT name + FROM rpm + WHERE SUBSTRING(name,1,1)='%s' + ORDER BY name", + substr($what, 1, 1) + ); } else { - $sql=sprintf("SELECT DISTINCT name FROM acls WHERE owner='%s' ORDER BY name", $what); + $sql = sprintf( + "SELECT DISTINCT name + FROM acls + WHERE owner='%s' + ORDER BY name", + $what + ); } //echo "

SQL=$sql

"; @@ -107,14 +120,16 @@ function report ($db) $display=""; $class=""; foreach ($repomain as $repo) { - if (isset($rpms[$repo->main."-".$repo->sub])) { - $rpm=$rpms[$repo->main."-".$repo->sub]; + if (isset($rpms[$repo['main']."-".$repo['sub']])) { + $rpm=$rpms[$repo['main']."-".$repo['sub']]; - $maxver = (isset($rpms["devel-"]) ? $rpms["devel-"]->ver : ""); + $maxver = (isset($rpms["devel-"]) + ? $rpms["devel-"]->ver + : ""); - switch ($repo->sub) { + switch ($repo['sub']) { case "base": - if (isset($rpms[$repo->main."-updates"])) { + if (isset($rpms[$repo['main']."-updates"])) { $display .= sprintf( "%s-%s
", $rpm->ver, @@ -126,7 +141,9 @@ function report ($db) $rpm->ver, $rpm->rel ); - $class = ($rpm->ver == $maxver ? "check" : "attn"); + $class = ($rpm->ver == $maxver + ? "check" + : "attn"); } break; case "": @@ -174,7 +191,9 @@ function report ($db) try { $db = new PDO ("mysql:dbname=" . MYBASE . ";host=" . MYHOST, MYUSER, MYPASS); - $sql="SELECT DISTINCT CONCAT('%',SUBSTRING(name,1,1)) as init FROM rpm ORDER BY init"; + $sql="SELECT DISTINCT CONCAT('%',SUBSTRING(name,1,1)) as init + FROM rpm + ORDER BY init"; $res=$db->query($sql); if ( $res ) { while ( $s = $res->fetchObject() ) { diff --git a/class/CommonTable.php b/class/CommonTable.php index 51c0cd6..a7d029d 100644 --- a/class/CommonTable.php +++ b/class/CommonTable.php @@ -146,10 +146,41 @@ abstract class CommonTable return $nb; } + /** + * Update a row in the table + * + * @param integer $id of the record + * @param hashtable $fields of key => value + * + * @return integer : number of row deleted + */ + public function update($id, array $fields) + { + $sql = "UPDATE `".$this->table."` "; + + $link = 'SET'; + foreach ($fields as $key => $value) { + $sql .= "$link `$key`="; + if (is_null($value)) { + $sql .= 'NULL'; + } else if (is_numeric($value)) { + $sql .= $value; + } else { + $sql .= "'".addslashes($value)."'"; + } + $link = ','; + } + $sql .= " WHERE `id`=".intval($id); + + $nb = $this->exec($sql); + + return $nb; + } + /** * Create the table - * - * @return void + * + * @return void */ abstract protected function createTable(); @@ -219,14 +250,15 @@ abstract class CommonTable * Retrieve a big array with all date from the table * * @param array|string $crit for the request + * @param string $key name of the key for the return array * * @return array, index is rowid, value is a hastable */ - public function getArray($crit='') + public function getArray($crit='', $key='id') { $tab = array(); foreach ($this->request($crit) as $id => $data) { - $tab[$id] = $data; + $tab[$data[$key]] = $data; } return $tab; } diff --git a/class/TableRpmRepo.php b/class/TableRpmRepo.php new file mode 100644 index 0000000..a8ae44f --- /dev/null +++ b/class/TableRpmRepo.php @@ -0,0 +1,113 @@ +. + * + * @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 TableRpmRepo extends CommonTable +{ + + /** + * Instanciate a TablePearRepo to manage pearrepo table + * + * @param object $db PDO instance of the DB connection + */ + function __construct($db) + { + parent::__construct($db, 'repo'); + } + + /** + * Create the table and populate it with known repo + * + * @return void + */ + protected function createTable() + { + // Table schema + $sql = "CREATE TABLE IF NOT EXISTS `repo` ( + `id` int(11) NOT NULL, + `main` varchar(16) NOT NULL, + `sub` varchar(16) NOT NULL, + `url` varchar(200) NOT NULL, + `stamp` int(11) DEFAULT NULL, + `active` tinyint(1) NOT NULL DEFAULT '1', + PRIMARY KEY (`ID`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8"; + $this->exec($sql); + + // add 'devel' repo, other could be add manually + $this->add( + array('id' => 999, + 'main' => 'devel', + 'sub' => '', + 'url' => 'http://download.fedora.redhat.com/pub/fedora/linux/development/rawhide/source/SRPMS/') + ); + } + + /** + * Retrieve all the known repository + * + * @param boolean $active true for only active repo (false for all) + * + * @return hastable of id => hastable + */ + function getAllRepo($active=true) + { + $crit = array("ORDER" => 'id'); + if ($active) { + $crit['active'] = 1; + } + return $this->getArray($crit); + } + + /** + * Retrieve all the known repository + * + * @param boolean $active true for only active repo (false for all) + * + * @return hastable of alias => url + */ + function getAllRepoHash($active=true) + { + $repos = $this->getAllRepo($active); + + $res = array(); + foreach ($repos as $repo) { + $res[$repo['main']][$repo['sub']] = $repo; + } + return $res; + } +} + +?> \ No newline at end of file diff --git a/include/main.php b/include/main.php index 1c3a863..5938618 100644 --- a/include/main.php +++ b/include/main.php @@ -65,30 +65,4 @@ function __autoload($classname) include dirname(__FILE__).'/../class/'.$classname.'.php'; } -/** -* Get repositories list -* -* @param object $db a reference to the database -* -* @return array -*/ -function listRepos($db) -{ - $repos = array(); - $res=$db->query("SELECT * FROM repo WHERE active=1 ORDER BY ID"); - if ( $res ) { - while ($repo = $res->fetchObject()) { - $repos[$repo->main][$repo->sub]=$repo; - } - } - // echo "
"; print_r($repos);echo "
"; - foreach ( $repos as $repomain ) { - foreach ($repomain as $repo) { - $repos[$repo->main][$repo->sub] = $repo; - break; - } - } - return $repos; -} - ?> \ No newline at end of file diff --git a/index.php b/index.php index 24d4fbd..03412ef 100644 --- a/index.php +++ b/index.php @@ -39,38 +39,38 @@ require 'include/main.php'; $smarty->assign('ariane', $ariane); $smarty->assign('page_title', 'Packages in Fedora repositories'); + try { $db = new PDO ("mysql:dbname=" . MYBASE . ";host=" . MYHOST, MYUSER, MYPASS); - $sql='SELECT * FROM repo ORDER BY ID'; - $res=$db->query($sql); - $repositories = null; - if ( $res ) { - for ( $i=0, $prev="xx"; - $repo=$res->fetchObject(); - $i++, $prev=$repo->main - ) { - $repositories[] = array( - 'repo_name' => ($repo->main != $prev ? $repo->main : " "), - 'active' => ($repo->main != $prev ? - ($repo->active ? "Yes" : "no") : - " " - ), - 'sub_name' => $repo->sub, - 'url' => $repo->url, - 'date' => ($repo->stamp - ? date("r", $repo->stamp) - : '') - ); - } - $smarty->assign('repositories', $repositories); + $repo = new TableRpmRepo($db); + + $prev = false; + $repositories = array(); + foreach ($repo->request(array('ORDER'=>'id')) as $repo) { + $repositories[] = array( + 'repo_name' => ($repo['main'] != $prev ? $repo['main'] : " "), + 'active' => ($repo['main'] != $prev ? + ($repo['active'] ? "Yes" : "no") : + " " + ), + 'sub_name' => $repo['sub'], + 'url' => $repo['url'], + 'date' => ($repo['stamp'] + ? date("r", $repo['stamp']) + : '') + ); + $prev = $repo['main']; } + $smarty->assign('repositories', $repositories); + } catch(PDOException $e) { $smarty->assign( 'error', sprintf("%s ERREUR : %s\n", date("r"), $e->getMessage()) ); } + $smarty->assign('rpmphp_version', RPMPHP_VERSION); $smarty->assign('fedcli_version', FedoraClient::VERSION); diff --git a/refresh.php b/refresh.php index aebeec2..4026f68 100644 --- a/refresh.php +++ b/refresh.php @@ -96,104 +96,100 @@ try { if ($_SERVER['argc']==1 || in_array('repo', $_SERVER['argv'])) { if (in_array('empty', $_SERVER['argv'])) { $res=$db->query("SELECT * FROM repo WHERE stamp IS NULL"); + $crit = array('stamp' => null); } else if (in_array('old', $_SERVER['argv'])) { $res=$db->query("SELECT * FROM repo"); + $crit = array(); } else { $res=$db->query("SELECT * FROM repo WHERE active=1"); + $crit = array('active' => 1); } - if (!$res) { - echo date("r : ") . "SQL ERROR = " . - implode(" ", $db->errorInfo()) . "\n"; - } else { - while ($row = $res->fetchObject()) { - echo date("r : ") . "REPOSITORY " . $row->main . " " . - $row->sub . "\n"; - $TimRemote = 0; - $repomd = simplexml_load_file($row->url . "repodata/repomd.xml"); - if ($repomd) { - foreach ($repomd->data as $data) { - if ($data->attributes()=="primary") { - $TimRemote = $data->timestamp; - $UrlRemote = $row->url . $data->location->attributes(); - } + $rpmrepo = new TableRpmRepo($db); + foreach ($rpmrepo->request($crit) as $row) { + echo date("r : ") . "REPOSITORY " . $row['main'] . " " . + $row['sub'] . "\n"; + $TimRemote = 0; + $repomd = @simplexml_load_file($row['url'] . "repodata/repomd.xml"); + if ($repomd) { + foreach ($repomd->data as $data) { + if ($data->attributes()=="primary") { + $TimRemote = $data->timestamp; + $UrlRemote = $row['url'] . $data->location->attributes(); } } - if ($TimRemote > $row->stamp) { - echo date("r : ") . "Loading $UrlRemote\n"; - - //$fic=gzopen("primary.xml.gz", "r"); - $fic=gzopen($UrlRemote, "r"); - if ($fic) { - $txt=""; - while ($buf=gzread($fic, 8196)) { - $txt .= $buf; - } - echo date("r : ") . "Read " . strlen($txt) . " bytes\n"; - gzclose($fic); - - $primary = simplexml_load_string($txt); - echo date("r : ") . "Read " . $primary->attributes() . - " packages\n"; - unset($txt); - - $sql = sprintf( - "DELETE FROM rpm WHERE repo_main='%s' AND repo_sub='%s'", - $row->main, - $row->sub - ); - $nb=$db->exec($sql); - if ($nb===false) { - echo date("r : ") . "SQL ERROR = " . - implode(" ", $db->errorInfo()) . "\n"; - } else { - echo date("r : ") . "Delete $nb packages\n"; - } + } + if (!$TimRemote) { + echo date("r : ") . "Can't read $UrlRemote\n"; + + } else if ($TimRemote > $row['stamp']) { + echo date("r : ") . "Loading $UrlRemote\n"; + + //$fic=gzopen("primary.xml.gz", "r"); + $fic=gzopen($UrlRemote, "r"); + if ($fic) { + $txt=""; + while ($buf=gzread($fic, 8196)) { + $txt .= $buf; + } + echo date("r : ") . "Read " . strlen($txt) . " bytes\n"; + gzclose($fic); + + $primary = simplexml_load_string($txt); + echo date("r : ") . "Read " . $primary->attributes() . + " packages\n"; + unset($txt); + + $sql = sprintf( + "DELETE FROM rpm WHERE repo_main='%s' AND repo_sub='%s'", + $row['main'], + $row['sub'] + ); + $nb=$db->exec($sql); + if ($nb===false) { + echo date("r : ") . "SQL ERROR = " . + implode(" ", $db->errorInfo()) . "\n"; + } else { + echo date("r : ") . "Delete $nb packages\n"; + } - $tot=0; - foreach ($primary->package as $package) { - if ($package->attributes()=='rpm') { - $ver = $package->version->attributes(); - $sql = sprintf( - "INSERT INTO rpm SET repo_main='%s', repo_sub='%s', name='%s', epoch='%s', ver='%s', rel='%s', summary='%s', url='%s'", - $row->main, - $row->sub, - $package->name, - $ver['epoch'], - $ver['ver'], - $ver['rel'], - addslashes($package->summary), - $package->url - ); - $nb=$db->exec($sql); - if ($nb) { - $tot+=$nb; - } else { - echo date("r : ") . "SQL ERROR = " . - implode(" ", $db->errorInfo()) . "\n"; - } + $tot=0; + foreach ($primary->package as $package) { + if ($package->attributes()=='rpm') { + $ver = $package->version->attributes(); + $sql = sprintf( + "INSERT INTO rpm + SET repo_main='%s', repo_sub='%s', name='%s', + epoch='%s', ver='%s', rel='%s', summary='%s', + url='%s'", + $row['main'], + $row['sub'], + $package->name, + $ver['epoch'], + $ver['ver'], + $ver['rel'], + addslashes($package->summary), + $package->url + ); + $nb=$db->exec($sql); + if ($nb) { + $tot+=$nb; + } else { + echo date("r : ") . "SQL ERROR = " . + implode(" ", $db->errorInfo()) . "\n"; } } - echo date("r : ") . "Write $tot packages\n"; + } + echo date("r : ") . "Write $tot packages\n"; - $sql=sprintf( - "UPDATE repo SET stamp=%d WHERE ID=%d", - $TimRemote, - $row->ID - ); - $nb=$db->exec($sql); - if ($nb===false) { - echo date("r : ") . "SQL ERROR = " . - implode(" ", $db->errorInfo()) . "\n"; - } + $rpmrepo->update($row['id'], array('stamp' =>$TimRemote)); - unset($primary); - } else { - echo date("r : ") . "ERROR : can't read $UrlRemote\n"; - } + unset($primary); } else { - echo date("r : ") . "no update needed : $TimRemote / " . - $row->stamp . "\n"; + echo date("r : ") . "ERROR : can't read $UrlRemote\n"; } + } else { + echo date("r : ") . "no update needed : $TimRemote / " . + $row['stamp'] . "\n"; } } } // If ask @@ -233,12 +229,14 @@ try { ) ) ); - $file = file_get_contents("http://pecl.php.net/xmlrpc.php", false, $context) or die ("Can't file_get_contents(http://pecl.php.net/xmlrpc.php)"); + $file = file_get_contents("http://pecl.php.net/xmlrpc.php", false, $context) + or die ("Can't file_get_contents(http://pecl.php.net/xmlrpc.php)"); $stable = xmlrpc_decode($file); if (xmlrpc_is_fault($stable)) { - echo date("r : ") . "ERROR xmlrpc: $stable[faultString] ($stable[faultCode])"; + echo date("r : ") . + "ERROR xmlrpc: $stable[faultString] ($stable[faultCode])"; } else { - $sql = sprintf("DELETE FROM upstream WHERE type='pecl' AND channel='pecl'"); + $sql = "DELETE FROM upstream WHERE type='pecl' AND channel='pecl'"; $nb=$db->exec($sql); if ($nb===false) { echo date("r : ") . "SQL ERROR = " . @@ -261,7 +259,6 @@ try { ); } echo date("r : ") . "Write $nb packages\n"; - // echo date("r : ") . "saved " . file_put_contents("cache/pecl-stable", serialize($stable)) . " bytes\n"; } // ------------------------------------------------------------------- @@ -277,7 +274,8 @@ try { ) ) ); - $file = file_get_contents("http://pecl.php.net/xmlrpc.php", false, $context) or die ("Can't file_get_contents(http://pecl.php.net/xmlrpc.php)"); + $file = file_get_contents("http://pecl.php.net/xmlrpc.php", false, $context) + or die ("Can't file_get_contents(http://pecl.php.net/xmlrpc.php)"); $unstable = xmlrpc_decode($file); if (xmlrpc_is_fault($unstable)) { echo date("r : ") . "ERROR xmlrpc: $stable[faultString] ". @@ -308,7 +306,7 @@ try { if ($_SERVER['argc']==1 || in_array('pear', $_SERVER['argv'])) { echo date("r : ") . "PEAR reading channels\n"; $pear = new TablePearRepo($db); - $channels = $pear->getAllRepo(); + $channels = $pear->getAllRepo(true); try { $nbtot=0; @@ -533,7 +531,8 @@ try { } } $results=array(); - if (preg_match_all('/Package: *(.*)\nVersion: *(.*)\n/i', $index, $results, PREG_SET_ORDER)) { + $pat = '/Package: *(.*)\nVersion: *(.*)\n/i'; + if (preg_match_all($pat, $index, $results, PREG_SET_ORDER)) { $tot=0; foreach ($results as $result) { //echo $result[1]." = ".$result[2]."\n"; @@ -610,7 +609,8 @@ try { $line[$i]=trim($line[$i]); } $sql=sprintf( - "INSERT INTO `acls` SET collection='%s', name='%s', summary='%s'", + "INSERT INTO `acls` + SET collection='%s', name='%s', summary='%s'", $line[0], $line[1], addslashes($line[2]) diff --git a/rpm.php b/rpm.php index 8b140a8..37c54b5 100644 --- a/rpm.php +++ b/rpm.php @@ -61,7 +61,8 @@ function report($db, $type) { global $what, $smarty; $packages = null; - $repos = listRepos($db); + $rpmrepo = new TableRpmRepo($db); + $repos = $rpmrepo->getAllRepoHash(); $smarty->assign('repos', $repos); $i=0; @@ -131,24 +132,26 @@ function report($db, $type) ' (' . $upstream->state . ')'; } - $verup = strtolower($upstream->stable ? $upstream->stable : $upstream->unstable); + $verup = strtolower( + $upstream->stable ? $upstream->stable : $upstream->unstable + ); $versions = null; foreach ( $repos as $repomain ) { $display=""; $class=""; foreach ( $repomain as $k=>$repo ) { - if ( isset($rpms[$repo->main."-".$repo->sub]) ) { - $rpm=$rpms[$repo->main."-".$repo->sub]; + if ( isset($rpms[$repo['main']."-".$repo['sub']]) ) { + $rpm=$rpms[$repo['main']."-".$repo['sub']]; $verpm=$rpm->ver; if (preg_match("/\.((beta|RC)\d*)\./i", $rpm->rel, $res)) { $verpm .= strtolower($res[1]); } - switch ($repo->sub) { + switch ($repo['sub']) { case "base": - if ( isset($rpms[$repo->main."-updates"]) ) { + if ( isset($rpms[$repo['main']."-updates"]) ) { $display .= sprintf( "%s-%s
", $rpm->ver, diff --git a/testdb.php b/testdb.php new file mode 100644 index 0000000..3454245 --- /dev/null +++ b/testdb.php @@ -0,0 +1,38 @@ +getAllRepo()); + +echo "PEAR\n"; + +$pear = new TablePearRepo($db); +print_r($pear->getAllRepo()); + +$pear = new TablePearRepo($db); +print_r($pear->getAllRepo(false)); + +$a=$pear->add(array('alias'=>'test', 'url'=>'testurl')); +echo "Ajout : $a\n"; + +$b=$pear->delete(array('id'=>$a)); +echo "Del : $b\n"; + +echo "RPM:"; +$rpm = new TableRpmRepo($db); + +if ($rpm->update(999, array('stamp'=>1))) +{ + echo "Update 1 ok\n"; +} +print_r($rpm->getAllRepo()); +if ($rpm->update(999, array('stamp'=>2))) +{ + echo "Update 2 ok\n"; +} +$all = $rpm->getAllRepo(); +print_r(array_pop($all)); +?> -- cgit