From 818cdf83cabe8d075d7b05b0adc7e5ec5317c21b Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 10 Nov 2022 20:42:47 -0500 Subject: [PATCH] [Database version 1500] Remove obsolete table fcontact --- database.sql | 35 +------- doc/database.md | 1 - doc/database/db_fcontact.md | 51 ----------- src/Database/DBStructure.php | 2 +- src/Database/PostUpdate.php | 5 ++ src/Model/FContact.php | 161 ---------------------------------- src/Protocol/DFRN.php | 1 - src/Worker/UpdateFContact.php | 41 --------- static/dbstructure.config.php | 37 +------- update.php | 8 +- 10 files changed, 16 insertions(+), 326 deletions(-) delete mode 100644 doc/database/db_fcontact.md delete mode 100644 src/Model/FContact.php delete mode 100644 src/Worker/UpdateFContact.php diff --git a/database.sql b/database.sql index ae2f8096ba..3169bac03a 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 2022.12-dev (Giant Rhubarb) --- DB_UPDATE_VERSION 1499 +-- DB_UPDATE_VERSION 1500 -- ------------------------------------------ @@ -667,39 +667,6 @@ CREATE TABLE IF NOT EXISTS `event` ( FOREIGN KEY (`uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Events'; --- --- TABLE fcontact --- -CREATE TABLE IF NOT EXISTS `fcontact` ( - `id` int unsigned NOT NULL auto_increment COMMENT 'sequential ID', - `guid` varbinary(255) NOT NULL DEFAULT '' COMMENT 'unique id', - `url` varbinary(383) NOT NULL DEFAULT '' COMMENT '', - `uri-id` int unsigned COMMENT 'Id of the item-uri table entry that contains the fcontact url', - `name` varchar(255) NOT NULL DEFAULT '' COMMENT '', - `photo` varbinary(383) NOT NULL DEFAULT '' COMMENT '', - `request` varbinary(383) NOT NULL DEFAULT '' COMMENT '', - `nick` varchar(255) NOT NULL DEFAULT '' COMMENT '', - `addr` varchar(255) NOT NULL DEFAULT '' COMMENT '', - `batch` varbinary(383) NOT NULL DEFAULT '' COMMENT '', - `notify` varbinary(383) NOT NULL DEFAULT '' COMMENT '', - `poll` varbinary(383) NOT NULL DEFAULT '' COMMENT '', - `confirm` varbinary(383) NOT NULL DEFAULT '' COMMENT '', - `priority` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '', - `network` char(4) NOT NULL DEFAULT '' COMMENT '', - `alias` varbinary(383) NOT NULL DEFAULT '' COMMENT '', - `pubkey` text COMMENT '', - `created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '', - `updated` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '', - `interacting_count` int unsigned DEFAULT 0 COMMENT 'Number of contacts this contact interactes with', - `interacted_count` int unsigned DEFAULT 0 COMMENT 'Number of contacts that interacted with this contact', - `post_count` int unsigned DEFAULT 0 COMMENT 'Number of posts and comments', - PRIMARY KEY(`id`), - INDEX `addr` (`addr`(32)), - UNIQUE INDEX `url` (`url`(190)), - UNIQUE INDEX `uri-id` (`uri-id`), - FOREIGN KEY (`uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE -) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Diaspora compatible contacts - used in the Diaspora implementation'; - -- -- TABLE fetch-entry -- diff --git a/doc/database.md b/doc/database.md index 8d460f772a..9fa438c8d2 100644 --- a/doc/database.md +++ b/doc/database.md @@ -27,7 +27,6 @@ Database Tables | [diaspora-interaction](help/database/db_diaspora-interaction) | Signed Diaspora Interaction | | [endpoint](help/database/db_endpoint) | ActivityPub endpoints - used in the ActivityPub implementation | | [event](help/database/db_event) | Events | -| [fcontact](help/database/db_fcontact) | Diaspora compatible contacts - used in the Diaspora implementation | | [fetch-entry](help/database/db_fetch-entry) | | | [fetched-activity](help/database/db_fetched-activity) | Id of fetched activities | | [fsuggest](help/database/db_fsuggest) | friend suggestion stuff | diff --git a/doc/database/db_fcontact.md b/doc/database/db_fcontact.md deleted file mode 100644 index 095c47aafd..0000000000 --- a/doc/database/db_fcontact.md +++ /dev/null @@ -1,51 +0,0 @@ -Table fcontact -=========== - -Diaspora compatible contacts - used in the Diaspora implementation - -Fields ------- - -| Field | Description | Type | Null | Key | Default | Extra | -| ----------------- | ------------------------------------------------------------- | ---------------- | ---- | --- | ------------------- | -------------- | -| id | sequential ID | int unsigned | NO | PRI | NULL | auto_increment | -| guid | unique id | varbinary(255) | NO | | | | -| url | | varbinary(383) | NO | | | | -| uri-id | Id of the item-uri table entry that contains the fcontact url | int unsigned | YES | | NULL | | -| name | | varchar(255) | NO | | | | -| photo | | varbinary(383) | NO | | | | -| request | | varbinary(383) | NO | | | | -| nick | | varchar(255) | NO | | | | -| addr | | varchar(255) | NO | | | | -| batch | | varbinary(383) | NO | | | | -| notify | | varbinary(383) | NO | | | | -| poll | | varbinary(383) | NO | | | | -| confirm | | varbinary(383) | NO | | | | -| priority | | tinyint unsigned | NO | | 0 | | -| network | | char(4) | NO | | | | -| alias | | varbinary(383) | NO | | | | -| pubkey | | text | YES | | NULL | | -| created | | datetime | NO | | 0001-01-01 00:00:00 | | -| updated | | datetime | NO | | 0001-01-01 00:00:00 | | -| interacting_count | Number of contacts this contact interactes with | int unsigned | YES | | 0 | | -| interacted_count | Number of contacts that interacted with this contact | int unsigned | YES | | 0 | | -| post_count | Number of posts and comments | int unsigned | YES | | 0 | | - -Indexes ------------- - -| Name | Fields | -| ------- | ---------------- | -| PRIMARY | id | -| addr | addr(32) | -| url | UNIQUE, url(190) | -| uri-id | UNIQUE, uri-id | - -Foreign Keys ------------- - -| Field | Target Table | Target Field | -|-------|--------------|--------------| -| uri-id | [item-uri](help/database/db_item-uri) | id | - -Return to [database documentation](help/database) diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index 74bd5b4237..6e50c9fb28 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -74,7 +74,7 @@ class DBStructure $old_tables = ['fserver', 'gcign', 'gcontact', 'gcontact-relation', 'gfollower' ,'glink', 'item-delivery-data', 'item-activity', 'item-content', 'item_id', 'participation', 'poll', 'poll_result', 'queue', 'retriever_rule', 'deliverq', 'dsprphotoq', 'ffinder', 'sign', 'spam', 'term', 'user-item', 'thread', 'item', 'challenge', - 'auth_codes', 'tokens', 'clients', 'profile_check', 'host', 'conversation']; + 'auth_codes', 'tokens', 'clients', 'profile_check', 'host', 'conversation', 'fcontact']; $tables = DBA::selectToArray('INFORMATION_SCHEMA.TABLES', ['TABLE_NAME'], ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_TYPE' => 'BASE TABLE']); diff --git a/src/Database/PostUpdate.php b/src/Database/PostUpdate.php index adc88b13e8..e57d92ceb7 100644 --- a/src/Database/PostUpdate.php +++ b/src/Database/PostUpdate.php @@ -899,6 +899,11 @@ class PostUpdate return true; } + if (!DBStructure::existsTable('fcontact')) { + DI::config()->set('system', 'post_update_version', 1425); + return true; + } + $condition = ["`uri-id` IS NULL"]; Logger::info('Start', ['rest' => DBA::count('fcontact', $condition)]); diff --git a/src/Model/FContact.php b/src/Model/FContact.php deleted file mode 100644 index e13dcd46c9..0000000000 --- a/src/Model/FContact.php +++ /dev/null @@ -1,161 +0,0 @@ -. - * - */ - -namespace Friendica\Model; - -use Friendica\Core\Logger; -use Friendica\Core\Protocol; -use Friendica\Core\Worker; -use Friendica\Database\DBA; -use Friendica\DI; -use Friendica\Model\Item; -use Friendica\Network\Probe; -use Friendica\Util\DateTimeFormat; -use Friendica\Util\Strings; - -class FContact -{ - /** - * Fetches data for a given handle - * - * @param string $handle The handle - * @param boolean $update true = always update, false = never update, null = update when not found or outdated - * - * @return array the queried data - * @throws \Friendica\Network\HTTPException\InternalServerErrorException - * @throws \ImagickException - */ - public static function getByURL(string $handle, $update = null): array - { - Logger::debug('Fetch fcontact', ['handle' => $handle, 'update' => $update]); - $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]); - if (!DBA::isResult($person)) { - $urls = [$handle, str_replace('http://', 'https://', $handle), Strings::normaliseLink($handle)]; - $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'url' => $urls]); - } - - if (DBA::isResult($person)) { - Logger::debug('In cache', ['handle' => $handle]); - - if (is_null($update)) { - $update = empty($person['guid']) || empty($person['uri-id']) || ($person['created'] <= DBA::NULL_DATETIME); - if (GServer::getNextUpdateDate(true, $person['created'], $person['updated'], false) < DateTimeFormat::utcNow()) { - Logger::debug('Start background update', ['handle' => $handle]); - Worker::add(['priority' => Worker::PRIORITY_LOW, 'dont_fork' => true], 'UpdateFContact', $handle); - } - } - } elseif (is_null($update)) { - $update = true; - } else { - $person = []; - } - - if ($update) { - Logger::info('create or refresh', ['handle' => $handle]); - $data = Probe::uri($handle, Protocol::DIASPORA); - - // Note that Friendica contacts will return a "Diaspora person" - // if Diaspora connectivity is enabled on their server - if ($data['network'] ?? '' === Protocol::DIASPORA) { - self::updateFromProbeArray($data); - - $person = self::getByURL($handle, false); - } - } - - return $person; - } - - /** - * Updates the fcontact table - * - * @param array $arr The fcontact data - * @throws \Exception - */ - public static function updateFromProbeArray(array $arr) - { - $uriid = ItemURI::insert(['uri' => $arr['url'], 'guid' => $arr['guid']]); - - $fcontact = DBA::selectFirst('fcontact', ['created'], ['url' => $arr['url'], 'network' => $arr['network']]); - $contact = Contact::getByUriId($uriid, ['id', 'created']); - $apcontact = APContact::getByURL($arr['url'], false); - if (!empty($apcontact)) { - $interacted = $apcontact['following_count']; - $interacting = $apcontact['followers_count']; - $posts = $apcontact['statuses_count']; - } elseif (!empty($contact['id'])) { - $last_interaction = DateTimeFormat::utc('now - 180 days'); - - $interacted = DBA::count('contact-relation', ["`cid` = ? AND NOT `follows` AND `last-interaction` > ?", $contact['id'], $last_interaction]); - $interacting = DBA::count('contact-relation', ["`relation-cid` = ? AND NOT `follows` AND `last-interaction` > ?", $contact['id'], $last_interaction]); - $posts = DBA::count('post', ['author-id' => $contact['id'], 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT]]); - } - - $fields = [ - 'name' => $arr['name'], - 'photo' => $arr['photo'], - 'request' => $arr['request'], - 'nick' => $arr['nick'], - 'addr' => strtolower($arr['addr']), - 'guid' => $arr['guid'], - 'batch' => $arr['batch'], - 'notify' => $arr['notify'], - 'poll' => $arr['poll'], - 'confirm' => $arr['confirm'], - 'alias' => $arr['alias'], - 'pubkey' => $arr['pubkey'], - 'uri-id' => $uriid, - 'interacting_count' => $interacting ?? 0, - 'interacted_count' => $interacted ?? 0, - 'post_count' => $posts ?? 0, - 'updated' => DateTimeFormat::utcNow(), - ]; - - if (empty($fcontact['created'])) { - $fields['created'] = $fields['updated']; - } elseif (!empty($contact['created']) && ($fcontact['created'] <= DBA::NULL_DATETIME)) { - $fields['created'] = $contact['created']; - } - - $fields = DI::dbaDefinition()->truncateFieldsForTable('fcontact', $fields); - DBA::update('fcontact', $fields, ['url' => $arr['url'], 'network' => $arr['network']], true); - } - - /** - * get a url (scheme://domain.tld/u/user) from a given Diaspora* - * fcontact guid - * - * @param string $fcontact_guid Hexadecimal string guid - * @return string|null the contact url or null - * @throws \Exception - */ - public static function getUrlByGuid(string $fcontact_guid) - { - Logger::info('fcontact', ['guid' => $fcontact_guid]); - - $fcontact = DBA::selectFirst('fcontact', ['url'], ["`url` != ? AND `network` = ? AND `guid` = ?", '', Protocol::DIASPORA, $fcontact_guid]); - if (DBA::isResult($fcontact)) { - return $fcontact['url']; - } - - return null; - } -} diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 621cc2e3cf..92f3e81545 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -34,7 +34,6 @@ use Friendica\DI; use Friendica\Model\Contact; use Friendica\Model\Conversation; use Friendica\Model\Event; -use Friendica\Model\FContact; use Friendica\Model\GServer; use Friendica\Model\Item; use Friendica\Model\ItemURI; diff --git a/src/Worker/UpdateFContact.php b/src/Worker/UpdateFContact.php deleted file mode 100644 index 260e071715..0000000000 --- a/src/Worker/UpdateFContact.php +++ /dev/null @@ -1,41 +0,0 @@ -. - * - */ - -namespace Friendica\Worker; - -use Friendica\Core\Logger; -use Friendica\Model\FContact; - -class UpdateFContact -{ - /** - * Update fcontact data via probe - * - * @param string $handle Contact handle - * @return void - */ - public static function execute(string $handle) - { - $success = FContact::getByURL($handle, true); - - Logger::info('Updated from probe', ['handle' => $handle, 'success' => $success]); - } -} diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 10ef6dad91..d5bbed666a 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -55,7 +55,7 @@ use Friendica\Database\DBA; if (!defined('DB_UPDATE_VERSION')) { - define('DB_UPDATE_VERSION', 1498); + define('DB_UPDATE_VERSION', 1500); } return [ @@ -723,39 +723,6 @@ return [ "uri-id" => ["uri-id"], ] ], - "fcontact" => [ - "comment" => "Diaspora compatible contacts - used in the Diaspora implementation", - "fields" => [ - "id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "sequential ID"], - "guid" => ["type" => "varbinary(255)", "not null" => "1", "default" => "", "comment" => "unique id"], - "url" => ["type" => "varbinary(383)", "not null" => "1", "default" => "", "comment" => ""], - "uri-id" => ["type" => "int unsigned", "foreign" => ["item-uri" => "id"], "comment" => "Id of the item-uri table entry that contains the fcontact url"], - "name" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""], - "photo" => ["type" => "varbinary(383)", "not null" => "1", "default" => "", "comment" => ""], - "request" => ["type" => "varbinary(383)", "not null" => "1", "default" => "", "comment" => ""], - "nick" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""], - "addr" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""], - "batch" => ["type" => "varbinary(383)", "not null" => "1", "default" => "", "comment" => ""], - "notify" => ["type" => "varbinary(383)", "not null" => "1", "default" => "", "comment" => ""], - "poll" => ["type" => "varbinary(383)", "not null" => "1", "default" => "", "comment" => ""], - "confirm" => ["type" => "varbinary(383)", "not null" => "1", "default" => "", "comment" => ""], - "priority" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => ""], - "network" => ["type" => "char(4)", "not null" => "1", "default" => "", "comment" => ""], - "alias" => ["type" => "varbinary(383)", "not null" => "1", "default" => "", "comment" => ""], - "pubkey" => ["type" => "text", "comment" => ""], - "created" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""], - "updated" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""], - "interacting_count" => ["type" => "int unsigned", "default" => 0, "comment" => "Number of contacts this contact interactes with"], - "interacted_count" => ["type" => "int unsigned", "default" => 0, "comment" => "Number of contacts that interacted with this contact"], - "post_count" => ["type" => "int unsigned", "default" => 0, "comment" => "Number of posts and comments"], - ], - "indexes" => [ - "PRIMARY" => ["id"], - "addr" => ["addr(32)"], - "url" => ["UNIQUE", "url(190)"], - "uri-id" => ["UNIQUE", "uri-id"], - ] - ], "fetch-entry" => [ "comment" => "", "fields" => [ @@ -903,7 +870,7 @@ return [ "fields" => [ "id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "sequential ID"], "uid" => ["type" => "mediumint unsigned", "not null" => "1", "default" => "0", "foreign" => ["user" => "uid"], "comment" => "User id"], - "fid" => ["type" => "int unsigned", "relation" => ["fcontact" => "id"], "comment" => "deprecated"], + "fid" => ["type" => "int unsigned", "comment" => "deprecated"], "contact-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id"], "comment" => ""], "suggest-cid" => ["type" => "int unsigned", "foreign" => ["contact" => "id"], "comment" => "Suggested contact"], "knowyou" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""], diff --git a/update.php b/update.php index 1767470571..12a3fb74e6 100644 --- a/update.php +++ b/update.php @@ -974,7 +974,7 @@ function update_1429() return Update::FAILED; } - if (!DBA::e("UPDATE `fcontact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) { + if (DBStructure::existsTable('fcontact') && !DBA::e("UPDATE `fcontact` SET `uri-id` = null WHERE NOT `uri-id` IS NULL")) { return Update::FAILED; } @@ -1013,6 +1013,10 @@ function update_1438() function update_1439() { + if (!DBStructure::existsTable('fcontact')) { + return Update::SUCCESS; + } + $intros = DBA::select('intro', ['id', 'fid'], ["NOT `fid` IS NULL AND `fid` != ?", 0]); while ($intro = DBA::fetch($intros)) { $fcontact = DBA::selectFirst('fcontact', ['url'], ['id' => $intro['fid']]); @@ -1024,6 +1028,8 @@ function update_1439() } } DBA::close($intros); + + return Update::SUCCESS; } function update_1440() -- 2.39.5