]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Post.php
The item table is now completely removed
[friendica.git] / src / Model / Post.php
index a3d537b3dacf5d157320c2500346d9a978344adf..e3114ca90995e05b44d5698ecb7743c56b54aac2 100644 (file)
 
 namespace Friendica\Model;
 
+use BadMethodCallException;
 use Friendica\Core\Logger;
+use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
 use Friendica\Protocol\Activity;
 
 class Post
 {
+       /**
+        * Insert a new post entry
+        *
+        * @param integer $uri_id
+        * @param array   $fields
+        * @return int    ID of inserted post
+        * @throws \Exception
+        */
+       public static function insert(int $uri_id, array $data = [])
+       {
+               if (empty($uri_id)) {
+                       throw new BadMethodCallException('Empty URI_id');
+               }
+
+               $fields = DBStructure::getFieldsForTable('post', $data);
+
+               // Additionally assign the key fields
+               $fields['uri-id'] = $uri_id;
+
+               if (!DBA::insert('post', $fields, Database::INSERT_IGNORE)) {
+                       return 0;
+               }
+
+               return DBA::lastInsertId();
+       }
+
        /**
         * Fetch a single post row
         *
@@ -65,6 +93,10 @@ class Post
                        }
                }
 
+               if (array_key_exists('extid', $row) && is_null($row['extid'])) {
+                       $row['extid'] = '';
+               }
+
                return $row;
        }
 
@@ -189,13 +221,10 @@ class Post
        private static function selectView(string $view, array $selected = [], array $condition = [], $params = [])
        {
                if (empty($selected)) {
-                       $selected = array_merge(['author-addr', 'author-nick', 'owner-addr', 'owner-nick', 'causer-addr', 'causer-nick',
-                               'causer-network', 'photo', 'name-date', 'uri-date', 'avatar-date', 'thumb', 'dfrn-id',
-                               'parent-guid', 'parent-network', 'parent-author-id', 'parent-author-link', 'parent-author-name',
-                               'parent-author-network', 'signed_text', 'language', 'raw-body'], Item::DISPLAY_FIELDLIST, Item::ITEM_FIELDLIST);
-                       
-                       if ($view != 'post-view') {
-                               $selected = array_merge($selected, ['ignored', 'iid']);
+                       $selected = array_merge(Item::DISPLAY_FIELDLIST, Item::ITEM_FIELDLIST);
+
+                       if ($view == 'post-thread-view') {
+                               $selected = array_merge($selected, ['ignored']);
                        }
                }
 
@@ -253,7 +282,7 @@ class Post
                }
 
                $condition = DBA::mergeConditions($condition,
-                       ["`visible` AND NOT `deleted` AND NOT `moderated`
+                       ["`visible` AND NOT `deleted`
                        AND NOT `author-blocked` AND NOT `owner-blocked`
                        AND (NOT `causer-blocked` OR `causer-id` = ?) AND NOT `contact-blocked`
                        AND ((NOT `contact-readonly` AND NOT `contact-pending` AND (`contact-rel` IN (?, ?)))
@@ -434,6 +463,20 @@ class Post
                        $affected = max($affected, DBA::affectedRows());
                }
 
+               $update_fields = DBStructure::getFieldsForTable('post', $fields);
+               if (!empty($update_fields)) {
+                       if (empty($uriids)) {
+                               $rows = DBA::selectToArray('post-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
+                               $uriids = array_column($rows, 'uri-id');
+                       }
+                       if (!DBA::update('post', $update_fields, ['uri-id' => $uriids])) {
+                               DBA::rollback();
+                               Logger::notice('Updating post failed', ['fields' => $update_fields, 'condition' => $condition]);
+                               return false;
+                       }
+                       $affected = max($affected, DBA::affectedRows());
+               }
+
                $update_fields = Post\DeliveryData::extractFields($fields);
                if (!empty($update_fields)) {
                        if (empty($uriids)) {
@@ -450,7 +493,7 @@ class Post
 
                $update_fields = DBStructure::getFieldsForTable('post-thread', $fields);
                if (!empty($update_fields)) {
-                       $rows = DBA::selectToArray('post-view', ['uri-id'], $condition, ['group_by' => ['uri-id']]);
+                       $rows = DBA::selectToArray('post-view', ['uri-id'], $thread_condition, ['group_by' => ['uri-id']]);
                        $uriids = array_column($rows, 'uri-id');
                        if (!DBA::update('post-thread', $update_fields, ['uri-id' => $uriids])) {
                                DBA::rollback();
@@ -472,26 +515,25 @@ class Post
                        $affected = max($affected, DBA::affectedRows());
                }
 
-               $update_fields = [];
-               foreach (Item::USED_FIELDLIST as $field) {
-                       if (array_key_exists($field, $fields)) {
-                               $update_fields[$field] = $fields[$field];
-                       }
-               }
-               if (!empty($update_fields)) {
-                       $rows = DBA::selectToArray('post-view', ['id'], $condition, []);
-                       $ids = array_column($rows, 'id');
-                       if (!DBA::update('item', $update_fields, ['id' => $ids])) {
-                               DBA::rollback();
-                               Logger::notice('Updating item failed', ['fields' => $update_fields, 'condition' => $condition]);
-                               return false;
-                       }
-                       $affected = max($affected, DBA::affectedRows());
-               }
-
                DBA::commit();
 
                Logger::info('Updated posts', ['rows' => $affected]);
                return $affected;
        }
+
+       /**
+        * Delete a row from the post table
+        *
+        * @param array        $conditions Field condition(s)
+        * @param array        $options
+        *                           - cascade: If true we delete records in other tables that depend on the one we're deleting through
+        *                           relations (default: true)
+        *
+        * @return boolean was the delete successful?
+        * @throws \Exception
+        */
+       public static function delete(array $conditions, array $options = [])
+       {
+               return DBA::delete('post', $conditions, $options);
+       }
 }