]> git.mxchange.org Git - friendica.git/blob - src/Worker/SearchDirectory.php
Merge pull request #8093 from annando/postcount
[friendica.git] / src / Worker / SearchDirectory.php
1 <?php
2 /**
3  * @file src/Worker/SearchDirectory.php
4  */
5 namespace Friendica\Worker;
6
7 use Friendica\Core\Cache\Cache;
8 use Friendica\Core\Config;
9 use Friendica\Core\Logger;
10 use Friendica\Core\Protocol;
11 use Friendica\Core\Search;
12 use Friendica\Database\DBA;
13 use Friendica\DI;
14 use Friendica\Model\GContact;
15 use Friendica\Model\GServer;
16 use Friendica\Network\Probe;
17 use Friendica\Util\Network;
18 use Friendica\Util\Strings;
19
20 class SearchDirectory
21 {
22         // <search pattern>: Searches for "search pattern" in the directory.
23         public static function execute($search)
24         {
25                 if (!Config::get('system', 'poco_local_search')) {
26                         Logger::info('Local search is not enabled');
27                         return;
28                 }
29
30                 $data = DI::cache()->get('SearchDirectory:' . $search);
31                 if (!is_null($data)) {
32                         // Only search for the same item every 24 hours
33                         if (time() < $data + (60 * 60 * 24)) {
34                                 Logger::info('Already searched this in the last 24 hours', ['search' => $search]);
35                                 return;
36                         }
37                 }
38
39                 $x = Network::fetchUrl(Search::getGlobalDirectory() . '/lsearch?p=1&n=500&search=' . urlencode($search));
40                 $j = json_decode($x);
41
42                 if (!empty($j->results)) {
43                         foreach ($j->results as $jj) {
44                                 // Check if the contact already exists
45                                 $gcontact = DBA::selectFirst('gcontact', ['id', 'last_contact', 'last_failure', 'updated'], ['nurl' => Strings::normaliseLink($jj->url)]);
46                                 if (DBA::isResult($gcontact)) {
47                                         Logger::info('Profile already exists', ['profile' => $jj->url, 'search' => $search]);
48
49                                         if (($gcontact['last_contact'] < $gcontact['last_failure']) &&
50                                                 ($gcontact['updated'] < $gcontact['last_failure'])) {
51                                                 continue;
52                                         }
53
54                                         // Update the contact
55                                         GContact::updateFromProbe($jj->url);
56                                         continue;
57                                 }
58
59                                 $server_url = GContact::getBasepath($jj->url, true);
60                                 if ($server_url != '') {
61                                         if (!GServer::check($server_url)) {
62                                                 Logger::info("Friendica server doesn't answer.", ['server' => $server_url]);
63                                                 continue;
64                                         }
65                                         Logger::info('Friendica server seems to be okay.', ['server' => $server_url]);
66                                 }
67
68                                 $data = Probe::uri($jj->url);
69                                 if ($data['network'] == Protocol::DFRN) {
70                                         Logger::info('Add profile to local directory', ['profile' => $jj->url]);
71
72                                         if ($jj->tags != '') {
73                                                 $data['keywords'] = $jj->tags;
74                                         }
75
76                                         $data['server_url'] = $data['baseurl'];
77
78                                         GContact::update($data);
79                                 } else {
80                                         Logger::info('Profile is not responding or no Friendica contact', ['profile' => $jj->url, 'network' => $data['network']]);
81                                 }
82                         }
83                 }
84                 DI::cache()->set('SearchDirectory:' . $search, time(), Cache::DAY);
85         }
86 }