| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?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', 'attributes',
- '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;
- }
- public function size($all = false) {
- $db = FreshRSS_Context::$system_conf->db;
- $sql = 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema=?'; //MySQL
- $values = array($db['base']);
- if (!$all) {
- $sql .= ' AND table_name LIKE ?';
- $values[] = $this->prefix . '%';
- }
- $stm = $this->bd->prepare($sql);
- $stm->execute($values);
- $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
- return $res[0];
- }
- public function optimize() {
- $ok = true;
- $sql = 'OPTIMIZE TABLE `' . $this->prefix . 'entry`'; //MySQL
- $stm = $this->bd->prepare($sql);
- $ok &= $stm != false;
- if ($stm) {
- $ok &= $stm->execute();
- }
- $sql = 'OPTIMIZE TABLE `' . $this->prefix . 'feed`'; //MySQL
- $stm = $this->bd->prepare($sql);
- $ok &= $stm != false;
- if ($stm) {
- $ok &= $stm->execute();
- }
- $sql = 'OPTIMIZE TABLE `' . $this->prefix . 'category`'; //MySQL
- $stm = $this->bd->prepare($sql);
- $ok &= $stm != false;
- if ($stm) {
- $ok &= $stm->execute();
- }
- return $ok;
- }
- }
|