2019-05-18 20:50:14 -04:00
|
|
|
<?php
|
2020-02-09 09:45:36 -05:00
|
|
|
/**
|
2022-01-02 02:27:47 -05:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2020-02-09 09:45:36 -05:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-05-18 20:50:14 -04:00
|
|
|
|
|
|
|
namespace Friendica\Module;
|
|
|
|
|
|
|
|
use Friendica\BaseModule;
|
|
|
|
use Friendica\Content\Pager;
|
|
|
|
use Friendica\Core\Renderer;
|
2019-05-20 12:42:27 -04:00
|
|
|
use Friendica\Core\Search;
|
2019-12-15 16:34:11 -05:00
|
|
|
use Friendica\DI;
|
2019-05-20 13:13:37 -04:00
|
|
|
use Friendica\Model;
|
|
|
|
use Friendica\Network\HTTPException;
|
2019-05-20 12:42:27 -04:00
|
|
|
use Friendica\Object\Search\ContactResult;
|
|
|
|
use Friendica\Object\Search\ResultList;
|
2022-05-11 02:34:25 -04:00
|
|
|
use Friendica\Util\Network;
|
2019-05-18 20:50:14 -04:00
|
|
|
|
2019-05-18 22:43:09 -04:00
|
|
|
/**
|
2019-05-18 23:01:46 -04:00
|
|
|
* Base class for search modules
|
2019-05-18 22:43:09 -04:00
|
|
|
*/
|
2020-01-22 23:14:14 -05:00
|
|
|
class BaseSearch extends BaseModule
|
2019-05-18 20:50:14 -04:00
|
|
|
{
|
2019-05-18 23:06:02 -04:00
|
|
|
/**
|
2019-12-24 17:15:41 -05:00
|
|
|
* Performs a contact search with an optional prefix
|
2019-05-18 23:06:02 -04:00
|
|
|
*
|
2019-10-07 14:27:20 -04:00
|
|
|
* @param string $search Search query
|
2019-05-18 23:06:02 -04:00
|
|
|
* @param string $prefix A optional prefix (e.g. @ or !) for searching
|
|
|
|
*
|
|
|
|
* @return string
|
2019-05-20 12:42:27 -04:00
|
|
|
* @throws HTTPException\InternalServerErrorException
|
2019-05-18 23:06:02 -04:00
|
|
|
* @throws \ImagickException
|
|
|
|
*/
|
2019-12-24 17:15:41 -05:00
|
|
|
public static function performContactSearch($search, $prefix = '')
|
2019-05-18 23:06:02 -04:00
|
|
|
{
|
2019-12-15 17:44:33 -05:00
|
|
|
$config = DI::config();
|
2019-05-18 23:06:02 -04:00
|
|
|
|
2019-05-20 13:13:37 -04:00
|
|
|
$type = Search::TYPE_ALL;
|
2019-05-18 23:06:02 -04:00
|
|
|
|
|
|
|
$localSearch = $config->get('system', 'poco_local_search');
|
|
|
|
|
2019-10-07 14:27:20 -04:00
|
|
|
$search = $prefix . $search;
|
2019-05-18 23:06:02 -04:00
|
|
|
|
|
|
|
if (!$search) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$header = '';
|
|
|
|
|
|
|
|
if (strpos($search, '@') === 0) {
|
|
|
|
$search = substr($search, 1);
|
2019-05-20 13:19:57 -04:00
|
|
|
$type = Search::TYPE_PEOPLE;
|
2020-01-18 14:52:34 -05:00
|
|
|
$header = DI::l10n()->t('People Search - %s', $search);
|
2019-06-20 17:56:18 -04:00
|
|
|
|
|
|
|
if (strrpos($search, '@') > 0) {
|
2022-05-11 02:34:25 -04:00
|
|
|
$results = Search::getContactsFromProbe(Network::convertToIdn($search));
|
2019-06-20 17:56:18 -04:00
|
|
|
}
|
2019-05-18 23:06:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($search, '!') === 0) {
|
2019-05-20 13:19:57 -04:00
|
|
|
$search = substr($search, 1);
|
|
|
|
$type = Search::TYPE_FORUM;
|
2020-01-18 14:52:34 -05:00
|
|
|
$header = DI::l10n()->t('Forum Search - %s', $search);
|
2019-05-18 23:06:02 -04:00
|
|
|
}
|
|
|
|
|
2022-05-11 02:34:25 -04:00
|
|
|
$search = Network::convertToIdn($search);
|
|
|
|
|
2020-02-16 13:04:26 -05:00
|
|
|
if (DI::mode()->isMobile()) {
|
|
|
|
$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
|
|
|
|
DI::config()->get('system', 'itemspage_network_mobile'));
|
|
|
|
} else {
|
|
|
|
$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
|
|
|
|
DI::config()->get('system', 'itemspage_network'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
|
2019-05-18 23:06:02 -04:00
|
|
|
|
|
|
|
if ($localSearch && empty($results)) {
|
|
|
|
$pager->setItemsPerPage(80);
|
2019-05-20 13:15:47 -04:00
|
|
|
$results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage());
|
2022-04-26 14:33:58 -04:00
|
|
|
} elseif (Search::getGlobalDirectory() && empty($results)) {
|
2019-05-20 13:19:57 -04:00
|
|
|
$results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage());
|
2019-05-18 23:06:02 -04:00
|
|
|
$pager->setItemsPerPage($results->getItemsPage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::printResult($results, $pager, $header);
|
|
|
|
}
|
|
|
|
|
2019-05-18 23:01:46 -04:00
|
|
|
/**
|
|
|
|
* Prints a human readable search result
|
|
|
|
*
|
2019-05-20 12:42:27 -04:00
|
|
|
* @param ResultList $results
|
2019-05-20 13:19:57 -04:00
|
|
|
* @param Pager $pager
|
|
|
|
* @param string $header
|
2019-05-18 23:01:46 -04:00
|
|
|
*
|
|
|
|
* @return string The result
|
2019-05-20 12:42:27 -04:00
|
|
|
* @throws HTTPException\InternalServerErrorException
|
2019-05-18 23:01:46 -04:00
|
|
|
* @throws \ImagickException
|
|
|
|
*/
|
2019-05-20 12:42:27 -04:00
|
|
|
protected static function printResult(ResultList $results, Pager $pager, $header = '')
|
2019-05-18 20:50:14 -04:00
|
|
|
{
|
2019-05-24 08:13:36 -04:00
|
|
|
if ($results->getTotal() == 0) {
|
2020-07-23 02:11:21 -04:00
|
|
|
notice(DI::l10n()->t('No matches'));
|
2019-05-18 20:50:14 -04:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$entries = [];
|
|
|
|
foreach ($results->getResults() as $result) {
|
|
|
|
|
2019-05-20 12:42:27 -04:00
|
|
|
// in case the result is a contact result, add a contact-specific entry
|
|
|
|
if ($result instanceof ContactResult) {
|
2020-07-30 23:55:01 -04:00
|
|
|
$contact = Model\Contact::getByURLForUser($result->getUrl(), local_user());
|
2020-07-30 17:16:15 -04:00
|
|
|
if (!empty($contact)) {
|
2020-07-31 00:28:26 -04:00
|
|
|
$entries[] = Contact::getContactTemplateVars($contact);
|
2020-07-30 17:16:15 -04:00
|
|
|
}
|
2019-05-18 20:50:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
|
|
|
|
return Renderer::replaceMacros($tpl, [
|
|
|
|
'title' => $header,
|
|
|
|
'$contacts' => $entries,
|
|
|
|
'$paginate' => $pager->renderFull($results->getTotal()),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|