]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/PostUpdate.php
The "term" table is removed
[friendica.git] / src / Database / PostUpdate.php
index cd7e8b94626376e027168d4c324a76437a7c8202..73ac374accd130f78518859863b65c17551e872e 100644 (file)
@@ -28,7 +28,11 @@ use Friendica\Model\Contact;
 use Friendica\Model\Item;
 use Friendica\Model\ItemURI;
 use Friendica\Model\PermissionSet;
+use Friendica\Model\Post\Category;
+use Friendica\Model\Tag;
 use Friendica\Model\UserItem;
+use Friendica\Model\Verb;
+use Friendica\Util\Strings;
 
 /**
  * These database-intensive post update routines are meant to be executed in the background by the cronjob.
@@ -38,6 +42,9 @@ use Friendica\Model\UserItem;
  */
 class PostUpdate
 {
+       // Needed for the helper function to read from the legacy term table
+       const OBJECT_TYPE_POST  = 1;
+
        /**
         * Calls the post update functions
         */
@@ -64,6 +71,21 @@ class PostUpdate
                if (!self::update1329()) {
                        return false;
                }
+               if (!self::update1341()) {
+                       return false;
+               }
+               if (!self::update1342()) {
+                       return false;
+               }
+               if (!self::update1345()) {
+                       return false;
+               }
+               if (!self::update1346()) {
+                       return false;
+               }
+               if (!self::update1347()) {
+                       return false;
+               }
 
                return true;
        }
@@ -533,4 +555,319 @@ class PostUpdate
 
                return false;
        }
