aboutsummaryrefslogtreecommitdiff
path: root/app/Models/DatabaseDAO.php
diff options
context:
space:
mode:
authorGravatar Clément <clement@selfhost.fr> 2017-02-15 14:14:03 +0100
committerGravatar Clément <clement@selfhost.fr> 2017-02-15 14:14:03 +0100
commit5a1bb1393b4496eb35a2ffb3cc63d41c9dc1e2e5 (patch)
tree67028e45792c575c25c92616633f64cc7a4a13eb /app/Models/DatabaseDAO.php
parent7e949d50320317b5c3b5a2da2bdaf324e794b2f7 (diff)
parent5f637bd816b7323885bfe1751a1724ee59a822f6 (diff)
Merge remote-tracking branch 'FreshRSS/master'
Diffstat (limited to 'app/Models/DatabaseDAO.php')
-rw-r--r--app/Models/DatabaseDAO.php83
1 files changed, 83 insertions, 0 deletions
diff --git a/app/Models/DatabaseDAO.php b/app/Models/DatabaseDAO.php
new file mode 100644
index 000000000..0d85718e3
--- /dev/null
+++ b/app/Models/DatabaseDAO.php
@@ -0,0 +1,83 @@
+<?php
+
+/**
+ * This class is used to test database is well-constructed.
+ */
+class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
+ public function tablesAreCorrect() {
+ $sql = 'SHOW TABLES';
+ $stm = $this->bd->prepare($sql);
+ $stm->execute();
+ $res = $stm->fetchAll(PDO::FETCH_ASSOC);
+
+ $tables = array(
+ $this->prefix . 'category' => false,
+ $this->prefix . 'feed' => false,
+ $this->prefix . 'entry' => false,
+ );
+ foreach ($res as $value) {
+ $tables[array_pop($value)] = true;
+ }
+
+ return count(array_keys($tables, true, true)) == count($tables);
+ }
+
+ public function getSchema($table) {
+ $sql = 'DESC ' . $this->prefix . $table;
+ $stm = $this->bd->prepare($sql);
+ $stm->execute();
+
+ return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC));
+ }
+
+ public function checkTable($table, $schema) {
+ $columns = $this->getSchema($table);
+
+ $ok = (count($columns) == count($schema));
+ foreach ($columns as $c) {
+ $ok &= in_array($c['name'], $schema);
+ }
+
+ return $ok;
+ }
+
+ public function categoryIsCorrect() {
+ return $this->checkTable('category', array(
+ 'id', 'name'
+ ));
+ }
+
+ public function feedIsCorrect() {
+ return $this->checkTable('feed', array(
+ 'id', 'url', 'category', 'name', 'website', 'description', 'lastUpdate',
+ 'priority', 'pathEntries', 'httpAuth', 'error', 'keep_history', 'ttl',
+ 'cache_nbEntries', 'cache_nbUnreads'
+ ));
+ }
+
+ public function entryIsCorrect() {
+ return $this->checkTable('entry', array(
+ 'id', 'guid', 'title', 'author', 'content_bin', 'link', 'date', 'is_read',
+ 'is_favorite', 'id_feed', 'tags'
+ ));
+ }
+
+ public function daoToSchema($dao) {
+ return array(
+ 'name' => $dao['Field'],
+ 'type' => strtolower($dao['Type']),
+ 'notnull' => (bool)$dao['Null'],
+ 'default' => $dao['Default'],
+ );
+ }
+
+ public function listDaoToSchema($listDAO) {
+ $list = array();
+
+ foreach ($listDAO as $dao) {
+ $list[] = $this->daoToSchema($dao);
+ }
+
+ return $list;
+ }
+}