]> git.mxchange.org Git - friendica.git/blob - src/Module/BaseSearch.php
3aae68f7b23f63f004c7c4df5e4710520117b745
[friendica.git] / src / Module / BaseSearch.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;
23
24 use Friendica\BaseModule;
25 use Friendica\Content\Pager;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\Search;
28 use Friendica\Core\Session;
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
67                 if (strpos($search, '@') === 0) {
68                         $search  = trim(substr($search, 1));
69                         $type    = Search::TYPE_PEOPLE;
70                         $header  = DI::l10n()->t('People Search - %s', $search);
71
72                         if (strrpos($search, '@') > 0) {
73                                 $results = Search::getContactsFromProbe(Network::convertToIdn($search));
74                         }
75                 }
76
77                 if (strpos($search, '!') === 0) {
78                         $search = trim(substr($search, 1));
79                         $type   = Search::TYPE_FORUM;
80                         $header = DI::l10n()->t('Forum Search - %s', $search);
81                 }
82
83                 $search = Network::convertToIdn($search);
84
85                 if (DI::mode()->isMobile()) {
86                         $itemsPerPage = DI::pConfig()->get(Session::getLocalUser(), 'system', 'itemspage_mobile_network',
87                                 DI::config()->get('system', 'itemspage_network_mobile'));
88                 } else {
89                         $itemsPerPage = DI::pConfig()->get(Session::getLocalUser(), 'system', 'itemspage_network',
90                                 DI::config()->get('system', 'itemspage_network'));
91                 }
92
93                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
94
95                 if ($localSearch && empty($results)) {
96                         $pager->setItemsPerPage(80);
97                         $results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage());
98                 } elseif (Search::getGlobalDirectory() && empty($results)) {
99                         $results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage());
100                         $pager->setItemsPerPage($results->getItemsPage());
101                 }
102
103                 return self::printResult($results, $pager, $header);
104         }
105
106         /**
107          * Prints a human readable search result
108          *
109          * @param ResultList $results
110          * @param Pager      $pager
111          * @param string     $header
112          *
113          * @return string The result
114          * @throws HTTPException\InternalServerErrorException
115          * @throws \ImagickException
116          */
117         protected static function printResult(ResultList $results, Pager $pager, string $header = ''): string
118         {
119                 if ($results->getTotal() == 0) {
120                         DI::sysmsg()->addNotice(DI::l10n()->t('No matches'));
121                         return '';
122                 }
123
124                 $entries = [];
125                 foreach ($results->getResults() as $result) {
126
127                         // in case the result is a contact result, add a contact-specific entry
128                         if ($result instanceof ContactResult) {
129                                 $contact = Model\Contact::getByURLForUser($result->getUrl(), Session::getLocalUser());
130                                 if (!empty($contact)) {
131                                         $entries[] = Contact::getContactTemplateVars($contact);
132                                 }
133                         }
134                 }
135
136                 $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
137                 return Renderer::replaceMacros($tpl, [
138                         'title'     => $header,
139                         '$contacts' => $entries,
140                         '$paginate' => $pager->renderFull($results->getTotal()),
141                 ]);
142         }
143 }