diff --git a/sql/tables.sql b/sql/tables.sql index b86161d..0a82a38 100755 --- a/sql/tables.sql +++ b/sql/tables.sql @@ -43,3 +43,20 @@ CREATE TABLE IF NOT EXISTS `items` ( CREATE TABLE IF NOT EXISTS `versions` ( version INTEGER PRIMARY KEY AUTOINCREMENT ); +CREATE TABLE IF NOT EXISTS `segments` ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + item_id INTEGER NOT NULL REFERENCES items(id) ON DELETE CASCADE, + file_id INTEGER NOT NULL REFERENCES files(id) ON DELETE CASCADE, + start REAL NOT NULL, + end REAL NOT NULL, + has_sponsor INTEGER NOT NULL DEFAULT 0 +); + +CREATE TABLE IF NOT EXISTS `clips` ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + segment_id INTEGER NOT NULL REFERENCES segments(id) ON DELETE CASCADE, + file_id INTEGER NOT NULL REFERENCES files(id) ON DELETE CASCADE, + has_sponsor INTEGER NOT NULL DEFAULT 0, + spectrogram_file INTEGER, + FOREIGN KEY (spectrogram_file) REFERENCES files(id) ON DELETE SET NULL +); diff --git a/src/Brickner/Podsumer/State.php b/src/Brickner/Podsumer/State.php index ff18500..b94186d 100755 --- a/src/Brickner/Podsumer/State.php +++ b/src/Brickner/Podsumer/State.php @@ -13,7 +13,7 @@ class State { use TStateSchemaMigrations; - CONST VERSION = 6; # The version of the schema for this commit. + CONST VERSION = 8; # The version of the schema for this commit. protected Main $main; protected $state_file_path; @@ -457,6 +457,64 @@ public function getItemAdSections(int $item_id): array return is_array($sections) ? $sections : []; } + public function addSegment(int $item_id, int $file_id, float $start, float $end, bool $has_sponsor = false): int + { + $sql = 'INSERT INTO segments (item_id, file_id, start, end, has_sponsor) VALUES (:item_id, :file_id, :start, :end, :has_sponsor)'; + $this->query($sql, [ + 'item_id' => $item_id, + 'file_id' => $file_id, + 'start' => $start, + 'end' => $end, + 'has_sponsor' => $has_sponsor ? 1 : 0 + ]); + return intval($this->pdo->lastInsertId()); + } + + public function getSegmentsForItem(int $item_id): array + { + $sql = 'SELECT * FROM segments WHERE item_id = :item_id ORDER BY start'; + $result = $this->query($sql, ['item_id' => $item_id]); + return $result && is_array($result) ? $result : []; + } + + public function getSegment(int $segment_id): array + { + $sql = 'SELECT * FROM segments WHERE id = :id'; + $result = $this->query($sql, ['id' => $segment_id]); + return $result && isset($result[0]) ? $result[0] : []; + } + + public function deleteSegment(int $segment_id): void + { + $sql = 'DELETE FROM segments WHERE id = :id'; + $this->query($sql, ['id' => $segment_id]); + } + + public function addClip(int $segment_id, int $file_id, bool $has_sponsor = false, ?int $spectrogram_file = null): int + { + $sql = 'INSERT INTO clips (segment_id, file_id, has_sponsor, spectrogram_file) VALUES (:segment_id, :file_id, :has_sponsor, :spectrogram_file)'; + $this->query($sql, [ + 'segment_id' => $segment_id, + 'file_id' => $file_id, + 'has_sponsor' => $has_sponsor ? 1 : 0, + 'spectrogram_file' => $spectrogram_file + ]); + return intval($this->pdo->lastInsertId()); + } + + public function getClipsForSegment(int $segment_id): array + { + $sql = 'SELECT * FROM clips WHERE segment_id = :segment_id'; + $result = $this->query($sql, ['segment_id' => $segment_id]); + return $result && is_array($result) ? $result : []; + } + + public function deleteClip(int $clip_id): void + { + $sql = 'DELETE FROM clips WHERE id = :id'; + $this->query($sql, ['id' => $clip_id]); + } + protected function loadFile(string $filename): string { $contents = false; diff --git a/src/Brickner/Podsumer/TStateSchemaMigrations.php b/src/Brickner/Podsumer/TStateSchemaMigrations.php index 5f7732c..a3948c9 100644 --- a/src/Brickner/Podsumer/TStateSchemaMigrations.php +++ b/src/Brickner/Podsumer/TStateSchemaMigrations.php @@ -16,7 +16,8 @@ trait TStateSchemaMigrations 'addJobsTable', 'updateJobsTableConstraints', 'addJobsLogColumn', - 'removeJobsProgressColumn' + 'removeJobsProgressColumn', + 'addSegmentsAndClipsTables' ]; protected function checkDBVersion() @@ -188,5 +189,31 @@ public function removeJobsProgressColumn(): bool { return $dropTable !== false && $createJobsTable !== false && $createJobsIndex !== false && $createJobsTypeIndex !== false; } + + public function addSegmentsAndClipsTables(): bool { + $createSegments = $this->query( + "CREATE TABLE IF NOT EXISTS segments (\n" . + " id INTEGER PRIMARY KEY AUTOINCREMENT,\n" . + " item_id INTEGER NOT NULL REFERENCES items(id) ON DELETE CASCADE,\n" . + " file_id INTEGER NOT NULL REFERENCES files(id) ON DELETE CASCADE,\n" . + " start REAL NOT NULL,\n" . + " end REAL NOT NULL,\n" . + " has_sponsor INTEGER NOT NULL DEFAULT 0\n" . + ")" + ); + + $createClips = $this->query( + "CREATE TABLE IF NOT EXISTS clips (\n" . + " id INTEGER PRIMARY KEY AUTOINCREMENT,\n" . + " segment_id INTEGER NOT NULL REFERENCES segments(id) ON DELETE CASCADE,\n" . + " file_id INTEGER NOT NULL REFERENCES files(id) ON DELETE CASCADE,\n" . + " has_sponsor INTEGER NOT NULL DEFAULT 0,\n" . + " spectrogram_file INTEGER,\n" . + " FOREIGN KEY (spectrogram_file) REFERENCES files(id) ON DELETE SET NULL\n" . + ")" + ); + + return $createSegments !== false && $createClips !== false; + } } diff --git a/templates/clips.html.php b/templates/clips.html.php new file mode 100644 index 0000000..a1e2f44 --- /dev/null +++ b/templates/clips.html.php @@ -0,0 +1,23 @@ +