]> git.mxchange.org Git - friendica.git/blob - src/Module/BaseSearch.php
Merge pull request #13599 from Raroun/Fix_for_Pull_Request_#13596_missing_a_hidden...
[friendica.git] / src / Module / BaseSearch.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\Module;
23
24 use Friendica\BaseModule;
25 use Friendica\Content\Pager;
26 use Friendica\Core\Logger;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Search;
29 use Friendica\DI;
30 use Friendica\Model;
31 use Friendica\Network\HTTPException;
32 use Friendica\Object\Search\ContactResult;
33 use Friendica\Object\Search\ResultList;
34 use Friendica\Util\Network;
35
36 /**
37  * Base class for search modules
38  */
39 class BaseSearch extends BaseModule
40 {
41         /**
42          * Performs a contact search with an optional prefix
43          *
44          * @param string $search Search query
45          * @param string $prefix A optional prefix (e.g. @ or !) for searching
46          *
47          * @return string
48          * @throws HTTPException\InternalServerErrorException
49          * @throws \ImagickException
50          */
51         public static function performContactSearch(string $search, string $prefix = ''): string
52         {
53                 $config = DI::config();
54
55                 $type = Search::TYPE_ALL;
56
57                 $localSearch = $config->get('system', 'poco_local_search');
58
59                 $search = $prefix . $search;
60
61                 if (!$search) {
62                         return '';
63                 }
64
65                 $header = '';
66                 $results = new ResultList();
67
68                 if (strpos($search, '@') === 0) {
69                         $search  = trim(substr($search, 1));
70                         $type    = Search::TYPE_PEOPLE;
71                         $header  = DI::l10n()->t('People Search - %s', $search);
72                 } elseif (strpos($search, '!') === 0) {
73                         $search = trim(substr($search, 1));
74                         $type   = Search::TYPE_GROUP;
75                         $header = DI::l10n()->t('Group Search - %s', $search);
76                 }
77
78                 $search = Network::convertToIdn($search);
79
80                 if (DI::mode()->isMobile()) {
81                         $itemsPerPage = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'itemspage_mobile_network',
82                                 DI::config()->get('system', 'itemspage_network_mobile'));
83                 } else {
84                         $itemsPerPage = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'itemspage_network',
85                                 DI::config()->get('system', 'itemspage_network'));
86                 }
87
88                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
89
90                 if (!$results->getTotal() && !$localSearch && Search::getGlobalDirectory()) {
91                         $results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage());
92                         $pager->setItemsPerPage($results->getItemsPage());
93                 }
94
95                 if (!$results->getTotal()) {
96                         $pager->setItemsPerPage(80);
97                         $results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage());
98                 }
99
100                 if (!$results->getTotal()) {
101                         $results = Search::getContactsFromProbe(Network::convertToIdn($search), $type == Search::TYPE_GROUP);
102                 }
103
104                 return self::printResult($results, $pager, $header);
105         }
106
107         /**
108          * Prints a human readable search result
109          *
110          * @param ResultList $results
111          * @param Pager      $pager
112          * @param string     $header
113          *
114          * @return string The result
115          * @throws HTTPException\InternalServerErrorException
116          * @throws \ImagickException
117          */
118         protected static function printResult(ResultList $results, Pager $pager, string $header = ''): string
119         {
120                 if ($results->getTotal() == 0) {
121                         DI::sysmsg()->addNotice(DI::l10n()->t('No matches'));
122                         return '';
123                 }
124
125                 $filtered = 0;
126
127                 $entries = [];
128                 foreach ($results->getResults() as $result) {
129                         // in case the result is a contact result, add a contact-specific entry
130                         if ($result instanceof ContactResult) {
131                                 if (Network::isUriBlocked($result->getUrl())) {
132                                         $filtered++;
133                                         continue;
134                                 }
135
136                                 $contact = Model\Contact::getByURLForUser($result->getUrl(), DI::userSession()->getLocalUserId());
137                                 if (!empty($contact)) {
138                                         $entries[] = Contact::getContactTemplateVars($contact);
139                                 }
140                         }
141                 }
142
143                 $tpl = Renderer::getMarkupTemplate('contact/list.tpl');
144                 return Renderer::replaceMacros($tpl, [
145                         '$title'    => $header,
146                         '$filtered' => $filtered ? DI::l10n()->tt(
147                                 '%d result was filtered out because your node blocks the domain it is registered on. You can review the list of domains your node is currently blocking in the <a href="/friendica">About page</a>.',
148                                 '%d results were filtered out because your node blocks the domain they are registered on. You can review the list of domains your node is currently blocking in the <a href="/friendica">About page</a>.',
149                                 $filtered) : '',
150                         '$contacts' => $entries,
151                         '$paginate' => $pager->renderFull($results->getTotal()),
152                 ]);
153         }
154 }