Postupdate added
authorMichael <heluecht@pirati.ca>
Wed, 17 Jan 2024 21:10:33 +0000 (21:10 +0000)
committerMichael <heluecht@pirati.ca>
Wed, 17 Jan 2024 21:10:33 +0000 (21:10 +0000)
database.sql
doc/database.md
doc/database/db_post-searchindex.md [new file with mode: 0644]
src/Database/PostUpdate.php
src/Model/Item.php
src/Model/Post/SearchIndex.php [new file with mode: 0644]

index 51df6a7..e3397a4 100644 (file)
@@ -1,6 +1,6 @@
 -- ------------------------------------------
 -- Friendica 2024.03-dev (Yellow Archangel)
--- DB_UPDATE_VERSION 1546
+-- DB_UPDATE_VERSION 1547
 -- ------------------------------------------
 
 
@@ -1460,6 +1460,19 @@ CREATE TABLE IF NOT EXISTS `post-question-option` (
        FOREIGN KEY (`uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE
 ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Question option';
 
+--
+-- TABLE post-searchindex
+--
+CREATE TABLE IF NOT EXISTS `post-searchindex` (
+       `uri-id` int unsigned NOT NULL COMMENT 'Id of the item-uri table entry that contains the item uri',
+       `network` char(4) COMMENT '',
+       `private` tinyint unsigned COMMENT '0=public, 1=private, 2=unlisted',
+       `searchtext` mediumtext COMMENT 'Simplified text for the full text search',
+        PRIMARY KEY(`uri-id`),
+        FULLTEXT INDEX `searchtext` (`searchtext`),
+       FOREIGN KEY (`uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE
+) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Content for all posts';
+
 --
 -- TABLE post-tag
 --
index f6b0406..5942b11 100644 (file)
@@ -70,6 +70,7 @@ Database Tables
 | [post-media](help/database/db_post-media) | Attached media |
 | [post-question](help/database/db_post-question) | Question |
 | [post-question-option](help/database/db_post-question-option) | Question option |
+| [post-searchindex](help/database/db_post-searchindex) | Content for all posts |
 | [post-tag](help/database/db_post-tag) | post relation to tags |
 | [post-thread](help/database/db_post-thread) | Thread related data |
 | [post-thread-user](help/database/db_post-thread-user) | Thread related data per user |
diff --git a/doc/database/db_post-searchindex.md b/doc/database/db_post-searchindex.md
new file mode 100644 (file)
index 0000000..a5dec8d
--- /dev/null
@@ -0,0 +1,31 @@
+Table post-searchindex
+===========
+
+Content for all posts
+
+Fields
+------
+
+| Field      | Description                                               | Type             | Null | Key | Default | Extra |
+| ---------- | --------------------------------------------------------- | ---------------- | ---- | --- | ------- | ----- |
+| uri-id     | Id of the item-uri table entry that contains the item uri | int unsigned     | NO   | PRI | NULL    |       |
+| network    |                                                           | char(4)          | YES  |     | NULL    |       |
+| private    | 0=public, 1=private, 2=unlisted                           | tinyint unsigned | YES  |     | NULL    |       |
+| searchtext | Simplified text for the full text search                  | mediumtext       | YES  |     | NULL    |       |
+
+Indexes
+------------
+
+| Name       | Fields               |
+| ---------- | -------------------- |
+| PRIMARY    | uri-id               |
+| searchtext | FULLTEXT, searchtext |
+
+Foreign Keys
+------------
+
+| Field | Target Table | Target Field |
+|-------|--------------|--------------|
+| uri-id | [item-uri](help/database/db_item-uri) | id |
+
+Return to [database documentation](help/database)
index 1cf8bc1..0a373d8 100644 (file)
@@ -52,7 +52,7 @@ class PostUpdate
        // Needed for the helper function to read from the legacy term table
        const OBJECT_TYPE_POST  = 1;
 
-       const VERSION = 1544;
+       const VERSION = 1547;
 
        /**
         * Calls the post update functions
@@ -128,6 +128,9 @@ class PostUpdate
                if (!self::update1544()) {
                        return false;
                }
+               if (!self::update1547()) {
+                       return false;
+               }
                return true;
        }
 
@@ -1358,4 +1361,55 @@ class PostUpdate
 
                return false;
        }
+
+       /**
+        * Create "post-searchindex" entries for old entries.
+        *
+        * @return bool "true" when the job is done
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       private static function update1547()
+       {
+               // Was the script completed?
+               if (DI::keyValue()->get('post_update_version') >= 1547) {
+                       return true;
+               }
+
+               $id = (int)(DI::keyValue()->get('post_update_version_1547_id') ?? 0);
+               if ($id == 0) {
+                       $post = Post::selectFirstPost(['uri-id'], [], ['order' => ['uri-id' => true]]);
+                       $id = (int)($post['uri-id'] ?? 0);
+               }
+
+               Logger::info('Start', ['uri-id' => $id]);
+
+               $rows = 0;
+
+               $posts = Post::selectPosts(['uri-id', 'network', 'private'], ["`uri-id` < ? AND `gravity` IN (?, ?)", $id, Item::GRAVITY_COMMENT, Item::GRAVITY_PARENT], ['order' => ['uri-id' => true], 'limit' => 1000]);
+
+               if (DBA::errorNo() != 0) {
+                       Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
+                       return false;
+               }
+
+               while ($post = Post::fetch($posts)) {
+                       $id = $post['uri-id'];
+                       Post\SearchIndex::insert($post['uri-id'], $post['network'], $post['private']);
+                       ++$rows;
+               }
+               DBA::close($posts);
+
+               DI::keyValue()->set('post_update_version_1547_id', $id);
+
+               Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
+
+               if ($rows <= 100) {
+                       DI::keyValue()->set('post_update_version', 1547);
+                       Logger::info('Done');
+                       return true;
+               }
+
+               return false;
+       }
 }
index d28009a..1637481 100644 (file)
@@ -247,8 +247,7 @@ class Item
 
                                $searchtext = Post\Engagement::getSearchTextForUriId($item['uri-id'], true);
                                DBA::update('post-engagement', ['searchtext' => $searchtext], ['uri-id' => $item['uri-id']]);
-                               DBA::update('post-searchindex', ['searchtext' => $searchtext], ['uri-id' => $item['uri-id']]);
-
+                               Post\SearchIndex::update($item['uri-id']);
                        }
 
                        if (!empty($fields['file'])) {
@@ -1450,14 +1449,8 @@ class Item
 
                        $engagement_uri_id = Post\Engagement::storeFromItem($posted_item);
                        
-                       if (in_array($item['gravity'], [self::GRAVITY_PARENT, self::GRAVITY_COMMENT])) {
-                               $search = [
-                                       'uri-id' => $posted_item['uri-id'],
-                                       'network' => $posted_item['network'],
-                                       'private' => $posted_item['private'],
-                                       'searchtext' => Post\Engagement::getSearchTextForUriId($posted_item['uri-id']),
-                               ];
-                               DBA::insert('post-searchindex', $search, Database::INSERT_IGNORE);
+                       if (in_array($posted_item['gravity'], [self::GRAVITY_PARENT, self::GRAVITY_COMMENT])) {
+                               Post\SearchIndex::insert($posted_item['uri-id'], $posted_item['network'], $posted_item['private']);
                        }
 
                        if (($posted_item['gravity'] == self::GRAVITY_ACTIVITY) && ($posted_item['verb'] == Activity::ANNOUNCE) && ($posted_item['parent-uri-id'] == $posted_item['thr-parent-id'])) {
diff --git a/src/Model/Post/SearchIndex.php b/src/Model/Post/SearchIndex.php
new file mode 100644 (file)
index 0000000..3cc2ae2
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2024, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Model\Post;
+
+use Friendica\Database\Database;
+use Friendica\Database\DBA;
+use Friendica\Model\Post;
+
+class SearchIndex
+{
+       /**
+        * Insert a post-searchindex entry
+        *
+        * @param int $uri_id
+        * @param string $network
+        * @param int $private
+        */
+       public static function insert(int $uri_id, string $network, int $private)
+       {
+               $search = [
+                       'uri-id' => $uri_id,
+                       'network' => $network,
+                       'private' => $private,
+                       'searchtext' => Post\Engagement::getSearchTextForUriId($uri_id),
+               ];
+               return DBA::insert('post-searchindex', $search, Database::INSERT_UPDATE);
+       }
+
+       /**
+        * update a post-searchindex entry
+        *
+        * @param int $uri_id
+        */
+       public static function update(int $uri_id)
+       {
+               $searchtext = Post\Engagement::getSearchTextForUriId($uri_id, true);
+               return DBA::update('post-searchindex', ['searchtext' => $searchtext], ['uri-id' => $uri_id]);
+       }
+}