]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Merge pull request #13541 from MrPetovan/bug/13534-image-blurred
[friendica.git] / src / Model / Item.php
index 0293517377e7024239db75ccf1b53a3b2ec052c8..5f83ae38dad2e6f030602c91735fc4e91ab14a59 100644 (file)
@@ -22,6 +22,7 @@
 namespace Friendica\Model;
 
 use Friendica\Contact\LocalRelationship\Entity\LocalRelationship;
+use Friendica\Content\Image;
 use Friendica\Content\Post\Collection\PostMedias;
 use Friendica\Content\Post\Entity\PostMedia;
 use Friendica\Content\Text\BBCode;
@@ -1211,8 +1212,6 @@ class Item
                // Check for hashtags in the body and repair or add hashtag links
                $item['body'] = self::setHashtags($item['body']);
 
-               $item['language'] = self::getLanguage($item);
-
                $notify_type = Delivery::POST;
 
                // Filling item related side tables
@@ -1261,6 +1260,8 @@ class Item
                        }
                }
 
+               $item['language'] = self::getLanguage($item);
+
                $inserted = Post::insert($item['uri-id'], $item);
 
                if ($item['gravity'] == self::GRAVITY_PARENT) {
@@ -1990,7 +1991,7 @@ class Item
                        return '';
                }
 
-               $languages = self::getLanguageArray(trim($item['title'] . "\n" . $item['body']), 3, $item['uri-id'], $item['author-id']);
+               $languages = self::getLanguageArray($item['title'] . ' ' . ($item['content-warning'] ?? '') . ' ' . $item['body'], 3, $item['uri-id'], $item['author-id']);
                if (empty($languages)) {
                        return '';
                }
