]> git.mxchange.org Git - friendica.git/commitdiff
replace "p" with higher level database functions
authorMichael <heluecht@pirati.ca>
Tue, 12 Oct 2021 05:53:29 +0000 (05:53 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 12 Oct 2021 05:53:29 +0000 (05:53 +0000)
mod/msearch.php
src/Console/FixAPDeliveryWorkerTaskParameters.php
src/Core/Worker.php
src/Model/Contact.php
src/Model/GServer.php
src/Protocol/ActivityPub/Transmitter.php
src/Worker/ExpireConversations.php
tests/DatabaseTestTrait.php

index 1e858f1db473504d7ba0ba86f26a40dad46fbe03..ce440d1ee25c86cf0a9164e8dbd8fd9d3b14124a 100644 (file)
@@ -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'],
index a8ad3b1f7b8be480a54645969c8563adb8169ee6..8b09a6b1734a4f49b8db1b58d68fbcd1bf71ef7e 100644 (file)
@@ -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);
index 3d3e11d8dd642e7c20eb9716fb3d439465a0fe2c..3519ce3e519f8e71c805c1eeba406b94e639f026 100644 (file)
@@ -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 {
index 60b303bd547c2034c9cfa7c9b1c73915db91e41b..93414d0ff9b29d2d7d16c6d343b00df8fe5248b8 100644 (file)
@@ -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;
        }
 
index d7a6cacbdc250aaca26d70523cd0676c7311d104..f09f53b0441025ef80c1fdfff395934add893b7f 100644 (file)
@@ -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']);
index 9bf846c28330f2bb14c2c4c4d94ea9c7938e071e..e741789dad217e22913df0bf6dbee8ad093b2f18 100644 (file)
@@ -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'];
                        }
index 8980a192acb9cf26e725e3b9c44cb9a710a4e4ed..f7aa593cf6f3cd9fb9e6b35877951b0097f7186c 100644 (file)
@@ -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]);
        }
 }
index eae5ce2ac9dd5c3a0eb24a69e120dd713266677b..9d46259ed1431fb1b35a894b720613a0c898444a 100644 (file)
@@ -68,7 +68,7 @@ trait DatabaseTestTrait
                        }
 
                        if (!is_array($rows)) {
-                               $dba->p('TRUNCATE TABLE `' . $tableName . '``');
+                               $dba->e('TRUNCATE TABLE `' . $tableName . '``');
                                continue;
                        }