Split Directory Search as a base for further changes

This commit is contained in:
Philipp Holzer 2019-05-19 05:01:46 +02:00
parent 2520286b1f
commit 5b44fdc202
No known key found for this signature in database
GPG Key ID: D8365C3D36B77D90
4 changed files with 106 additions and 69 deletions

View File

@ -14,7 +14,7 @@ use Friendica\Core\Logger;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Module\DirectorySearch; use Friendica\Module\Directory;
use Friendica\Util\Strings; use Friendica\Util\Strings;
function search_saved_searches() { function search_saved_searches() {
@ -148,10 +148,10 @@ function search_content(App $a) {
$search = substr($search,1); $search = substr($search,1);
} }
if (strpos($search,'@') === 0) { if (strpos($search,'@') === 0) {
return DirectorySearch::performSearch(); return Directory::performSearch();
} }
if (strpos($search,'!') === 0) { if (strpos($search,'!') === 0) {
return DirectorySearch::performSearch(); return Directory::performSearch();
} }
if (!empty($_GET['search-option'])) if (!empty($_GET['search-option']))
@ -162,9 +162,9 @@ function search_content(App $a) {
$tag = true; $tag = true;
break; break;
case 'contacts': case 'contacts':
return DirectorySearch::performSearch('@'); return Directory::performSearch('@');
case 'forums': case 'forums':
return DirectorySearch::performSearch('!'); return Directory::performSearch('!');
} }
if (!$search) if (!$search)

View File

@ -113,7 +113,7 @@ class Router
$collector->addRoute(['GET'], '/ignored', Module\Contact::class); $collector->addRoute(['GET'], '/ignored', Module\Contact::class);
}); });
$this->routeCollector->addRoute(['GET'], '/credits', Module\Credits::class); $this->routeCollector->addRoute(['GET'], '/credits', Module\Credits::class);
$this->routeCollector->addRoute(['GET'], '/dirfind', Module\DirectorySearch::class); $this->routeCollector->addRoute(['GET'], '/dirfind', Module\Search\Directory::class);
$this->routeCollector->addRoute(['GET'], '/directory', Module\Directory::class); $this->routeCollector->addRoute(['GET'], '/directory', Module\Directory::class);
$this->routeCollector->addGroup('/feed', function (RouteCollector $collector) { $this->routeCollector->addGroup('/feed', function (RouteCollector $collector) {
$collector->addRoute(['GET'], '/{nickname}', Module\Feed::class); $collector->addRoute(['GET'], '/{nickname}', Module\Feed::class);

View File

@ -5,83 +5,38 @@ namespace Friendica\Module;
use Friendica\BaseModule; use Friendica\BaseModule;
use Friendica\Content\ContactSelector; use Friendica\Content\ContactSelector;
use Friendica\Content\Pager; use Friendica\Content\Pager;
use Friendica\Content\Widget;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Object\Search\ResultList;
use Friendica\Util\Proxy as ProxyUtils; use Friendica\Util\Proxy as ProxyUtils;
use Friendica\Util\Strings;
use Friendica\Model; use Friendica\Model;
/** /**
* Multi search module, which is needed for further search operations * Base class for search modules
*/ */
class DirectorySearch extends BaseModule abstract class BaseSearchModule extends BaseModule
{ {
public static function content() /**
* Prints a human readable search result
*
* @param ResultList $results
* @param Pager $pager
* @param string $header
*
* @return string The result
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
protected static function printResult(ResultList $results, Pager $pager, $header = '')
{ {
if (!local_user()) {
notice(L10n::t('Permission denied.'));
return Login::form();
}
$a = self::getApp();
if (empty($a->page['aside'])) {
$a->page['aside'] = '';
}
$a->page['aside'] .= Widget::findPeople();
$a->page['aside'] .= Widget::follow();
return self::performSearch();
}
public static function performSearch($prefix = '')
{
$a = self::getApp();
$config = $a->getConfig();
$community = false;
$localSearch = $config->get('system', 'poco_local_search');
$search = $prefix . Strings::escapeTags(trim(defaults($_REQUEST, 'search', '')));
if (!$search) {
return '';
}
$header = '';
if (strpos($search, '@') === 0) {
$search = substr($search, 1);
$header = L10n::t('People Search - %s', $search);
$results = Model\Search::searchUser($search);
}
if (strpos($search, '!') === 0) {
$search = substr($search, 1);
$community = true;
$header = L10n::t('Forum Search - %s', $search);
}
$pager = new Pager($a->query_string);
if ($localSearch && empty($results)) {
$pager->setItemsPerPage(80);
$results = Model\Search::searchLocal($search, $pager->getStart(), $pager->getItemsPerPage(), $community);
} elseif (strlen($config->get('system', 'directory')) && empty($results)) {
$results = Model\Search::searchDirectory($search, $pager->getPage());
$pager->setItemsPerPage($results->getItemsPage());
}
if (empty($results) || empty($results->getResults())) { if (empty($results) || empty($results->getResults())) {
info(L10n::t('No matches') . EOL); info(L10n::t('No matches') . EOL);
return ''; return '';
} }
$id = 0;
$a = self::getApp();
$id = 0;
$entries = []; $entries = [];
foreach ($results->getResults() as $result) { foreach ($results->getResults() as $result) {

View File

@ -0,0 +1,82 @@
<?php
namespace Friendica\Module\Search;
use Friendica\BaseModule;
use Friendica\Content\ContactSelector;
use Friendica\Content\Pager;
use Friendica\Content\Widget;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Module\BaseSearchModule;
use Friendica\Util\Proxy as ProxyUtils;
use Friendica\Util\Strings;
use Friendica\Model;
/**
* Multi search module, which is needed for further search operations
*/
class Directory extends BaseSearchModule
{
public static function content()
{
if (!local_user()) {
notice(L10n::t('Permission denied.'));
return Login::form();
}
$a = self::getApp();
if (empty($a->page['aside'])) {
$a->page['aside'] = '';
}
$a->page['aside'] .= Widget::findPeople();
$a->page['aside'] .= Widget::follow();
return self::performSearch();
}
public static function performSearch($prefix = '')
{
$a = self::getApp();
$config = $a->getConfig();
$community = false;
$localSearch = $config->get('system', 'poco_local_search');
$search = $prefix . Strings::escapeTags(trim(defaults($_REQUEST, 'search', '')));
if (!$search) {
return '';
}
$header = '';
if (strpos($search, '@') === 0) {
$search = substr($search, 1);
$header = L10n::t('People Search - %s', $search);
$results = Model\Search::searchUser($search);
}
if (strpos($search, '!') === 0) {
$search = substr($search, 1);
$community = true;
$header = L10n::t('Forum Search - %s', $search);
}
$pager = new Pager($a->query_string);
if ($localSearch && empty($results)) {
$pager->setItemsPerPage(80);
$results = Model\Search::searchLocal($search, $pager->getStart(), $pager->getItemsPerPage(), $community);
} elseif (strlen($config->get('system', 'directory')) && empty($results)) {
$results = Model\Search::searchDirectory($search, $pager->getPage());
$pager->setItemsPerPage($results->getItemsPage());
}
return self::printResult($results, $pager, $header);
}
}