diff options
author | Remi Collet <fedora@famillecollet.com> | 2010-11-01 10:06:52 +0100 |
---|---|---|
committer | Remi Collet <fedora@famillecollet.com> | 2010-11-01 10:06:52 +0100 |
commit | 93c01077b80cc8d6044d1ec82e981c9f2b26f362 (patch) | |
tree | 5fa5216428ad0fbeb05a8310374be4ffeefe16bd | |
parent | 72eaeb9aedf14936ae7601ed0ac61c4014943129 (diff) |
start to move the parsing from refresh.php to a Parser class
-rw-r--r-- | class/Parser.php | 166 | ||||
-rw-r--r-- | refresh.php | 86 |
2 files changed, 170 insertions, 82 deletions
diff --git a/class/Parser.php b/class/Parser.php new file mode 100644 index 0000000..76e289f --- /dev/null +++ b/class/Parser.php @@ -0,0 +1,166 @@ +<?php + +/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ + +/** + * Class for "repo" Table management + * + * PHP version 5 + * + * Copyright © 2010 Remi Collet + * + * This file is part of rpmphp. + * + * rpmphp is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * rpmphp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with rpmphp. If not, see <http://www.gnu.org/licenses/>. + * + * @category Main + * @package RPMPHP + * + * @author Remi Collet <unknown@unknwown.com> + * @author Johan Cwiklinski <johan@x-tnd.be> + * @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 Parser +{ + /** + * Display a message + */ + static private function log($msg) + { + echo date("r : ") . $msg ."\n"; + } + /** + * Parse the Bugzilla ACL list from pkgdb + * + * @param TableAcl $acls the table to write to + * @param string $url the file to read from + * + * @return integer number of parsed line + */ + static public function readAcls(TableAcls $acls, $url) + { + $tot = 0; + + self::log("Read pkgdb/owner"); + $fic=fopen($url, 'r'); + if (!$fic) { + self::log("ERROR reading '$url'"); + } else { + $nb = $acls->getCount(); + $acls->truncate(); + self::log("Delete $nb owners"); + + for ($tot=0 ; $line=fgetcsv($fic, 1024, '|'); ) { + if (count($line)>5 && substr($line[0], 0, 1)!='#') { + for ($i=0; $i<6; $i++) { + $line[$i]=trim($line[$i]); + } + $input = array( + 'collection' => $line[0], + 'name' => $line[1], + 'summary' => $line[2], + ); + if (!empty($line[3])) { + $input['owner'] = $line[3]; + } + if (!empty($line[4])) { + $input['qa'] = $line[3]; + } + if (!empty($line[5])) { + $input['cc'] = $line[3]; + } + if ($acls->add($input)) { + $tot++; + } + } + } + fclose($fic); + self::log("wrote $tot package's owner"); + } + return $tot; + } + + /** + * Parse the content of a R repository + * + * @param TableUpstream $uptable the table to write to + * @param hastable $repo the repo to read from + * + * @return integer number of parsed line + */ + static private function readOneR(TableUpstream $uptable, $repo) + { + $tot = 0; + + self::log("Reading " . $repo["name"] . " (" . + $repo["state"] . ")"); + $index = @file_get_contents($repo["url"]); + if (!$index) { + self::log("Can't read [" . $repo["url"] . "], skip this channel"); + return 0; + } + if ($repo["state"]=="stable") { + $crit = array('type'=>'R', 'channel'=>$repo['name']); + $nb = $uptable->delete($crit); + self::log("Delete $nb packages"); + } + $results=array(); + $pat = '/Package: *(.*)\nVersion: *(.*)\n/i'; + if (preg_match_all($pat, $index, $results, PREG_SET_ORDER)) { + foreach ($results as $result) { + $rpmname = "R-".$result[1]; + $ver = str_replace('-', '.', $result[2]); + $add = $uptable->record( + "R", + $repo["name"], + $rpmname, + $ver, + $repo["state"]=='stable', + ($repo["state"]=="stable"?"":"devel") + ); + + if ($add) { + $tot++; + } + } + self::log("Write $tot packages in this channel"); + } else { + self::log("No package in this channel"); + } + return $tot; + } + + /** + * Parse the content of a R repository + * + * @param TableUpstream $uptable the table to write to + * @param TableRRepo $rrepo the table to read from + * + * @return integer number of parsed line + */ + static public function readR(TableUpstream $uptable, TableRRepo $rrepo) + { + $tot = 0; + + foreach ($rrepo->request() as $repo) { + $tot += self::readOneR($uptable, $repo); + } + self::log("Write $tot packages in all channels"); + return $tot; + } +} +?>
\ No newline at end of file diff --git a/refresh.php b/refresh.php index 117af84..aa4d486 100644 --- a/refresh.php +++ b/refresh.php @@ -425,50 +425,8 @@ try { // ------------------------------------------------------------------- if ($_SERVER['argc']==1 || in_array('R', $_SERVER['argv'])) { - $tottot=0; - $rrepo = new TableRRepo($db); - foreach ($rrepo->request() as $repo) { - echo date("r : ") . "Reading " . $repo["name"] . " (" . - $repo["state"] . ")\n"; - $index = @file_get_contents($repo["url"]); - if (!$index) { - echo date("r : ") . "Can't read [" . $repo["url"] . - "], skip this channel\n"; - continue; - } - if ($repo["state"]=="stable") { - $crit = array('type'=>'R', 'channel'=>$repo['name']); - $nb = $uptable->delete($crit); - echo date("r : ") . "Delete $nb packages\n"; - } - $results=array(); - $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"; - $rpmname = "R-".$result[1]; - $ver = str_replace('-', '.', $result[2]); - $add_upstream = $uptable->record( - "R", - $repo["name"], - $rpmname, - $ver, - $repo["state"]=='stable', - ($repo["state"]=="stable"?"":"devel") - ); - if ($add_upstream) { - $tot++; - } - } - echo date("r : ") . "Write $tot packages in this channel\n"; - $tottot += $tot; - } else { - echo date("r : ") . "No package in this channel\n"; - } - } - echo date("r : ") . "Write $tottot packages in all channels\n"; + Parser::readR($uptable, new TableRRepo($db)); } // If R in options @@ -477,45 +435,9 @@ try { // ------------------------------------------------------------------- if ($_SERVER['argc']==1 || in_array('owner', $_SERVER['argv'])) { - $acls = new TableAcls($db); - $fic=fopen( - "https://admin.fedoraproject.org/pkgdb/lists/bugzilla?tg_format=plain", - "r" - ); - if (!$fic) { - echo date("r : ") . "ERROR reading pkgdb\n"; - } else { - $nb = $acls->getCount(); - $acls->truncate(); - echo date("r : ") . "Delete $nb owners\n"; - - for ($tot=0 ; $line=fgetcsv($fic, 1024, '|'); ) { - if (count($line)>5 && substr($line[0], 0, 1)!='#') { - for ($i=0; $i<6; $i++) { - $line[$i]=trim($line[$i]); - } - $input = array( - 'collection' => $line[0], - 'name' => $line[1], - 'summary' => $line[2], - ); - if (!empty($line[3])) { - $input['owner'] = $line[3]; - } - if (!empty($line[4])) { - $input['qa'] = $line[3]; - } - if (!empty($line[5])) { - $input['cc'] = $line[3]; - } - if ($acls->add($input)) { - $tot++; - } - } - } - fclose($fic); - echo date("r : ") . "wrote $tot package's owner\n"; - } + $url = "https://admin.fedoraproject.org/pkgdb/lists/bugzilla?tg_format=plain"; + Parser::readAcls(new TableAcls($db), $url); + } // If in options } catch(PDOException $e) { |