Skip to content

Commit

Permalink
feat: added optional sanitize function for databases
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricerenck committed May 12, 2023
1 parent 272519f commit 51a99a7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
60 changes: 59 additions & 1 deletion app/Migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,62 @@ public function migrate()
}
}
}
}

public function sanitize()
{
if (option('mauricerenck.podcaster.statsInternal', false)) {
$podcast = new Podcast();

$dbType = option('mauricerenck.podcaster.statsType', 'sqlite');
$podcasterDatabase = new PodcasterDatabase();
$db = $podcasterDatabase->connect($dbType);

$pagesWithPodcastFeed = site()->index()->filterBy('template', 'podcasterfeed');

$allEpisodes = [];
foreach ($pagesWithPodcastFeed as $podcastFeed) {
foreach ($podcast->getEpisodes($podcastFeed) as $episode) {
$audio = $podcast->getAudioFile($episode);
$fileSize = (!is_null($audio) && !is_null($audio->toFile())) ? $audio->size() : 0;
$uuid = $episode->uuid()->value();

$allEpisodes[$podcastFeed->podcastId()->value() . '___' . $episode->slug()] = [
'uuid' => $uuid,
'fileSize' => $fileSize
];
}
}

$episodesWithoutUuid = $db->query('SELECT id, episode_slug, podcast_slug FROM episodes WHERE uuid = ""');

foreach ($episodesWithoutUuid->data as $episode) {

if (!isset($allEpisodes[$episode->podcast_slug . '___' . $episode->episode_slug])) {
continue;
}
$page = $allEpisodes[$episode->podcast_slug . '___' . $episode->episode_slug];

if (!$page || !isset($page)) {
continue;
}

$db->execute('UPDATE episodes SET uuid = "' . $page['uuid'] . '" WHERE id = "' . $episode->id . '"');
}

$episodesWithoutFileSize = $db->query('SELECT id, episode_slug, podcast_slug FROM episodes WHERE file_size IS NULL');

foreach ($episodesWithoutFileSize->data as $episode) {
if (!isset($allEpisodes[$episode->podcast_slug . '___' . $episode->episode_slug])) {
continue;
}
$page = $allEpisodes[$episode->podcast_slug . '___' . $episode->episode_slug];

if (!$page || !isset($page)) {
continue;
}

$db->execute('UPDATE episodes SET file_size = "' . $page['fileSize'] . '" WHERE id = "' . $episode->id . '"');
}
}
}
}
4 changes: 4 additions & 0 deletions internal/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@
'system.loadPlugins:after' => function () {
$migrations = new Migrations();
$migrations->migrate();

if (option('mauricerenck.podcaster.sanitize', false)) {
$migrations->sanitize();
}
},
];

0 comments on commit 51a99a7

Please sign in to comment.