Moving Search to Core, Refactor Objects

This commit is contained in:
Philipp Holzer 2019-05-20 18:42:27 +02:00
parent 419cc81ef1
commit 1e36c68e48
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
6 changed files with 306 additions and 279 deletions

222
src/Core/Search.php Normal file
View file

@ -0,0 +1,222 @@
<?php
namespace Friendica\Core;
use Friendica\BaseObject;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Network\Probe;
use Friendica\Network\HTTPException;
use Friendica\Object\Search\ContactResult;
use Friendica\Object\Search\ResultList;
use Friendica\Protocol\PortableContact;
use Friendica\Util\Network;
use Friendica\Util\Strings;
class Search extends BaseObject
{
const DEFAULT_DIRECTORY = 'https://dir.friendica.social';
/**
* Search a user based on his/her profile address
* pattern: @username@domain.tld
*
* @param string $user The user to search for
*
* @return ResultList|null
* @throws HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public static function getContactsFromProbe($user)
{
if ((filter_var($user, FILTER_VALIDATE_EMAIL) && Network::isEmailDomainValid($user)) ||
(substr(Strings::normaliseLink($user), 0, 7) == "http://")) {
$user_data = Probe::uri($user);
if (empty($user_data)) {
return null;
}
if (!(in_array($user_data["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::DIASPORA]))) {
return null;
}
$contactDetails = Contact::getDetailsByURL(defaults($user_data, 'url', ''), local_user());
$itemUrl = (($contactDetails["addr"] != "") ? $contactDetails["addr"] : defaults($user_data, 'url', ''));
$result = new ContactResult(
defaults($user_data, 'name', ''),
defaults($user_data, 'addr', ''),
$itemUrl,
defaults($user_data, 'url', ''),
defaults($user_data, 'photo', ''),
defaults($user_data, 'network', ''),
defaults($contactDetails, 'cid', 0),
0,
defaults($user_data, 'tags', '')
);
return new ResultList(1, 1, 1, [$result]);
} else {
return null;
}
}
/**
* Search in the global directory for occurrences of the search string
* This is mainly based on the JSON results of https://dir.friendica.social
*
* @param string $search
* @param int $page
*
* @return ResultList|null
* @throws HTTPException\InternalServerErrorException
*/
public static function getContactsFromGlobalDirectory($search, $page = 1)
{
$config = self::getApp()->getConfig();
$server = $config->get('system', 'directory', self::DEFAULT_DIRECTORY);
$searchUrl = $server . '/search?q=' . urlencode($search);
if ($page > 1) {
$searchUrl .= '&page=' . $page;
}
$red = 0;
$resultJson = Network::fetchUrl($searchUrl, false,$red, 0, 'application/json');
$results = json_decode($resultJson, true);
$resultList = new ResultList(
defaults($results, 'page', 1),
defaults($results, 'count', 1),
defaults($results, 'itemsperpage', 1)
);
$profiles = defaults($results, 'profiles', []);
foreach ($profiles as $profile) {
$contactDetails = Contact::getDetailsByURL(defaults($profile, 'profile_url', ''), local_user());
$itemUrl = (!empty($contactDetails['addr']) ? $contactDetails['addr'] : defaults($profile, 'profile_url', ''));
$result = new ContactResult(
defaults($profile, 'name', ''),
defaults($profile, 'addr', ''),
$itemUrl,
defaults($profile, 'profile_url', ''),
defaults($profile, 'photo', ''),
Protocol::DFRN,
defaults($contactDetails, 'cid', 0),
0,
defaults($profile, 'tags', ''));
$resultList->addResult($result);
}
return $resultList;
}
/**
* Search in the local database for occurrences of the search string
*
* @param string $search
* @param int $start
* @param int $itemPage
* @param bool $community
*
* @return ResultList|null
* @throws HTTPException\InternalServerErrorException
*/
public static function getContactsFromLocalDirectory($search, $start = 0, $itemPage = 80, $community = false)
{
$config = self::getApp()->getConfig();
$diaspora = $config->get('system', 'diaspora_enabled') ? Protocol::DIASPORA : Protocol::DFRN;
$ostatus = !$config->get('system', 'ostatus_disabled') ? Protocol::OSTATUS : Protocol::DFRN;
$wildcard = Strings::escapeHtml('%' . $search . '%');
$count = DBA::count('gcontact', [
'NOT `hide`
AND `network` IN (?, ?, ?, ?)
AND ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`))
AND (`url` LIKE ? OR `name` LIKE ? OR `location` LIKE ?
OR `addr` LIKE ? OR `about` LIKE ? OR `keywords` LIKE ?)
AND `community` = ?',
Protocol::ACTIVITYPUB, Protocol::DFRN, $ostatus, $diaspora,
$wildcard, $wildcard, $wildcard,
$wildcard, $wildcard, $wildcard,
$community,
]);
if (empty($count)) {
return null;
}
$data = DBA::select('gcontact', ['nurl'], [
'NOT `hide`
AND `network` IN (?, ?, ?, ?)
AND ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`))
AND (`url` LIKE ? OR `name` LIKE ? OR `location` LIKE ?
OR `addr` LIKE ? OR `about` LIKE ? OR `keywords` LIKE ?)
AND `community` = ?',
Protocol::ACTIVITYPUB, Protocol::DFRN, $ostatus, $diaspora,
$wildcard, $wildcard, $wildcard,
$wildcard, $wildcard, $wildcard,
$community,
], [
'group_by' => ['nurl', 'updated'],
'limit' => [$start, $itemPage],
'order' => ['updated' => 'DESC']
]);
if (!DBA::isResult($data)) {
return null;
}
$resultList = new ResultList($start, $itemPage, $count);
while ($row = DBA::fetch($data)) {
if (PortableContact::alternateOStatusUrl($row["nurl"])) {
continue;
}
$urlParts = parse_url($row["nurl"]);
// Ignore results that look strange.
// For historic reasons the gcontact table does contain some garbage.
if (!empty($urlParts['query']) || !empty($urlParts['fragment'])) {
continue;
}
$contact = Contact::getDetailsByURL($row["nurl"], local_user());
if ($contact["name"] == "") {
$contact["name"] = end(explode("/", $urlParts["path"]));
}
$result = new ContactResult(
$contact["name"],
$contact["addr"],
$contact["addr"],
$contact["url"],
$contact["photo"],
$contact["network"],
$contact["cid"],
$contact["zid"],
$contact["keywords"]
);
$resultList->addResult($result);
}
DBA::close($data);
// Add found profiles from the global directory to the local directory
Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));
return $resultList;
}
}

