From 5ab2786c00b84b3546d6c6c820c0f278baf6fb33 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Wed, 6 Jan 2016 22:23:12 +0100 Subject: [PATCH] Get rid of the table "unique_contacts" --- database.sql | 16 +---- doc/database.md | 1 - doc/database/db_unique_contacts.md | 14 ----- friendica_test_data.sql | 29 --------- include/Contact.php | 13 ---- include/api.php | 96 ++++++++---------------------- include/dbstructure.php | 15 ----- include/ostatus.php | 7 ++- mod/admin.php | 2 +- mod/display.php | 8 ++- 10 files changed, 38 insertions(+), 163 deletions(-) delete mode 100644 doc/database/db_unique_contacts.md diff --git a/database.sql b/database.sql index 90604457b..4e6f3d35c 100644 --- a/database.sql +++ b/database.sql @@ -332,6 +332,7 @@ CREATE TABLE IF NOT EXISTS `gcontact` ( `about` text NOT NULL, `keywords` text NOT NULL, `gender` varchar(32) NOT NULL DEFAULT '', + `birthday` varchar(32) NOT NULL DEFAULT '0000-00-00', `community` tinyint(1) NOT NULL DEFAULT 0, `hide` tinyint(1) NOT NULL DEFAULT 0, `nsfw` tinyint(1) NOT NULL DEFAULT 0, @@ -1003,21 +1004,6 @@ CREATE TABLE IF NOT EXISTS `tokens` ( PRIMARY KEY(`id`) ) DEFAULT CHARSET=utf8; --- --- TABLE unique_contacts --- -CREATE TABLE IF NOT EXISTS `unique_contacts` ( - `id` int(11) NOT NULL auto_increment, - `url` varchar(255) NOT NULL DEFAULT '', - `nick` varchar(255) NOT NULL DEFAULT '', - `name` varchar(255) NOT NULL DEFAULT '', - `avatar` varchar(255) NOT NULL DEFAULT '', - `location` varchar(255) NOT NULL DEFAULT '', - `about` text NOT NULL, - PRIMARY KEY(`id`), - INDEX `url` (`url`) -) DEFAULT CHARSET=utf8; - -- -- TABLE user -- diff --git a/doc/database.md b/doc/database.md index 1893f77d0..2ef77c6da 100644 --- a/doc/database.md +++ b/doc/database.md @@ -54,7 +54,6 @@ Database Tables | [term](help/database/db_term) | item taxonomy (categories, tags, etc.) table | | [thread](help/database/db_thread) | | | [tokens](help/database/db_tokens) | OAuth usage | -| [unique_contacts](help/database/db_unique_contacts) | | | [user](help/database/db_user) | local user table | | [userd](help/database/db_userd) | | | [workerqueue](help/database/db_workerqueue) | | diff --git a/doc/database/db_unique_contacts.md b/doc/database/db_unique_contacts.md deleted file mode 100644 index 491da6aac..000000000 --- a/doc/database/db_unique_contacts.md +++ /dev/null @@ -1,14 +0,0 @@ -Table unique_contacts -===================== - -| Field | Description | Type | Null | Key | Default | Extra | -|----------|------------------|--------------|------|-----|---------|----------------| -| id | sequential ID | int(11) | NO | PRI | NULL | auto_increment | -| url | | varchar(255) | NO | MUL | | | -| nick | | varchar(255) | NO | | | | -| name | | varchar(255) | NO | | | | -| avatar | | varchar(255) | NO | | | | -| location | | varchar(255) | NO | | | | -| about | | text | NO | | NULL | | - -Return to [database documentation](help/database) diff --git a/friendica_test_data.sql b/friendica_test_data.sql index c39a05765..45080e44d 100644 --- a/friendica_test_data.sql +++ b/friendica_test_data.sql @@ -1657,35 +1657,6 @@ LOCK TABLES `tokens` WRITE; /*!40000 ALTER TABLE `tokens` ENABLE KEYS */; UNLOCK TABLES; --- --- Table structure for table `unique_contacts` --- - -DROP TABLE IF EXISTS `unique_contacts`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `unique_contacts` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `url` varchar(255) NOT NULL DEFAULT '', - `nick` varchar(255) NOT NULL DEFAULT '', - `name` varchar(255) NOT NULL DEFAULT '', - `avatar` varchar(255) NOT NULL DEFAULT '', - `location` varchar(255) NOT NULL DEFAULT '', - `about` text NOT NULL, - PRIMARY KEY (`id`), - KEY `url` (`url`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Dumping data for table `unique_contacts` --- - -LOCK TABLES `unique_contacts` WRITE; -/*!40000 ALTER TABLE `unique_contacts` DISABLE KEYS */; -/*!40000 ALTER TABLE `unique_contacts` ENABLE KEYS */; -UNLOCK TABLES; - -- -- Table structure for table `user` -- diff --git a/include/Contact.php b/include/Contact.php index a3cbbfed1..3185e8734 100644 --- a/include/Contact.php +++ b/include/Contact.php @@ -205,19 +205,6 @@ function get_contact_details_by_url($url, $uid = -1) { if ((($profile["addr"] == "") OR ($profile["name"] == "")) AND in_array($profile["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) proc_run('php',"include/update_gcontact.php", $profile["gid"]); - - } else { - $r = q("SELECT `url`, `name`, `nick`, `avatar` AS `photo`, `location`, `about` FROM `unique_contacts` WHERE `url` = '%s'", - dbesc(normalise_link($url))); - - if (count($r)) { - $profile = $r[0]; - $profile["keywords"] = ""; - $profile["gender"] = ""; - $profile["community"] = false; - $profile["network"] = ""; - $profile["addr"] = ""; - } } // Fetching further contact data from the contact table diff --git a/include/api.php b/include/api.php index 790894d3f..3bc7c8bab 100644 --- a/include/api.php +++ b/include/api.php @@ -393,7 +393,7 @@ * Contact url or False if contact id is unknown */ function api_unique_id_to_url($id){ - $r = q("SELECT `url` FROM `unique_contacts` WHERE `id`=%d LIMIT 1", + $r = q("SELECT `url` FROM `gcontact` WHERE `id`=%d LIMIT 1", intval($id)); if ($r) return ($r[0]["url"]); @@ -503,9 +503,7 @@ $r = array(); if ($url != "") - $r = q("SELECT * FROM `unique_contacts` WHERE `url`='%s' LIMIT 1", $url); - elseif ($nick != "") - $r = q("SELECT * FROM `unique_contacts` WHERE `nick`='%s' LIMIT 1", $nick); + $r = q("SELECT * FROM `gcontact` WHERE `nurl`='%s' LIMIT 1", dbesc(normalise_link($url))); if ($r) { // If no nick where given, extract it from the address @@ -517,14 +515,14 @@ 'id_str' => (string) $r[0]["id"], 'name' => $r[0]["name"], 'screen_name' => (($r[0]['nick']) ? $r[0]['nick'] : $r[0]['name']), - 'location' => NULL, - 'description' => NULL, + 'location' => $r[0]["location"], + 'description' => $r[0]["about"], 'url' => $r[0]["url"], 'protected' => false, 'followers_count' => 0, 'friends_count' => 0, 'listed_count' => 0, - 'created_at' => api_date(0), + 'created_at' => api_date($r[0]["created"]), 'favourites_count' => 0, 'utc_offset' => 0, 'time_zone' => 'UTC', @@ -535,8 +533,8 @@ 'contributors_enabled' => false, 'is_translator' => false, 'is_translation_enabled' => false, - 'profile_image_url' => $r[0]["avatar"], - 'profile_image_url_https' => $r[0]["avatar"], + 'profile_image_url' => $r[0]["photo"], + 'profile_image_url_https' => $r[0]["photo"], 'following' => false, 'follow_request_sent' => false, 'notifications' => false, @@ -546,7 +544,7 @@ 'uid' => 0, 'cid' => 0, 'self' => 0, - 'network' => '', + 'network' => $r[0]["network"], ); return $ret; @@ -617,22 +615,14 @@ $uinfo[0]['nick'] = api_get_nick($uinfo[0]["url"]); } - // Fetching unique id - $r = q("SELECT id FROM `unique_contacts` WHERE `url`='%s' LIMIT 1", dbesc(normalise_link($uinfo[0]['url']))); - - // If not there, then add it - if (count($r) == 0) { - q("INSERT INTO `unique_contacts` (`url`, `name`, `nick`, `avatar`) VALUES ('%s', '%s', '%s', '%s')", - dbesc(normalise_link($uinfo[0]['url'])), dbesc($uinfo[0]['name']),dbesc($uinfo[0]['nick']), dbesc($uinfo[0]['micro'])); - - $r = q("SELECT `id` FROM `unique_contacts` WHERE `url`='%s' LIMIT 1", dbesc(normalise_link($uinfo[0]['url']))); - } - $network_name = network_to_name($uinfo[0]['network'], $uinfo[0]['url']); + $gcontact_id = get_gcontact_id(array("url" => $uinfo[0]['url'], "network" => $uinfo[0]['network'], + "photo" => $uinfo[0]['micro'], "name" => $uinfo[0]['name'])); + $ret = Array( - 'id' => intval($r[0]['id']), - 'id_str' => (string) intval($r[0]['id']), + 'id' => intval($gcontact_id), + 'id_str' => (string) intval($gcontact_id), 'name' => (($uinfo[0]['name']) ? $uinfo[0]['name'] : $uinfo[0]['nick']), 'screen_name' => (($uinfo[0]['nick']) ? $uinfo[0]['nick'] : $uinfo[0]['name']), 'location' => ($usr) ? $usr[0]['default-location'] : $network_name, @@ -666,45 +656,12 @@ function api_item_get_user(&$a, $item) { - $author = q("SELECT * FROM `unique_contacts` WHERE `url`='%s' LIMIT 1", - dbesc(normalise_link($item['author-link']))); + // Make sure that there is an entry in the global contacts for author and owner + get_gcontact_id(array("url" => $item['author-link'], "network" => $item['network'], + "photo" => $item['author-avatar'], "name" => $item['author-name'])); - if (count($author) == 0) { - q("INSERT INTO `unique_contacts` (`url`, `name`, `avatar`) VALUES ('%s', '%s', '%s')", - dbesc(normalise_link($item["author-link"])), dbesc($item["author-name"]), dbesc($item["author-avatar"])); - - $author = q("SELECT `id` FROM `unique_contacts` WHERE `url`='%s' LIMIT 1", - dbesc(normalise_link($item['author-link']))); - } else if ($item["author-link"].$item["author-name"] != $author[0]["url"].$author[0]["name"]) { - $r = q("SELECT `id` FROM `unique_contacts` WHERE `name` = '%s' AND `avatar` = '%s' AND url = '%s'", - dbesc($item["author-name"]), dbesc($item["author-avatar"]), - dbesc(normalise_link($item["author-link"]))); - - if (!$r) - q("UPDATE `unique_contacts` SET `name` = '%s', `avatar` = '%s' WHERE `url` = '%s'", - dbesc($item["author-name"]), dbesc($item["author-avatar"]), - dbesc(normalise_link($item["author-link"]))); - } - - $owner = q("SELECT `id` FROM `unique_contacts` WHERE `url`='%s' LIMIT 1", - dbesc(normalise_link($item['owner-link']))); - - if (count($owner) == 0) { - q("INSERT INTO `unique_contacts` (`url`, `name`, `avatar`) VALUES ('%s', '%s', '%s')", - dbesc(normalise_link($item["owner-link"])), dbesc($item["owner-name"]), dbesc($item["owner-avatar"])); - - $owner = q("SELECT `id` FROM `unique_contacts` WHERE `url`='%s' LIMIT 1", - dbesc(normalise_link($item['owner-link']))); - } else if ($item["owner-link"].$item["owner-name"] != $owner[0]["url"].$owner[0]["name"]) { - $r = q("SELECT `id` FROM `unique_contacts` WHERE `name` = '%s' AND `avatar` = '%s' AND url = '%s'", - dbesc($item["owner-name"]), dbesc($item["owner-avatar"]), - dbesc(normalise_link($item["owner-link"]))); - - if (!$r) - q("UPDATE `unique_contacts` SET `name` = '%s', `avatar` = '%s' WHERE `url` = '%s'", - dbesc($item["owner-name"]), dbesc($item["owner-avatar"]), - dbesc(normalise_link($item["owner-link"]))); - } + get_gcontact_id(array("url" => $item['owner-link'], "network" => $item['network'], + "photo" => $item['owner-avatar'], "name" => $item['owner-name'])); // Comments in threads may appear as wall-to-wall postings. // So only take the owner at the top posting. @@ -1073,7 +1030,7 @@ $in_reply_to_status_id= intval($lastwall['parent']); $in_reply_to_status_id_str = (string) intval($lastwall['parent']); - $r = q("SELECT * FROM `unique_contacts` WHERE `url` = '%s'", dbesc(normalise_link($lastwall['item-author']))); + $r = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($lastwall['item-author']))); if ($r) { if ($r[0]['nick'] == "") $r[0]['nick'] = api_get_nick($r[0]["url"]); @@ -1195,7 +1152,7 @@ $in_reply_to_status_id = intval($lastwall['parent']); $in_reply_to_status_id_str = (string) intval($lastwall['parent']); - $r = q("SELECT * FROM `unique_contacts` WHERE `url` = '%s'", dbesc(normalise_link($reply[0]['item-author']))); + $r = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($reply[0]['item-author']))); if ($r) { if ($r[0]['nick'] == "") $r[0]['nick'] = api_get_nick($r[0]["url"]); @@ -1256,9 +1213,9 @@ $userlist = array(); if (isset($_GET["q"])) { - $r = q("SELECT id FROM `unique_contacts` WHERE `name`='%s'", dbesc($_GET["q"])); + $r = q("SELECT id FROM `gcontact` WHERE `name`='%s'", dbesc($_GET["q"])); if (!count($r)) - $r = q("SELECT `id` FROM `unique_contacts` WHERE `nick`='%s'", dbesc($_GET["q"])); + $r = q("SELECT `id` FROM `gcontact` WHERE `nick`='%s'", dbesc($_GET["q"])); if (count($r)) { foreach ($r AS $user) { @@ -2341,7 +2298,7 @@ intval(api_user()), intval($in_reply_to_status_id)); if ($r) { - $r = q("SELECT * FROM `unique_contacts` WHERE `url` = '%s'", dbesc(normalise_link($r[0]['author-link']))); + $r = q("SELECT * FROM `gcontact` WHERE `url` = '%s'", dbesc(normalise_link($r[0]['author-link']))); if ($r) { if ($r[0]['nick'] == "") @@ -2596,7 +2553,7 @@ $stringify_ids = (x($_REQUEST,'stringify_ids')?$_REQUEST['stringify_ids']:false); - $r = q("SELECT `unique_contacts`.`id` FROM `contact`, `unique_contacts` WHERE `contact`.`nurl` = `unique_contacts`.`url` AND `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` $sql_extra", + $r = q("SELECT `gcontact`.`id` FROM `contact`, `gcontact` WHERE `contact`.`nurl` = `gcontact`.`nurl` AND `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` $sql_extra", intval(api_user()) ); @@ -3094,11 +3051,8 @@ //} - if ($nick != "") { - q("UPDATE `unique_contacts` SET `nick` = '%s' WHERE `nick` != '%s' AND url = '%s'", - dbesc($nick), dbesc($nick), dbesc(normalise_link($profile))); + if ($nick != "") return($nick); - } return(false); } diff --git a/include/dbstructure.php b/include/dbstructure.php index 1fc388d6c..1b414c70f 100644 --- a/include/dbstructure.php +++ b/include/dbstructure.php @@ -1340,21 +1340,6 @@ function db_definition() { "PRIMARY" => array("id"), ) ); - $database["unique_contacts"] = array( - "fields" => array( - "id" => array("type" => "int(11)", "not null" => "1", "extra" => "auto_increment", "primary" => "1"), - "url" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), - "nick" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), - "name" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), - "avatar" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), - "location" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), - "about" => array("type" => "text", "not null" => "1"), - ), - "indexes" => array( - "PRIMARY" => array("id"), - "url" => array("url"), - ) - ); $database["user"] = array( "fields" => array( "uid" => array("type" => "int(11)", "not null" => "1", "extra" => "auto_increment", "primary" => "1"), diff --git a/include/ostatus.php b/include/ostatus.php index 4a999ae2c..c1b823329 100644 --- a/include/ostatus.php +++ b/include/ostatus.php @@ -1452,9 +1452,12 @@ function ostatus_entry($doc, $item, $owner, $toplevel = false, $repeat = false) $repeated_owner["about"] = ""; $repeated_owner["uid"] = 0; - $r =q("SELECT * FROM `unique_contacts` WHERE `url` = '%s'", normalise_link($repeated_item["author-link"])); + // Fetch the missing data from the global contacts + $r =q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'", normalise_link($repeated_item["author-link"])); if ($r) { - $repeated_owner["nick"] = $r[0]["nick"]; + if ($r[0]["nick"] != "") + $repeated_owner["nick"] = $r[0]["nick"]; + $repeated_owner["location"] = $r[0]["location"]; $repeated_owner["about"] = $r[0]["about"]; } diff --git a/mod/admin.php b/mod/admin.php index 9e330fd1d..0636a3420 100644 --- a/mod/admin.php +++ b/mod/admin.php @@ -341,7 +341,7 @@ function admin_page_site_post(&$a){ update_table("profile", array('photo', 'thumb'), $old_url, $new_url); update_table("term", array('url'), $old_url, $new_url); update_table("contact", array('photo','thumb','micro','url','nurl','request','notify','poll','confirm','poco'), $old_url, $new_url); - update_table("unique_contacts", array('url'), $old_url, $new_url); + update_table("gcontact", array('photo','url','nurl','server_url'), $old_url, $new_url); update_table("item", array('owner-link','owner-avatar','author-name','author-link','author-avatar','body','plink','tag'), $old_url, $new_url); // update config diff --git a/mod/display.php b/mod/display.php index 6d1f417e7..c173c5a91 100644 --- a/mod/display.php +++ b/mod/display.php @@ -179,8 +179,8 @@ function display_fetchauthor($a, $item) { $profiledata["nickname"] = $r[0]["nick"]; } - // Fetching profile data from unique contacts - $r = q("SELECT `avatar`, `nick`, `location`, `about` FROM `unique_contacts` WHERE `url` = '%s'", dbesc(normalise_link($profiledata["url"]))); + // Fetching profile data from global contacts + $r = q("SELECT `photo`, `nick`, `addr`, `location`, `about`, `gender` FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($profiledata["url"]))); if (count($r)) { if ($profiledata["photo"] == "") $profiledata["photo"] = $r[0]["avatar"]; @@ -190,6 +190,10 @@ function display_fetchauthor($a, $item) { $profiledata["about"] = $r[0]["about"]; if (($profiledata["nickname"] == "") AND ($r[0]["nick"] != "")) $profiledata["nickname"] = $r[0]["nick"]; + if ($profiledata["gender"] == "") + $profiledata["gender"] = $r[0]["gender"]; + if ($profiledata["addr"] == "") + $profiledata["addr"] = $r[0]["addr"]; } if (local_user()) {