+
+       /**
+        * Fill the "tag" table with tags and mentions from the body
+        *
+        * @return bool "true" when the job is done
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       private static function update1341()
+       {
+               // Was the script completed?
+               if (DI::config()->get('system', 'post_update_version') >= 1341) {
+                       return true;
+               }
+
+               $id = DI::config()->get('system', 'post_update_version_1341_id', 0);
+
+               Logger::info('Start', ['item' => $id]);
+
+               $rows = 0;
+
+               $items = DBA::p("SELECT `uri-id`,`body` FROM `item-content` WHERE
+                       (`body` LIKE ? OR `body` LIKE ? OR `body` LIKE ?) AND `uri-id` >= ?
+                       ORDER BY `uri-id` LIMIT 100000", '%#%', '%@%', '%!%', $id);
+
+               if (DBA::errorNo() != 0) {
+                       Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
+                       return false;
+               }
+
+               while ($item = DBA::fetch($items)) {
+                       Tag::storeFromBody($item['uri-id'], $item['body'], '#!@', false);
+                       $id = $item['uri-id'];
+                       ++$rows;
+                       if ($rows % 1000 == 0) {
+                               DI::config()->set('system', 'post_update_version_1341_id', $id);
+                       }
+               }
+               DBA::close($items);
+
+               DI::config()->set('system', 'post_update_version_1341_id', $id);
+
+               Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
+
+               // When there are less than 1,000 items processed this means that we reached the end
+               // The other entries will then be processed with the regular functionality
+               if ($rows < 1000) {
+                       DI::config()->set('system', 'post_update_version', 1341);
+                       Logger::info('Done');
+                       return true;
+               }
+
+               return false;
+       }
+
+       /**
+        * Fill the "tag" table with tags and mentions from the "term" table
+        *
+        * @return bool "true" when the job is done
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       private static function update1342()
+       {
+               // Was the script completed?
+               if (DI::config()->get('system', 'post_update_version') >= 1342) {
+                       return true;
+               }
+
+               $id = DI::config()->get('system', 'post_update_version_1342_id', 0);
+
+               Logger::info('Start', ['item' => $id]);
+
+               $rows = 0;
+
+               $terms = DBA::p("SELECT `term`.`tid`, `item`.`uri-id`, `term`.`type`, `term`.`term`, `term`.`url`, `item-content`.`body`
+                       FROM `term`
+                       INNER JOIN `item` ON `item`.`id` = `term`.`oid`
+                       INNER JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
+                       WHERE term.type IN (?, ?, ?, ?) AND `tid` >= ? ORDER BY `tid` LIMIT 100000",
+                       Tag::HASHTAG, Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION, $id);
+
+               if (DBA::errorNo() != 0) {
+                       Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
+                       return false;
+               }
+
+               while ($term = DBA::fetch($terms)) {
+                       if (($term['type'] == Tag::MENTION) && !empty($term['url']) && !strstr($term['body'], $term['url'])) {
+                $condition = ['nurl' => Strings::normaliseLink($term['url']), 'uid' => 0, 'deleted' => false];
+                $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
+                if (!DBA::isResult($contact)) {
+                        $ssl_url = str_replace('http://', 'https://', $term['url']);
+                        $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $term['url'], Strings::normaliseLink($term['url']), $ssl_url, 0];
+                        $contact = DBA::selectFirst('contact', ['url', 'alias'], $condition, ['order' => ['id']]);
+                }
+
+                if (DBA::isResult($contact) && (!strstr($term['body'], $contact['url']) && (empty($contact['alias']) || !strstr($term['body'], $contact['alias'])))) {
+                        $term['type'] = Tag::IMPLICIT_MENTION;
+                }
+                       }
+
+                       Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url'], false);
+
+                       $id = $term['tid'];
+                       ++$rows;
+                       if ($rows % 1000 == 0) {
+                               DI::config()->set('system', 'post_update_version_1342_id', $id);
+                       }
+               }
+               DBA::close($terms);
+
+               DI::config()->set('system', 'post_update_version_1342_id', $id);
+
+               Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
+
+               // When there are less than 1,000 items processed this means that we reached the end
+               // The other entries will then be processed with the regular functionality
+               if ($rows < 1000) {
+                       DI::config()->set('system', 'post_update_version', 1342);
+                       Logger::info('Done');
+                       return true;
+               }
+
+               return false;
+       }
+
+       /**
+        * Fill the "post-delivery-data" table with data from the "item-delivery-data" table
+        *
+        * @return bool "true" when the job is done
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       private static function update1345()
+       {
+               // Was the script completed?
+               if (DI::config()->get('system', 'post_update_version') >= 1345) {
+                       return true;
+               }
+
+               $id = DI::config()->get('system', 'post_update_version_1345_id', 0);
+
+               Logger::info('Start', ['item' => $id]);
+
+               $rows = 0;
+
+               $deliveries = DBA::p("SELECT `uri-id`, `iid`, `item-delivery-data`.`postopts`, `item-delivery-data`.`inform`,
+                       `queue_count`, `queue_done`, `activitypub`, `dfrn`, `diaspora`, `ostatus`, `legacy_dfrn`, `queue_failed`
+                       FROM `item-delivery-data`
+                       INNER JOIN `item` ON `item`.`id` = `item-delivery-data`.`iid`
+                       WHERE `iid` >= ? ORDER BY `iid` LIMIT 10000", $id);
+
+               if (DBA::errorNo() != 0) {
+                       Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
+                       return false;
+               }
+
+               while ($delivery = DBA::fetch($deliveries)) {
+                       $id = $delivery['iid'];
+                       unset($delivery['iid']);
+                       DBA::insert('post-delivery-data', $delivery, true);
+                       ++$rows;
+               }
+               DBA::close($deliveries);
+
+               DI::config()->set('system', 'post_update_version_1345_id', $id);
+
+               Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
+
+               // When there are less than 100 items processed this means that we reached the end
+               // The other entries will then be processed with the regular functionality
+               if ($rows < 100) {
+                       DI::config()->set('system', 'post_update_version', 1345);
+                       Logger::info('Done');
+                       return true;
+               }
+
+               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
+        *
+        * @return bool "true" when the job is done
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       private static function update1346()
+       {
+               // Was the script completed?
+               if (DI::config()->get('system', 'post_update_version') >= 1346) {
+                       return true;
+               }
+
+               $id = DI::config()->get('system', 'post_update_version_1346_id', 0);
+
+               Logger::info('Start', ['item' => $id]);
+
+               $rows = 0;
+
+               $terms = DBA::select('term', ['oid'],
+                       ["`type` IN (?, ?) AND `oid` >= ?", Category::CATEGORY, Category::FILE, $id],
+                       ['order' => ['oid'], 'limit' => 1000, 'group_by' => ['oid']]);
+
+               if (DBA::errorNo() != 0) {
+                       Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
+                       return false;
+               }
+
+               while ($term = DBA::fetch($terms)) {
+                       $item = Item::selectFirst(['uri-id', 'uid'], ['id' => $term['oid']]);
+                       if (!DBA::isResult($item)) {
+                               continue;
+                       }
+
+                       $file = self::fileTextFromItemId($term['oid']);
+                       if (!empty($file)) {
+                               Category::storeTextByURIId($item['uri-id'], $item['uid'], $file);
+                       }
+
+                       $id = $term['oid'];
+                       ++$rows;
+                       if ($rows % 100 == 0) {
+                               DI::config()->set('system', 'post_update_version_1346_id', $id);
+                       }
+               }
+               DBA::close($terms);
+
+               DI::config()->set('system', 'post_update_version_1346_id', $id);
+
+               Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
+
+               // When there are less than 10 items processed this means that we reached the end
+               // The other entries will then be processed with the regular functionality
+               if ($rows < 10) {
+                       DI::config()->set('system', 'post_update_version', 1346);
+                       Logger::info('Done');
+                       return true;
+               }
+
+               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;
+       }
 }