From fd0ef2f2c7ccaae0a25d1c8b59a694116acfa2af Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 17 Jan 2017 19:01:27 +0000 Subject: [PATCH] Pumpio: Optimised query --- pumpio/pumpio.php | 69 +++++++++++++++-------------------------------- 1 file changed, 21 insertions(+), 48 deletions(-) diff --git a/pumpio/pumpio.php b/pumpio/pumpio.php index 69dd5d31..65f55552 100644 --- a/pumpio/pumpio.php +++ b/pumpio/pumpio.php @@ -835,8 +835,8 @@ function pumpio_dounlike(&$a, $uid, $self, $post, $own_id) { if(link_compare($post->actor->url, $own_id)) { $contactid = $self[0]['id']; } else { - $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1", - dbesc($post->actor->url), + $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1", + dbesc(normalise_link($post->actor->url)), intval($uid) ); @@ -960,52 +960,24 @@ function pumpio_dolike(&$a, $uid, $self, $post, $own_id, $threadcompletion = tru function pumpio_get_contact($uid, $contact, $no_insert = false) { - if (function_exists("update_gcontact")) { - update_gcontact(array("url" => $contact->url, "network" => NETWORK_PUMPIO, "generation" => 2, - "photo" => $contact->image->url, "name" => $contact->displayName, "hide" => true, - "nick" => $contact->preferredUsername, "location" => $contact->location->displayName, - "about" => $contact->summary, "addr" => str_replace("acct:", "", $contact->id))); - - $cid = get_contact($contact->url, $uid); - } else { - // Old Code - $r = q("SELECT id FROM unique_contacts WHERE url='%s' LIMIT 1", - dbesc(normalise_link($contact->url))); - - if (count($r) == 0) - q("INSERT INTO unique_contacts (url, name, nick, avatar) VALUES ('%s', '%s', '%s', '%s')", - dbesc(normalise_link($contact->url)), - dbesc($contact->displayName), - dbesc($contact->preferredUsername), - dbesc($contact->image->url)); - else - q("UPDATE unique_contacts SET name = '%s', nick = '%s', avatar = '%s' WHERE url = '%s'", - dbesc($contact->displayName), - dbesc($contact->preferredUsername), - dbesc($contact->image->url), - dbesc(normalise_link($contact->url))); - - if (DB_UPDATE_VERSION >= "1177") - q("UPDATE `unique_contacts` SET `location` = '%s', `about` = '%s' WHERE url = '%s'", - dbesc($contact->location->displayName), - dbesc($contact->summary), - dbesc(normalise_link($contact->url))); - - $cid = 0; - } + update_gcontact(array("url" => $contact->url, "network" => NETWORK_PUMPIO, "generation" => 2, + "photo" => $contact->image->url, "name" => $contact->displayName, "hide" => true, + "nick" => $contact->preferredUsername, "location" => $contact->location->displayName, + "about" => $contact->summary, "addr" => str_replace("acct:", "", $contact->id))); + $cid = get_contact($contact->url, $uid); if ($no_insert) return($cid); - $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1", - intval($uid), dbesc($contact->url)); + $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1", + intval($uid), dbesc(normalise_link($contact->url))); - if(!count($r)) { + if (!count($r)) { // create contact record - q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`, + q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`, `writable`, `blocked`, `readonly`, `pending` ) - VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ", + VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0)", intval($uid), dbesc(datetime_convert()), dbesc($contact->url), @@ -1023,15 +995,16 @@ function pumpio_get_contact($uid, $contact, $no_insert = false) { intval(1) ); - $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1", - dbesc($contact->url), + $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d LIMIT 1", + dbesc(normalise_link($contact->url)), intval($uid) ); - if(! count($r)) + if (!count($r)) { return(false); + } - $contact_id = $r[0]['id']; + $contact_id = $r[0]['id']; $g = q("select def_gid from user where uid = %d limit 1", intval($uid) @@ -1168,16 +1141,16 @@ function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id, $threadcomplet $post->actor->image->url = $self[0]['photo']; } elseif ($contact_id == 0) { // Take an existing contact, the contact of the note or - as a fallback - the id of the user - $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1", - dbesc($post->actor->url), + $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1", + dbesc(normalise_link($post->actor->url)), intval($uid) ); if(count($r)) $contact_id = $r[0]['id']; else { - $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1", - dbesc($post->actor->url), + $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1", + dbesc(normalise_link($post->actor->url)), intval($uid) );