View file

@ -3,23 +3,13 @@
namespace Friendica\Model; namespace Friendica\Model;
use Friendica\BaseObject; use Friendica\BaseObject;
use Friendica\Core\Protocol;
use Friendica\Core\Worker;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Network\Probe;
use Friendica\Object\Search\ContactResult;
use Friendica\Object\Search\ContactResultList;
use Friendica\Protocol\PortableContact;
use Friendica\Util\Network;
use Friendica\Util\Strings;
/** /**
* Model for searches * Model for DB specific logic for the search entity
*/ */
class Search extends BaseObject class Search extends BaseObject
{ {
const DEFAULT_DIRECTORY = 'https://dir.friendica.social';
/** /**
* Returns the list of user defined tags (e.g. #Friendica) * Returns the list of user defined tags (e.g. #Friendica)
* *
@ -39,207 +29,4 @@ class Search extends BaseObject
return $tags; return $tags;
} }
/**
* Search a user based on his/her profile address
* pattern: @username@domain.tld
*
* @param string $user The user to search for
*
* @return ContactResultList|null
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public static function searchUser($user)
{
if ((filter_var($user, FILTER_VALIDATE_EMAIL) && Network::isEmailDomainValid($user)) ||
(substr(Strings::normaliseLink($user), 0, 7) == "http://")) {
$user_data = Probe::uri($user);
if (empty($user_data)) {
return null;
}
if (!(in_array($user_data["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::DIASPORA]))) {
return null;
}
$contactDetails = Contact::getDetailsByURL(defaults($user_data, 'url', ''), local_user());
$itemurl = (($contactDetails["addr"] != "") ? $contactDetails["addr"] : defaults($user_data, 'url', ''));
$result = new ContactResult(
defaults($user_data, 'name', ''),
defaults($user_data, 'addr', ''),
$itemurl,
defaults($user_data, 'url', ''),
defaults($user_data, 'photo', ''),
defaults($user_data, 'network', ''),
defaults($contactDetails, 'cid', 0),
0,
defaults($user_data, 'tags', '')
);
return new ContactResultList(1, 1, 1, [$result]);
} else {
return null;
}
}
/**
* Search in the global directory for occurrences of the search string
* This is mainly based on the JSON results of https://dir.friendica.social
*
* @param string $search
* @param int $page
*
* @return ContactResultList|null
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function searchDirectory($search, $page = 1)
{
$config = self::getApp()->getConfig();
$server = $config->get('system', 'directory', self::DEFAULT_DIRECTORY);
$searchUrl = $server . '/search?q=' . urlencode($search);
if ($page > 1) {
$searchUrl .= '&page=' . $page;
}
$red = 0;
$resultJson = Network::fetchUrl($searchUrl, false,$red, 0, 'application/json');
$results = json_decode($resultJson, true);
$resultList = new ContactResultList(
defaults($results, 'page', 1),
defaults($results, 'count', 1),
defaults($results, 'itemsperpage', 1)
);
$profiles = defaults($results, 'profiles', []);
foreach ($profiles as $profile) {
$contactDetails = Contact::getDetailsByURL(defaults($profile, 'profile_url', ''), local_user());
$itemurl = (!empty($contactDetails['addr']) ? $contactDetails['addr'] : defaults($profile, 'profile_url', ''));
$result = new ContactResult(
defaults($profile, 'name', ''),
defaults($profile, 'addr', ''),
$itemurl,
defaults($profile, 'profile_url', ''),
defaults($profile, 'photo', ''),
Protocol::DFRN,
defaults($contactDetails, 'cid', 0),
0,
defaults($profile, 'tags', ''));
$resultList->addResult($result);
}
return $resultList;
}
/**
* Search in the local database for occurrences of the search string
*
* @param string $search
* @param int $start
* @param int $itemPage
* @param bool $community
*
* @return ContactResultList|null
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function searchLocal($search, $start = 0, $itemPage = 80, $community = false)
{
$config = self::getApp()->getConfig();
$diaspora = $config->get('system', 'diaspora_enabled') ? Protocol::DIASPORA : Protocol::DFRN;
$ostatus = !$config->get('system', 'ostatus_disabled') ? Protocol::OSTATUS : Protocol::DFRN;
$wildcard = Strings::escapeHtml('%' . $search . '%');
$count = DBA::count('gcontact', [
'NOT `hide`
AND `network` IN (?, ?, ?, ?)
AND ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`))
AND (`url` LIKE ? OR `name` LIKE ? OR `location` LIKE ?
OR `addr` LIKE ? OR `about` LIKE ? OR `keywords` LIKE ?)
AND `community` = ?',
Protocol::ACTIVITYPUB, Protocol::DFRN, $ostatus, $diaspora,
$wildcard, $wildcard, $wildcard,
$wildcard, $wildcard, $wildcard,
$community,
]);
if (empty($count)) {
return null;
}
$data = DBA::select('gcontact', ['nurl'], [
'NOT `hide`
AND `network` IN (?, ?, ?, ?)
AND ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`))
AND (`url` LIKE ? OR `name` LIKE ? OR `location` LIKE ?
OR `addr` LIKE ? OR `about` LIKE ? OR `keywords` LIKE ?)
AND `community` = ?',
Protocol::ACTIVITYPUB, Protocol::DFRN, $ostatus, $diaspora,
$wildcard, $wildcard, $wildcard,
$wildcard, $wildcard, $wildcard,
$community,
], [
'group_by' => ['nurl', 'updated'],
'limit' => [$start, $itemPage],
'order' => ['updated' => 'DESC']
]);
if (!DBA::isResult($data)) {
return null;
}
$resultList = new ContactResultList($start, $itemPage, $count);
while ($row = DBA::fetch($data)) {
if (PortableContact::alternateOStatusUrl($row["nurl"])) {
continue;
}
$urlparts = parse_url($row["nurl"]);
// Ignore results that look strange.
// For historic reasons the gcontact table does contain some garbage.
if (!empty($urlparts['query']) || !empty($urlparts['fragment'])) {
continue;
}
$contact = Contact::getDetailsByURL($row["nurl"], local_user());
if ($contact["name"] == "") {
$contact["name"] = end(explode("/", $urlparts["path"]));
}
$result = new ContactResult(
$contact["name"],
$contact["addr"],
$contact["addr"],
$contact["url"],
$contact["photo"],
$contact["network"],
$contact["cid"],
$contact["zid"],
$contact["keywords"]
);
$resultList->addResult($result);
}
DBA::close($data);
// Add found profiles from the global directory to the local directory
Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));
return $resultList;
}
} }

View file

@ -7,9 +7,12 @@ use Friendica\Content\ContactSelector;
use Friendica\Content\Pager; use Friendica\Content\Pager;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Object\Search\ContactResultList; use Friendica\Core\Search;
use Friendica\Object\Search\ContactResult;
use Friendica\Object\Search\ResultList;
use Friendica\Util\Proxy as ProxyUtils; use Friendica\Util\Proxy as ProxyUtils;
use Friendica\Model; use Friendica\Model;
use Friendica\Network\HTTPException;
use Friendica\Util\Strings; use Friendica\Util\Strings;
/** /**
@ -23,7 +26,7 @@ class BaseSearchModule extends BaseModule
* @param string $prefix A optional prefix (e.g. @ or !) for searching * @param string $prefix A optional prefix (e.g. @ or !) for searching
* *
* @return string * @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
*/ */
public static function performSearch($prefix = '') public static function performSearch($prefix = '')
@ -46,7 +49,7 @@ class BaseSearchModule extends BaseModule
if (strpos($search, '@') === 0) { if (strpos($search, '@') === 0) {
$search = substr($search, 1); $search = substr($search, 1);
$header = L10n::t('People Search - %s', $search); $header = L10n::t('People Search - %s', $search);
$results = Model\Search::searchUser($search); $results = Search::getContactsFromProbe($search);
} }
if (strpos($search, '!') === 0) { if (strpos($search, '!') === 0) {
@ -59,10 +62,10 @@ class BaseSearchModule extends BaseModule
if ($localSearch && empty($results)) { if ($localSearch && empty($results)) {
$pager->setItemsPerPage(80); $pager->setItemsPerPage(80);
$results = Model\Search::searchLocal($search, $pager->getStart(), $pager->getItemsPerPage(), $community); $results = Search::getContactsFromLocalDirectory($search, $pager->getStart(), $pager->getItemsPerPage(), $community);
} elseif (strlen($config->get('system', 'directory')) && empty($results)) { } elseif (strlen($config->get('system', 'directory')) && empty($results)) {
$results = Model\Search::searchDirectory($search, $pager->getPage()); $results = Search::getContactsFromGlobalDirectory($search, $pager->getPage());
$pager->setItemsPerPage($results->getItemsPage()); $pager->setItemsPerPage($results->getItemsPage());
} }
@ -72,15 +75,15 @@ class BaseSearchModule extends BaseModule
/** /**
* Prints a human readable search result * Prints a human readable search result
* *
* @param ContactResultList $results * @param ResultList $results
* @param Pager $pager * @param Pager $pager
* @param string $header * @param string $header
* *
* @return string The result * @return string The result
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
*/ */
protected static function printResult(ContactResultList $results, Pager $pager, $header = '') protected static function printResult(ResultList $results, Pager $pager, $header = '')
{ {
if (empty($results) || empty($results->getResults())) { if (empty($results) || empty($results->getResults())) {
info(L10n::t('No matches') . EOL); info(L10n::t('No matches') . EOL);
@ -93,6 +96,9 @@ class BaseSearchModule extends BaseModule
$entries = []; $entries = [];
foreach ($results->getResults() as $result) { foreach ($results->getResults() as $result) {
// in case the result is a contact result, add a contact-specific entry
if ($result instanceof ContactResult) {
$alt_text = ''; $alt_text = '';
$location = ''; $location = '';
$about = ''; $about = '';
@ -146,6 +152,7 @@ class BaseSearchModule extends BaseModule
]; ];
$entries[] = $entry; $entries[] = $entry;
} }
}
$tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl'); $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
return Renderer::replaceMacros($tpl, [ return Renderer::replaceMacros($tpl, [

View file

@ -9,7 +9,7 @@ use Friendica\Model\Search;
* *
* @see Search for details * @see Search for details
*/ */
class ContactResult class ContactResult implements IResult
{ {
/** /**
* @var int * @var int
@ -123,14 +123,15 @@ class ContactResult
/** /**
* @param string $name * @param string $name
* @param string $addr * @param string $addr
* @param string $item
* @param string $url * @param string $url
* @param string $photo * @param string $photo
* @param string $network * @param string $network
* @param int $cid * @param int $cid
* @param int $pcid * @param int $pCid
* @param string $tags * @param string $tags
*/ */
public function __construct($name, $addr, $item, $url, $photo, $network, $cid = 0, $pcid = 0, $tags = '') public function __construct($name, $addr, $item, $url, $photo, $network, $cid = 0, $pCid = 0, $tags = '')
{ {
$this->name = $name; $this->name = $name;
$this->addr = $addr; $this->addr = $addr;
@ -140,7 +141,7 @@ class ContactResult
$this->network = $network; $this->network = $network;
$this->cid = $cid; $this->cid = $cid;
$this->pCid = $pcid; $this->pCid = $pCid;
$this->tags = $tags; $this->tags = $tags;
} }
} }

View file

@ -0,0 +1,10 @@
<?php
namespace Friendica\Object\Search;
interface IResult
{
}

View file

@ -5,11 +5,11 @@ namespace Friendica\Object\Search;
use Friendica\Model\Search; use Friendica\Model\Search;
/** /**
* A list of search results for contacts with metadata * A list of search results with metadata
* *
* @see Search for details * @see Search for details
*/ */
class ContactResultList class ResultList
{ {
/** /**
* Page of the result list * Page of the result list
@ -29,7 +29,7 @@ class ContactResultList
/** /**
* Array of results * Array of results
* *
* @var ContactResult[] * @var IResult[]
*/ */
private $results; private $results;
@ -58,7 +58,7 @@ class ContactResultList
} }
/** /**
* @return ContactResult[] * @return IResult[]
*/ */
public function getResults() public function getResults()
{ {
@ -69,7 +69,7 @@ class ContactResultList
* @param int $page * @param int $page
* @param int $total * @param int $total
* @param int $itemsPage * @param int $itemsPage
* @param ContactResult[] $results * @param IResult[] $results
*/ */
public function __construct($page = 0, $total = 0, $itemsPage = 0, array $results = []) public function __construct($page = 0, $total = 0, $itemsPage = 0, array $results = [])
{ {
@ -83,9 +83,9 @@ class ContactResultList
/** /**
* Adds a result to the result list * Adds a result to the result list
* *
* @param ContactResult $result * @param IResult $result
*/ */
public function addResult(ContactResult $result) public function addResult(IResult $result)
{ {
$this->results[] = $result; $this->results[] = $result;
} }