4
0

DatabaseDAOSQLite.php 2.4 KB

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