]> git.mxchange.org Git - friendica.git/blob - src/Model/Search.php
Fix uddate issues and improve speed when displaying contact posts
[friendica.git] / src / Model / Search.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Friendica\Database\DBA;
25 use Friendica\DI;
26 use Friendica\Util\DateTimeFormat;
27
28 /**
29  * Model for DB specific logic for the search entity
30  */
31 class Search
32 {
33         /**
34          * Returns the list of user defined tags (e.g. #Friendica)
35          *
36          * @return array
37          * @throws \Exception
38          */
39         public static function getUserTags(): array
40         {
41                 $user_condition = ["`verified` AND NOT `blocked` AND NOT `account_removed` AND NOT `account_expired` AND `user`.`uid` > ?", 0];
42
43                 $abandon_days = intval(DI::config()->get('system', 'account_abandon_days'));
44                 if (!empty($abandon_days)) {
45                         $user_condition = DBA::mergeConditions($user_condition, ["`last-activity` > ?", DateTimeFormat::utc('now - ' . $abandon_days . ' days')]);
46                 }
47
48                 $condition = $user_condition;
49                 $condition[0] = "SELECT DISTINCT(`term`) FROM `search` INNER JOIN `user` ON `search`.`uid` = `user`.`uid` WHERE " . $user_condition[0];
50                 $sql = array_shift($condition);
51                 $termsStmt = DBA::p($sql, $condition);
52
53                 $tags = [];
54                 while ($term = DBA::fetch($termsStmt)) {
55                         $tags[] = trim(mb_strtolower($term['term']), '#');
56                 }
57                 DBA::close($termsStmt);
58
59                 $condition = $user_condition;
60                 $condition[0] = "SELECT `include-tags` FROM `channel` INNER JOIN `user` ON `channel`.`uid` = `user`.`uid` WHERE " . $user_condition[0];
61                 $sql = array_shift($condition);
62                 $channels = DBA::p($sql, $condition);
63                 while ($channel = DBA::fetch($channels)) {
64                         foreach (explode(',', $channel['include-tags']) as $tag) {
65                                 $tag = trim(mb_strtolower($tag));
66                                 if (empty($tag)) {
67                                         continue;
68                                 }
69                                 if (!in_array($tag, $tags)) {
70                                         $tags[] = $tag;
71                                 }
72                         }
73                 }
74                 DBA::close($channels);
75
76                 sort($tags);
77
78                 return $tags;
79         }
80 }