]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Merge remote-tracking branch 'upstream/2022.09-rc' into language
[friendica.git] / src / Model / Item.php
index 598fca84073fb4df2db22bfc936d499e51867ce3..78bc8c64d18b26410117ba65d0546db74ebb32e8 100644 (file)
@@ -279,7 +279,7 @@ class Item
                        if ($item['uid'] == $uid) {
                                self::markForDeletionById($item['id'], PRIORITY_HIGH);
                        } elseif ($item['uid'] != 0) {
-                               Logger::notice('Wrong ownership. Not deleting item', ['id' => $item['id']]);
+                               Logger::warning('Wrong ownership. Not deleting item', ['id' => $item['id']]);
                        }
                }
                DBA::close($items);
@@ -580,7 +580,7 @@ class Item
                if (!empty($item['uid'])) {
                        $owner = User::getOwnerDataById($item['uid'], false);
                        if (!$owner) {
-                               Logger::notice('Missing item user owner data', ['uid' => $item['uid']]);
+                               Logger::warning('Missing item user owner data', ['uid' => $item['uid']]);
                                return false;
                        }
 
@@ -919,8 +919,6 @@ class Item
 
                $item['gravity'] = self::getGravity($item);
 
-               $item['language'] = self::getLanguage($item);
-
                $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
                        'photo' => $item['author-avatar'], 'network' => $item['network']];
                $item['author-id'] = ($item['author-id'] ?? 0) ?: Contact::getIdForURL($item['author-link'], 0, null, $default);
@@ -1108,6 +1106,8 @@ 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
@@ -1869,6 +1869,8 @@ class Item
                        return '';
                }
 
+               $naked_body = self::getDominantLanguage($naked_body);
+
                $availableLanguages = DI::l10n()->getAvailableLanguages();
                // See https://github.com/friendica/friendica/issues/10511
                // Persian is manually added to language detection until a persian translation is provided for the interface, at
@@ -1884,6 +1886,33 @@ class Item
                return '';
        }
 
+       /**
+        * Check if latin or non latin are dominant in the body and only return the dominant one
+        *
+        * @param string $body
+        * @return string
+        */
+       private static function getDominantLanguage(string $body): string
+       {
+               $latin = '';
+               $non_latin = '';
+               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;
+                       } else {
+                               $non_latin .= $character;
+                       }
+               }
+               return (mb_strlen($latin) > mb_strlen($non_latin)) ? $latin : $non_latin;
+       }
+
        public static function getLanguageMessage(array $item): string
        {
                $iso639 = new \Matriphe\ISO639\ISO639;
@@ -2515,7 +2544,7 @@ class Item
 
                $item = Post::selectFirst(self::ITEM_FIELDLIST, ['id' => $item_id]);
                if (!DBA::isResult($item)) {
-                       Logger::notice('like: unknown item', ['id' => $item_id]);
+                       Logger::warning('Post had not been fetched', ['id' => $item_id]);
                        return false;
                }
 
@@ -2581,7 +2610,7 @@ class Item
                                $activity = Activity::ANNOUNCE;
                                break;
                        default:
-                               Logger::notice('unknown verb', ['verb' => $verb, 'item' => $item_id]);
+                               Logger::warning('unknown verb', ['verb' => $verb, 'item' => $item_id]);
                                return false;
                }
 
@@ -2674,9 +2703,11 @@ class Item
                        'unseen'        => 1,
                ];
 
-               $signed = Diaspora::createLikeSignature($uid, $new_item);
-               if (!empty($signed)) {
-                       $new_item['diaspora_signed_text'] = json_encode($signed);
+               if (in_array($activity, [Activity::LIKE, Activity::DISLIKE])) {
+                       $signed = Diaspora::createLikeSignature($uid, $new_item);
+                       if (!empty($signed)) {
+                               $new_item['diaspora_signed_text'] = json_encode($signed);
+                       }
                }
 
                self::insert($new_item, true);