From 27b9da3df51acc9fa7c5f92053af289c8f9fd2a7 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 12 Oct 2021 05:53:29 +0000 Subject: [PATCH 1/3] replace "p" with higher level database functions --- mod/msearch.php | 15 ++-------- .../FixAPDeliveryWorkerTaskParameters.php | 2 +- src/Core/Worker.php | 18 ++++-------- src/Model/Contact.php | 28 +++++++------------ src/Model/GServer.php | 12 +++----- src/Protocol/ActivityPub/Transmitter.php | 21 +++----------- src/Worker/ExpireConversations.php | 3 +- tests/DatabaseTestTrait.php | 2 +- 8 files changed, 30 insertions(+), 71 deletions(-) diff --git a/mod/msearch.php b/mod/msearch.php index 1e858f1db4..ce440d1ee2 100644 --- a/mod/msearch.php +++ b/mod/msearch.php @@ -43,6 +43,8 @@ function msearch_post(App $a) $total = 0; + $condition = ["`net-publish` AND MATCH(`pub_keywords`) AGAINST (?)", $search]; + $total = DBA::count('owner-view', $condition); $count_stmt = DBA::p( "SELECT COUNT(*) AS `total` FROM `profile` @@ -58,18 +60,7 @@ function msearch_post(App $a) DBA::close($count_stmt); - $search_stmt = DBA::p( - "SELECT `pub_keywords`, `username`, `nickname`, `user`.`uid` - FROM `user` - JOIN `profile` ON `user`.`uid` = `profile`.`uid` - WHERE `profile`.`net-publish` - AND MATCH(`pub_keywords`) AGAINST (?) - LIMIT ?, ?", - $search, - $startrec, - $perpage - ); - + $search_stmt = DBA::select('owner-view', ['pub_keywords', 'name', 'nickname', 'uid'], $condition, ['limit' => [$startrec, $perpage]]); while ($search_result = DBA::fetch($search_stmt)) { $results[] = [ 'name' => $search_result['name'], diff --git a/src/Console/FixAPDeliveryWorkerTaskParameters.php b/src/Console/FixAPDeliveryWorkerTaskParameters.php index a8ad3b1f7b..8b09a6b173 100644 --- a/src/Console/FixAPDeliveryWorkerTaskParameters.php +++ b/src/Console/FixAPDeliveryWorkerTaskParameters.php @@ -106,7 +106,7 @@ HELP; $this->errored = 0; do { - $result = $this->dba->p('SELECT `id`, `parameter` FROM `workerqueue` WHERE `command` = "APDelivery" AND `parameter` LIKE "[\"%\",\"\",%" LIMIT ' . $this->examined . ', 100'); + $result = $this->dba->select('workerqueue', ['id', 'parameter'], ["`command` = ? AND `parameter` LIKE ?", "APDelivery", "[\"%\",\"\",%"], ['limit' => [$this->examined, 100]]); while ($row = $this->dba->fetch($result)) { $this->examined++; $this->processRow($row); diff --git a/src/Core/Worker.php b/src/Core/Worker.php index 3d3e11d8dd..3519ce3e51 100644 --- a/src/Core/Worker.php +++ b/src/Core/Worker.php @@ -712,13 +712,10 @@ class Worker } $stamp = (float)microtime(true); - $jobs = DBA::p("SELECT COUNT(*) AS `jobs` FROM `workerqueue` WHERE `done` AND `executed` > UTC_TIMESTAMP() - INTERVAL ? MINUTE", $interval); + $jobs = DBA::count('workerqueue', ["`done` AND `executed` > UTC_TIMESTAMP() - INTERVAL ? MINUTE", $interval]); self::$db_duration += (microtime(true) - $stamp); self::$db_duration_stat += (microtime(true) - $stamp); - if ($job = DBA::fetch($jobs)) { - $jobs_per_minute[$interval] = number_format($job['jobs'] / $interval, 0); - } - DBA::close($jobs); + $jobs_per_minute[$interval] = number_format($jobs / $interval, 0); } $processlist = ' - jpm: '.implode('/', $jobs_per_minute); } @@ -739,15 +736,12 @@ class Worker self::$db_duration_stat += (microtime(true) - $stamp); while ($entry = DBA::fetch($jobs)) { $stamp = (float)microtime(true); - $processes = DBA::p("SELECT COUNT(*) AS `running` FROM `workerqueue-view` WHERE `priority` = ?", $entry["priority"]); + $running = DBA::count('workerqueue-view', ['priority' => $entry["priority"]]); self::$db_duration += (microtime(true) - $stamp); self::$db_duration_stat += (microtime(true) - $stamp); - if ($process = DBA::fetch($processes)) { - $idle_workers -= $process["running"]; - $waiting_processes += $entry["entries"]; - $listitem[$entry["priority"]] = $entry["priority"].":".$process["running"]."/".$entry["entries"]; - } - DBA::close($processes); + $idle_workers -= $running; + $waiting_processes += $entry["entries"]; + $listitem[$entry["priority"]] = $entry["priority"] . ":" . $running . "/" . $entry["entries"]; } DBA::close($jobs); } else { diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 60b303bd54..93414d0ff9 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -3013,37 +3013,29 @@ class Contact } // check supported networks + $networks = [Protocol::DFRN, Protocol::ACTIVITYPUB]; if (DI::config()->get('system', 'diaspora_enabled')) { - $diaspora = Protocol::DIASPORA; - } else { - $diaspora = Protocol::DFRN; + $networks[] = Protocol::DIASPORA; } if (!DI::config()->get('system', 'ostatus_disabled')) { - $ostatus = Protocol::OSTATUS; - } else { - $ostatus = Protocol::DFRN; + $networks[] = Protocol::OSTATUS; } + $condition = ['network' => $networks, 'failed' => false, 'uid' => $uid]; + // check if we search only communities or every contact if ($mode === 'community') { - $extra_sql = sprintf(' AND `contact-type` = %d', self::TYPE_COMMUNITY); - } else { - $extra_sql = ''; + $condition['contact-type'] = self::TYPE_COMMUNITY; } $search .= '%'; - $results = DBA::p("SELECT * FROM `contact` - WHERE (NOT `unsearchable` OR `nurl` IN (SELECT `nurl` FROM `owner-view` where `publish` OR `net-publish`)) - AND `network` IN (?, ?, ?, ?) AND - NOT `failed` AND `uid` = ? AND - (`addr` LIKE ? OR `name` LIKE ? OR `nick` LIKE ?) $extra_sql - ORDER BY `nurl` DESC LIMIT 1000", - Protocol::DFRN, Protocol::ACTIVITYPUB, $ostatus, $diaspora, $uid, $search, $search, $search - ); + $condition = DBA::mergeConditions($condition, + ["(NOT `unsearchable` OR `nurl` IN (SELECT `nurl` FROM `owner-view` where `publish` OR `net-publish`)) + AND (`addr` LIKE ? OR `name` LIKE ? OR `nick` LIKE ?)", $search, $search, $search]); - $contacts = DBA::toArray($results); + $contacts = self::selectToArray([], $condition); return $contacts; } diff --git a/src/Model/GServer.php b/src/Model/GServer.php index d7a6cacbdc..f09f53b044 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -1667,14 +1667,10 @@ class GServer $last_update = date('c', time() - (60 * 60 * 24 * $requery_days)); - $gservers = DBA::p("SELECT `id`, `url`, `nurl`, `network`, `poco`, `directory-type` - FROM `gserver` - WHERE NOT `failed` - AND `directory-type` != ? - AND `last_poco_query` < ? - ORDER BY RAND()", self::DT_NONE, $last_update - ); - + $gservers = DBA::select('gserver', ['id', 'url', 'nurl', 'network', 'poco', 'directory-type'], + ["NOT `failed` AND `directory-type` != ? AND `last_poco_query` < ?", GServer::DT_NONE, $last_update], + ['order' => ['RAND()']]); + while ($gserver = DBA::fetch($gservers)) { Logger::info('Update peer list', ['server' => $gserver['url'], 'id' => $gserver['id']]); Worker::add(PRIORITY_LOW, 'UpdateServerPeers', $gserver['url']); diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 9bf846c283..e741789dad 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -166,21 +166,17 @@ class Transmitter 'pending' => false, 'blocked' => false, ]; - $condition = DBA::buildCondition($parameters); - $sql = "SELECT COUNT(*) as `count` - FROM `contact` - JOIN `apcontact` ON `apcontact`.`url` = `contact`.`url` - " . $condition; + $condition = DBA::mergeConditions($parameters, ["`url` IN (SELECT `url` FROM `apcontact`)"]); - $contacts = DBA::fetchFirst($sql, ...$parameters); + $total = DBA::count('contact', $condition); $modulePath = '/' . $module . '/'; $data = ['@context' => ActivityPub::CONTEXT]; $data['id'] = DI::baseUrl() . $modulePath . $owner['nickname']; $data['type'] = 'OrderedCollection'; - $data['totalItems'] = $contacts['count']; + $data['totalItems'] = $total; // When we hide our friends we will only show the pure number but don't allow more. $profile = Profile::getByUID($owner['uid']); @@ -194,16 +190,7 @@ class Transmitter $data['type'] = 'OrderedCollectionPage'; $list = []; - $sql = "SELECT `contact`.`url` - FROM `contact` - JOIN `apcontact` ON `apcontact`.`url` = `contact`.`url` - " . $condition . " - LIMIT ?, ?"; - - $parameters[] = ($page - 1) * 100; - $parameters[] = 100; - - $contacts = DBA::p($sql, ...$parameters); + $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]); while ($contact = DBA::fetch($contacts)) { $list[] = $contact['url']; } diff --git a/src/Worker/ExpireConversations.php b/src/Worker/ExpireConversations.php index 8980a192ac..f7aa593cf6 100644 --- a/src/Worker/ExpireConversations.php +++ b/src/Worker/ExpireConversations.php @@ -36,7 +36,6 @@ class ExpireConversations return; } - DBA::e("DELETE FROM `conversation` WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY", $days); - + DBA::delete('conversation', ["`received` < UTC_TIMESTAMP() - INTERVAL ? DAY", $days]); } } diff --git a/tests/DatabaseTestTrait.php b/tests/DatabaseTestTrait.php index eae5ce2ac9..9d46259ed1 100644 --- a/tests/DatabaseTestTrait.php +++ b/tests/DatabaseTestTrait.php @@ -68,7 +68,7 @@ trait DatabaseTestTrait } if (!is_array($rows)) { - $dba->p('TRUNCATE TABLE `' . $tableName . '``'); + $dba->e('TRUNCATE TABLE `' . $tableName . '``'); continue; } From 2904f4ca0e604f6af8cc1d873326587dafa89a66 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 13 Oct 2021 05:11:36 +0000 Subject: [PATCH 2/3] Olq query removed, checking for deleted as well --- mod/msearch.php | 14 -------------- src/Model/Contact.php | 4 ++-- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/mod/msearch.php b/mod/msearch.php index ce440d1ee2..969446ffb5 100644 --- a/mod/msearch.php +++ b/mod/msearch.php @@ -45,20 +45,6 @@ function msearch_post(App $a) $condition = ["`net-publish` AND MATCH(`pub_keywords`) AGAINST (?)", $search]; $total = DBA::count('owner-view', $condition); - $count_stmt = DBA::p( - "SELECT COUNT(*) AS `total` - FROM `profile` - JOIN `user` ON `user`.`uid` = `profile`.`uid` - WHERE `profile`.`net-publish` - AND MATCH(`pub_keywords`) AGAINST (?)", - $search - ); - if (DBA::isResult($count_stmt)) { - $row = DBA::fetch($count_stmt); - $total = $row['total']; - } - - DBA::close($count_stmt); $search_stmt = DBA::select('owner-view', ['pub_keywords', 'name', 'nickname', 'uid'], $condition, ['limit' => [$startrec, $perpage]]); while ($search_result = DBA::fetch($search_stmt)) { diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 93414d0ff9..236c0b9b99 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -3022,7 +3022,7 @@ class Contact $networks[] = Protocol::OSTATUS; } - $condition = ['network' => $networks, 'failed' => false, 'uid' => $uid]; + $condition = ['network' => $networks, 'failed' => false, 'deleted' => false, 'uid' => $uid]; // check if we search only communities or every contact if ($mode === 'community') { @@ -3032,7 +3032,7 @@ class Contact $search .= '%'; $condition = DBA::mergeConditions($condition, - ["(NOT `unsearchable` OR `nurl` IN (SELECT `nurl` FROM `owner-view` where `publish` OR `net-publish`)) + ["(NOT `unsearchable` OR `nurl` IN (SELECT `nurl` FROM `owner-view` WHERE `publish` OR `net-publish`)) AND (`addr` LIKE ? OR `name` LIKE ? OR `nick` LIKE ?)", $search, $search, $search]); $contacts = self::selectToArray([], $condition); From 8d7846142ddb2f500a615e5f68cd68cbae5a0702 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 13 Oct 2021 21:04:46 +0000 Subject: [PATCH 3/3] Hide blocked contacts when searching public contacts --- src/Model/Contact.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 236c0b9b99..f095543ebd 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -3024,6 +3024,10 @@ class Contact $condition = ['network' => $networks, 'failed' => false, 'deleted' => false, 'uid' => $uid]; + if ($uid == 0) { + $condition['blocked'] = false; + } + // check if we search only communities or every contact if ($mode === 'community') { $condition['contact-type'] = self::TYPE_COMMUNITY;