]> git.mxchange.org Git - friendica.git/blob - src/Module/BaseSearch.php
"escapeTags" is finally removed
[friendica.git] / src / Module / BaseSearch.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\DI;
29 use Friendica\Model;
30 use Friendica\Network\HTTPException;
31 use Friendica\Object\Search\ContactResult;
32 use Friendica\Object\Search\ResultList;
33
34 /**
35  * Base class for search modules
36  */
37 class BaseSearch extends BaseModule
38 {
39         /**
40          * Performs a contact search with an optional prefix
41          *
42          * @param string $search Search query
43          * @param string $prefix A optional prefix (e.g. @ or !) for searching
44          *
45          * @return string
46          * @throws HTTPException\InternalServerErrorException
47          * @throws \ImagickException
48          */
49         public static function performContactSearch($search, $prefix = '')
50         {
51                 $config = DI::config();
52
53                 $type = Search::TYPE_ALL;
54
55                 $localSearch = $config->get('system', 'poco_local_search');
56
57                 $search = $prefix . $search;
58
59                 if (!$search) {
60                         return '';
61                 }
62
63                 $header = '';
64
65                 if (strpos($search, '@') === 0) {
66                         $search  = substr($search, 1);
67                         $type    = Search::TYPE_PEOPLE;
68                         $header  = DI::l10n()->t('People Search - %s', $search);
69
70                         if (strrpos($search, '@') > 0) {
71                                 $results = Search::getContactsFromProbe($search);
72                         }
73                 }
74
75                 if (strpos($search, '!') === 0) {
76                         $search = substr($search, 1);
77                         $type   = Search::TYPE_FORUM;
78                         $header = DI::l10n()->t('Forum Search - %s', $search);
79                 }
80
81                 if (DI::mode()->isMobile()) {
82                         $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
83                                 DI::config()->get('system', 'itemspage_network_mobile'));
84                 } else {
85                         $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
86                                 DI::config()->get('system', 'itemspage_network'));
87                 }
88
89                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
90
91                 if ($localSearch && empty($results)) {
92                         $pager->setItemsPerPage(80);
93                         $results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage());
94                 } elseif (strlen($config->get('system', 'directory')) && empty($results)) {
95                         $results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage());
96                         $pager->setItemsPerPage($results->getItemsPage());
97                 }
98
99                 return self::printResult($results, $pager, $header);
100         }
101
102         /**
103          * Prints a human readable search result
104          *
105          * @param ResultList $results
106          * @param Pager      $pager
107          * @param string     $header
108          *
109          * @return string The result
110          * @throws HTTPException\InternalServerErrorException
111          * @throws \ImagickException
112          */
113         protected static function printResult(ResultList $results, Pager $pager, $header = '')
114         {
115                 if ($results->getTotal() == 0) {
116                         notice(DI::l10n()->t('No matches'));
117                         return '';
118                 }
119
120                 $entries = [];
121                 foreach ($results->getResults() as $result) {
122
123                         // in case the result is a contact result, add a contact-specific entry
124                         if ($result instanceof ContactResult) {
125                                 $contact = Model\Contact::getByURLForUser($result->getUrl(), local_user());
126                                 if (!empty($contact)) {
127                                         $entries[] = Contact::getContactTemplateVars($contact);
128                                 }
129                         }
130                 }
131
132                 $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
133                 return Renderer::replaceMacros($tpl, [
134                         'title'     => $header,
135                         '$contacts' => $entries,
136                         '$paginate' => $pager->renderFull($results->getTotal()),
137                 ]);
138         }
139 }