X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModule%2FSearch%2FIndex.php;h=debaec7301516ad3353d95c903d485005716913e;hb=7c7cb8373c6e5444507b04fdbb7d0d4ce830f895;hp=98c593f45fd842e159b5e850713934101229702e;hpb=c742c62f0aae6a033823dd34ae97426dc90e7580;p=friendica.git diff --git a/src/Module/Search/Index.php b/src/Module/Search/Index.php index 98c593f45f..ec96b559a7 100644 --- a/src/Module/Search/Index.php +++ b/src/Module/Search/Index.php @@ -1,60 +1,91 @@ . + * + */ namespace Friendica\Module\Search; +use Friendica\App; use Friendica\Content\Nav; use Friendica\Content\Pager; use Friendica\Content\Text\HTML; use Friendica\Content\Widget; -use Friendica\Core\Cache\Duration; -use Friendica\Core\Config; +use Friendica\Core\Cache\Enum\Duration; use Friendica\Core\L10n; use Friendica\Core\Logger; use Friendica\Core\Renderer; -use Friendica\Core\Session; +use Friendica\Core\Search; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Contact; use Friendica\Model\Item; -use Friendica\Model\Term; -use Friendica\Module\BaseSearchModule; +use Friendica\Model\Post; +use Friendica\Model\Tag; +use Friendica\Module\BaseSearch; +use Friendica\Module\Response; use Friendica\Network\HTTPException; -use Friendica\Util\Strings; +use Friendica\Util\Network; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; -class Index extends BaseSearchModule +class Index extends BaseSearch { - public static function content(array $parameters = []) + /** @var string */ + private $remoteAddress; + + public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, App\Request $request, array $server, array $parameters = []) + { + parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->remoteAddress = $request->getRemoteAddress(); + } + + protected function content(array $request = []): string { - $search = (!empty($_GET['q']) ? Strings::escapeTags(trim(rawurldecode($_GET['q']))) : ''); + $search = (!empty($_GET['q']) ? trim(rawurldecode($_GET['q'])) : ''); - if (Config::get('system', 'block_public') && !Session::isAuthenticated()) { - throw new HTTPException\ForbiddenException(L10n::t('Public access denied.')); + if (DI::config()->get('system', 'block_public') && !DI::userSession()->isAuthenticated()) { + throw new HTTPException\ForbiddenException(DI::l10n()->t('Public access denied.')); } - if (Config::get('system', 'local_search') && !Session::isAuthenticated()) { - $e = new HTTPException\ForbiddenException(L10n::t('Only logged in users are permitted to perform a search.')); - $e->httpdesc = L10n::t('Public access denied.'); - throw $e; + if (DI::config()->get('system', 'local_search') && !DI::userSession()->isAuthenticated()) { + throw new HTTPException\ForbiddenException(DI::l10n()->t('Only logged in users are permitted to perform a search.')); } - if (Config::get('system', 'permit_crawling') && !Session::isAuthenticated()) { + if (DI::config()->get('system', 'permit_crawling') && !DI::userSession()->isAuthenticated()) { // Default values: // 10 requests are "free", after the 11th only a call per minute is allowed - $free_crawls = intval(Config::get('system', 'free_crawls')); + $free_crawls = intval(DI::config()->get('system', 'free_crawls')); if ($free_crawls == 0) $free_crawls = 10; - $crawl_permit_period = intval(Config::get('system', 'crawl_permit_period')); + $crawl_permit_period = intval(DI::config()->get('system', 'crawl_permit_period')); if ($crawl_permit_period == 0) $crawl_permit_period = 10; - $remote = $_SERVER['REMOTE_ADDR']; + $remote = $this->remoteAddress; $result = DI::cache()->get('remote_search:' . $remote); if (!is_null($result)) { $resultdata = json_decode($result); if (($resultdata->time > (time() - $crawl_permit_period)) && ($resultdata->accesses > $free_crawls)) { - throw new HTTPException\TooManyRequestsException(L10n::t('Only one search per minute is permitted for not logged in users.')); + throw new HTTPException\TooManyRequestsException(DI::l10n()->t('Only one search per minute is permitted for not logged in users.')); } DI::cache()->set('remote_search:' . $remote, json_encode(['time' => time(), 'accesses' => $resultdata->accesses + 1]), Duration::HOUR); } else { @@ -62,8 +93,8 @@ class Index extends BaseSearchModule } } - if (local_user()) { - DI::page()['aside'] .= Widget\SavedSearches::getHTML('search?q=' . urlencode($search), $search); + if (DI::userSession()->getLocalUserId()) { + DI::page()['aside'] .= Widget\SavedSearches::getHTML(Search::getSearchPath($search), $search); } Nav::setSelected('search'); @@ -71,13 +102,13 @@ class Index extends BaseSearchModule $tag = false; if (!empty($_GET['tag'])) { $tag = true; - $search = '#' . Strings::escapeTags(trim(rawurldecode($_GET['tag']))); + $search = '#' . trim(rawurldecode($_GET['tag'])); } // contruct a wrapper for the search header $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('content_wrapper.tpl'), [ 'name' => 'search-header', - '$title' => L10n::t('Search'), + '$title' => DI::l10n()->t('Search'), '$title_size' => 3, '$content' => HTML::search($search, 'search-box', false) ]); @@ -89,92 +120,90 @@ class Index extends BaseSearchModule if (strpos($search, '#') === 0) { $tag = true; $search = substr($search, 1); - } - - self::tryRedirectToProfile($search); + } else { + if (strpos($search, '@') === 0 || strpos($search, '!') === 0) { + return self::performContactSearch($search); + } - if (strpos($search, '@') === 0 || strpos($search, '!') === 0) { - return self::performContactSearch($search); + self::tryRedirectToPost($search); + + self::tryRedirectToProfile($search); + + if (!empty($_GET['search-option'])) { + switch ($_GET['search-option']) { + case 'fulltext': + break; + case 'tags': + $tag = true; + break; + case 'contacts': + return self::performContactSearch($search, '@'); + case 'forums': + return self::performContactSearch($search, '!'); + } + } } - self::tryRedirectToPost($search); - - if (!empty($_GET['search-option'])) { - switch ($_GET['search-option']) { - case 'fulltext': - break; - case 'tags': - $tag = true; - break; - case 'contacts': - return self::performContactSearch($search, '@'); - case 'forums': - return self::performContactSearch($search, '!'); - } + // Don't perform a fulltext or tag search on search results that look like an URL + // Tags don't look like an URL and the fulltext search does only work with natural words + if (parse_url($search, PHP_URL_SCHEME) && parse_url($search, PHP_URL_HOST)) { + Logger::info('Skipping tag and fulltext search since the search looks like a URL.', ['q' => $search]); + DI::sysmsg()->addNotice(DI::l10n()->t('No results.')); + return $o; } - $tag = $tag || Config::get('system', 'only_tag_search'); + $tag = $tag || DI::config()->get('system', 'only_tag_search'); // Here is the way permissions work in the search module... // Only public posts can be shown // OR your own posts if you are a logged in member // No items will be shown if the member has a blocked profile wall. - $pager = new Pager(DI::args()->getQueryString()); - - if ($tag) { - Logger::info('Start tag search.', ['q' => $search]); + if (DI::mode()->isMobile()) { + $itemsPerPage = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'itemspage_mobile_network', + DI::config()->get('system', 'itemspage_network_mobile')); + } else { + $itemsPerPage = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'itemspage_network', + DI::config()->get('system', 'itemspage_network')); + } - $condition = [ - "(`uid` = 0 OR (`uid` = ? AND NOT `global`)) - AND `otype` = ? AND `type` = ? AND `term` = ?", - local_user(), Term::OBJECT_TYPE_POST, Term::HASHTAG, $search - ]; - $params = [ - 'order' => ['received' => true], - 'limit' => [$pager->getStart(), $pager->getItemsPerPage()] - ]; - $terms = DBA::select('term', ['oid'], $condition, $params); - - $itemids = []; - while ($term = DBA::fetch($terms)) { - $itemids[] = $term['oid']; - } + $last_uriid = isset($_GET['last_uriid']) ? intval($_GET['last_uriid']) : 0; - DBA::close($terms); + $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage); - if (!empty($itemids)) { - $params = ['order' => ['id' => true]]; - $items = Item::selectForUser(local_user(), [], ['id' => $itemids], $params); - $r = Item::inArray($items); - } else { - $r = []; - } + if ($tag) { + Logger::info('Start tag search.', ['q' => $search]); + $uriids = Tag::getURIIdListByTag($search, DI::userSession()->getLocalUserId(), $pager->getStart(), $pager->getItemsPerPage(), $last_uriid); + $count = Tag::countByTag($search, DI::userSession()->getLocalUserId()); } else { Logger::info('Start fulltext search.', ['q' => $search]); + $uriids = Post\Content::getURIIdListBySearch($search, DI::userSession()->getLocalUserId(), $pager->getStart(), $pager->getItemsPerPage(), $last_uriid); + $count = Post\Content::countBySearch($search, DI::userSession()->getLocalUserId()); + } - $condition = [ - "(`uid` = 0 OR (`uid` = ? AND NOT `global`)) - AND `body` LIKE CONCAT('%',?,'%')", - local_user(), $search - ]; - $params = [ - 'order' => ['id' => true], - 'limit' => [$pager->getStart(), $pager->getItemsPerPage()] - ]; - $items = Item::selectForUser(local_user(), [], $condition, $params); - $r = Item::inArray($items); + if (!empty($uriids)) { + $condition = ["(`uid` = ? OR (`uid` = ? AND NOT `global`))", 0, DI::userSession()->getLocalUserId()]; + $condition = DBA::mergeConditions($condition, ['uri-id' => $uriids]); + $params = ['order' => ['id' => true]]; + $items = Post::toArray(Post::selectForUser(DI::userSession()->getLocalUserId(), Item::DISPLAY_FIELDLIST, $condition, $params)); } - if (!DBA::isResult($r)) { - info(L10n::t('No results.')); + if (empty($items)) { + if (empty($last_uriid)) { + DI::sysmsg()->addNotice(DI::l10n()->t('No results.')); + } return $o; } + if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) { + $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl'); + $o .= Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]); + } + if ($tag) { - $title = L10n::t('Items tagged with: %s', $search); + $title = DI::l10n()->t('Items tagged with: %s', $search); } else { - $title = L10n::t('Results for: %s', $search); + $title = DI::l10n()->t('Results for: %s', $search); } $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [ @@ -183,9 +212,14 @@ class Index extends BaseSearchModule Logger::info('Start Conversation.', ['q' => $search]); - $o .= conversation(DI::app(), $r, $pager, 'search', false, false, 'commented', local_user()); + $o .= DI::conversation()->create($items, 'search', false, false, 'commented', DI::userSession()->getLocalUserId()); + + if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) { + $o .= HTML::scrollLoader(); + } else { + $o .= $pager->renderMinimal($count); + } - $o .= $pager->renderMinimal(count($r)); return $o; } @@ -207,7 +241,8 @@ class Index extends BaseSearchModule */ private static function tryRedirectToProfile(string $search) { - $isUrl = !empty(parse_url($search, PHP_URL_SCHEME)); + $search = Network::convertToIdn($search); + $isUrl = !empty(parse_url($search, PHP_URL_SCHEME)); $isAddr = (bool)preg_match('/^@?([a-z0-9.-_]+@[a-z0-9.-_:]+)$/i', trim($search), $matches); if (!$isUrl && !$isAddr) { @@ -218,9 +253,9 @@ class Index extends BaseSearchModule $search = $matches[1]; } - if (local_user()) { + if (DI::userSession()->getLocalUserId()) { // User-specific contact URL/address search - $contact_id = Contact::getIdForURL($search, local_user()); + $contact_id = Contact::getIdForURL($search, DI::userSession()->getLocalUserId()); if (!$contact_id) { // User-specific contact URL/address search and probe $contact_id = Contact::getIdForURL($search); @@ -228,13 +263,13 @@ class Index extends BaseSearchModule } else { // Cheaper local lookup for anonymous users, no probe if ($isAddr) { - $contact = Contact::selectFirst(['id' => 'cid'], ['addr' => $search, 'uid' => 0]); + $contact = Contact::selectFirst(['id'], ['addr' => $search, 'uid' => 0]); } else { - $contact = Contact::getDetailsByURL($search, 0, ['cid' => 0]); + $contact = Contact::getByURL($search, null, ['id']) ?: ['id' => 0]; } if (DBA::isResult($contact)) { - $contact_id = $contact['cid']; + $contact_id = $contact['id']; } } @@ -255,9 +290,11 @@ class Index extends BaseSearchModule return; } - if (local_user()) { + $search = Network::convertToIdn($search); + + if (DI::userSession()->getLocalUserId()) { // Post URL search - $item_id = Item::fetchByLink($search, local_user()); + $item_id = Item::fetchByLink($search, DI::userSession()->getLocalUserId()); if (!$item_id) { // If the user-specific search failed, we search and probe a public post $item_id = Item::fetchByLink($search); @@ -268,7 +305,7 @@ class Index extends BaseSearchModule } if (!empty($item_id)) { - $item = Item::selectFirst(['guid'], ['id' => $item_id]); + $item = Post::selectFirst(['guid'], ['id' => $item_id]); if (DBA::isResult($item)) { DI::baseUrl()->redirect('display/' . $item['guid']); }