3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module;
24 use Friendica\BaseModule;
25 use Friendica\Content\ContactSelector;
26 use Friendica\Content\Pager;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Search;
31 use Friendica\Network\HTTPException;
32 use Friendica\Object\Search\ContactResult;
33 use Friendica\Object\Search\ResultList;
36 * Base class for search modules
38 class BaseSearch extends BaseModule
41 * Performs a contact search with an optional prefix
43 * @param string $search Search query
44 * @param string $prefix A optional prefix (e.g. @ or !) for searching
47 * @throws HTTPException\InternalServerErrorException
48 * @throws \ImagickException
50 public static function performContactSearch($search, $prefix = '')
53 $config = DI::config();
55 $type = Search::TYPE_ALL;
57 $localSearch = $config->get('system', 'poco_local_search');
59 $search = $prefix . $search;
67 if (strpos($search, '@') === 0) {
68 $search = substr($search, 1);
69 $type = Search::TYPE_PEOPLE;
70 $header = DI::l10n()->t('People Search - %s', $search);
72 if (strrpos($search, '@') > 0) {
73 $results = Search::getContactsFromProbe($search);
77 if (strpos($search, '!') === 0) {
78 $search = substr($search, 1);
79 $type = Search::TYPE_FORUM;
80 $header = DI::l10n()->t('Forum Search - %s', $search);
83 if (DI::mode()->isMobile()) {
84 $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
85 DI::config()->get('system', 'itemspage_network_mobile'));
87 $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
88 DI::config()->get('system', 'itemspage_network'));
91 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
93 if ($localSearch && empty($results)) {
94 $pager->setItemsPerPage(80);
95 $results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage());
96 } elseif (strlen($config->get('system', 'directory')) && empty($results)) {
97 $results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage());
98 $pager->setItemsPerPage($results->getItemsPage());
101 return self::printResult($results, $pager, $header);
105 * Prints a human readable search result
107 * @param ResultList $results
108 * @param Pager $pager
109 * @param string $header
111 * @return string The result
112 * @throws HTTPException\InternalServerErrorException
113 * @throws \ImagickException
115 protected static function printResult(ResultList $results, Pager $pager, $header = '')
117 if ($results->getTotal() == 0) {
118 notice(DI::l10n()->t('No matches'));
124 foreach ($results->getResults() as $result) {
126 // in case the result is a contact result, add a contact-specific entry
127 if ($result instanceof ContactResult) {
128 $contact = Model\Contact::getByURL($result->getUrl());
129 if (!empty($contact)) {
130 $entries[] = Model\Contact::getTemplateData($contact, ++$id);
135 $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
136 return Renderer::replaceMacros($tpl, [
138 '$contacts' => $entries,
139 '$paginate' => $pager->renderFull($results->getTotal()),