Commit c815ecd6 authored by Yassine Doghri's avatar Yassine Doghri
Browse files

feat(rss): generate rss feed from podcast entity

- refactor episode, podcast and category entities to add dynamic properties
- refactor Routes when adding feed route
- update migration files to better fit itunes' and rss' specs
- update podcast and episode forms
- add SimpleRSSElement class to Libraries
- add rss_helper
- update home controller to redirect if system has only one podcast
parent d3119173
......@@ -100,7 +100,7 @@ class App extends BaseConfig
| dates with the date helper, and can be retrieved through app_timezone()
|
*/
public $appTimezone = 'America/Chicago';
public $appTimezone = 'UTC';
/*
|--------------------------------------------------------------------------
......
......@@ -22,7 +22,7 @@ $routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(false);
$routes->addPlaceholder('podcastSlug', '@[a-z0-9\_]{1,191}');
$routes->addPlaceholder('podcastName', '[a-z0-9\_]{1,191}');
$routes->addPlaceholder('episodeSlug', '[a-z0-9\-]{1,191}');
/**
......@@ -34,24 +34,27 @@ $routes->addPlaceholder('episodeSlug', '[a-z0-9\-]{1,191}');
// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/', 'Home::index', ['as' => 'home']);
$routes->add('new-podcast', 'Podcasts::create', ['as' => 'podcasts_create']);
$routes->add('new-podcast', 'Podcast::create', ['as' => 'podcast_create']);
$routes->group('(:podcastSlug)', function ($routes) {
$routes->add('/', 'Podcasts::view/$1', ['as' => 'podcasts_view']);
$routes->add('new-episode', 'Episodes::create/$1', [
'as' => 'episodes_create',
$routes->group('@(:podcastName)', function ($routes) {
$routes->add('/', 'Podcast::view/$1', ['as' => 'podcast_view']);
$routes->add('feed.xml', 'Feed/$1', ['as' => 'podcast_feed']);
$routes->add('new-episode', 'Episode::create/$1', [
'as' => 'episode_create',
]);
$routes->add('(:episodeSlug)', 'Episodes::view/$1/$2', [
'as' => 'episodes_view',
$routes->add('episodes/(:episodeSlug)', 'Episode::view/$1/$2', [
'as' => 'episode_view',
]);
});
// Route for podcast audio file analytics (/stats/podcast_id/episode_id/podcast_folder/filename.mp3)
$routes->add('/stats/(:num)/(:num)/(:any)', 'Analytics::hit/$1/$2/$3');
$routes->add('stats/(:num)/(:num)/(:any)', 'Analytics::hit/$1/$2/$3', [
'as' => 'analytics_hit',
]);
// Show the Unknown UserAgents
$routes->add('/.well-known/unknown-useragents', 'UnknownUserAgents');
$routes->add('/.well-known/unknown-useragents/(:num)', 'UnknownUserAgents/$1');
$routes->add('.well-known/unknown-useragents', 'UnknownUserAgents');
$routes->add('.well-known/unknown-useragents/(:num)', 'UnknownUserAgents/$1');
/**
* --------------------------------------------------------------------
......
......@@ -21,7 +21,7 @@ class Toolbar extends BaseConfig
\CodeIgniter\Debug\Toolbar\Collectors\Database::class,
\CodeIgniter\Debug\Toolbar\Collectors\Logs::class,
\CodeIgniter\Debug\Toolbar\Collectors\Views::class,
// \CodeIgniter\Debug\Toolbar\Collectors\Cache::class,
\CodeIgniter\Debug\Toolbar\Collectors\Cache::class,
\CodeIgniter\Debug\Toolbar\Collectors\Files::class,
\CodeIgniter\Debug\Toolbar\Collectors\Routes::class,
\CodeIgniter\Debug\Toolbar\Collectors\Events::class,
......
......@@ -50,7 +50,7 @@ class BaseController extends Controller
set_user_session_referer();
}
protected function stats($postcast_id)
protected static function triggerWebpageHit($postcast_id)
{
webpage_hit($postcast_id);
}
......
......@@ -10,15 +10,17 @@ namespace App\Controllers;
use App\Models\EpisodeModel;
use App\Models\PodcastModel;
class Episodes extends BaseController
helper('podcast');
class Episode extends BaseController
{
public function create($podcast_slug)
public function create($podcast_name)
{
helper(['form', 'database', 'media', 'id3']);
$episode_model = new EpisodeModel();
$podcast_model = new PodcastModel();
$podcast_name = substr($podcast_slug, 1);
$podcast = $podcast_model->where('name', $podcast_name)->first();
if (
......@@ -33,13 +35,9 @@ class Episodes extends BaseController
) {
$data = [
'podcast' => $podcast,
'episode_types' => field_enums(
$episode_model->prefixTable('episodes'),
'type'
),
];
echo view('episodes/create', $data);
echo view('episode/create', $data);
} else {
$episode_slug = $this->request->getVar('slug');
......@@ -49,7 +47,7 @@ class Episodes extends BaseController
$image = $this->request->getFile('image');
// By default, the episode's image path is set to the podcast's
$image_path = $podcast->image;
$image_path = $podcast->image_uri;
// check whether the user has inputted an image and store it
if ($image->isValid()) {
......@@ -81,20 +79,21 @@ class Episodes extends BaseController
'podcast_id' => $podcast->id,
'title' => $this->request->getVar('title'),
'slug' => $episode_slug,
'enclosure_url' => $episode_path,
'enclosure_uri' => $episode_path,
'enclosure_length' => $episode_file->getSize(),
'enclosure_type' => $episode_file_metadata['mime_type'],
'guid' => $podcast_slug . '/' . $episode_slug,
'pub_date' => $this->request->getVar('pub_date'),
'description' => $this->request->getVar('description'),
'duration' => $episode_file_metadata['playtime_seconds'],
'image' => $image_path,
'image_uri' => $image_path,
'explicit' => $this->request->getVar('explicit') or false,
'number' => $this->request->getVar('episode_number'),
'season_number' => $this->request->getVar('season_number')
? $this->request->getVar('season_number')
: null,
'type' => $this->request->getVar('type'),
'author_name' => $this->request->getVar('author_name'),
'author_email' => $this->request->getVar('author_email'),
'block' => $this->request->getVar('block') or false,
]);
......@@ -103,30 +102,25 @@ class Episodes extends BaseController
$episode_file = write_file_tags($podcast, $episode);
return redirect()->to(
base_url(
route_to(
'episodes_view',
'/@' . $podcast_name,
$episode_slug
)
)
base_url(route_to('episode_view', $podcast_name, $episode_slug))
);
}
}
public function view($podcast_slug, $episode_slug)
public function view($podcast_name, $episode_slug)
{
$podcast_model = new PodcastModel();
$episode_model = new EpisodeModel();
$podcast = $podcast_model->where('name', $podcast_name)->first();
$episode = $episode_model->where('slug', $episode_slug)->first();
$data = [
'podcast' => $podcast_model
->where('name', substr($podcast_slug, 1))
->first(),
'episode' => $episode_model->where('slug', $episode_slug)->first(),
'podcast' => $podcast,
'episode' => $episode,
];
self::stats($data['podcast']->id);
self::triggerWebpageHit($data['podcast']->id);
return view('episodes/view.php', $data);
return view('episode/view.php', $data);
}
}
<?php
namespace App\Controllers;
use App\Models\PodcastModel;
use CodeIgniter\Controller;
class Feed extends Controller
{
public function index($podcast_name)
{
helper('rss');
$podcast_model = new PodcastModel();
$podcast = $podcast_model->where('name', $podcast_name)->first();
// The page cache is set to a decade so it is deleted manually upon podcast update
$this->cachePage(DECADE);
return $this->response->setXML(get_rss_feed($podcast));
}
}
......@@ -14,8 +14,18 @@ class Home extends BaseController
public function index()
{
$model = new PodcastModel();
$data = ['podcasts' => $model->findAll()];
$all_podcasts = $model->findAll();
// check if there's only one podcast to redirect user to it
if (count($all_podcasts) == 1) {
return redirect()->to(
base_url(route_to('podcast_view', $all_podcasts[0]->name))
);
}
// default behavior: list all podcasts on home page
$data = ['podcasts' => $all_podcasts];
return view('home', $data);
}
}
......@@ -4,16 +4,14 @@
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Controllers;
use App\Entities\Podcast;
use App\Models\CategoryModel;
use App\Models\EpisodeModel;
use App\Models\LanguageModel;
use App\Models\PodcastModel;
class Podcasts extends BaseController
class Podcast extends BaseController
{
public function create()
{
......@@ -39,30 +37,27 @@ class Podcasts extends BaseController
'browser_lang' => get_browser_language(
$this->request->getServer('HTTP_ACCEPT_LANGUAGE')
),
'podcast_types' => field_enums(
$podcast_model->prefixTable('podcasts'),
'type'
),
];
echo view('podcasts/create', $data);
echo view('podcast/create', $data);
} else {
$image = $this->request->getFile('image');
$podcast_name = $this->request->getVar('name');
$image_path = save_podcast_media($image, $podcast_name, 'cover');
$podcast = new Podcast([
$podcast = new \App\Entities\Podcast([
'title' => $this->request->getVar('title'),
'name' => $podcast_name,
'description' => $this->request->getVar('description'),
'episode_description_footer' => $this->request->getVar(
'episode_description_footer'
),
'image' => $image_path,
'image_uri' => $image_path,
'language' => $this->request->getVar('language'),
'category' => $this->request->getVar('category'),
'explicit' => $this->request->getVar('explicit') or false,
'author' => $this->request->getVar('author'),
'author_name' => $this->request->getVar('author_name'),
'author_email' => $this->request->getVar('author_email'),
'owner_name' => $this->request->getVar('owner_name'),
'owner_email' => $this->request->getVar('owner_email'),
'type' => $this->request->getVar('type'),
......@@ -77,18 +72,16 @@ class Podcasts extends BaseController
$podcast_model->save($podcast);
return redirect()->to(
base_url(route_to('podcasts_view', '@' . $podcast_name))
base_url(route_to('podcast_view', $podcast->name))
);
}
}
public function view($slug)
public function view($podcast_name)
{
$podcast_model = new PodcastModel();
$episode_model = new EpisodeModel();
$podcast_name = substr($slug, 1);
$podcast = $podcast_model->where('name', $podcast_name)->first();
$data = [
'podcast' => $podcast,
......@@ -96,8 +89,8 @@ class Podcasts extends BaseController
->where('podcast_id', $podcast->id)
->findAll(),
];
self::stats($podcast->id);
self::triggerWebpageHit($podcast->id);
return view('podcasts/view', $data);
return view('podcast/view', $data);
}
}
......@@ -29,8 +29,7 @@ class AddCategories extends Migration
],
'code' => [
'type' => 'VARCHAR',
'constraint' => 1024,
'unique' => true,
'constraint' => 191,
],
'apple_category' => [
'type' => 'VARCHAR',
......@@ -42,6 +41,7 @@ class AddCategories extends Migration
],
]);
$this->forge->addKey('id', true);
$this->forge->addUniqueKey('code');
$this->forge->addForeignKey('parent_id', 'categories', 'id');
$this->forge->createTable('categories');
}
......
......@@ -41,7 +41,7 @@ class AddPodcasts extends Migration
'comment' =>
'The show description. Where description is text containing one or more sentences describing your podcast to potential listeners. The maximum amount of text allowed for this tag is 4000 characters. To include links in your description or rich HTML, adhere to the following technical guidelines: enclose all portions of your XML that contain embedded HTML in a CDATA section to prevent formatting issues, and to ensure proper link functionality.',
],
'image' => [
'image_uri' => [
'type' => 'VARCHAR',
'constraint' => 1024,
'comment' =>
......@@ -67,11 +67,18 @@ class AddPodcasts extends Migration
'comment' =>
'The podcast parental advisory information. The explicit value can be one of the following: True: If you specify true, indicating the presence of explicit content, Apple Podcasts displays an Explicit parental advisory graphic for your podcast. Podcasts containing explicit material aren’t available in some Apple Podcasts territories. False: If you specify false, indicating that your podcast doesn’t contain explicit language or adult content, Apple Podcasts displays a Clean parental advisory graphic for your podcast.',
],
'author' => [
'author_name' => [
'type' => 'VARCHAR',
'constraint' => 1024,
'comment' =>
'The group responsible for creating the show. Show author most often refers to the parent company or network of a podcast, but it can also be used to identify the host(s) if none exists. Author information is especially useful if a company or organization publishes multiple podcasts. Providing this information will allow listeners to see all shows created by the same entity.',
'Name of the group responsible for creating the show. Show author most often refers to the parent company or network of a podcast, but it can also be used to identify the host(s) if none exists. Author information is especially useful if a company or organization publishes multiple podcasts. Providing this information will allow listeners to see all shows created by the same entity.',
'null' => true,
],
'author_email' => [
'type' => 'VARCHAR',
'constraint' => 1024,
'owner_email' =>
'Email of the group responsible for creating the show. Show author most often refers to the parent company or network of a podcast, but it can also be used to identify the host(s) if none exists. Author information is especially useful if a company or organization publishes multiple podcasts. Providing this information will allow listeners to see all shows created by the same entity.',
'null' => true,
],
'owner_name' => [
......
......@@ -41,11 +41,11 @@ class AddEpisodes extends Migration
'constraint' => 191,
'comment' => 'Episode slug for URLs',
],
'enclosure_url' => [
'enclosure_uri' => [
'type' => 'VARCHAR',
'constraint' => 1024,
'comment' =>
'The URL attribute points to your podcast media file. The file extension specified within the URL attribute determines whether or not content appears in the podcast directory. Supported file formats include M4A, MP3, MOV, MP4, M4V, and PDF.',
'The URI attribute points to your podcast media file. The file extension specified within the URI attribute determines whether or not content appears in the podcast directory. Supported file formats include M4A, MP3, MOV, MP4, M4V, and PDF.',
],
'enclosure_length' => [
'type' => 'INT',
......@@ -69,7 +69,7 @@ class AddEpisodes extends Migration
'pub_date' => [
'type' => 'DATETIME',
'comment' =>
'The date and time when an episode was released. Format the date using the RFC 2822 specifications. For example: Wed, 15 Jun 2019 19:00:00 GMT.',
'The date and time when an episode was released. Format the date using the RFC 2822 specifications. For example: Wed, 15 Jun 2019 19:00:00 UTC.',
],
'description' => [
'type' => 'TEXT',
......@@ -84,7 +84,7 @@ class AddEpisodes extends Migration
'comment' =>
'The duration of an episode. Different duration formats are accepted however it is recommended to convert the length of the episode into seconds.',
],
'image' => [
'image_uri' => [
'type' => 'VARCHAR',
'constraint' => 1024,
'comment' =>
......@@ -112,6 +112,20 @@ class AddEpisodes extends Migration
'comment' =>
'The episode season number. If an episode is within a season use this tag. Where season is a non-zero integer (1, 2, 3, etc.) representing your season number. To allow the season feature for shows containing a single season, if only one season exists in the RSS feed, Apple Podcasts doesn’t display a season number. When you add a second season to the RSS feed, Apple Podcasts displays the season numbers.',
],
'author_name' => [
'type' => 'VARCHAR',
'constraint' => 1024,
'comment' =>
'Name of the group responsible for creating the episode. Episode author most often refers to the parent company or network of a podcast, but it can also be used to identify the host(s) if none exists. Author information is especially useful if a company or organization publishes multiple podcasts. Providing this information will allow listeners to see all episodes created by the same entity.',
'null' => true,
],
'author_email' => [
'type' => 'VARCHAR',
'constraint' => 1024,
'owner_email' =>
'Email of the group responsible for creating the episode. Episode author most often refers to the parent company or network of a podcast, but it can also be used to identify the host(s) if none exists. Author information is especially useful if a company or organization publishes multiple podcasts. Providing this information will allow listeners to see all episodes created by the same entity.',
'null' => true,
],
'type' => [
'type' => 'ENUM',
'constraint' => ['full', 'trailer', 'bonus'],
......
......@@ -7,13 +7,27 @@
namespace App\Entities;
use App\Models\CategoryModel;
use CodeIgniter\Entity;
class Category extends Entity
{
protected $parent;
protected $casts = [
'parent_id' => 'integer',
'code' => 'string',
'apple_category' => 'string',
'google_category' => 'string',
];
public function getParent()
{
$category_model = new CategoryModel();
$parent_id = $this->attributes['parent_id'];
return $parent_id != 0
? $category_model->find($this->attributes['parent_id'])
: null;
}
}
......@@ -7,25 +7,85 @@
namespace App\Entities;
use App\Models\PodcastModel;
use CodeIgniter\Entity;
class Episode extends Entity
{
protected $link;
protected $image_media_path;
protected $image_url;
protected $enclosure_media_path;
protected $enclosure_url;
protected $guid;
protected $podcast;
protected $casts = [
'slug' => 'string',
'title' => 'string',
'enclosure_url' => 'string',
'enclosure_uri' => 'string',
'enclosure_length' => 'integer',
'enclosure_type' => 'string',
'guid' => 'string',
'pub_date' => 'datetime',
'description' => 'string',
'duration' => 'integer',
'image' => 'string',
'image_uri' => 'string',
'author_name' => '?string',
'author_email' => '?string',
'explicit' => 'boolean',
'number' => 'integer',
'season_number' => '?integer',
'type' => 'string',
'block' => 'boolean',
];
public function getImageMediaPath()
{
return media_path($this->attributes['image_uri']);
}
public function getImageUrl()
{
return media_url($this->attributes['image_uri']);
}
public function getEnclosureMediaPath()
{
return media_path($this->attributes['enclosure_uri']);
}
public function getEnclosureUrl()
{
return base_url(
route_to(
'analytics_hit',
$this->attributes['podcast_id'],
$this->attributes['id'],
$this->attributes['enclosure_uri']
)
);
}
public function getLink()
{
return base_url(
route_to(
'episode_view',
$this->getPodcast()->name,
$this->attributes['slug']
)
);
}
public function getGuid()
{
return $this->getLink();
}
public function getPodcast()
{
$podcast_model = new PodcastModel();
return $podcast_model->find($this->attributes['podcast_id']);
}
}
......@@ -7,27 +7,57 @@
namespace App\Entities;
use App\Models\EpisodeModel;
use CodeIgniter\Entity;
class Podcast extends Entity
{
protected $link;
protected $image_url;
protected $episodes;
protected $casts = [
'id' => 'integer',
'title' => 'string',
'name' => 'string',
'description' => 'string',
'image' => 'string',
'image_uri' => 'string',
'language' => 'string',
'category' => 'string',
'explicit' => 'boolean',
'author' => '?string',
'author_name' => '?string',
'author_email' => '?string',
'owner_name' => '?string',
'owner_email' => '?string',
'type' => '?string',
'type' => 'string',
'copyright' => '?string',
'block' => 'boolean',
'complete' => 'boolean',
'episode_description_footer' => '?string',
'custom_html_head' => '?string',
];
public function getImageUrl()
{
return media_url($this->attributes['image_uri']);
}
public function getLink()
{
return base_url(route_to('podcast_view', $this->attributes['name']));
}
public function getFeedUrl()
{
return base_url(route_to('podcast_feed', $this->attributes['name']));
}
public function getEpisodes()
{
$episode_model = new EpisodeModel();
return $episode_model
->where('podcast_id', $this->attributes['id'])
->findAll();
}
}
<?php
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
/**
* Get all possible enum values for a table field
*
* @param string $table
* @param string $field
*
* @return array $enums
*/
function field_enums($table = '', $field = '')
{