diff --git a/src/Utility/OEmbed.php b/src/Utility/OEmbed.php index cd7900f95..f5ea7be46 100644 --- a/src/Utility/OEmbed.php +++ b/src/Utility/OEmbed.php @@ -31,7 +31,7 @@ class OEmbed */ public function readMetadata(string $url): array { - $parsed = parse_url($url); + $parsed = (array)parse_url($url); $host = Hash::get($parsed, 'host', ''); $provider = $this->findProvider($host); $oEmbedUrl = Hash::get($provider, 'url'); @@ -42,7 +42,7 @@ public function readMetadata(string $url): array $oEmbedUrl = sprintf('%s?url=%s&format=json', $oEmbedUrl, rawurlencode($url)); // custom UID if not found via oEmbed => query or path - $providerUID = Hash::get($parsed, 'query', Hash::get($parsed, 'path')); + $providerUID = $this->providerUid($parsed, $host); $meta = $this->fetchJson($oEmbedUrl, (array)Hash::get($provider, 'options')); if (empty($meta)) { @@ -60,6 +60,66 @@ public function readMetadata(string $url): array ]; } + /** + * Get provider uid from url and host + * + * @param array $parsedUrl The parsed URL array + * @param string $host The host + * @return string + */ + public function providerUid(array $parsedUrl, string $host): string + { + $host = strtolower($host); + $path = trim((string)Hash::get($parsedUrl, 'path'), '/'); + $segments = $path === '' ? [] : explode('/', $path); + $query = []; + parse_str((string)Hash::get($parsedUrl, 'query'), $query); + + if ($host === 'youtu.be') { + return $segments[0] ?? ''; + } + + if (in_array($host, ['youtube.com', 'www.youtube.com'], true)) { + foreach (['v', 'list'] as $key) { + if (!empty($query[$key])) { + return (string)$query[$key]; + } + } + foreach (['embed', 'shorts', 'live', 'v'] as $marker) { + $index = array_search($marker, $segments, true); + if ($index !== false && isset($segments[$index + 1])) { + return $segments[$index + 1]; + } + } + + return $path; + } + + if ($host === 'soundcloud.com') { + return $path; + } + + if (str_ends_with($host, '.spotify.com')) { + return $segments[array_key_last($segments)] ?? ''; + } + + if ($host === 'vimeo.com' || str_ends_with($host, '.vimeo.com')) { + $videoIndex = array_search('video', $segments, true); + if ($videoIndex !== false && isset($segments[$videoIndex + 1]) && ctype_digit($segments[$videoIndex + 1])) { + return $segments[$videoIndex + 1]; + } + foreach ($segments as $segment) { + if (ctype_digit($segment)) { + return $segment; + } + } + + return $path; + } + + return (string)Hash::get($parsedUrl, 'query', $path); + } + /** * Fetch oEmbed JSON response from oEmbed provider URL * diff --git a/tests/TestCase/Controller/Component/ModulesComponentTest.php b/tests/TestCase/Controller/Component/ModulesComponentTest.php index d587ba734..adcdcd9c1 100644 --- a/tests/TestCase/Controller/Component/ModulesComponentTest.php +++ b/tests/TestCase/Controller/Component/ModulesComponentTest.php @@ -1024,7 +1024,7 @@ public static function uploadProvider(): array null, [ 'provider' => 'YouTube', - 'provider_uid' => 'v=fE50xrnJnR8', + 'provider_uid' => 'fE50xrnJnR8', ], null, ], diff --git a/tests/TestCase/Utility/OEmbedTest.php b/tests/TestCase/Utility/OEmbedTest.php index 1565a4ac4..566ca156e 100644 --- a/tests/TestCase/Utility/OEmbedTest.php +++ b/tests/TestCase/Utility/OEmbedTest.php @@ -24,8 +24,60 @@ #[CoversClass(OEmbed::class)] #[CoversMethod(OEmbed::class, 'findProvider')] #[CoversMethod(OEmbed::class, 'readMetadata')] +#[CoversMethod(OEmbed::class, 'providerUid')] class OEmbedTest extends TestCase { + /** + * Data provider for `testProviderUid` + * + * @return array + */ + public static function providerUidProvider(): array + { + return [ + 'youtube watch share link' => [ + 'WGyEelzJaMs', + 'https://www.youtube.com/watch?v=WGyEelzJaMs&si=58cEvfbrPf_ICutz', + ], + 'youtube short share link' => [ + 'WGyEelzJaMs', + 'https://youtu.be/WGyEelzJaMs?si=58cEvfbrPf_ICutz', + ], + 'youtube no share link' => [ + 'WGyEelzJaMs', + 'https://www.youtube.com/WGyEelzJaMs', + ], + 'youtube shorts path' => [ + 'WGyEelzJaMs', + 'https://www.youtube.com/shorts/WGyEelzJaMs?si=58cEvfbrPf_ICutz', + ], + 'spotify track share link' => [ + '4uLU6hMCjMI75M1A2tKUQC', + 'https://open.spotify.com/intl-it/track/4uLU6hMCjMI75M1A2tKUQC?si=share-token', + ], + 'soundcloud track share link' => [ + 'artist/track-name', + 'https://soundcloud.com/artist/track-name?utm_source=clipboard&utm_medium=text', + ], + 'vimeo share link' => [ + '123456789', + 'https://player.vimeo.com/video/123456789?h=private-hash&share=copy', + ], + 'vimeo showcase link' => [ + '987654321', + 'https://vimeo.com/showcase/123456/video/987654321?share=copy', + ], + 'vimeo unlisted link with numeric hash' => [ + '123456789', + 'https://vimeo.com/123456789/9876543210?share=copy', + ], + 'other host' => [ + '123', + 'https://other.com/123', + ], + ]; + } + /** * Data provider for `testReadMetadata` * @@ -55,7 +107,7 @@ public static function readMetadataProvider(): array 'description' => null, 'provider' => 'YouTube', 'provider_thumbnail' => 'https://i.ytimg.com/vi/_qSYCXHNvJo/hqdefault.jpg', - 'provider_uid' => 'v=_qSYCXHNvJo', + 'provider_uid' => '_qSYCXHNvJo', 'provider_extra' => [ 'title' => 'Inscenare la Corazzata Potëmkin all\'imbrunire', 'provider_name' => 'YouTube', @@ -71,9 +123,40 @@ public static function readMetadataProvider(): array 'provider_url' => 'https://www.youtube.com/', ], ], + 'youtube short link with share parameter' => [ + [ + 'provider_url' => 'https://youtu.be/WGyEelzJaMs?si=58cEvfbrPf_ICutz', + 'title' => null, + 'description' => null, + 'provider' => 'YouTube', + 'provider_uid' => 'WGyEelzJaMs', + 'provider_thumbnail' => null, + 'provider_extra' => [ + 'provider_name' => 'YouTube', + ], + ], + 'https://youtu.be/WGyEelzJaMs?si=58cEvfbrPf_ICutz', + [ + 'provider_name' => 'YouTube', + ], + ], ]; } + /** + * Test `providerUid` method + * + * @return void + */ + #[DataProvider('providerUidProvider')] + public function testProviderUid(string $expected, string $url): void + { + $parsedUrl = (array)parse_url($url); + $oembed = new OEmbed(); + + static::assertSame($expected, $oembed->providerUid($parsedUrl, $parsedUrl['host'])); + } + /** * Test `readMetadata` method *