Merge pull request #7180 from MrPetovan/bug/fatal-errors
Ensures Search methods return an empty ResultList instead of null
This commit is contained in:
commit
c98da2fe43
|
@ -33,22 +33,24 @@ class Search extends BaseObject
|
||||||
*
|
*
|
||||||
* @param string $user The user to search for
|
* @param string $user The user to search for
|
||||||
*
|
*
|
||||||
* @return ResultList|null
|
* @return ResultList
|
||||||
* @throws HTTPException\InternalServerErrorException
|
* @throws HTTPException\InternalServerErrorException
|
||||||
* @throws \ImagickException
|
* @throws \ImagickException
|
||||||
*/
|
*/
|
||||||
public static function getContactsFromProbe($user)
|
public static function getContactsFromProbe($user)
|
||||||
{
|
{
|
||||||
|
$emptyResultList = new ResultList(1, 0, 1);
|
||||||
|
|
||||||
if ((filter_var($user, FILTER_VALIDATE_EMAIL) && Network::isEmailDomainValid($user)) ||
|
if ((filter_var($user, FILTER_VALIDATE_EMAIL) && Network::isEmailDomainValid($user)) ||
|
||||||
(substr(Strings::normaliseLink($user), 0, 7) == "http://")) {
|
(substr(Strings::normaliseLink($user), 0, 7) == "http://")) {
|
||||||
|
|
||||||
$user_data = Probe::uri($user);
|
$user_data = Probe::uri($user);
|
||||||
if (empty($user_data)) {
|
if (empty($user_data)) {
|
||||||
return null;
|
return $emptyResultList;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(in_array($user_data["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::DIASPORA]))) {
|
if (!(in_array($user_data["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::DIASPORA]))) {
|
||||||
return null;
|
return $emptyResultList;
|
||||||
}
|
}
|
||||||
|
|
||||||
$contactDetails = Contact::getDetailsByURL(defaults($user_data, 'url', ''), local_user());
|
$contactDetails = Contact::getDetailsByURL(defaults($user_data, 'url', ''), local_user());
|
||||||
|
@ -67,9 +69,8 @@ class Search extends BaseObject
|
||||||
);
|
);
|
||||||
|
|
||||||
return new ResultList(1, 1, 1, [$result]);
|
return new ResultList(1, 1, 1, [$result]);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return $emptyResultList;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,7 +83,7 @@ class Search extends BaseObject
|
||||||
* @param int $type specific type of searching
|
* @param int $type specific type of searching
|
||||||
* @param int $page
|
* @param int $page
|
||||||
*
|
*
|
||||||
* @return ResultList|null
|
* @return ResultList
|
||||||
* @throws HTTPException\InternalServerErrorException
|
* @throws HTTPException\InternalServerErrorException
|
||||||
*/
|
*/
|
||||||
public static function getContactsFromGlobalDirectory($search, $type = self::TYPE_ALL, $page = 1)
|
public static function getContactsFromGlobalDirectory($search, $type = self::TYPE_ALL, $page = 1)
|
||||||
|
@ -113,8 +114,8 @@ class Search extends BaseObject
|
||||||
|
|
||||||
$resultList = new ResultList(
|
$resultList = new ResultList(
|
||||||
defaults($results, 'page', 1),
|
defaults($results, 'page', 1),
|
||||||
defaults($results, 'count', 1),
|
defaults($results, 'count', 0),
|
||||||
defaults($results, 'itemsperpage', 1)
|
defaults($results, 'itemsperpage', 30)
|
||||||
);
|
);
|
||||||
|
|
||||||
$profiles = defaults($results, 'profiles', []);
|
$profiles = defaults($results, 'profiles', []);
|
||||||
|
@ -148,7 +149,7 @@ class Search extends BaseObject
|
||||||
* @param int $start
|
* @param int $start
|
||||||
* @param int $itemPage
|
* @param int $itemPage
|
||||||
*
|
*
|
||||||
* @return ResultList|null
|
* @return ResultList
|
||||||
* @throws HTTPException\InternalServerErrorException
|
* @throws HTTPException\InternalServerErrorException
|
||||||
*/
|
*/
|
||||||
public static function getContactsFromLocalDirectory($search, $type = self::TYPE_ALL, $start = 0, $itemPage = 80)
|
public static function getContactsFromLocalDirectory($search, $type = self::TYPE_ALL, $start = 0, $itemPage = 80)
|
||||||
|
@ -173,8 +174,10 @@ class Search extends BaseObject
|
||||||
($type === self::TYPE_FORUM),
|
($type === self::TYPE_FORUM),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$resultList = new ResultList($start, $itemPage, $count);
|
||||||
|
|
||||||
if (empty($count)) {
|
if (empty($count)) {
|
||||||
return null;
|
return $resultList;
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = DBA::select('gcontact', ['nurl'], [
|
$data = DBA::select('gcontact', ['nurl'], [
|
||||||
|
@ -195,11 +198,9 @@ class Search extends BaseObject
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (!DBA::isResult($data)) {
|
if (!DBA::isResult($data)) {
|
||||||
return null;
|
return $resultList;
|
||||||
}
|
}
|
||||||
|
|
||||||
$resultList = new ResultList($start, $itemPage, $count);
|
|
||||||
|
|
||||||
while ($row = DBA::fetch($data)) {
|
while ($row = DBA::fetch($data)) {
|
||||||
if (PortableContact::alternateOStatusUrl($row["nurl"])) {
|
if (PortableContact::alternateOStatusUrl($row["nurl"])) {
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -64,7 +64,6 @@ class BaseSearchModule extends BaseModule
|
||||||
if ($localSearch && empty($results)) {
|
if ($localSearch && empty($results)) {
|
||||||
$pager->setItemsPerPage(80);
|
$pager->setItemsPerPage(80);
|
||||||
$results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage());
|
$results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage());
|
||||||
|
|
||||||
} elseif (strlen($config->get('system', 'directory')) && empty($results)) {
|
} elseif (strlen($config->get('system', 'directory')) && empty($results)) {
|
||||||
$results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage());
|
$results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage());
|
||||||
$pager->setItemsPerPage($results->getItemsPage());
|
$pager->setItemsPerPage($results->getItemsPage());
|
||||||
|
@ -86,8 +85,8 @@ class BaseSearchModule extends BaseModule
|
||||||
*/
|
*/
|
||||||
protected static function printResult(ResultList $results, Pager $pager, $header = '')
|
protected static function printResult(ResultList $results, Pager $pager, $header = '')
|
||||||
{
|
{
|
||||||
if (empty($results) || empty($results->getResults())) {
|
if ($results->getTotal() == 0) {
|
||||||
info(L10n::t('No matches') . EOL);
|
info(L10n::t('No matches'));
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue