From 1e36c68e48b0e88ceacac1a730bd7b7f6c14748c Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Mon, 20 May 2019 18:42:27 +0200 Subject: [PATCH] Moving Search to Core, Refactor Objects --- src/Core/Search.php | 222 ++++++++++++++++++ src/Model/Search.php | 215 +---------------- src/Module/BaseSearchModule.php | 115 ++++----- src/Object/Search/ContactResult.php | 9 +- src/Object/Search/IResult.php | 10 + .../{ContactResultList.php => ResultList.php} | 14 +- 6 files changed, 306 insertions(+), 279 deletions(-) create mode 100644 src/Core/Search.php create mode 100644 src/Object/Search/IResult.php rename src/Object/Search/{ContactResultList.php => ResultList.php} (81%) diff --git a/src/Core/Search.php b/src/Core/Search.php new file mode 100644 index 000000000..c08d4ebcc --- /dev/null +++ b/src/Core/Search.php @@ -0,0 +1,222 @@ +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; + } +} diff --git a/src/Model/Search.php b/src/Model/Search.php index 80d5e6698..5829ff91d 100644 --- a/src/Model/Search.php +++ b/src/Model/Search.php @@ -3,23 +3,13 @@ namespace Friendica\Model; use Friendica\BaseObject; -use Friendica\Core\Protocol; -use Friendica\Core\Worker; 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 { - const DEFAULT_DIRECTORY = 'https://dir.friendica.social'; - /** * Returns the list of user defined tags (e.g. #Friendica) * @@ -39,207 +29,4 @@ class Search extends BaseObject 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; - } } diff --git a/src/Module/BaseSearchModule.php b/src/Module/BaseSearchModule.php index 9430ec1b9..7e13a590e 100644 --- a/src/Module/BaseSearchModule.php +++ b/src/Module/BaseSearchModule.php @@ -7,9 +7,12 @@ use Friendica\Content\ContactSelector; use Friendica\Content\Pager; use Friendica\Core\L10n; 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\Model; +use Friendica\Network\HTTPException; use Friendica\Util\Strings; /** @@ -23,7 +26,7 @@ class BaseSearchModule extends BaseModule * @param string $prefix A optional prefix (e.g. @ or !) for searching * * @return string - * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ public static function performSearch($prefix = '') @@ -46,7 +49,7 @@ class BaseSearchModule extends BaseModule if (strpos($search, '@') === 0) { $search = substr($search, 1); $header = L10n::t('People Search - %s', $search); - $results = Model\Search::searchUser($search); + $results = Search::getContactsFromProbe($search); } if (strpos($search, '!') === 0) { @@ -59,10 +62,10 @@ class BaseSearchModule extends BaseModule if ($localSearch && empty($results)) { $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)) { - $results = Model\Search::searchDirectory($search, $pager->getPage()); + $results = Search::getContactsFromGlobalDirectory($search, $pager->getPage()); $pager->setItemsPerPage($results->getItemsPage()); } @@ -72,15 +75,15 @@ class BaseSearchModule extends BaseModule /** * Prints a human readable search result * - * @param ContactResultList $results + * @param ResultList $results * @param Pager $pager * @param string $header * * @return string The result - * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws HTTPException\InternalServerErrorException * @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())) { info(L10n::t('No matches') . EOL); @@ -93,58 +96,62 @@ class BaseSearchModule extends BaseModule $entries = []; foreach ($results->getResults() as $result) { - $alt_text = ''; - $location = ''; - $about = ''; - $accountType = ''; - $photo_menu = []; + // in case the result is a contact result, add a contact-specific entry + if ($result instanceof ContactResult) { - // If We already know this contact then don't show the "connect" button - if ($result->getCid() > 0 || $result->getPCid() > 0) { - $connLink = ""; - $connTxt = ""; - $contact = Model\Contact::getById( - ($result->getCid() > 0) ? $result->getCid() : $result->getPCid() - ); + $alt_text = ''; + $location = ''; + $about = ''; + $accountType = ''; + $photo_menu = []; - if (!empty($contact)) { - $photo_menu = Model\Contact::photoMenu($contact); - $details = Contact::getContactTemplateVars($contact); - $alt_text = $details['alt_text']; - $location = $contact['location']; - $about = $contact['about']; - $accountType = Model\Contact::getAccountType($contact); + // If We already know this contact then don't show the "connect" button + if ($result->getCid() > 0 || $result->getPCid() > 0) { + $connLink = ""; + $connTxt = ""; + $contact = Model\Contact::getById( + ($result->getCid() > 0) ? $result->getCid() : $result->getPCid() + ); + + if (!empty($contact)) { + $photo_menu = Model\Contact::photoMenu($contact); + $details = Contact::getContactTemplateVars($contact); + $alt_text = $details['alt_text']; + $location = $contact['location']; + $about = $contact['about']; + $accountType = Model\Contact::getAccountType($contact); + } else { + $photo_menu = []; + } } else { - $photo_menu = []; + $connLink = $a->getBaseURL() . '/follow/?url=' . $result->getUrl(); + $connTxt = L10n::t('Connect'); + + $photo_menu['profile'] = [L10n::t("View Profile"), Model\Contact::magicLink($result->getUrl())]; + $photo_menu['follow'] = [L10n::t("Connect/Follow"), $connLink]; } - } else { - $connLink = $a->getBaseURL() . '/follow/?url=' . $result->getUrl(); - $connTxt = L10n::t('Connect'); - $photo_menu['profile'] = [L10n::t("View Profile"), Model\Contact::magicLink($result->getUrl())]; - $photo_menu['follow'] = [L10n::t("Connect/Follow"), $connLink]; + $photo = str_replace("http:///photo/", get_server() . "/photo/", $result->getPhoto()); + + $entry = [ + 'alt_text' => $alt_text, + 'url' => Model\Contact::magicLink($result->getUrl()), + 'itemurl' => $result->getItem(), + 'name' => $result->getName(), + 'thumb' => ProxyUtils::proxifyUrl($photo, false, ProxyUtils::SIZE_THUMB), + 'img_hover' => $result->getTags(), + 'conntxt' => $connTxt, + 'connlnk' => $connLink, + 'photo_menu' => $photo_menu, + 'details' => $location, + 'tags' => $result->getTags(), + 'about' => $about, + 'account_type' => $accountType, + 'network' => ContactSelector::networkToName($result->getNetwork(), $result->getUrl()), + 'id' => ++$id, + ]; + $entries[] = $entry; } - - $photo = str_replace("http:///photo/", get_server() . "/photo/", $result->getPhoto()); - - $entry = [ - 'alt_text' => $alt_text, - 'url' => Model\Contact::magicLink($result->getUrl()), - 'itemurl' => $result->getItem(), - 'name' => $result->getName(), - 'thumb' => ProxyUtils::proxifyUrl($photo, false, ProxyUtils::SIZE_THUMB), - 'img_hover' => $result->getTags(), - 'conntxt' => $connTxt, - 'connlnk' => $connLink, - 'photo_menu' => $photo_menu, - 'details' => $location, - 'tags' => $result->getTags(), - 'about' => $about, - 'account_type' => $accountType, - 'network' => ContactSelector::networkToName($result->getNetwork(), $result->getUrl()), - 'id' => ++$id, - ]; - $entries[] = $entry; } $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl'); diff --git a/src/Object/Search/ContactResult.php b/src/Object/Search/ContactResult.php index 699afa862..ccfb5f4cf 100644 --- a/src/Object/Search/ContactResult.php +++ b/src/Object/Search/ContactResult.php @@ -9,7 +9,7 @@ use Friendica\Model\Search; * * @see Search for details */ -class ContactResult +class ContactResult implements IResult { /** * @var int @@ -123,14 +123,15 @@ class ContactResult /** * @param string $name * @param string $addr + * @param string $item * @param string $url * @param string $photo * @param string $network * @param int $cid - * @param int $pcid + * @param int $pCid * @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->addr = $addr; @@ -140,7 +141,7 @@ class ContactResult $this->network = $network; $this->cid = $cid; - $this->pCid = $pcid; + $this->pCid = $pCid; $this->tags = $tags; } } diff --git a/src/Object/Search/IResult.php b/src/Object/Search/IResult.php new file mode 100644 index 000000000..bf5dfc914 --- /dev/null +++ b/src/Object/Search/IResult.php @@ -0,0 +1,10 @@ +results[] = $result; }