@@ -2009,67 +2010,122 @@ class Item
         */
        public static function getLanguageArray(string $body, int $count, int $uri_id = 0, int $author_id = 0): array
        {
-               $naked_body = BBCode::toSearchText($body, $uri_id);
+               $searchtext = BBCode::toSearchText($body, $uri_id);
 
-               if ((count(explode(' ', $naked_body)) < 10) && (mb_strlen($naked_body) < 30) && $author_id) {
+               if ((count(explode(' ', $searchtext)) < 10) && (mb_strlen($searchtext) < 30) && $author_id) {
                        $author = Contact::selectFirst(['about'], ['id' => $author_id]);
                        if (!empty($author['about'])) {
                                $about = BBCode::toSearchText($author['about'], 0);
-                               $about = self::getDominantLanguage($about);
-                               Logger::debug('About field added', ['author' => $author_id, 'body' => $naked_body, 'about' => $about]);
-                               $naked_body .= ' ' . $about;
+                               Logger::debug('About field added', ['author' => $author_id, 'body' => $searchtext, 'about' => $about]);
+                               $searchtext .= ' ' . $about;
                        }
                }
 
-               if (empty($naked_body)) {
+               if (empty($searchtext)) {
                        return [];
                }
 
-               $naked_body = self::getDominantLanguage($naked_body);
-
                $availableLanguages = DI::l10n()->getAvailableLanguages(true);
                $availableLanguages = DI::l10n()->convertForLanguageDetection($availableLanguages);
 
                $ld = new Language(array_keys($availableLanguages));
-               $languages = $ld->detect($naked_body)->limit(0, $count)->close() ?: [];
 
-               $data = [
-                       'text'     => $naked_body,
-                       'detected' => $languages,
-                       'uri-id'   => $uri_id,
-               ];
+               $result = [];
 
-               Hook::callAll('detect_languages', $data);
-               $languages = $data['detected'];
+               foreach (self::splitByBlocks($searchtext) as $block) {
+                       $languages = $ld->detect($block)->limit(0, $count)->close() ?: [];
 
-               return $languages;
+                       $data = [
+                               'text'      => $block,
+                               'detected'  => $languages,
+                               'uri-id'    => $uri_id,
+                               'author-id' => $author_id,
+                       ];
+                       Hook::callAll('detect_languages', $data);
+
+                       foreach ($data['detected'] as $language => $quality) {
+                               $result[$language] = max($result[$language] ?? 0, $quality * (strlen($block) / strlen($searchtext)));
+                       }
+               }
+
+               arsort($result);
+               $result = array_slice($result, 0, $count);
+
+               return $result;
        }
 
        /**
-        * Check if latin or non latin are dominant in the body and only return the dominant one
+        * Split a string into different unicode blocks
+        * Currently the text is split into the latin and the non latin part.
         *
         * @param string $body
-        * @return string
+        * @return array
         */
-       private static function getDominantLanguage(string $body): string
+       private static function splitByBlocks(string $body): array
        {
-               $latin = '';
-               $non_latin = '';
+               if (!class_exists('IntlChar')) {
+                       return [$body];
+               }
+
+               $blocks         = [];
+               $previous_block = 0;
+
                for ($i = 0; $i < mb_strlen($body); $i++) {
                        $character = mb_substr($body, $i, 1);
-                       $ord = mb_ord($character);
-
-                       // We add the most common characters to both strings.
-                       if (($ord <= 64) || ($ord >= 91 && $ord <= 96) || ($ord >= 123 && $ord <= 191) || in_array($ord, [215, 247]) || ($ord >= 697 && $ord <= 735) || ($ord > 65535)) {
-                               $latin .= $character;
-                               $non_latin .= $character;
-                       } elseif ($ord < 768) {
-                               $latin .= $character;
+                       $previous  = ($i > 0) ? mb_substr($body, $i - 1, 1) : '';
+                       $next      = ($i < mb_strlen($body)) ? mb_substr($body, $i + 1, 1) : '';
+
+                       if (!\IntlChar::isalpha($character)) {
+                               if (($previous != '') && (\IntlChar::isalpha($previous))) {
+                                       $previous_block = self::getBlockCode($previous);
+                               }
+
+                               $block = (($next != '') && \IntlChar::isalpha($next)) ? self::getBlockCode($next) : $previous_block;
+                               $blocks[$block] = ($blocks[$block] ?? '') . $character;
                        } else {
-                               $non_latin .= $character;
+                               $block = self::getBlockCode($character);
+                               $blocks[$block] = ($blocks[$block] ?? '') . $character;
+                       }
+               }
+
+               foreach (array_keys($blocks) as $key) {
+                       $blocks[$key] = trim($blocks[$key]);
+                       if (empty($blocks[$key])) {
+                               unset($blocks[$key]);
                        }
                }
-               return (mb_strlen($latin) > mb_strlen($non_latin)) ? $latin : $non_latin;
+
+               return array_values($blocks);
+       }
+
+       /**
+        * returns the block code for the given character
+        *
+        * @param string $character
+        * @return integer 0 = no alpha character (blank, signs, emojis, ...), 1 = latin character, 2 = character in every other language
+        */
+       private static function getBlockCode(string $character): int
+       {
+               if (!\IntlChar::isalpha($character)) {
+                       return 0;
+               }
+               return self::isLatin($character) ? 1 : 2;
+       }
+
+       /**
+        * Checks if the given character is in one of the latin code blocks
+        *
+        * @param string $character
+        * @return boolean
+        */
+       private static function isLatin(string $character): bool
+       {
+               return in_array(\IntlChar::getBlockCode($character), [
+                       \IntlChar::BLOCK_CODE_BASIC_LATIN, \IntlChar::BLOCK_CODE_LATIN_1_SUPPLEMENT,
+                       \IntlChar::BLOCK_CODE_LATIN_EXTENDED_A, \IntlChar::BLOCK_CODE_LATIN_EXTENDED_B,
+                       \IntlChar::BLOCK_CODE_LATIN_EXTENDED_C, \IntlChar::BLOCK_CODE_LATIN_EXTENDED_D,
+                       \IntlChar::BLOCK_CODE_LATIN_EXTENDED_E, \IntlChar::BLOCK_CODE_LATIN_EXTENDED_ADDITIONAL
+               ]);
        }
 
        public static function getLanguageMessage(array $item): string
@@ -2078,7 +2134,7 @@ class Item
 
                $used_languages = '';
                foreach (json_decode($item['language'], true) as $language => $reliability) {
-                       $used_languages .= $iso639->languageByCode1($language) . ' (' . $language . "): " . number_format($reliability, 5) . '\n';
+                       $used_languages .= $iso639->nativeByCode1(substr($language, 0, 2)) . ' (' . $iso639->languageByCode1(substr($language, 0, 2)) . ' - ' . $language . "): " . number_format($reliability, 5) . '\n';
                }
                $used_languages = DI::l10n()->t('Detected languages in this post:\n%s', $used_languages);
                return $used_languages;
@@ -3241,7 +3297,7 @@ class Item
                }
 
                if (!empty($sharedSplitAttachments)) {
-                       $s = self::addGallery($s, $sharedSplitAttachments['visual'], $item['uri-id']);
+                       $s = self::addGallery($s, $sharedSplitAttachments['visual']);
                        $s = self::addVisualAttachments($sharedSplitAttachments['visual'], $shared_item, $s, true);
                        $s = self::addLinkAttachment($shared_uri_id ?: $item['uri-id'], $sharedSplitAttachments, $body, $s, true, $quote_shared_links);
                        $s = self::addNonVisualAttachments($sharedSplitAttachments['additional'], $item, $s, true);
@@ -3254,7 +3310,7 @@ class Item
                        $s = substr($s, 0, $pos);
                }
 
-               $s = self::addGallery($s, $itemSplitAttachments['visual'], $item['uri-id']);
+               $s = self::addGallery($s, $itemSplitAttachments['visual']);
                $s = self::addVisualAttachments($itemSplitAttachments['visual'], $item, $s, false);
                $s = self::addLinkAttachment($item['uri-id'], $itemSplitAttachments, $body, $s, false, $shared_links);
                $s = self::addNonVisualAttachments($itemSplitAttachments['additional'], $item, $s, false);
@@ -3285,45 +3341,33 @@ class Item
                return $hook_data['html'];
        }
 
-       /**
-        * @param array $images
-        * @return string
-        * @throws \Friendica\Network\HTTPException\ServiceUnavailableException
-        */
-       private static function makeImageGrid(array $images): string
-       {
-               // Image for first column (fc) and second column (sc)
-               $images_fc = [];
-               $images_sc = [];
-
-               for ($i = 0; $i < count($images); $i++) {
-                       ($i % 2 == 0) ? ($images_fc[] = $images[$i]) : ($images_sc[] = $images[$i]);
-               }
-
-               return Renderer::replaceMacros(Renderer::getMarkupTemplate('content/image_grid.tpl'), [
-                       'columns' => [
-                               'fc' => $images_fc,
-                               'sc' => $images_sc,
-                       ],
-               ]);
-       }
-
        /**
         * Modify links to pictures to links for the "Fancybox" gallery
         *
         * @param string     $s
         * @param PostMedias $PostMedias
-        * @param int        $uri_id
         * @return string
         */
-       private static function addGallery(string $s, PostMedias $PostMedias, int $uri_id): string
+       private static function addGallery(string $s, PostMedias $PostMedias): string
        {
                foreach ($PostMedias as $PostMedia) {
                        if (!$PostMedia->preview || ($PostMedia->type !== Post\Media::IMAGE)) {
                                continue;
                        }
 
-                       $s = str_replace('<a href="' . $PostMedia->url . '"', '<a data-fancybox="' . $uri_id . '" href="' . $PostMedia->url . '"', $s);
+                       if ($PostMedia->hasDimensions()) {
+                               $pattern = '#<a href="' . preg_quote($PostMedia->url) . '">(.*?)"></a>#';
+
+                               $s = preg_replace_callback($pattern, function () use ($PostMedia) {
+                                       return Renderer::replaceMacros(Renderer::getMarkupTemplate('content/image/single_with_height_allocation.tpl'), [
+                                               '$image' => $PostMedia,
+                                               '$allocated_height' => $PostMedia->getAllocatedHeight(),
+                                               '$allocated_max_width' => ($PostMedia->previewWidth ?? $PostMedia->width) . 'px',
+                                       ]);
+                               }, $s);
+                       } else {
+                               $s = str_replace('<a href="' . $PostMedia->url . '"', '<a data-fancybox="uri-id-' . $PostMedia->uriId . '" href="' . $PostMedia->url . '"', $s);
+                       }
                }
 
                return $s;
@@ -3431,7 +3475,7 @@ class Item
                DI::profiler()->startRecording('rendering');
                $leading  = '';
                $trailing = '';
-               $images   = [];
+               $images   = new PostMedias();
 
                // @todo In the future we should make a single for the template engine with all media in it. This allows more flexibilty.
                foreach ($PostMedias as $PostMedia) {
@@ -3439,11 +3483,11 @@ class Item
                                continue;
                        }
 
-                       if ($PostMedia->mimetype->type == 'image') {
-                               $preview_url = DI::baseUrl() . $PostMedia->getPreviewPath($PostMedia->width > $PostMedia->height ? Proxy::SIZE_MEDIUM : Proxy::SIZE_LARGE);
-                       } elseif ($PostMedia->preview) {
-                               $preview_url = DI::baseUrl() . $PostMedia->getPreviewPath(Proxy::SIZE_LARGE);
+                       if ($PostMedia->mimetype->type == 'image' || $PostMedia->preview) {
+                               $preview_size = Proxy::SIZE_MEDIUM;
+                               $preview_url = DI::baseUrl() . $PostMedia->getPreviewPath($preview_size);
                        } else {
+                               $preview_size = 0;
                                $preview_url = '';
                        }
 
@@ -3487,22 +3531,11 @@ class Item
                                        continue;
                                }
 
-                               $images[] = [
-                                       'src'        => $src_url,
-                                       'preview'    => $preview_url,
-                                       'attachment' => $PostMedia,
-                               ];
+                               $images[] = $PostMedia->withUrl(new Uri($src_url))->withPreview(new Uri($preview_url), $preview_size);
                        }
                }
 
-               $media = '';
-               if (count($images) > 1) {
-                       $media = self::makeImageGrid($images);
-               } elseif (count($images) == 1) {
-                       $media = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/image.tpl'), [
-                               '$image' => $images[0],
-                       ]);
-               }
+               $media = Image::getBodyAttachHtml($images);
 
                // On Diaspora posts the attached pictures are leading
                if ($item['network'] == Protocol::DIASPORA) {