1
0
Fork 0

Merge pull request #10303 from annando/issue-10232

Issue 10232: Improved search for embedded media links
This commit is contained in:
Tobias Diekershoff 2021-05-23 15:30:53 +02:00 committed by GitHub
commit e2ad000273
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 4 deletions

View file

@ -2739,9 +2739,10 @@ class Item
* *
* @param string $body * @param string $body
* @param string $url * @param string $url
* @param int $type
* @return bool * @return bool
*/ */
public static function containsLink(string $body, string $url) public static function containsLink(string $body, string $url, int $type = 0)
{ {
// Make sure that for example site parameters aren't used when testing if the link is contained in the body // Make sure that for example site parameters aren't used when testing if the link is contained in the body
$urlparts = parse_url($url); $urlparts = parse_url($url);
@ -2749,6 +2750,12 @@ class Item
unset($urlparts['fragment']); unset($urlparts['fragment']);
$url = Network::unparseURL($urlparts); $url = Network::unparseURL($urlparts);
// Remove media links to only search in embedded content
// @todo Check images for image link, audio for audio links, ...
if (in_array($type, [Post\Media::AUDIO, Post\Media::VIDEO, Post\Media::IMAGE])) {
$body = preg_replace("/\[url=[^\[\]]*\](.*)\[\/url\]/Usi", ' $1 ', $body);
}
if (strpos($body, $url)) { if (strpos($body, $url)) {
return true; return true;
} }
@ -2777,7 +2784,7 @@ class Item
// @todo In the future we should make a single for the template engine with all media in it. This allows more flexibilty. // @todo In the future we should make a single for the template engine with all media in it. This allows more flexibilty.
foreach ($attachments['visual'] as $attachment) { foreach ($attachments['visual'] as $attachment) {
if (self::containsLink($item['body'], $attachment['url'])) { if (self::containsLink($item['body'], $attachment['url'], $attachment['type'])) {
continue; continue;
} }
@ -2955,7 +2962,7 @@ class Item
// @todo Use a template // @todo Use a template
$rendered = BBCode::convertAttachment('', BBCode::INTERNAL, false, $data); $rendered = BBCode::convertAttachment('', BBCode::INTERNAL, false, $data);
} elseif (!self::containsLink($content, $data['url'])) { } elseif (!self::containsLink($content, $data['url'], Post\Media::HTML)) {
$rendered = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/link.tpl'), [ $rendered = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/link.tpl'), [
'$url' => $data['url'], '$url' => $data['url'],
'$title' => $data['title'], '$title' => $data['title'],

View file

@ -600,7 +600,7 @@ class Media
$body = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", '', $body); $body = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", '', $body);
foreach (self::getByURIId($uriid, [self::IMAGE, self::AUDIO, self::VIDEO]) as $media) { foreach (self::getByURIId($uriid, [self::IMAGE, self::AUDIO, self::VIDEO]) as $media) {
if (Item::containsLink($body, $media['url'])) { if (Item::containsLink($body, $media['url'], $media['type'])) {
continue; continue;
} }