Improve page load performance with attached links

This commit is contained in:
Michael 2021-04-27 12:29:54 +00:00
parent 53e38b0313
commit 44c801905a
2 changed files with 36 additions and 22 deletions

View File

@ -605,16 +605,19 @@ class BBCode
* @param string $text * @param string $text
* @param integer $simplehtml * @param integer $simplehtml
* @param bool $tryoembed * @param bool $tryoembed
* @param array $data
* @return string * @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
private static function convertAttachment($text, $simplehtml = self::INTERNAL, $tryoembed = true) public static function convertAttachment($text, $simplehtml = self::INTERNAL, $tryoembed = true, array $data = [])
{ {
$data = self::getAttachmentData($text); $data = $data ?: self::getAttachmentData($text);
if (empty($data) || empty($data['url'])) { if (empty($data) || empty($data['url'])) {
return $text; return $text;
} }
$stamp1 = microtime(true);
if (isset($data['title'])) { if (isset($data['title'])) {
$data['title'] = strip_tags($data['title']); $data['title'] = strip_tags($data['title']);
$data['title'] = str_replace(['http://', 'https://'], '', $data['title']); $data['title'] = str_replace(['http://', 'https://'], '', $data['title']);
@ -655,9 +658,8 @@ class BBCode
} }
if (!empty($data['description']) && $data['description'] != $data['title']) { if (!empty($data['description']) && $data['description'] != $data['title']) {
// Sanitize the HTML by converting it to BBCode // Sanitize the HTML
$bbcode = HTML::toBBCode($data['description']); $return .= sprintf('<blockquote>%s</blockquote>', trim(HTML::purify($data['description'])));
$return .= sprintf('<blockquote>%s</blockquote>', trim(self::convert($bbcode)));
} }
if (!empty($data['provider_url']) && !empty($data['provider_name'])) { if (!empty($data['provider_url']) && !empty($data['provider_name'])) {
@ -673,6 +675,7 @@ class BBCode
} }
} }
DI::profiler()->saveTimestamp($stamp1, 'rendering');
return trim(($data['text'] ?? '') . ' ' . $return . ' ' . ($data['after'] ?? '')); return trim(($data['text'] ?? '') . ' ' . $return . ' ' . ($data['after'] ?? ''));
} }

View File

@ -2734,6 +2734,7 @@ class Item
*/ */
private static function addVisualAttachments(array $attachments, array $item, string $content, bool $shared) private static function addVisualAttachments(array $attachments, array $item, string $content, bool $shared)
{ {
$stamp1 = microtime(true);
$leading = ''; $leading = '';
$trailing = ''; $trailing = '';
@ -2797,6 +2798,7 @@ class Item
} }
} }
DI::profiler()->saveTimestamp($stamp1, 'rendering');
return $content; return $content;
} }
@ -2811,6 +2813,7 @@ class Item
*/ */
private static function addLinkAttachment(array $attachments, array $item, string $content, bool $shared, string $ignore_link) private static function addLinkAttachment(array $attachments, array $item, string $content, bool $shared, string $ignore_link)
{ {
$stamp1 = microtime(true);
// @ToDo Check only for audio and video // @ToDo Check only for audio and video
$preview = empty($attachments['visual']); $preview = empty($attachments['visual']);
@ -2823,31 +2826,37 @@ class Item
} }
if (!empty($attachment)) { if (!empty($attachment)) {
$footer = '';
$data = [ $data = [
'author_img' => $attachment['author-image'] ?? '', 'after' => '',
'author_name' => $attachment['author-name'] ?? '', 'author_name' => $attachment['author-name'] ?? '',
'author_url' => $attachment['author-url'] ?? '', 'author_url' => $attachment['author-url'] ?? '',
'publisher_img' => $attachment['publisher-image'] ?? '', 'description' => $attachment['description'] ?? '',
'publisher_name' => $attachment['publisher-name'] ?? '', 'image' => '',
'publisher_url' => $attachment['publisher-url'] ?? '', 'preview' => '',
'text' => $attachment['description'] ?? '', 'provider_name' => $attachment['publisher-name'] ?? '',
'provider_url' => $attachment['publisher-url'] ?? '',
'text' => '',
'title' => $attachment['name'] ?? '', 'title' => $attachment['name'] ?? '',
'type' => 'link', 'type' => 'link',
'url' => $attachment['url'] ?? '', 'url' => $attachment['url']];
];
if ($preview && !empty($attachment['preview']) && !empty($attachment['preview-height']) && !empty($attachment['preview-width'])) { if ($preview) {
$data['images'][] = ['src' => $attachment['preview'], if ($attachment['preview-width'] >= 500) {
'width' => $attachment['preview-width'], 'height' => $attachment['preview-height']]; $data['image'] = $attachment['preview'] ?? '';
} else {
$data['preview'] = $attachment['preview'] ?? '';
}
} }
$footer = PageInfo::getFooterFromData($data);
} elseif (preg_match("/.*(\[attachment.*?\].*?\[\/attachment\]).*/ism", $item['body'], $match)) { } elseif (preg_match("/.*(\[attachment.*?\].*?\[\/attachment\]).*/ism", $item['body'], $match)) {
$footer = $match[1]; $footer = $match[1];
$data = [];
} }
DI::profiler()->saveTimestamp($stamp1, 'rendering');
if (!empty($footer)) { if (!empty($footer) || !empty($data)) {
// @todo Use a template // @todo Use a template
$rendered = BBCode::convert($footer); $rendered = BBCode::convertAttachment($footer, BBCode::INTERNAL, false, $data);
if ($shared) { if ($shared) {
return str_replace(BBCode::ANCHOR, BBCode::ANCHOR . $rendered, $content); return str_replace(BBCode::ANCHOR, BBCode::ANCHOR . $rendered, $content);
} else { } else {
@ -2867,6 +2876,7 @@ class Item
*/ */
private static function addNonVisualAttachments(array $attachments, array $item, string $content) private static function addNonVisualAttachments(array $attachments, array $item, string $content)
{ {
$stamp1 = microtime(true);
$trailing = ''; $trailing = '';
foreach ($attachments['additional'] as $attachment) { foreach ($attachments['additional'] as $attachment) {
if (strpos($item['body'], $attachment['url'])) { if (strpos($item['body'], $attachment['url'])) {
@ -2892,6 +2902,7 @@ class Item
$content .= '<div class="body-attach">' . $trailing . '<div class="clear"></div></div>'; $content .= '<div class="body-attach">' . $trailing . '<div class="clear"></div></div>';
} }
DI::profiler()->saveTimestamp($stamp1, 'rendering');
return $content; return $content;
} }