From 5cd7540f6d5aa824106ab7e9171fd36ed380bacd Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Mon, 1 Nov 2010 08:05:30 +0100 Subject: Create TableRpm class and use it in refresh --- class/TableRpm.php | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ refresh.php | 54 +++++++++++++++++--------------------------- testdb.php | 2 ++ 3 files changed, 89 insertions(+), 33 deletions(-) create mode 100644 class/TableRpm.php diff --git a/class/TableRpm.php b/class/TableRpm.php new file mode 100644 index 0000000..8a6b39b --- /dev/null +++ b/class/TableRpm.php @@ -0,0 +1,66 @@ +. + * + * @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 TableRpm extends CommonTable +{ + /** + * Create the table and populate it with known repo + * + * @return void + */ + protected function createTable() + { + // Table schema + $sql = "CREATE TABLE IF NOT EXISTS `rpm` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(100) NOT NULL, + `arch` varchar(20) DEFAULT NULL, + `epoch` varchar(20) NOT NULL, + `ver` varchar(20) NOT NULL, + `rel` varchar(40) NOT NULL, + `summary` varchar(200) NOT NULL, + `url` varchar(200) NOT NULL, + `location` varchar(200) DEFAULT NULL, + `repo_main` varchar(16) NOT NULL, + `repo_sub` varchar(16) NOT NULL, + PRIMARY KEY (`id`), + KEY `name` (`name`), + KEY `repo` (`repo_main`, `repo_sub`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"; + $this->exec($sql); + } +} +?> \ No newline at end of file diff --git a/refresh.php b/refresh.php index 539e1a5..4ab482a 100644 --- a/refresh.php +++ b/refresh.php @@ -36,6 +36,8 @@ */ chdir(dirname($_SERVER["SCRIPT_FILENAME"])); +date_default_timezone_set('Europe/Paris'); + if (isset($_SERVER["SERVER_NAME"])) { echo "
";
     ini_set("max_execution_time", "0");
@@ -62,16 +64,15 @@ 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);
         }
         $rpmrepo = new TableRpmRepo($db);
+        $rpmtable = new TableRpm($db);
+
         foreach ($rpmrepo->request($crit) as $row) {
             echo date("r : ") . "REPOSITORY " . $row['main'] . " " .
                 $row['sub'] . "\n";
@@ -106,43 +107,30 @@ try {
                         " packages\n";
                     unset($txt);
 
-                    $sql = sprintf(
-                        "DELETE FROM rpm WHERE repo_main='%s' AND repo_sub='%s'",
-                        $row['main'],
-                        $row['sub']
+                    $crit = array(
+                        'repo_main' => $row['main'],
+                        'repo_sub'  => $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";
-                    }
+                    $nb = $rpmtable->delete($crit);
+                    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
+
+                            $input = array(
+                                'repo_main' => $row['main'],
+                                'repo_sub'  => $row['sub'],
+                                'name'      => $package->name,
+                                'epoch'     => $ver['epoch'],
+                                'ver'       => $ver['ver'],
+                                'rel'       => $ver['rel'],
+                                'summary'   => $package->summary,
+                                'url'       => $package->url
                             );
-                            $nb=$db->exec($sql);
-                            if ($nb) {
-                                $tot+=$nb;
-                            } else {
-                                echo date("r : ") . "SQL ERROR = " .
-                                    implode(" ", $db->errorInfo()) . "\n";
+                            if ($rpmtable->add($input)) {
+                                $tot++;
                             }
                         }
                     }
diff --git a/testdb.php b/testdb.php
index 3523fff..cc7c301 100644
--- a/testdb.php
+++ b/testdb.php
@@ -88,4 +88,6 @@ echo "request(test) : ";
 foreach($up->request(array('type'=>'test', 'ORDER'=>'name')) as $upstr) {
     print_r($upstr);
 }
+
+$rpm = new TableRpm($db);
 ?>
-- 
cgit