]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/PostUpdate.php
Merge pull request #9146 from tobiasd/2020.09-CHANGELOG
[friendica.git] / src / Database / PostUpdate.php
index 73ac374accd130f78518859863b65c17551e872e..a418a7948dd5c99d6ecbb74dcf3f1cc30573e58f 100644 (file)
@@ -25,6 +25,7 @@ use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\DI;
 use Friendica\Model\Contact;
+use Friendica\Model\GServer;
 use Friendica\Model\Item;
 use Friendica\Model\ItemURI;
 use Friendica\Model\PermissionSet;
@@ -86,6 +87,12 @@ class PostUpdate
                if (!self::update1347()) {
                        return false;
                }
+               if (!self::update1348()) {
+                       return false;
+               }
+               if (!self::update1349()) {
+                       return false;
+               }
 
                return true;
        }
@@ -235,14 +242,14 @@ class PostUpdate
                                $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
                                        'photo' => $item['author-avatar'], 'network' => $item['network']];
 
-                               $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, false, $default);
+                               $item['author-id'] = Contact::getIdForURL($item["author-link"], 0, null, $default);
                        }
 
                        if (empty($item['owner-id'])) {
                                $default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
                                        'photo' => $item['owner-avatar'], 'network' => $item['network']];
 
-                               $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, false, $default);
+                               $item['owner-id'] = Contact::getIdForURL($item["owner-link"], 0, null, $default);
                        }
 
                        if (empty($item['psid'])) {
@@ -449,6 +456,11 @@ class PostUpdate
                        return true;
                }
 
+               if (!DBStructure::existsTable('item-delivery-data')) {
+                       DI::config()->set('system', 'post_update_version', 1297);
+                       return true;
+               }
+
                $max_item_delivery_data = DBA::selectFirst('item-delivery-data', ['iid'], ['queue_count > 0 OR queue_done > 0'], ['order' => ['iid']]);
                $max_iid = $max_item_delivery_data['iid'];
 
@@ -693,6 +705,11 @@ class PostUpdate
                        return true;
                }
 
+               if (!DBStructure::existsTable('item-delivery-data')) {
+                       DI::config()->set('system', 'post_update_version', 1345);
+                       return true;
+               }
+
                $id = DI::config()->get('system', 'post_update_version_1345_id', 0);
 
                Logger::info('Start', ['item' => $id]);
@@ -821,7 +838,7 @@ class PostUpdate
        }
 
        /**
-        * update the "vid" (verb) field in the item table 
+        * update the "vid" (verb) field in the item table
         *
         * @return bool "true" when the job is done
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
@@ -840,20 +857,31 @@ class PostUpdate
 
                $start_id = $id;
                $rows = 0;
-               $condition = ["`id` > ? AND `vid` IS NULL", $id];
-               $params = ['order' => ['id'], 'limit' => 10000];
-               $items = Item::select(['id', 'verb'], $condition, $params);
+
+               $items = DBA::p("SELECT `item`.`id`, `item`.`verb` AS `item-verb`, `item-content`.`verb`, `item-activity`.`activity`
+                       FROM `item` LEFT JOIN `item-content` ON `item-content`.`uri-id` = `item`.`uri-id`
+                       LEFT JOIN `item-activity` ON `item-activity`.`uri-id` = `item`.`uri-id` AND `item`.`gravity` = ?
+                       WHERE `item`.`id` >= ? AND `item`.`vid` IS NULL ORDER BY `item`.`id` LIMIT 10000", GRAVITY_ACTIVITY, $id);
 
                if (DBA::errorNo() != 0) {
                        Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
                        return false;
                }
 
-               while ($item = Item::fetch($items)) {
+               while ($item = DBA::fetch($items)) {
                        $id = $item['id'];
+                       $verb = $item['item-verb'];
+                       if (empty($verb)) {
+                               $verb = $item['verb'];
+                       }
+                       if (empty($verb) && is_int($item['activity'])) {
+                               $verb = Item::ACTIVITIES[$item['activity']];
+                       }
+                       if (empty($verb)) {
+                               continue;
+                       }
 
-                       DBA::update('item', ['vid' => Verb::getID($item['verb'])], ['id' => $item['id']]);
-
+                       DBA::update('item', ['vid' => Verb::getID($verb)], ['id' => $item['id']]);
                        ++$rows;
                }
                DBA::close($items);
@@ -870,4 +898,110 @@ class PostUpdate
 
                return false;
        }
+
+       /**
+        * update the "gsid" (global server id) field in the contact table
+        *
+        * @return bool "true" when the job is done
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       private static function update1348()
+       {
+               // Was the script completed?
+               if (DI::config()->get("system", "post_update_version") >= 1348) {
+                       return true;
+               }
+
+               $id = DI::config()->get("system", "post_update_version_1348_id", 0);
+
+               Logger::info('Start', ['contact' => $id]);
+
+               $start_id = $id;
+               $rows = 0;
+               $condition = ["`id` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
+               $params = ['order' => ['id'], 'limit' => 10000];
+               $contacts = DBA::select('contact', ['id', 'baseurl'], $condition, $params);
+
+               if (DBA::errorNo() != 0) {
+                       Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
+                       return false;
+               }
+
+               while ($contact = DBA::fetch($contacts)) {
+                       $id = $contact['id'];
+
+                       DBA::update('contact',
+                               ['gsid' => GServer::getID($contact['baseurl'], true), 'baseurl' => GServer::cleanURL($contact['baseurl'])],
+                               ['id' => $contact['id']]);
+
+                       ++$rows;
+               }
+               DBA::close($contacts);
+
+               DI::config()->set("system", "post_update_version_1348_id", $id);
+
+               Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
+
+               if ($start_id == $id) {
+                       DI::config()->set("system", "post_update_version", 1348);
+                       Logger::info('Done');
+                       return true;
+               }
+
+               return false;
+       }
+
+       /**
+        * update the "gsid" (global server id) field in the apcontact table
+        *
+        * @return bool "true" when the job is done
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       private static function update1349()
+       {
+               // Was the script completed?
+               if (DI::config()->get("system", "post_update_version") >= 1349) {
+                       return true;
+               }
+
+               $id = DI::config()->get("system", "post_update_version_1349_id", '');
+
+               Logger::info('Start', ['apcontact' => $id]);
+
+               $start_id = $id;
+               $rows = 0;
+               $condition = ["`url` > ? AND `gsid` IS NULL AND `baseurl` != '' AND NOT `baseurl` IS NULL", $id];
+               $params = ['order' => ['url'], 'limit' => 10000];
+               $apcontacts = DBA::select('apcontact', ['url', 'baseurl'], $condition, $params);
+
+               if (DBA::errorNo() != 0) {
+                       Logger::error('Database error', ['no' => DBA::errorNo(), 'message' => DBA::errorMessage()]);
+                       return false;
+               }
+
+               while ($apcontact = DBA::fetch($apcontacts)) {
+                       $id = $apcontact['url'];
+
+                       DBA::update('apcontact',
+                               ['gsid' => GServer::getID($apcontact['baseurl'], true), 'baseurl' => GServer::cleanURL($apcontact['baseurl'])],
+                               ['url' => $apcontact['url']]);
+
+                       ++$rows;
+               }
+               DBA::close($apcontacts);
+
+               DI::config()->set("system", "post_update_version_1349_id", $id);
+
+               Logger::info('Processed', ['rows' => $rows, 'last' => $id]);
+
+               if ($start_id == $id) {
+                       DI::config()->set("system", "post_update_version", 1349);
+                       Logger::info('Done');
+                       return true;
+               }
+
+               return false;
+       }
 }