DatabaseDAOSQLite.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * This class is used to test database is well-constructed (SQLite).
  4. */
  5. class FreshRSS_DatabaseDAOSQLite extends FreshRSS_DatabaseDAO {
  6. public function tablesAreCorrect(): bool {
  7. $sql = 'SELECT name FROM sqlite_master WHERE type="table"';
  8. $stm = $this->pdo->query($sql);
  9. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  10. $tables = array(
  11. $this->pdo->prefix() . 'category' => false,
  12. $this->pdo->prefix() . 'feed' => false,
  13. $this->pdo->prefix() . 'entry' => false,
  14. $this->pdo->prefix() . 'entrytmp' => false,
  15. $this->pdo->prefix() . 'tag' => false,
  16. $this->pdo->prefix() . 'entrytag' => false,
  17. );
  18. foreach ($res as $value) {
  19. $tables[$value['name']] = true;
  20. }
  21. return count(array_keys($tables, true, true)) == count($tables);
  22. }
  23. /** @return array<array<string,string|bool>> */
  24. public function getSchema(string $table): array {
  25. $sql = 'PRAGMA table_info(' . $table . ')';
  26. $stm = $this->pdo->query($sql);
  27. return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC));
  28. }
  29. public function entryIsCorrect(): bool {
  30. return $this->checkTable('entry', array(
  31. 'id', 'guid', 'title', 'author', 'content', 'link', 'date', 'lastSeen', 'hash', 'is_read',
  32. 'is_favorite', 'id_feed', 'tags',
  33. ));
  34. }
  35. public function entrytmpIsCorrect(): bool {
  36. return $this->checkTable('entrytmp', array(
  37. 'id', 'guid', 'title', 'author', 'content', 'link', 'date', 'lastSeen', 'hash', 'is_read',
  38. 'is_favorite', 'id_feed', 'tags',
  39. ));
  40. }
  41. /**
  42. * @param array<string,string> $dao
  43. * @return array<string,string|bool>
  44. */
  45. public function daoToSchema(array $dao): array {
  46. return [
  47. 'name' => $dao['name'],
  48. 'type' => strtolower($dao['type']),
  49. 'notnull' => $dao['notnull'] === '1' ? true : false,
  50. 'default' => $dao['dflt_value'],
  51. ];
  52. }
  53. public function size(bool $all = false): int {
  54. $sum = 0;
  55. if ($all) {
  56. foreach (glob(DATA_PATH . '/users/*/db.sqlite') as $filename) {
  57. $sum += @filesize($filename);
  58. }
  59. } else {
  60. $sum = @filesize(DATA_PATH . '/users/' . $this->current_user . '/db.sqlite');
  61. }
  62. return intval($sum);
  63. }
  64. public function optimize(): bool {
  65. $ok = $this->pdo->exec('VACUUM') !== false;
  66. if (!$ok) {
  67. $info = $this->pdo->errorInfo();
  68. Minz_Log::warning(__METHOD__ . ' error : ' . json_encode($info));
  69. }
  70. return $ok;
  71. }
  72. }