Avoid double fetches
authorMichael <heluecht@pirati.ca>
Tue, 16 Mar 2021 07:04:16 +0000 (07:04 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 16 Mar 2021 07:04:16 +0000 (07:04 +0000)
src/Content/OEmbed.php
src/Util/ParseUrl.php

index 4e26e45..bf3c113 100644 (file)
@@ -62,11 +62,12 @@ class OEmbed
         *
         * @param string $embedurl     The URL from which the data should be fetched.
         * @param bool   $no_rich_type If set to true rich type content won't be fetched.
+        * @param bool   $use_parseurl Use the "ParseUrl" functionality to add additional data
         *
         * @return \Friendica\Object\OEmbed
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public static function fetchURL($embedurl, $no_rich_type = false)
+       public static function fetchURL($embedurl, bool $no_rich_type = false, bool $use_parseurl = true)
        {
                $embedurl = trim($embedurl, '\'"');
 
@@ -149,48 +150,51 @@ class OEmbed
                }
 
                // Improve the OEmbed data with data from OpenGraph, Twitter cards and other sources
-               $data = ParseUrl::getSiteinfoCached($embedurl, true, false);
-               if (($oembed->type == 'error') && empty($data['title']) && empty($data['text'])) {
-                       return $oembed;
-               }
+               if ($use_parseurl) {
+                       $data = ParseUrl::getSiteinfoCached($embedurl, true, false);
+
+                       if (($oembed->type == 'error') && empty($data['title']) && empty($data['text'])) {
+                               return $oembed;
+                       }
 
-               if ($no_rich_type || ($oembed->type == 'error')) {
-                       $oembed->html = '';
-                       $oembed->type = $data['type'];
+                       if ($no_rich_type || ($oembed->type == 'error')) {
+                               $oembed->html = '';
+                               $oembed->type = $data['type'];
 
-                       if ($oembed->type == 'photo') {
-                               $oembed->url = $data['url'];
+                               if ($oembed->type == 'photo') {
+                                       $oembed->url = $data['url'];
+                               }
                        }
-               }
 
-               if (!empty($data['title'])) {
-                       $oembed->title = $data['title'];
-               }
+                       if (!empty($data['title'])) {
+                               $oembed->title = $data['title'];
+                       }
 
-               if (!empty($data['text'])) {
-                       $oembed->description = $data['text'];
-               }
+                       if (!empty($data['text'])) {
+                               $oembed->description = $data['text'];
+                       }
 
-               if (!empty($data['publisher_name'])) {
-                       $oembed->provider_name = $data['publisher_name'];
-               }
+                       if (!empty($data['publisher_name'])) {
+                               $oembed->provider_name = $data['publisher_name'];
+                       }
 
-               if (!empty($data['publisher_url'])) {
-                       $oembed->provider_url = $data['publisher_url'];
-               }
+                       if (!empty($data['publisher_url'])) {
+                               $oembed->provider_url = $data['publisher_url'];
+                       }
 
-               if (!empty($data['author_name'])) {
-                       $oembed->author_name = $data['author_name'];
-               }
+                       if (!empty($data['author_name'])) {
+                               $oembed->author_name = $data['author_name'];
+                       }
 
-               if (!empty($data['author_url'])) {
-                       $oembed->author_url = $data['author_url'];
-               }
+                       if (!empty($data['author_url'])) {
+                               $oembed->author_url = $data['author_url'];
+                       }
 
-               if (!empty($data['images'])) {
-                       $oembed->thumbnail_url = $data['images'][0]['src'];
-                       $oembed->thumbnail_width = $data['images'][0]['width'];
-                       $oembed->thumbnail_height = $data['images'][0]['height'];
+                       if (!empty($data['images'])) {
+                               $oembed->thumbnail_url = $data['images'][0]['src'];
+                               $oembed->thumbnail_width = $data['images'][0]['width'];
+                               $oembed->thumbnail_height = $data['images'][0]['height'];
+                       }
                }
 
                Hook::callAll('oembed_fetch_url', $embedurl, $oembed);
index 8bde05f..cca158c 100644 (file)
@@ -241,7 +241,7 @@ class ParseUrl
                $body = $curlResult->getBody();
 
                if ($do_oembed) {
-                       $oembed_data = OEmbed::fetchURL($url);
+                       $oembed_data = OEmbed::fetchURL($url, false, false);
 
                        if (!empty($oembed_data->type)) {
                                if (!in_array($oembed_data->type, ['error', 'rich', 'image', 'video', 'audio', ''])) {
@@ -250,13 +250,25 @@ class ParseUrl
 
                                // See https://github.com/friendica/friendica/pull/5763#discussion_r217913178
                                if ($siteinfo['type'] != 'photo') {
-                                       if (isset($oembed_data->title)) {
+                                       if (!empty($oembed_data->title)) {
                                                $siteinfo['title'] = trim($oembed_data->title);
                                        }
-                                       if (isset($oembed_data->description)) {
+                                       if (!empty($oembed_data->description)) {
                                                $siteinfo['text'] = trim($oembed_data->description);
                                        }
-                                       if (isset($oembed_data->thumbnail_url)) {
+                                       if (!empty($oembed_data->author_name)) {
+                                               $siteinfo['author_name'] = trim($oembed_data->author_name);
+                                       }
+                                       if (!empty($oembed_data->author_url)) {
+                                               $siteinfo['author_url'] = trim($oembed_data->author_url);
+                                       }
+                                       if (!empty($oembed_data->provider_name)) {
+                                               $siteinfo['publisher_name'] = trim($oembed_data->provider_name);
+                                       }
+                                       if (!empty($oembed_data->provider_url)) {
+                                               $siteinfo['publisher_url'] = trim($oembed_data->provider_url);
+                                       }
+                                       if (!empty($oembed_data->thumbnail_url)) {
                                                $siteinfo['image'] = $oembed_data->thumbnail_url;
                                        }
                                }