DatabaseDAOSQLite.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 = [
  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', [
  34. 'id', 'guid', 'title', 'author', 'content', 'link', 'date', 'lastSeen', 'hash', 'is_read', 'is_favorite', 'id_feed', 'tags',
  35. ]);
  36. }
  37. public function entrytmpIsCorrect(): bool {
  38. return $this->checkTable('entrytmp', [
  39. 'id', 'guid', 'title', 'author', 'content', 'link', 'date', 'lastSeen', 'hash', 'is_read', 'is_favorite', 'id_feed', 'tags'
  40. ]);
  41. }
  42. /**
  43. * @param array<string,string|int|bool|null> $dao
  44. * @return array{'name':string,'type':string,'notnull':bool,'default':mixed}
  45. */
  46. public function daoToSchema(array $dao): array {
  47. return [
  48. 'name' => (string)$dao['name'],
  49. 'type' => strtolower((string)$dao['type']),
  50. 'notnull' => $dao['notnull'] == '1' ? true : false,
  51. 'default' => $dao['dflt_value'],
  52. ];
  53. }
  54. public function size(bool $all = false): int {
  55. $sum = 0;
  56. if ($all) {
  57. foreach (glob(DATA_PATH . '/users/*/db.sqlite') ?: [] as $filename) {
  58. $sum += @filesize($filename);
  59. }
  60. } else {
  61. $sum = @filesize(DATA_PATH . '/users/' . $this->current_user . '/db.sqlite');
  62. }
  63. return intval($sum);
  64. }
  65. public function optimize(): bool {
  66. $ok = $this->pdo->exec('VACUUM') !== false;
  67. if (!$ok) {
  68. $info = $this->pdo->errorInfo();
  69. Minz_Log::warning(__METHOD__ . ' error : ' . json_encode($info));
  70. }
  71. return $ok;
  72. }
  73. }