DatabaseDAO.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * This class is used to test database is well-constructed.
  4. */
  5. class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
  6. public function tablesAreCorrect() {
  7. $sql = 'SHOW TABLES';
  8. $stm = $this->bd->prepare($sql);
  9. $stm->execute();
  10. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  11. $tables = array(
  12. $this->prefix . 'category' => false,
  13. $this->prefix . 'feed' => false,
  14. $this->prefix . 'entry' => false,
  15. );
  16. foreach ($res as $value) {
  17. $tables[array_pop($value)] = true;
  18. }
  19. return count(array_keys($tables, true, true)) == count($tables);
  20. }
  21. public function getSchema($table) {
  22. $sql = 'DESC ' . $this->prefix . $table;
  23. $stm = $this->bd->prepare($sql);
  24. $stm->execute();
  25. return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC));
  26. }
  27. public function checkTable($table, $schema) {
  28. $columns = $this->getSchema($table);
  29. $ok = (count($columns) == count($schema));
  30. foreach ($columns as $c) {
  31. $ok &= in_array($c['name'], $schema);
  32. }
  33. return $ok;
  34. }
  35. public function categoryIsCorrect() {
  36. return $this->checkTable('category', array(
  37. 'id', 'name'
  38. ));
  39. }
  40. public function feedIsCorrect() {
  41. return $this->checkTable('feed', array(
  42. 'id', 'url', 'category', 'name', 'website', 'description', 'lastUpdate',
  43. 'priority', 'pathEntries', 'httpAuth', 'error', 'keep_history', 'ttl', 'attributes',
  44. 'cache_nbEntries', 'cache_nbUnreads'
  45. ));
  46. }
  47. public function entryIsCorrect() {
  48. return $this->checkTable('entry', array(
  49. 'id', 'guid', 'title', 'author', 'content_bin', 'link', 'date', 'is_read',
  50. 'is_favorite', 'id_feed', 'tags'
  51. ));
  52. }
  53. public function daoToSchema($dao) {
  54. return array(
  55. 'name' => $dao['Field'],
  56. 'type' => strtolower($dao['Type']),
  57. 'notnull' => (bool)$dao['Null'],
  58. 'default' => $dao['Default'],
  59. );
  60. }
  61. public function listDaoToSchema($listDAO) {
  62. $list = array();
  63. foreach ($listDAO as $dao) {
  64. $list[] = $this->daoToSchema($dao);
  65. }
  66. return $list;
  67. }
  68. public function size($all = false) {
  69. $db = FreshRSS_Context::$system_conf->db;
  70. $sql = 'SELECT SUM(data_length + index_length) FROM information_schema.TABLES WHERE table_schema=?'; //MySQL
  71. $values = array($db['base']);
  72. if (!$all) {
  73. $sql .= ' AND table_name LIKE ?';
  74. $values[] = $this->prefix . '%';
  75. }
  76. $stm = $this->bd->prepare($sql);
  77. $stm->execute($values);
  78. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  79. return $res[0];
  80. }
  81. public function optimize() {
  82. $ok = true;
  83. $sql = 'OPTIMIZE TABLE `' . $this->prefix . 'entry`'; //MySQL
  84. $stm = $this->bd->prepare($sql);
  85. $ok &= $stm != false;
  86. if ($stm) {
  87. $ok &= $stm->execute();
  88. }
  89. $sql = 'OPTIMIZE TABLE `' . $this->prefix . 'feed`'; //MySQL
  90. $stm = $this->bd->prepare($sql);
  91. $ok &= $stm != false;
  92. if ($stm) {
  93. $ok &= $stm->execute();
  94. }
  95. $sql = 'OPTIMIZE TABLE `' . $this->prefix . 'category`'; //MySQL
  96. $stm = $this->bd->prepare($sql);
  97. $ok &= $stm != false;
  98. if ($stm) {
  99. $ok &= $stm->execute();
  100. }
  101. return $ok;
  102. }
  103. }