Add search types
This commit is contained in:
parent
83fd03b124
commit
58c8959da0
2 changed files with 29 additions and 13 deletions
|
@ -23,6 +23,10 @@ class Search extends BaseObject
|
||||||
{
|
{
|
||||||
const DEFAULT_DIRECTORY = 'https://dir.friendica.social';
|
const DEFAULT_DIRECTORY = 'https://dir.friendica.social';
|
||||||
|
|
||||||
|
const TYPE_PEOPLE = 0;
|
||||||
|
const TYPE_FORUM = 1;
|
||||||
|
const TYPE_ALL = 2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search a user based on his/her profile address
|
* Search a user based on his/her profile address
|
||||||
* pattern: @username@domain.tld
|
* pattern: @username@domain.tld
|
||||||
|
@ -71,20 +75,31 @@ class Search extends BaseObject
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search in the global directory for occurrences of the search string
|
* Search in the global directory for occurrences of the search string
|
||||||
* This is mainly based on the JSON results of https://dir.friendica.social
|
* @see https://github.com/friendica/friendica-directory/blob/master/docs/Protocol.md#search
|
||||||
*
|
*
|
||||||
* @param string $search
|
* @param string $search
|
||||||
|
* @param int $type specific type of searching
|
||||||
* @param int $page
|
* @param int $page
|
||||||
*
|
*
|
||||||
* @return ResultList|null
|
* @return ResultList|null
|
||||||
* @throws HTTPException\InternalServerErrorException
|
* @throws HTTPException\InternalServerErrorException
|
||||||
*/
|
*/
|
||||||
public static function getContactsFromGlobalDirectory($search, $page = 1)
|
public static function getContactsFromGlobalDirectory($search, $type = self::TYPE_ALL, $page = 1)
|
||||||
{
|
{
|
||||||
$config = self::getApp()->getConfig();
|
$config = self::getApp()->getConfig();
|
||||||
$server = $config->get('system', 'directory', self::DEFAULT_DIRECTORY);
|
$server = $config->get('system', 'directory', self::DEFAULT_DIRECTORY);
|
||||||
|
|
||||||
$searchUrl = $server . '/search?q=' . urlencode($search);
|
$searchUrl = $server . '/search';
|
||||||
|
|
||||||
|
switch ($type) {
|
||||||
|
case self::TYPE_FORUM:
|
||||||
|
$searchUrl .= '/forum';
|
||||||
|
break;
|
||||||
|
case self::TYPE_PEOPLE:
|
||||||
|
$searchUrl .= '/people';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$searchUrl .= '?q=' . urlencode($search);
|
||||||
|
|
||||||
if ($page > 1) {
|
if ($page > 1) {
|
||||||
$searchUrl .= '&page=' . $page;
|
$searchUrl .= '&page=' . $page;
|
||||||
|
@ -130,12 +145,12 @@ class Search extends BaseObject
|
||||||
* @param string $search
|
* @param string $search
|
||||||
* @param int $start
|
* @param int $start
|
||||||
* @param int $itemPage
|
* @param int $itemPage
|
||||||
* @param bool $community
|
* @param int $type
|
||||||
*
|
*
|
||||||
* @return ResultList|null
|
* @return ResultList|null
|
||||||
* @throws HTTPException\InternalServerErrorException
|
* @throws HTTPException\InternalServerErrorException
|
||||||
*/
|
*/
|
||||||
public static function getContactsFromLocalDirectory($search, $start = 0, $itemPage = 80, $community = false)
|
public static function getContactsFromLocalDirectory($search, $start = 0, $itemPage = 80, $type = self::TYPE_ALL)
|
||||||
{
|
{
|
||||||
$config = self::getApp()->getConfig();
|
$config = self::getApp()->getConfig();
|
||||||
|
|
||||||
|
@ -154,7 +169,7 @@ class Search extends BaseObject
|
||||||
Protocol::ACTIVITYPUB, Protocol::DFRN, $ostatus, $diaspora,
|
Protocol::ACTIVITYPUB, Protocol::DFRN, $ostatus, $diaspora,
|
||||||
$wildcard, $wildcard, $wildcard,
|
$wildcard, $wildcard, $wildcard,
|
||||||
$wildcard, $wildcard, $wildcard,
|
$wildcard, $wildcard, $wildcard,
|
||||||
$community,
|
($type === self::TYPE_FORUM),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (empty($count)) {
|
if (empty($count)) {
|
||||||
|
@ -171,7 +186,7 @@ class Search extends BaseObject
|
||||||
Protocol::ACTIVITYPUB, Protocol::DFRN, $ostatus, $diaspora,
|
Protocol::ACTIVITYPUB, Protocol::DFRN, $ostatus, $diaspora,
|
||||||
$wildcard, $wildcard, $wildcard,
|
$wildcard, $wildcard, $wildcard,
|
||||||
$wildcard, $wildcard, $wildcard,
|
$wildcard, $wildcard, $wildcard,
|
||||||
$community,
|
($type === self::TYPE_FORUM),
|
||||||
], [
|
], [
|
||||||
'group_by' => ['nurl', 'updated'],
|
'group_by' => ['nurl', 'updated'],
|
||||||
'limit' => [$start, $itemPage],
|
'limit' => [$start, $itemPage],
|
||||||
|
|
|
@ -8,11 +8,11 @@ use Friendica\Content\Pager;
|
||||||
use Friendica\Core\L10n;
|
use Friendica\Core\L10n;
|
||||||
use Friendica\Core\Renderer;
|
use Friendica\Core\Renderer;
|
||||||
use Friendica\Core\Search;
|
use Friendica\Core\Search;
|
||||||
|
use Friendica\Model;
|
||||||
|
use Friendica\Network\HTTPException;
|
||||||
use Friendica\Object\Search\ContactResult;
|
use Friendica\Object\Search\ContactResult;
|
||||||
use Friendica\Object\Search\ResultList;
|
use Friendica\Object\Search\ResultList;
|
||||||
use Friendica\Util\Proxy as ProxyUtils;
|
use Friendica\Util\Proxy as ProxyUtils;
|
||||||
use Friendica\Model;
|
|
||||||
use Friendica\Network\HTTPException;
|
|
||||||
use Friendica\Util\Strings;
|
use Friendica\Util\Strings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -34,7 +34,7 @@ class BaseSearchModule extends BaseModule
|
||||||
$a = self::getApp();
|
$a = self::getApp();
|
||||||
$config = $a->getConfig();
|
$config = $a->getConfig();
|
||||||
|
|
||||||
$community = false;
|
$type = Search::TYPE_ALL;
|
||||||
|
|
||||||
$localSearch = $config->get('system', 'poco_local_search');
|
$localSearch = $config->get('system', 'poco_local_search');
|
||||||
|
|
||||||
|
@ -48,13 +48,14 @@ class BaseSearchModule extends BaseModule
|
||||||
|
|
||||||
if (strpos($search, '@') === 0) {
|
if (strpos($search, '@') === 0) {
|
||||||
$search = substr($search, 1);
|
$search = substr($search, 1);
|
||||||
|
$type = Search::TYPE_PEOPLE;
|
||||||
$header = L10n::t('People Search - %s', $search);
|
$header = L10n::t('People Search - %s', $search);
|
||||||
$results = Search::getContactsFromProbe($search);
|
$results = Search::getContactsFromProbe($search);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strpos($search, '!') === 0) {
|
if (strpos($search, '!') === 0) {
|
||||||
$search = substr($search, 1);
|
$search = substr($search, 1);
|
||||||
$community = true;
|
$type = Search::TYPE_FORUM;
|
||||||
$header = L10n::t('Forum Search - %s', $search);
|
$header = L10n::t('Forum Search - %s', $search);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,10 +63,10 @@ class BaseSearchModule extends BaseModule
|
||||||
|
|
||||||
if ($localSearch && empty($results)) {
|
if ($localSearch && empty($results)) {
|
||||||
$pager->setItemsPerPage(80);
|
$pager->setItemsPerPage(80);
|
||||||
$results = Search::getContactsFromLocalDirectory($search, $pager->getStart(), $pager->getItemsPerPage(), $community);
|
$results = Search::getContactsFromLocalDirectory($search, $pager->getStart(), $pager->getItemsPerPage(), $type);
|
||||||
|
|
||||||
} elseif (strlen($config->get('system', 'directory')) && empty($results)) {
|
} elseif (strlen($config->get('system', 'directory')) && empty($results)) {
|
||||||
$results = Search::getContactsFromGlobalDirectory($search, $pager->getPage());
|
$results = Search::getContactsFromGlobalDirectory($search, $pager->getPage(), $type);
|
||||||
$pager->setItemsPerPage($results->getItemsPage());
|
$pager->setItemsPerPage($results->getItemsPage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue