From 7deab3588b0a52a760e9aa5ecf51efa147a3de08 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Mon, 11 Dec 2017 00:29:55 +0100 Subject: [PATCH] Write update data into database. (only files for now) --- lib/TclUpdates/SQLiteWriter.php | 51 +++++++++++++++++++++++++++++++++ parse_files.php | 3 ++ 2 files changed, 54 insertions(+) create mode 100755 lib/TclUpdates/SQLiteWriter.php diff --git a/lib/TclUpdates/SQLiteWriter.php b/lib/TclUpdates/SQLiteWriter.php new file mode 100755 index 0000000..2171454 --- /dev/null +++ b/lib/TclUpdates/SQLiteWriter.php @@ -0,0 +1,51 @@ +dbFile = 'otadb.db3'; + $this->pdo = new \PDO('sqlite:' . $this->dbFile, 0666, $sqlerror); + if ($this->pdo === false) { + return $sqlerror; + } + } + + private function insertArray($table, $data) + { + $placeholders = array_fill(0, count($data), '?'); + $sql = 'INSERT OR REPLACE INTO "' . $table . '" (' . implode(', ', array_keys($data)) . ') VALUES (' . implode(', ', $placeholders) . ')'; + $stmt = $this->pdo->prepare($sql); + $ok = $stmt->execute(array_values($data)); + return $ok; + } + + public function addGotu(GotuObject $g) + { + $this->insertArray('updates', array( + 'tv' => $g->tv, + 'fv' => $g->fv, + 'svn' => $g->svn, + 'pubDate' => $g->time, + 'publisher' => $g->publisher, + 'fwId' => $g->fw_id, + 'file_id' => $g->file_id, + 'file_name' => $g->filename, + 'file_size' => $g->file_size, + 'file_sha1' => $g->file_chksum, + 'type' => $g->type, + 'note' => json_encode(array( + 'en' => $g->description_en, + 'ja' => $g->description_ja, + 'zh' => $g->description_zh, + )) + )); + $key = $this->pdo->lastInsertId(); + echo "Added entry " . $key . PHP_EOL; + } +} diff --git a/parse_files.php b/parse_files.php index abf266f..4363a6b 100755 --- a/parse_files.php +++ b/parse_files.php @@ -5,10 +5,12 @@ require_once __DIR__ . '/lib/autoloader.php'; use \TclUpdates\GotuObject; use \TclUpdates\XmlParser; +use \TclUpdates\SQLiteWriter; $bkup_dir = __DIR__ . '/data/'; $file_list = glob($bkup_dir . '*.xml'); +$sqlw = new SQLiteWriter(); foreach ($file_list as $file) { $filename = basename($file); @@ -26,4 +28,5 @@ foreach ($file_list as $file) { echo 'Processing ' . $filename . PHP_EOL; $g = GotuObject::fromXmlParser($xp); print_r($g); + $sqlw->addGotu($g); }