]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Merge pull request #8629 from annando/item-insert
[friendica.git] / src / Model / Item.php
index e2ea9eb816f33133fca3e11105ac563506facaab..f10537d75ab2de88a2a3eb8731636981be3d07a5 100644 (file)
@@ -1331,7 +1331,13 @@ class Item
                }
        }
 
-       private static function isDuplicate($item)
+       /**
+        * Check if the item array is a duplicate
+        *
+        * @param array $item
+        * @return boolean is it a duplicate?
+        */
+       private static function isDuplicate(array $item)
        {
                // Checking if there is already an item with the same guid
                $condition = ['guid' => $item['guid'], 'network' => $item['network'], 'uid' => $item['uid']];
@@ -1373,7 +1379,7 @@ class Item
                 * There is a timing issue here that sometimes creates double postings.
                 * An unique index would help - but the limitations of MySQL (maximum size of index values) prevent this.
                 */
-               if (($item["uid"] == 0) && self::exists(['uri' => trim($item['uri']), 'uid' => 0])) {
+               if (($item['uid'] == 0) && self::exists(['uri' => trim($item['uri']), 'uid' => 0])) {
                        Logger::notice('Global item already stored.', ['uri' => $item['uri'], 'network' => $item['network']]);
                        return true;
                }
@@ -1381,7 +1387,13 @@ class Item
                return false;
        }
 
-       private static function validItem($item)
+       /**
+        * Check if the item array is valid
+        *
+        * @param array $item
+        * @return boolean item is valid
+        */
+       private static function isValid(array $item)
        {
                // When there is no content then we don't post it
                if ($item['body'].$item['title'] == '') {
@@ -1454,7 +1466,7 @@ class Item
                if ($item['verb'] == Activity::FOLLOW) {
                        if (!$item['origin'] && ($item['author-id'] == Contact::getPublicIdByUserId($item['uid']))) {
                                // Our own follow request can be relayed to us. We don't store it to avoid notification chaos.
-                               Logger::log("Follow: Don't store not origin follow request from us for " . $item['parent-uri'], Logger::DEBUG);
+                               Logger::info("Follow: Don't store not origin follow request", ['parent-uri' => $item['parent-uri']]);
                                return false;
                        }
 
@@ -1462,7 +1474,7 @@ class Item
                                'parent-uri' => $item['parent-uri'], 'author-id' => $item['author-id']];
                        if (self::exists($condition)) {
                                // It happens that we receive multiple follow requests by the same author - we only store one.
-                               Logger::log('Follow: Found existing follow request from author ' . $item['author-id'] . ' for ' . $item['parent-uri'], Logger::DEBUG);
+                               Logger::info('Follow: Found existing follow request from author', ['author-id' => $item['author-id'], 'parent-uri' => $item['parent-uri']]);
                                return false;
                        }
                }
@@ -1470,7 +1482,13 @@ class Item
                return true;
        }
 
-       private static function getDuplicateID($item)
+       /**
+        * Return the id of the given item array if it has been stored before
+        *
+        * @param array $item
+        * @return integer item id
+        */
+       private static function getDuplicateID(array $item)
        {
                if (empty($item['network']) || in_array($item['network'], Protocol::FEDERATED)) {
                        $condition = ["`uri` = ? AND `uid` = ? AND `network` IN (?, ?, ?, ?)",
@@ -1495,7 +1513,13 @@ class Item
                return 0;
        }
 
-       private static function getParentData($item)
+       /**
+        * Fetch parent data for the given item array
+        *
+        * @param array $item
+        * @return array item array with parent data
+        */
+       private static function getParentData(array $item)
        {
                // find the parent and snarf the item id and ACLs
                // and anything else we need to inherit
@@ -1576,7 +1600,13 @@ class Item
                return $item;
        }
 
-       private static function getGravity($item)
+       /**
+        * Get the gravity for the given item array
+        *
+        * @param array $item
+        * @return integer gravity
+        */
+       private static function getGravity(array $item)
        {
                $activity = DI::activity();
 
@@ -1685,8 +1715,6 @@ class Item
                $item['inform']        = trim($item['inform'] ?? '');
                $item['file']          = trim($item['file'] ?? '');
 
-               self::addLanguageToItemArray($item);
-
                // Items cannot be stored before they happen ...
                if ($item['created'] > DateTimeFormat::utcNow()) {
                        $item['created'] = DateTimeFormat::utcNow();
@@ -1699,47 +1727,39 @@ class Item
 
                $item['plink'] = ($item['plink'] ?? '') ?: DI::baseUrl() . '/display/' . urlencode($item['guid']);
 
+               $item['language'] = self::getLanguage($item);
+
                $item['gravity'] = self::getGravity($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, false, $default);
 
-               unset($item['author-link']);
-               unset($item['author-name']);
-               unset($item['author-avatar']);
-               unset($item['author-network']);
-
                $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
                        'photo' => $item['owner-avatar'], 'network' => $item['network']];
-
                $item['owner-id'] = ($item['owner-id'] ?? 0) ?: Contact::getIdForURL($item['owner-link'], 0, false, $default);
 
-               unset($item['owner-link']);
-               unset($item['owner-name']);
-               unset($item['owner-avatar']);
-
                // The contact-id should be set before "self::insert" was called - but there seems to be issues sometimes
                $item["contact-id"] = self::contactId($item);
 
-               if ($item['network'] == Protocol::PHANTOM) {
-                       $item['network'] = Protocol::DFRN;
-                       Logger::notice('Missing network, setting to {network}.', [
-                               'uri' => $item["uri"],
-                               'network' => $item['network'],
-                               'callstack' => System::callstack()
-                       ]);
-               }
-
-               if (!self::validItem($item)) {
+               if (!self::isValid($item)) {
                        return 0;
                }
 
                // We don't store the causer, we only have it here for the checks in the function above
                unset($item['causer-id']);
                unset($item['causer-link']);
-               
+
+               // We don't store these fields anymore in the item table
+               unset($item['author-link']);
+               unset($item['author-name']);
+               unset($item['author-avatar']);
+               unset($item['author-network']);
+
+               unset($item['owner-link']);
+               unset($item['owner-name']);
+               unset($item['owner-avatar']);
+
                $item['thr-parent'] = $item['parent-uri'];
 
                if ($item['parent-uri'] != $item['uri']) {
@@ -1812,67 +1832,60 @@ class Item
                // It is mainly used in the "post_local" hook.
                unset($item['api_source']);
 
-               // Filling item related side tables
-               if (!empty($item['dsprsig'])) {
-                       $dsprsig = json_decode(base64_decode($item['dsprsig']));
 
-                       /*
-                        * Friendica servers lower than 3.4.3-2 had double encoded the signature ...
-                        * We can check for this condition when we decode and encode the stuff again.
-                        */
-                       if (base64_encode(base64_decode(base64_decode($dsprsig->signature))) == base64_decode($dsprsig->signature)) {
-                               $dsprsig->signature = base64_decode($dsprsig->signature);
-                               Logger::log("Repaired double encoded signature from handle ".$dsprsig->signer, Logger::DEBUG);
-                       }
+               // Check for hashtags in the body and repair or add hashtag links
+               self::setHashtags($item);
+               
+               // Fill the cache field
+               self::putInCache($item);
 
-                       if (!empty($dsprsig->signed_text) && empty($dsprsig->signature) && empty($dsprsig->signer)) {
-                               DBA::insert('diaspora-interaction', ['uri-id' => $item['uri-id'], 'interaction' => $dsprsig->signed_text], true);
-                       }
+               if (stristr($item['verb'], Activity::POKE)) {
+                       $notify_type = Delivery::POKE;
+               } else {
+                       $notify_type = Delivery::POST;
                }
 
-               unset($item['dsprsig']);
+               $body = $item['body'];
+               
+               // We are doing this outside of the transaction to avoid timing problems
+               if (!self::insertActivity($item)) {
+                       self::insertContent($item);
+               }
+
+               $like_no_comment = DI::config()->get('system', 'like_no_comment');
+
+               DBA::transaction();
+
+               // Filling item related side tables
 
+               // Diaspora signature
                if (!empty($item['diaspora_signed_text'])) {
                        DBA::insert('diaspora-interaction', ['uri-id' => $item['uri-id'], 'interaction' => $item['diaspora_signed_text']], true);
                }
 
                unset($item['diaspora_signed_text']);
 
-               if (array_key_exists('file', $item) && !empty($item['file'])) {
+               // Attached file links
+               if (!empty($item['file'])) {
                        Category::storeTextByURIId($item['uri-id'], $item['uid'], $item['file']);
                }
 
                unset($item['file']);
 
+               // Delivery relevant data
                $delivery_data = Post\DeliveryData::extractFields($item);
                unset($item['postopts']);
                unset($item['inform']);
 
-               // Check for hashtags in the body and repair or add hashtag links
-               self::setHashtags($item);
+               if (!empty($item['origin']) || !empty($item['wall']) || !empty($delivery_data['postopts']) || !empty($delivery_data['inform'])) {
+                       Post\DeliveryData::insert($item['uri-id'], $delivery_data);
+               }
 
                // Store tags from the body if this hadn't been handled previously in the protocol classes
                if (!Tag::existsForPost($item['uri-id'])) {
-                       Tag::storeFromBody($item['uri-id'], $item['body']);
+                       Tag::storeFromBody($item['uri-id'], $body);
                }
                
-               // Fill the cache field
-               self::putInCache($item);
-
-               if (stristr($item['verb'], Activity::POKE)) {
-                       $notify_type = Delivery::POKE;
-               } else {
-                       $notify_type = Delivery::POST;
-               }
-
-               // We are doing this outside of the transaction to avoid timing problems
-               if (!self::insertActivity($item)) {
-                       self::insertContent($item);
-               }
-
-               $like_no_comment = DI::config()->get('system', 'like_no_comment');
-
-               DBA::transaction();
                $ret = DBA::insert('item', $item);
 
                // When the item was successfully stored we fetch the ID of the item.
@@ -1938,11 +1951,6 @@ class Item
                } else {
                        self::updateThread($parent_id);
                }
-
-               if (!empty($item['origin']) || !empty($item['wall']) || !empty($delivery_data['postopts']) || !empty($delivery_data['inform'])) {
-                       Post\DeliveryData::insert($item['uri-id'], $delivery_data);
-               }
-
                DBA::commit();
 
                // In that function we check if this is a forum post. Additionally we delete the item under certain circumstances
@@ -2415,20 +2423,22 @@ class Item
         * Adds a language specification in a "language" element of given $arr.
         * Expects "body" element to exist in $arr.
         *
-        * @param $item
+        * @param array $item
+        * @return string detected language
         * @throws \Text_LanguageDetect_Exception
         */
-       private static function addLanguageToItemArray(&$item)
+       private static function getLanguage(array $item)
        {
                $naked_body = BBCode::toPlaintext($item['body'], false);
 
                $ld = new Text_LanguageDetect();
                $ld->setNameMode(2);
                $languages = $ld->detect($naked_body, 3);
-
                if (is_array($languages)) {
-                       $item['language'] = json_encode($languages);
+                       return json_encode($languages);
                }
+
+               return '';
        }
 
        /**