com web/rmtools: started to implement pickleweb compliance: client/include/PickleDb.php client/include/PickleJob.php client/script/pickle_ctl.php
| From: | Anatol Belski | Date: | Fri, 08 May 2015 20:42:21 +0000 |
| Subject: | com web/rmtools: started to implement pickleweb compliance: client/include/PickleDb.php client/include/PickleJob.php client/script/pickle_ctl.php | ||
| Groups: | php.webmaster | ||
| Request: | Send a blank email to php-webmaster+get-25989@lists.php.net to get a copy of this message | ||
Commit: 9ca036c6a2d925b7c3e034acfb201c066927cf3b
Author: Anatol Belski <ab@php.net> Fri, 8 May 2015 22:42:21 +0200
Parents: eea1dea234b03596581c8d6b0c4fac87aca7c0dc
Branches: master pecl_legacy
Link: http://git.php.net/?p=web/rmtools.git;a=commitdiff;h=9ca036c6a2d925b7c3e034acfb201c066927cf3b
Log:
started to implement pickleweb compliance
Changed paths:
A client/include/PickleDb.php
A client/include/PickleJob.php
A client/script/pickle_ctl.php
Diff:
diff --git a/client/include/PickleDb.php b/client/include/PickleDb.php
new file mode 100644
index 0000000..decb1c0
--- /dev/null
+++ b/client/include/PickleDb.php
@@ -0,0 +1,118 @@
+<?php
+
+namespace rmtools;
+
+
+class PDb 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 package_release (package_release_id INTEGER PRIMARY KEY, package_hash
STRING, package_name STRING);";
+ $this->exec($sql);
+
+ $sql = "CREATE TABLE package_build (package_build_id INTEGER PRIMARY KEY, package_hash
STRING, build_status INTEGER, ts_last_build INTEGER, ts_last_);";
+ $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 package_release (package_name, package_hash, 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 package_release WHERE package_name = '$name' AND package_hash
= '$version';";
+ $this->exec($sql);
+ }
+
+ public function exists($name, $version, $where = '')
+ {
+ /* cant check such thing, so trust :) */
+ if ($where) {
+ $where = "AND $where";
+ }
+
+ $name = $this->escapeString($name);
+ $version = $this->escapeString($version);
+ $sql = "SELECT ts_built FROM package_release WHERE package_name = '$name' AND
package_hash = '$version' $where;";
+
+ $res = $this->query($sql);
+
+ $ret = false !== $res->fetchArray(SQLITE3_NUM);
+ //return $res->numColumns() > 0;
+
+ $res->finalize();
+
+ return $ret;
+ }
+
+ public function done($name, $version)
+ {
+ /* XXX That's an assumption as the latest timestamp should be about 30 mit old.
+ Need to extend pecl.php to set the real statuses when in't really done */
+ return $this->exists($name, $version, "ts_built - " . time() . " >
1800");
+ }
+
+ public function dump($where = '')
+ {
+ /* cant check such thing, so trust :) */
+ if ($where) {
+ $where = "WHERE $where";
+ }
+
+ $res = $this->query("SELECT * FROM package_release $where ORDER BY package_name,
package_hash ASC");
+ echo "DUMP package_release " . PHP_EOL . PHP_EOL;
+ while(false !== ($row = $res->fetchArray(SQLITE3_ASSOC))) {
+ foreach ($row as $col => $val) {
+ echo "$col=$val" . PHP_EOL;
+ }
+ echo PHP_EOL;
+ }
+ $res->finalize();
+ }
+
+ public function dumpQueue()
+ {
+ $this->dump("ts_built <= 0");
+ }
+
+ public function touch($name, $version)
+ {
+ $name = $this->escapeString($name);
+ $version = $this->escapeString($version);
+ $sql = "UPDATE package_release SET ts_built=" . time() . " WHERE
lower(package_name) = lower('$name') AND lower(package_hash) =
lower('$version');";
+ $this->exec($sql);
+ }
+}
+
diff --git a/client/include/PickleJob.php b/client/include/PickleJob.php
new file mode 100644
index 0000000..b18ff78
--- /dev/null
+++ b/client/include/PickleJob.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace rmtools;
+
+
+class PickleJob
+{
+
+}
+
diff --git a/client/script/pickle_ctl.php b/client/script/pickle_ctl.php
new file mode 100644
index 0000000..cee2849
--- /dev/null
+++ b/client/script/pickle_ctl.php
@@ -0,0 +1,97 @@
+<?php
+
+include __DIR__ . '/../data/config.php';
+include __DIR__ . '/../include/PickleDb.php';
+include __DIR__ . '/../include/PickleJob.php';
+
+use rmtools as rm;
+
+$sync_host = "http://9fe756ee.ngrok.io";
+$base_uri = "/packages.json";
+$db_path = __DIR__ . '/../data/pickle.sqlite';
+
+$longopts = array("help", "sync", "init-db");
+
+$options = getopt(NULL, $longopts);
+
+$help_cmd = isset($options['help']);
+$sync_cmd = isset($options['sync']);
+$init_cmd = isset($options['sync']);
+
+/* --help */
+if ($_SERVER['argc'] <= 1 || $help_cmd) {
+ echo "Usage: pickle_ctl.php [OPTION] ..." . PHP_EOL;
+ echo " --help Show help and exit, optional." . PHP_EOL;
+ echo " --sync Fetch new jobs from pickleweb, optional." . PHP_EOL;
+
+
+ echo PHP_EOL;
+ echo "Example: pickle_ctl --sync" . PHP_EOL;
+ echo PHP_EOL;
+ exit(0);
+}
+
+if ($init_cmd) {
+ try {
+ $db = new rm\PickleDb($db_path, true);
+ echo "Pickle DB initialized";
+ exit(0);
+ } catch (Exception $e) {
+ echo "Error: " . $e->getMessage();
+ exit(3);
+ }
+}
+
+if ($sync_cmd) {
+ $url = "$sync_host$base_uri";
+ $__tmp = file_get_contents($url);
+
+ if (!$__tmp) {
+ echo "Empty content received from '$url'";
+ exit(3);
+ }
+
+ $info = json_decode($__tmp, true);
+ if (!$info) {
+ echo "Couldn't decode JSON from '$url'";
+ exit(3);
+ }
+
+ if (!isset($info["provider-includes"])) {
+ echo "No provider includes found";
+ exit(3);
+ }
+
+ $db = new rm\PickleDb($db_path);
+
+ /* XXX What meaning does the $hash have? */
+ foreach ($info["provider-includes"] as $uri => $hash) {
+ $url = "$sync_host$uri";
+ $__tmp = file_get_contents($url);
+
+ if (!$__tmp) {
+ echo "Empty content received from '$url'";
+ continue;
+ }
+
+ $pkgs = json_decode($__tmp, true);
+ if (!is_array($pkgs) || !isset($pkgs["providers"]) ||
!is_array($pkgs["providers"]) || empty($pkgs["providers"])) {
+ echo "No packages provided from '$url'";
+ continue;
+ }
+ $pkgs = $pkgs["providers"];
+
+ foreach ($pkgs as $pkg => $phash) {
+
+ }
+
+ var_dump($pkgs);
+ }
+
+ var_dump($info);
+}
+
+
+
+exit(0);
+