]> git.mxchange.org Git - friendica.git/commitdiff
Optimized Profile::searchProfiles & Bugfix Contact::getProbeDataFromDatabase
authorPhilipp Holzer <admin@philipp.info>
Sun, 5 May 2019 16:40:36 +0000 (18:40 +0200)
committerPhilipp Holzer <admin@philipp.info>
Sun, 5 May 2019 16:40:36 +0000 (18:40 +0200)
src/Model/Contact.php
src/Model/Profile.php

index f4c62fea809d554e220e180b292a9e1004f9268f..3f25fd938aa55843af403ffea61e9e99a3037b39 100644 (file)
@@ -1168,7 +1168,7 @@ class Contact extends BaseObject
                // The link could be provided as http although we stored it as https
                $ssl_url = str_replace('http://', 'https://', $url);
 
-               $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick',
+               $fields = ['id', 'uid', 'url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick',
                        'photo', 'keywords', 'location', 'about', 'network',
                        'priority', 'batch', 'request', 'confirm', 'poco'];
 
index 9d2600b3daab16156c825ff4f458cd352ad1bf6a..a854b1c9e019d3c6c11e753eb1cd94a13aabf6bd 100644 (file)
@@ -1249,56 +1249,96 @@ class Profile
         */
        public static function searchProfiles($start = 0, $count = 100, $search = null)
        {
-               if ($search) {
-                       $search = DBA::escape($search);
-
-                       $sql_extra = " AND ((`profile`.`name` LIKE '%$search%') OR
-                               (`user`.`nickname` LIKE '%$search%') OR
-                               (`profile`.`pdesc` LIKE '%$search%') OR
-                               (`profile`.`locality` LIKE '%$search%') OR
-                               (`profile`.`region` LIKE '%$search%') OR
-                               (`profile`.`country-name` LIKE '%$search%') OR
-                               (`profile`.`gender` LIKE '%$search%') OR
-                               (`profile`.`marital` LIKE '%$search%') OR
-                               (`profile`.`sexual` LIKE '%$search%') OR
-                               (`profile`.`about` LIKE '%$search%') OR
-                               (`profile`.`romance` LIKE '%$search%') OR
-                               (`profile`.`work` LIKE '%$search%') OR
-                               (`profile`.`education` LIKE '%$search%') OR
-                               (`profile`.`pub_keywords` LIKE '%$search%') OR
-                               (`profile`.`prv_keywords` LIKE '%$search%'))";
-               } else {
-                       $sql_extra = '';
-               }
-
                $publish = (Config::get('system', 'publish_all') ? '' : " AND `publish` = 1 ");
-
                $total = 0;
-               $cnt = DBA::fetchFirst("SELECT COUNT(*) AS `total` 
+
+               if (!empty($search)) {
+                       $searchTerm = '%' . $search . '%';
+                       $cnt = DBA::fetchFirst("SELECT COUNT(*) AS `total` 
                                FROM `profile`
                                LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
-                               WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed` $sql_extra");
+                               WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed`
+                               AND ((`profile`.`name` LIKE ?) OR
+                               (`user`.`nickname` LIKE ?) OR
+                               (`profile`.`pdesc` LIKE ?) OR
+                               (`profile`.`locality` LIKE ?) OR
+                               (`profile`.`region` LIKE ?) OR
+                               (`profile`.`country-name` LIKE ?) OR
+                               (`profile`.`gender` LIKE ?) OR
+                               (`profile`.`marital` LIKE ?) OR
+                               (`profile`.`sexual` LIKE ?) OR
+                               (`profile`.`about` LIKE ?) OR
+                               (`profile`.`romance` LIKE ?) OR
+                               (`profile`.`work` LIKE ?) OR
+                               (`profile`.`education` LIKE ?) OR
+                               (`profile`.`pub_keywords` LIKE ?) OR
+                               (`profile`.`prv_keywords` LIKE ?))",
+                               $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm,
+                               $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm);
+               } else {
+                       $cnt = DBA::fetchFirst("SELECT COUNT(*) AS `total` 
+                               FROM `profile`
+                               LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
+                               WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed`");
+               }
+
                if (DBA::isResult($cnt)) {
                        $total = $cnt['total'];
                }
 
                $order = " ORDER BY `name` ASC ";
-               $limit = $start . ',' . $count;
+               $profiles = [];
+
+               // If nothing found, don't try to select details
+               if ($total > 0) {
+                       if (!empty($search)) {
+                               $searchTerm = '%' . $search . '%';
 
-               $profiles = DBA::p("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`, `user`.`timezone` , `user`.`page-flags`,
+                               $profiles = DBA::p("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`, `user`.`timezone` , `user`.`page-flags`,
                        `contact`.`addr`, `contact`.`url` AS `profile_url`
                        FROM `profile`
                        LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
                        LEFT JOIN `contact` ON `contact`.`uid` = `user`.`uid`
                        WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed` AND `contact`.`self`
-                       $sql_extra $order LIMIT $limit"
-               );
+                       AND ((`profile`.`name` LIKE ?) OR
+                               (`user`.`nickname` LIKE ?) OR
+                               (`profile`.`pdesc` LIKE ?) OR
+                               (`profile`.`locality` LIKE ?) OR
+                               (`profile`.`region` LIKE ?) OR
+                               (`profile`.`country-name` LIKE ?) OR
+                               (`profile`.`gender` LIKE ?) OR
+                               (`profile`.`marital` LIKE ?) OR
+                               (`profile`.`sexual` LIKE ?) OR
+                               (`profile`.`about` LIKE ?) OR
+                               (`profile`.`romance` LIKE ?) OR
+                               (`profile`.`work` LIKE ?) OR
+                               (`profile`.`education` LIKE ?) OR
+                               (`profile`.`pub_keywords` LIKE ?) OR
+                               (`profile`.`prv_keywords` LIKE ?))
+                       $order LIMIT ?,?",
+                                       $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm,
+                                       $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm,
+                                       $start, $count
+                               );
+                       } else {
+                               $profiles = DBA::p("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`, `user`.`timezone` , `user`.`page-flags`,
+                       `contact`.`addr`, `contact`.`url` AS `profile_url`
+                       FROM `profile`
+                       LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
+                       LEFT JOIN `contact` ON `contact`.`uid` = `user`.`uid`
+                       WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed` AND `contact`.`self`
+                       $order LIMIT ?,?",
+                                       $start, $count
+                               );
+                       }
+               }
 
-               if (DBA::isResult($profiles)) {
+               if (DBA::isResult($profiles) && $total > 0) {
                        return [
                                'total'   => $total,
                                'entries' => DBA::toArray($profiles),
                        ];
+
                } else {
                        return [
                                'total'   => $total,