]> git.mxchange.org Git - friendica.git/commitdiff
Extract isDuplicate() into ItemHelper
authorArt4 <art4@wlabs.de>
Thu, 23 Jan 2025 13:17:04 +0000 (13:17 +0000)
committerArt4 <art4@wlabs.de>
Thu, 23 Jan 2025 13:17:04 +0000 (13:17 +0000)
src/Model/Item.php
src/Model/ItemHelper.php

index b0ec02956357a4860c3b1ba7280f5dc71b782ea0..ce62e86c4f8f0779fd454240e3e3f7bd750bbd64 100644 (file)
@@ -580,51 +580,7 @@ class Item
                }
        }
 
-       /**
-        * Check if the item array is a duplicate
-        *
-        * @param array $item Item record
-        * @return boolean is it a duplicate?
-        */
-       private static function isDuplicate(array $item): bool
-       {
-               // Checking if there is already an item with the same guid
-               $condition = ['guid' => $item['guid'], 'network' => $item['network'], 'uid' => $item['uid']];
-               if (Post::exists($condition)) {
-                       DI::logger()->notice('Found already existing item', $condition);
-                       return true;
-               }
-
-               $condition = [
-                       'uri-id'  => $item['uri-id'], 'uid' => $item['uid'],
-                       'network' => [$item['network'], Protocol::DFRN]
-               ];
-               if (Post::exists($condition)) {
-                       DI::logger()->notice('duplicated item with the same uri found.', $condition);
-                       return true;
-               }
 
-               // On Friendica and Diaspora the GUID is unique
-               if (in_array($item['network'], [Protocol::DFRN, Protocol::DIASPORA])) {
-                       $condition = ['guid' => $item['guid'], 'uid' => $item['uid']];
-                       if (Post::exists($condition)) {
-                               DI::logger()->notice('duplicated item with the same guid found.', $condition);
-                               return true;
-                       }
-               }
-
-               /*
-                * Check for already added items.
-                * 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) && Post::exists(['uri-id' => $item['uri-id'], 'uid' => 0])) {
-                       DI::logger()->notice('Global item already stored.', ['uri-id' => $item['uri-id'], 'network' => $item['network']]);
-                       return true;
-               }
-
-               return false;
-       }
 
        /**
         * Check if the item array is valid
@@ -809,7 +765,7 @@ class Item
 
                // Additional duplicate checks
                /// @todo Check why the first duplication check returns the item number and the second a 0
-               if (self::isDuplicate($item)) {
+               if ($itemHelper->isDuplicate($item)) {
                        return 0;
                }
 
index ab17b845a9c593c794ed4039685e62c810a2b2dc..000208d9134c5dd1e9b89c098b6826b006f56ee9 100644 (file)
@@ -77,6 +77,54 @@ final class ItemHelper
                return $item;
        }
 
+       /**
+        * Check if the item array is a duplicate
+        *
+        * @private
+        *
+        * @param array $item Item record
+        * @return boolean is it a duplicate?
+        */
+       public function isDuplicate(array $item): bool
+       {
+               // Checking if there is already an item with the same guid
+               $condition = ['guid' => $item['guid'], 'network' => $item['network'], 'uid' => $item['uid']];
+               if (Post::exists($condition)) {
+                       $this->logger->notice('Found already existing item', $condition);
+                       return true;
+               }
+
+               $condition = [
+                       'uri-id'  => $item['uri-id'], 'uid' => $item['uid'],
+                       'network' => [$item['network'], Protocol::DFRN]
+               ];
+               if (Post::exists($condition)) {
+                       $this->logger->notice('duplicated item with the same uri found.', $condition);
+                       return true;
+               }
+
+               // On Friendica and Diaspora the GUID is unique
+               if (in_array($item['network'], [Protocol::DFRN, Protocol::DIASPORA])) {
+                       $condition = ['guid' => $item['guid'], 'uid' => $item['uid']];
+                       if (Post::exists($condition)) {
+                               $this->logger->notice('duplicated item with the same guid found.', $condition);
+                               return true;
+                       }
+               }
+
+               /*
+                * Check for already added items.
+                * 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) && Post::exists(['uri-id' => $item['uri-id'], 'uid' => 0])) {
+                       $this->logger->notice('Global item already stored.', ['uri-id' => $item['uri-id'], 'network' => $item['network']]);
+                       return true;
+               }
+
+               return false;
+       }
+
        public function validateItemData(array $item): array
        {
                $item['wall']          = intval($item['wall'] ?? 0);