From ae53adefadea4b05a419afff1f2ae447bf555132 Mon Sep 17 00:00:00 2001 From: Dag Date: Tue, 26 Sep 2023 00:27:45 +0200 Subject: [PATCH] refactor: FeedItem::setTimestamp() (#3711) --- bridges/CNETFranceBridge.php | 3 ++- bridges/MastodonBridge.php | 9 +++++---- docs/04_For_Developers/index.md | 8 +++++--- lib/FeedItem.php | 14 ++++++++------ tests/FeedItemTest.php | 19 ++++++++++++++++++- 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/bridges/CNETFranceBridge.php b/bridges/CNETFranceBridge.php index 724564fa..da808596 100644 --- a/bridges/CNETFranceBridge.php +++ b/bridges/CNETFranceBridge.php @@ -54,7 +54,8 @@ class CNETFranceBridge extends FeedExpander } foreach ($this->bannedURL as $term) { - if (preg_match('/' . $term . '/mi', $item['uri']) === 1) { + $preg_match = preg_match('#' . $term . '#mi', $item['uri']); + if ($preg_match === 1) { return null; } } diff --git a/bridges/MastodonBridge.php b/bridges/MastodonBridge.php index 54ac55bd..cae556b8 100644 --- a/bridges/MastodonBridge.php +++ b/bridges/MastodonBridge.php @@ -161,8 +161,8 @@ class MastodonBridge extends BridgeAbstract $object = $this->fetchAP($object); } - $item['content'] = $object['content']; - $strippedContent = strip_tags(str_replace('
', ' ', $object['content'])); + $item['content'] = $object['content'] ?? ''; + $strippedContent = strip_tags(str_replace('
', ' ', $item['content'])); if (isset($object['name'])) { $item['title'] = $object['name']; @@ -186,9 +186,10 @@ class MastodonBridge extends BridgeAbstract foreach ($object['attachment'] as $attachment) { // Only process REMOTE pictures (prevent xss) + $mediaType = $attachment['mediaType'] ?? null; if ( - $attachment['mediaType'] - && preg_match('/^image\//', $attachment['mediaType'], $match) + $mediaType + && preg_match('/^image\//', $mediaType, $match) && preg_match('/^http(s|):\/\//', $attachment['url'], $match) ) { $item['content'] = $item['content'] . '
timestamp; } - public function setTimestamp($timestamp) + public function setTimestamp($datetime) { $this->timestamp = null; - if ( - !is_numeric($timestamp) - && !$timestamp = strtotime($timestamp) - ) { - Debug::log('Unable to parse timestamp!'); + if (is_numeric($datetime)) { + $timestamp = $datetime; + } else { + $timestamp = strtotime($datetime); + if ($timestamp === false) { + Debug::log('Unable to parse timestamp!'); + } } if ($timestamp <= 0) { Debug::log('Timestamp must be greater than zero!'); diff --git a/tests/FeedItemTest.php b/tests/FeedItemTest.php index 92833753..0e7af222 100644 --- a/tests/FeedItemTest.php +++ b/tests/FeedItemTest.php @@ -24,7 +24,24 @@ class FeedItemTest extends TestCase $item = new FeedItem(); $item->title = 'aa'; - $this->assertSame('aa', $item->getTitle()); $this->assertSame('aa', $item->title); + $this->assertSame('aa', $item->getTitle()); + } + + public function testTimestamp() + { + $item = new FeedItem(); + $item->setTimestamp(5); + $this->assertSame(5, $item->getTimestamp()); + + $item->setTimestamp('5'); + $this->assertSame(5, $item->getTimestamp()); + + $item->setTimestamp('1970-01-01 18:00:00'); + $this->assertSame(64800, $item->getTimestamp()); + + $item->setTimestamp('1st jan last year'); + // This will fail at 2024-01-01 hehe + $this->assertSame(1640995200, $item->getTimestamp()); } }