3 * @file src/Worker/SearchDirectory.php
5 namespace Friendica\Worker;
7 use Friendica\Core\Cache;
8 use Friendica\Core\Config;
9 use Friendica\Core\Logger;
10 use Friendica\Core\Protocol;
11 use Friendica\Database\DBA;
12 use Friendica\Model\GContact;
13 use Friendica\Model\GServer;
14 use Friendica\Network\Probe;
15 use Friendica\Util\Network;
16 use Friendica\Util\Strings;
20 // <search pattern>: Searches for "search pattern" in the directory.
21 public static function execute($search)
23 if (!Config::get('system', 'poco_local_search')) {
24 Logger::info('Local search is not enabled');
28 $data = Cache::get('SearchDirectory:' . $search);
29 if (!is_null($data)) {
30 // Only search for the same item every 24 hours
31 if (time() < $data + (60 * 60 * 24)) {
32 Logger::info('Already searched this in the last 24 hours', ['search' => $search]);
37 $x = Network::fetchUrl(get_server() . '/lsearch?p=1&n=500&search=' . urlencode($search));
40 if (!empty($j->results)) {
41 foreach ($j->results as $jj) {
42 // Check if the contact already exists
43 $gcontact = DBA::selectFirst('gcontact', ['id', 'last_contact', 'last_failure', 'updated'], ['nurl' => Strings::normaliseLink($jj->url)]);
44 if (DBA::isResult($gcontact)) {
45 Logger::info('Profile already exists', ['profile' => $jj->url, 'search' => $search]);
47 if (($gcontact['last_contact'] < $gcontact['last_failure']) &&
48 ($gcontact['updated'] < $gcontact['last_failure'])) {
53 GContact::updateFromProbe($jj->url);
57 $server_url = GContact::getBasepath($jj->url, true);
58 if ($server_url != '') {
59 if (!GServer::check($server_url)) {
60 Logger::info("Friendica server doesn't answer.", ['server' => $server_url]);
63 Logger::info('Friendica server seems to be okay.', ['server' => $server_url]);
66 $data = Probe::uri($jj->url);
67 if ($data['network'] == Protocol::DFRN) {
68 Logger::info('Add profile to local directory', ['profile' => $jj->url]);
70 if ($jj->tags != '') {
71 $data['keywords'] = $jj->tags;
74 $data['server_url'] = $data['baseurl'];
76 GContact::update($data);
78 Logger::info('Profile is not responding or no Friendica contact', ['profile' => $jj->url, 'network' => $data['network']]);
82 Cache::set('SearchDirectory:' . $search, time(), Cache::DAY);