]> git.mxchange.org Git - friendica.git/blob - src/Module/Search/Tags.php
The GNU-Social import is removed
[friendica.git] / src / Module / Search / Tags.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Module\Search;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Core\L10n;
27 use Friendica\Core\System;
28 use Friendica\Database\Database;
29 use Friendica\Model\User;
30 use Friendica\Module\Response;
31 use Friendica\Util\Profiler;
32 use Friendica\Util\Proxy;
33 use Psr\Log\LoggerInterface;
34
35 /**
36  * Search users because of their public/private tags
37  */
38 class Tags extends BaseModule
39 {
40         const DEFAULT_ITEMS_PER_PAGE = 80;
41
42         /** @var Database */
43         protected $database;
44
45         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, Database $database, array $server, array $parameters = [])
46         {
47                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
48
49                 $this->database = $database;
50         }
51
52         protected function rawContent(array $request = [])
53         {
54                 $tags     = $request['s'] ?? '';
55                 $perPage  = intval($request['n'] ?? self::DEFAULT_ITEMS_PER_PAGE);
56                 $page     = intval($request['p'] ?? 1);
57                 $startRec = ($page - 1) * $perPage;
58
59                 $results = [];
60
61                 if (empty($tags)) {
62                         System::jsonExit([
63                                 'total'      => 0,
64                                 'items_page' => $perPage,
65                                 'page'       => $page,
66                                 'results'    => $results,
67                         ]);
68                 }
69
70                 $condition = [
71                         "`net-publish` AND MATCH(`pub_keywords`) AGAINST (?)",
72                         $tags
73                 ];
74
75                 $totalCount = $this->database->count('owner-view', $condition);
76                 if ($totalCount === 0) {
77                         System::jsonExit([
78                                 'total'      => 0,
79                                 'items_page' => $perPage,
80                                 'page'       => $page,
81                                 'results'    => $results,
82                         ]);
83                 }
84
85                 $searchStmt = $this->database->select('owner-view',
86                         ['pub_keywords', 'name', 'nickname', 'uid'],
87                         $condition,
88                         ['limit' => [$startRec, $perPage]]);
89
90                 while ($searchResult = $this->database->fetch($searchStmt)) {
91                         $results[] = [
92                                 'name'  => $searchResult['name'],
93                                 'url'   => $this->baseUrl . '/profile/' . $searchResult['nickname'],
94                                 'photo' => User::getAvatarUrl($searchResult, Proxy::SIZE_THUMB),
95                         ];
96                 }
97
98                 $this->database->close($searchStmt);
99
100                 System::jsonExit([
101                         'total'      => $totalCount,
102                         'items_page' => $perPage,
103                         'page'       => $page,
104                         'results'    => $results,
105                 ]);
106         }
107 }