Skip to content

Commit 18a4f90

Browse files
committed
Update composer file; Optimize library loading; Update library versions; Optimize backend preview
1 parent f1fe633 commit 18a4f90

8 files changed

Lines changed: 87 additions & 62 deletions

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.css linguist-detectable=false
2+
*.js linguist-detectable=false

ProcessPodcastSubscriptions.module

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,34 @@
11
<?php
22
/**
33
* COPYRIGHT NOTICE
4-
* Copyright (c) 2022 Neue Rituale GbR
4+
* Copyright (c) 2023 Neue Rituale GbR
55
* @author NR <code@neuerituale.com>
66
*/
77

88
namespace ProcessWire;
99

10-
use Exception;
1110
use Lukaswhite\PodcastFeedParser\Exceptions\FileNotFoundException;
1211
use Lukaswhite\PodcastFeedParser\Exceptions\InvalidXmlException;
1312
use Lukaswhite\PodcastFeedParser\Parser;
1413
use Lukaswhite\PodcastFeedParser\Podcast;
15-
use PDO;
16-
use PDOException;
17-
18-
require_once(/*NoCompile*/__DIR__ . '/vendor/autoload.php');
1914

2015
/**
16+
* @method void processPodcast(WireData $feed, Podcast $podcast)
17+
* @method array filterDbFeedInput(string $feedUrl, Podcast $podcast)
18+
* @method array filterDbFeedUpdate(int $id, Podcast $podcast)
2119
* @method Parser getFeedParserInstance($args = null)
2220
*/
23-
24-
class ProcessPodcastSubscriptions extends Process implements Module, ConfigurableModule
25-
{
21+
class ProcessPodcastSubscriptions extends Process implements Module, ConfigurableModule {
2622

2723
const dbTableName = 'podcast_subscriptions';
2824
const logFileName = 'podcast-subscriptions';
2925
const SCHEMA_VERSION = 2;
3026
private ?WireArray $feedsCache = null;
3127

32-
public static function getModuleInfo() {
33-
return array(
28+
public static function getModuleInfo(): array {
29+
return [
3430
'title' => 'Process Podcast Subscriptions',
35-
'version' => 102,
31+
'version' => 103,
3632
'summary' => 'Subscribe Podcast RSS feed and save as new page',
3733
'icon' => 'clock-o',
3834
'requires' => ['LazyCron'],
@@ -44,7 +40,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
4440
'parent' => 'admin',
4541
'title' => __('Podcasts'),
4642
],
47-
);
43+
];
4844
}
4945

5046
/**
@@ -72,7 +68,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
7268
$this->subscriptionLinks = $subscriptionLinks;
7369

7470
// find hookname and init lazy cron hook
75-
$hookName = @$this->timeFuncs[$this->cronSchedule];
71+
$hookName = $this->timeFuncs[$this->cronSchedule] ?? false;
7672
if($hookName && $this->modules->isInstalled('LazyCron')) $this->addHook('LazyCron::' . $hookName, $this, 'updateAllFeeds');
7773
}
7874

@@ -108,7 +104,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
108104
$feedUrl = $this->input->post('feed_url', 'url');
109105
try {
110106
$this->addFeed($feedUrl);
111-
} catch(Exception $exception) {
107+
} catch(\Exception $exception) {
112108
$this->error($exception->getMessage());
113109
}
114110
}
@@ -171,16 +167,16 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
171167
public function addFeed(string $feedUrl = '') : Podcast {
172168

173169
// check url
174-
if(empty($feedUrl)) throw new Exception($this->_('Empty feed url'));
170+
if(empty($feedUrl)) throw new \Exception($this->_('Empty feed url'));
175171

176172
// duplication check
177173
$checkStatement = $this->database->prepare('SELECT * FROM ' . self::dbTableName . ' WHERE feed_url=:feedUrl');
178174
$checkStatement->execute(['feedUrl' => $feedUrl]);
179-
if($checkStatement->fetchColumn(0)) throw new Exception($this->_('Feed already exists'));
175+
if($checkStatement->fetchColumn(0)) throw new \Exception($this->_('Feed already exists'));
180176

181177
// check feed
182178
$podcast = $this->fetchAndParseFeed($feedUrl);
183-
if(!$podcast) throw new Exception($this->_('Invalid feed, no type found.'));
179+
if(!$podcast) throw new \Exception($this->_('Invalid feed, no type found.'));
184180

185181
// add to db
186182
$addStatement = $this->database->prepare('INSERT INTO ' . self::dbTableName . ' (title,description,artwork_url,feed_url,media_count) VALUE (:title,:description,:artwork_url,:feed_url,:media_count)');
@@ -208,7 +204,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
208204

209205
// find feed
210206
$feed = $this->getFeeds('id=' . $id)->first();
211-
if(!$feed) throw new Exception($this->_('Invalid feed id'));
207+
if(!$feed) throw new \Exception($this->_('Invalid feed id'));
212208
$podcast = $this->fetchAndParseFeed($feed->feed_url);
213209

214210
// Update feed in db and the wireData for return
@@ -258,7 +254,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
258254
// Get from database
259255
$statement = $this->database->prepare('SELECT * FROM ' . self::dbTableName);
260256
$statement->execute();
261-
$items = $statement->fetchAll(PDO::FETCH_CLASS, Feed::class);
257+
$items = $statement->fetchAll(\PDO::FETCH_CLASS, Feed::class);
262258

263259
if(!is_array($items)) {
264260
$this->feedsCache = null;
@@ -276,14 +272,14 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
276272
* Delete feed by id
277273
* @param int $id
278274
* @return bool
279-
* @throws WireException|Exception
275+
* @throws WireException|\Exception
280276
*/
281277
public function deleteFeed(int $id) : bool {
282278
$feed = $this->getFeeds('id=' . $id)->first();
283-
if(!$feed) throw new Exception($this->_('Invalid feed id'));
279+
if(!$feed) throw new \Exception($this->_('Invalid feed id'));
284280

285281
$statement = $this->database->prepare('DELETE FROM '.self::dbTableName.' WHERE id=:id');
286-
$statement->bindValue('id', $feed->id, PDO::PARAM_INT);
282+
$statement->bindValue('id', $feed->id, \PDO::PARAM_INT);
287283
return $statement->execute();
288284
}
289285

@@ -292,7 +288,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
292288
* @param array $meta
293289
* @return Feed
294290
*/
295-
public function updateFeedMeta(Feed $feed, array $meta = []) {
291+
public function updateFeedMeta(Feed $feed, array $meta = []): Feed {
296292
$json = count($meta)
297293
? json_encode($meta, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)
298294
: 'null'
@@ -316,7 +312,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
316312
public function fetchAndParseFeed($feedUrl) : ?Podcast {
317313

318314
$fileContent = $this->files->fileGetContents($feedUrl);
319-
if(!$fileContent) throw new Exception($this->_('Invalid feed url'));
315+
if(!$fileContent) throw new \Exception($this->_('Invalid feed url'));
320316

321317
libxml_use_internal_errors(true);
322318
$parser = $this->getFeedParserInstance();
@@ -343,7 +339,6 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
343339
*
344340
* @param string $feedUrl
345341
* @param Podcast $podcast
346-
* @param int $id
347342
* @return array
348343
* @throws WireException
349344
*/
@@ -382,7 +377,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
382377
* @return array
383378
* @throws WireException
384379
*/
385-
public function processMetaInput() {
380+
public function processMetaInput(): array {
386381

387382
$meta = [];
388383

@@ -405,7 +400,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
405400
public function install() {
406401

407402
// Create Database
408-
$this->wire('database')->exec("
403+
wire()->database->exec("
409404
CREATE TABLE ".self::dbTableName." (
410405
`id` int(11) NOT NULL,
411406
`title` tinytext NOT NULL,
@@ -435,8 +430,8 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
435430
*/
436431
public function uninstall() {
437432
try {
438-
$this->wire('database')->exec('DROP TABLE ' . self::dbTableName);
439-
} catch(Exception $exception) {
433+
wire()->database->exec('DROP TABLE ' . self::dbTableName);
434+
} catch(\Exception $exception) {
440435
$this->error($exception->getMessage());
441436
}
442437
}
@@ -511,7 +506,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
511506
try {
512507
$updatedRows = $this->database->exec($sql);
513508
return $updatedRows !== false;
514-
} catch (PDOException $e) {
509+
} catch (\PDOException $e) {
515510
if (isset($e->errorInfo[1]) && in_array($e->errorInfo[1], [1060, 1061, 1091])) {
516511
// 1060 (column already exists), 1061 (duplicate key name), and 1091 (can't drop index) are errors that
517512
// can be safely ignored here; the most likely issue would be that this update has already been applied
@@ -560,11 +555,25 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
560555
* Get Parser Instance
561556
* @param $args
562557
* @return Parser
558+
* @throws \Exception
563559
*/
564-
public function ___getFeedParserInstance($args = null) {
560+
public function ___getFeedParserInstance($args = null): Parser {
561+
$this->loadFeedParserLib();
565562
return new Parser($args);
566563
}
567564

565+
/**
566+
* Load PodcastFeedParser Library
567+
* @return void
568+
* @throws \Exception
569+
*/
570+
public function loadFeedParserLib() {
571+
if(!class_exists("\Lukaswhite\PodcastFeedParser\Parser")) {
572+
if(!file_exists(__DIR__ . '/vendor/autoload.php')) throw new \Exception("Please install the PodcastFeedParser library via `composer install` in the ProcessPodcastSubscriptions module directory.");
573+
require_once(/*NoCompile*/__DIR__ . '/vendor/autoload.php');
574+
}
575+
}
576+
568577
}
569578

570579
class Feed extends WireData {

ProcessPodcastSubscriptionsConfig.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
<?php
22
/**
33
* COPYRIGHT NOTICE
4-
* Copyright (c) 2021 Neue Rituale GbR
4+
* Copyright (c) 2023 Neue Rituale GbR
55
* @author NR <code@neuerituale.com>
66
*/
77

88
namespace ProcessWire;
99

10-
1110
class ProcessPodcastSubscriptionsConfig extends ModuleConfig
1211
{
1312
/**
1413
* @return array
1514
* @throws WireException
1615
*/
17-
public function getDefaults() {
16+
public function getDefaults(): array {
1817

1918
// get schedules from Lazy Cron
2019
$lazyCronInstance = $this->modules->get('LazyCron');
@@ -30,9 +29,8 @@ public function getDefaults() {
3029

3130
/**
3231
* @return InputfieldWrapper
33-
* @throws WireException
3432
*/
35-
public function getInputfields() {
33+
public function getInputfields(): InputfieldWrapper {
3634

3735
$inputfields = parent::getInputfields();
3836

ProcessPodcastSubscriptionsEpisodes.module

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* COPYRIGHT NOTICE
4-
* Copyright (c) 2022 Neue Rituale GbR
4+
* Copyright (c) 2023 Neue Rituale GbR
55
* @author NR <code@neuerituale.com>
66
*/
77

@@ -11,12 +11,17 @@ use Lukaswhite\PodcastFeedParser\Artwork;
1111
use Lukaswhite\PodcastFeedParser\Episode;
1212
use Lukaswhite\PodcastFeedParser\Podcast;
1313

14+
/**
15+
* @method void updateOrCreatePodcastEpisode(Episode $episode, Podcast $podcast, WireData $feed)
16+
* @method NullPage|Page getPodcastEpisodePageByIdOrCreateNew($episode_id, string $title = '')
17+
* @method float parseDuration($duration)
18+
*/
1419
class ProcessPodcastSubscriptionsEpisodes extends WireData implements Module, ConfigurableModule
1520
{
1621
public static function getModuleInfo() {
1722
return array(
1823
'title' => 'Process Podcast Create Episode Pages',
19-
'version' => 102,
24+
'version' => 103,
2025
'summary' => 'Example Module for creating podcast episodes pages with ProcessPodcastSubscriptions',
2126
'icon' => 'clock-o',
2227
'requires' => ['LazyCron', 'ProcessPodcastSubscriptions'],
@@ -55,13 +60,13 @@ class ProcessPodcastSubscriptionsEpisodes extends WireData implements Module, Co
5560
* @param Episode $episode
5661
* @param Podcast $podcast
5762
* @param WireData $feed
58-
* @return void|null
63+
* @return void
5964
* @throws WireException
6065
*/
6166
public function ___updateOrCreatePodcastEpisode(Episode $episode, Podcast $podcast, WireData $feed) {
6267

6368
$podcastId = $episode->getGuid();
64-
if(!$podcastId) return null;
69+
if(!$podcastId) return;
6570

6671
$title = $episode->getTitle();
6772

@@ -153,9 +158,9 @@ class ProcessPodcastSubscriptionsEpisodes extends WireData implements Module, Co
153158
/**
154159
* Parse time to seconds
155160
* @param $duration
156-
* @return float|int
161+
* @return float
157162
*/
158-
public function ___parseDuration($duration) {
163+
public function ___parseDuration($duration): float {
159164

160165
// HH:MM:SS, MM:SS, MMM:SS
161166
if (preg_match('/\:/', $duration)) {

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# ProcessPodcastSubscriptions
22

33
## What it does
4-
54
Subscribe to podcast RSS feeds and save the data as anything you want.
65
The module uses the great PHP Library [podcast-feed-parser](https://github.com/lukaswhite/podcast-feed-parser) by
76
Lukas White, which makes processing the podcast data a breeze. Thanks!
@@ -16,7 +15,6 @@ The module comes with an example module `ProcessPodcastSubscriptionsEpisodes` to
1615
- Optional module `ProcessPodcastSubscriptionsEpisodes`
1716

1817
## Install
19-
2018
1. Copy the files for this module to /site/modules/ProcessPodcastSubscriptions/
2119
2. Execute the following command in the /site/modules/ProcessPodcastSubscriptions/ directory.
2220
```bash
@@ -26,9 +24,13 @@ The module comes with an example module `ProcessPodcastSubscriptionsEpisodes` to
2624
4. Install the additional module `ProcessPodcastSubscriptionsEpisodes` (optional) or build your own processor
2725
5. Subscribe to Podcast feeds...
2826

27+
## Install via composer
28+
1. Execute the following command in your website root directory.
29+
```bash
30+
composer require nr/processpodcastsubscriptions
31+
```
2932

3033
## Configuration Subscriptions
31-
3234
`Modules` > `Configure` > `ProcessPodcastSubscriptions`
3335

3436
### Lazycron

composer.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
{
22
"name": "nr/processpodcastsubscriptions",
3+
"type": "pw-module",
4+
"description": "Subscribe Podcast RSS feed and save as new page",
5+
"keywords": [ "processwire", "module", "podcast" ],
6+
"homepage": "https://github.com/neuerituale/ProcessPodcastSubscriptions",
37
"license": "MIT",
48
"authors": [
59
{
610
"name": "Neue Rituale",
7-
"email": "code@neuerituale.com"
11+
"email": "code@neuerituale.com",
12+
"homepage": "https://neuerituale.com/",
13+
"role": "Developer"
814
}
915
],
16+
"minimum-stability": "beta",
1017
"require": {
11-
"lukaswhite/podcast-feed-parser": "^0.0.7"
18+
"hari/pw-module": "~1.0",
19+
"lukaswhite/podcast-feed-parser": "^1.2.0"
20+
},
21+
"config": {
22+
"allow-plugins": {
23+
"hari/pw-module": true
24+
}
1225
}
1326
}

templates/podcasts-example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class="uk-text-muted"
110110
: $datetime->formatDate($episode->created, '%e. %h %Y'); ?>
111111
</time>
112112
</span>
113-
<span style="white-space: nowrap"><?= sprintf(__('%d min.'), round($episode->episode_duration/60, 0)); ?></span>
113+
<span style="white-space: nowrap"><?= sprintf(__('%d min.'), round((int)$episode->episode_duration/60, 0)); ?></span>
114114
</div>
115115

116116
</a>

0 commit comments

Comments
 (0)