Commit: 8c85bf7d28de05688d445ffc2e2d9665434c4b5c Author: Anatol Belski <a...@php.net> Tue, 24 Sep 2013 10:14:26 +0200 Parents: 373e52c29f6aa8576a50e64452674c3f4b20c5b7 Branches: master
Link: http://git.php.net/?p=web/rmtools.git;a=commitdiff;h=8c85bf7d28de05688d445ffc2e2d9665434c4b5c Log: primitives to read and save the RSS data from PECL Changed paths: A client/include/PeclDb.php A client/script/pecl_rss.php Diff: diff --git a/client/include/PeclDb.php b/client/include/PeclDb.php new file mode 100644 index 0000000..60dfba4 --- /dev/null +++ b/client/include/PeclDb.php @@ -0,0 +1,86 @@ +<?php + +namespace rmtools; + + +class PeclDb extends \SQLite3 { + + public function __construct($db_path, $autoinit = true) + { + $flags = SQLITE3_OPEN_READWRITE; + $existent = file_exists($db_path); + + if (!$existent) { + $flags |= SQLITE3_OPEN_CREATE; + } + + $this->open($db_path); + + if (!$existent && $autoinit) { + $this->initDb(); + } + } + + public function initDb() + { + $sql = "CREATE TABLE ext_release (ext_name STRING, ext_version STRING, ts_built INTEGER);"; + $this->exec($sql); + + } + + public function add($name, $version, $force = false) + { + if ($force) { + $this->remove($name, $version); + } + + if ($this->exists($name, $version)) { + return false; + } + + $name = $this->escapeString($name); + $version = $this->escapeString($version); + $sql = "INSERT INTO ext_release (ext_name, ext_version, ts_built) VALUES ('$name', '$version', 0);"; + $this->exec($sql); + + return true; + } + + public function remove($name, $version) + { + $name = $this->escapeString($name); + $version = $this->escapeString($version); + $sql = "DELETE FROM ext_release WHERE ext_name = '$name' AND ext_version = '$version';"; + $this->exec($sql); + } + + public function exists($name, $version) + { + $name = $this->escapeString($name); + $version = $this->escapeString($version); + $sql = "SELECT ts_built FROM ext_release WHERE ext_name = '$name' AND ext_version = '$version';"; + + $res = $this->query($sql); + + $ret = false !== $res->fetchArray(SQLITE3_NUM); + //return $res->numColumns() > 0; + + $res->finalize(); + + return $ret; + } + + public function dump() + { + $res = $this->query("SELECT * FROM ext_release ORDER BY ext_name, ext_version ASC"); + echo "DUMP ext_release " . PHP_EOL; + while(false !== ($row = $res->fetchArray(SQLITE3_ASSOC))) { + foreach ($row as $col => $val) { + echo "$col=$val" . PHP_EOL; + } + echo PHP_EOL; + } + $res->finalize(); + } +} + diff --git a/client/script/pecl_rss.php b/client/script/pecl_rss.php new file mode 100644 index 0000000..59684e7 --- /dev/null +++ b/client/script/pecl_rss.php @@ -0,0 +1,56 @@ +<?php + + +include __DIR__ . '/../include/PeclDb.php'; + +use rmtools as rm; + +$longopts = array("refresh-db"); + +$options = getopt(NULL, $longopts); + +$refresh_db = isset($options['']); + +if ($_SERVER['argv'] <= 1) { + echo "Usage: pecl_rss.php [OPTION] ..." . PHP_EOL; + echo " --refresh-db Read new data from the PECL RSS feed and save it to db, optional" . PHP_EOL; + echo PHP_EOL; + echo "Example: pecl_rss --refresh-db" . PHP_EOL; + echo PHP_EOL; + exit(0); +} + +$db_path = __DIR__ . '/../data/pecl.sqlite'; + +$rss = 'http://pecl.php.net/feeds/latest.rss'; +$latest = simplexml_load_file($rss); +if (!isset($latest->item)) { + echo "No items could be found in $rss" . PHP_EOL; +} + +$db = new rm\PeclDb($db_path); + +foreach($latest->item as $item) { + if (!$item->title) { + continue; + } + + $tmp = explode(' ', (string)$item->title); + $name = $tmp[0]; + $version = $tmp[1]; + + if (!$name || !$version) { + continue; + } + + if ($db->add($name, $version)) { + echo "Read ext <$name> of version <$version>" . PHP_EOL; + } + /* + * when need more, look here (or /r) using name and version + * $url = 'http://pecl.php.net/rest/p/'; + */ +} + +$db->dump(); + -- PHP Webmaster List Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php