]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/PostUpdate.php
The "term" table is removed
[friendica.git] / src / Database / PostUpdate.php
index 82cc07c6d6d7a5ed1550d097b850b80bf5ad4a92..73ac374accd130f78518859863b65c17551e872e 100644 (file)
@@ -30,8 +30,8 @@ use Friendica\Model\ItemURI;
 use Friendica\Model\PermissionSet;
 use Friendica\Model\Post\Category;
 use Friendica\Model\Tag;
-use Friendica\Model\Term;
 use Friendica\Model\UserItem;
+use Friendica\Model\Verb;
 use Friendica\Util\Strings;
 
 /**
@@ -42,6 +42,9 @@ use Friendica\Util\Strings;
  */
 class PostUpdate
 {
+       // Needed for the helper function to read from the legacy term table
+       const OBJECT_TYPE_POST  = 1;
+
        /**
         * Calls the post update functions
         */
@@ -80,6 +83,9 @@ class PostUpdate
                if (!self::update1346()) {
                        return false;
                }
+               if (!self::update1347()) {
+                       return false;
+               }
 
                return true;
        }
@@ -727,6 +733,31 @@ class PostUpdate
                return false;
        }
 
+       /**
+        * Generates the legacy item.file field string from an item ID.
+        * Includes only file and category terms.
+        *
+        * @param int $item_id
+        * @return string
+        * @throws \Exception
+        */
+       private static function fileTextFromItemId($item_id)
+       {
+               $file_text = '';
+
+               $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [Category::FILE, Category::CATEGORY]];
+               $tags = DBA::selectToArray('term', ['type', 'term', 'url'], $condition);
+               foreach ($tags as $tag) {
+                       if ($tag['type'] == Category::CATEGORY) {
+                               $file_text .= '<' . $tag['term'] . '>';
+                       } else {
+                               $file_text .= '[' . $tag['term'] . ']';
+                       }
+               }
+
+               return $file_text;
+       }
+
        /**
         * Fill the "tag" table with tags and mentions from the "term" table
         *
@@ -761,7 +792,7 @@ class PostUpdate
                                continue;
                        }
 
-                       $file = Term::fileTextFromItemId($term['oid']);
+                       $file = self::fileTextFromItemId($term['oid']);
                        if (!empty($file)) {
                                Category::storeTextByURIId($item['uri-id'], $item['uid'], $file);
                        }
@@ -787,5 +818,56 @@ class PostUpdate
                }
 
                return false;
-       }       
+       }
+
+       /**
+        * update the "vid" (verb) field in the item table 
+        *
+        * @return bool "true" when the job is done
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       private static function update1347()
+       {
+               // Was the script completed?
+               if (DI::config()->get("system", "post_update_version") >= 1347) {
+                       return true;
+               }
+
+               $id = DI::config()->get("system", "post_update_version_1347_id", 0);
+
+               Logger::info('Start', ['item' => $id]);
+
+               $start_id = $id;
+               $rows = 0;
+               $condition = ["`id` > ? AND `vid` IS NULL", $id];
+               $params = ['order' => ['id'], 'limit' => 10000];
+               $items = Item::select(['id', 'verb'], $condition, $params);
+
+               if (DBA::errorNo() != 0) {
+                       Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
+                       return false;
+               }
+
+               while ($item = Item::fetch($items)) {
+                       $id = $item['id'];
+
+                       DBA::update('item', ['vid' => Verb::getID($item['verb'])], ['id' => $item['id']]);
+
+                       ++$rows;
+               }
+               DBA::close($items);
+
+               DI::config()->set("system", "post_update_version_1347_id", $id);
+
+               Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
+
+               if ($start_id == $id) {
+                       DI::config()->set("system", "post_update_version", 1347);
+                       Logger::info('Done');
+                       return true;
+               }
+
+               return false;
+       }
 }