]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/PortableContact.php
Contact updates should now work more reliable
[friendica.git] / src / Protocol / PortableContact.php
index 04497f16bac072a034f07ce01aba5c7132937874..5b419e42eff3d15deb989d8cc8cac66231f71eca 100644 (file)
@@ -15,15 +15,14 @@ use Friendica\Database\DBM;
 use Friendica\Model\GContact;
 use Friendica\Model\Profile;
 use Friendica\Network\Probe;
+use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
-use Friendica\Util\Temporal;
 use dba;
 use DOMDocument;
 use DOMXPath;
 use Exception;
 
 require_once 'include/dba.php';
-require_once 'include/datetime.php';
 require_once 'include/html2bbcode.php';
 
 class PortableContact
@@ -144,7 +143,7 @@ class PortableContact
                        }
 
                        if (isset($entry->updated)) {
-                               $updated = date(Temporal::MYSQL, strtotime($entry->updated));
+                               $updated = date(DateTimeFormat::MYSQL, strtotime($entry->updated));
                        }
 
                        if (isset($entry->network)) {
@@ -315,9 +314,10 @@ class PortableContact
                $contact = ["url" => $profile];
 
                if ($gcontacts[0]["created"] <= NULL_DATE) {
-                       $contact['created'] = Temporal::convert();
+                       $contact['created'] = DateTimeFormat::utcNow();
                }
 
+               $server_url = '';
                if ($force) {
                        $server_url = normalise_link(self::detectServer($profile));
                }
@@ -338,7 +338,7 @@ class PortableContact
                if ($server_url != "") {
                        if (!self::checkServer($server_url, $gcontacts[0]["network"], $force)) {
                                if ($force) {
-                                       $fields = ['last_failure' => Temporal::convert()];
+                                       $fields = ['last_failure' => DateTimeFormat::utcNow()];
                                        dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
                                }
 
@@ -412,14 +412,14 @@ class PortableContact
 
                                                // Set the date of the last contact
                                                /// @todo By now the function "update_gcontact" doesn't work with this field
-                                               //$contact["last_contact"] = Temporal::convert();
+                                               //$contact["last_contact"] = DateTimeFormat::utcNow();
 
                                                $contact = array_merge($contact, $noscrape);
 
                                                GContact::update($contact);
 
                                                if (trim($noscrape["updated"]) != "") {
-                                                       $fields = ['last_contact' => Temporal::convert()];
+                                                       $fields = ['last_contact' => DateTimeFormat::utcNow()];
                                                        dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
 
                                                        logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG);
@@ -468,7 +468,7 @@ class PortableContact
                }
 
                if (($data["poll"] == "") || (in_array($data["network"], [NETWORK_FEED, NETWORK_PHANTOM]))) {
-                       $fields = ['last_failure' => Temporal::convert()];
+                       $fields = ['last_failure' => DateTimeFormat::utcNow()];
                        dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
 
                        logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG);
@@ -484,7 +484,7 @@ class PortableContact
                $feedret = Network::curl($data["poll"]);
 
                if (!$feedret["success"]) {
-                       $fields = ['last_failure' => Temporal::convert()];
+                       $fields = ['last_failure' => DateTimeFormat::utcNow()];
                        dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
 
                        logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG);
@@ -502,14 +502,16 @@ class PortableContact
                $last_updated = "";
 
                foreach ($entries as $entry) {
-                       $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
-                       $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
+                       $published = DateTimeFormat::utc($xpath->query('atom:published/text()', $entry)->item(0)->nodeValue);
+                       $updated   = DateTimeFormat::utc($xpath->query('atom:updated/text()'  , $entry)->item(0)->nodeValue);
 
-                       if ($last_updated < $published)
+                       if ($last_updated < $published) {
                                $last_updated = $published;
+                       }
 
-                       if ($last_updated < $updated)
+                       if ($last_updated < $updated) {
                                $last_updated = $updated;
+                       }
                }
 
                // Maybe there aren't any entries. Then check if it is a valid feed
@@ -518,7 +520,8 @@ class PortableContact
                                $last_updated = NULL_DATE;
                        }
                }
-               $fields = ['updated' => DBM::date($last_updated), 'last_contact' => DBM::date()];
+
+               $fields = ['updated' => $last_updated, 'last_contact' => DateTimeFormat::utcNow()];
                dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
 
                if (($gcontacts[0]["generation"] == 0)) {
@@ -528,12 +531,12 @@ class PortableContact
 
                logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG);
 
-               return($last_updated);
+               return $last_updated;
        }
 
        public static function updateNeeded($created, $updated, $last_failure, $last_contact)
        {
-               $now = strtotime(Temporal::convert());
+               $now = strtotime(DateTimeFormat::utcNow());
 
                if ($updated > $last_contact) {
                        $contact_time = strtotime($updated);
@@ -922,7 +925,7 @@ class PortableContact
                $gserver = dba::selectFirst('gserver', [], ['nurl' => normalise_link($server_url)]);
                if (DBM::is_result($gserver)) {
                        if ($gserver["created"] <= NULL_DATE) {
-                               $fields = ['created' => Temporal::convert()];
+                               $fields = ['created' => DateTimeFormat::utcNow()];
                                $condition = ['nurl' => normalise_link($server_url)];
                                dba::update('gserver', $fields, $condition);
                        }
@@ -969,7 +972,7 @@ class PortableContact
                // Mastodon uses the "@" for user profiles.
                // But this can be misunderstood.
                if (parse_url($server_url, PHP_URL_USER) != '') {
-                       dba::update('gserver', ['last_failure' => Temporal::convert()], ['nurl' => normalise_link($server_url)]);
+                       dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
                        return false;
                }
 
@@ -985,7 +988,7 @@ class PortableContact
                if (DBM::is_result($gserver) && ($orig_server_url == $server_url) &&
                        ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT)) {
                        logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
-                       dba::update('gserver', ['last_failure' => Temporal::convert()], ['nurl' => normalise_link($server_url)]);
+                       dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
                        return false;
                }
 
@@ -1000,7 +1003,7 @@ class PortableContact
                        // Quit if there is a timeout
                        if ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT) {
                                logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
-                               dba::update('gserver', ['last_failure' => Temporal::convert()], ['nurl' => normalise_link($server_url)]);
+                               dba::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => normalise_link($server_url)]);
                                return false;
                        }
 
@@ -1332,9 +1335,9 @@ class PortableContact
 
                if ($failure) {
                        $last_contact = $orig_last_contact;
-                       $last_failure = Temporal::convert();
+                       $last_failure = DateTimeFormat::utcNow();
                } else {
-                       $last_contact = Temporal::convert();
+                       $last_contact = DateTimeFormat::utcNow();
                        $last_failure = $orig_last_failure;
                }
 
@@ -1362,7 +1365,7 @@ class PortableContact
                        dba::update('gserver', $fields, ['nurl' => normalise_link($server_url)]);
                } elseif (!$failure) {
                        $fields['nurl'] = normalise_link($server_url);
-                       $fields['created'] = Temporal::convert();
+                       $fields['created'] = DateTimeFormat::utcNow();
                        dba::insert('gserver', $fields);
                }
                logger("End discovery for server " . $server_url, LOGGER_DEBUG);
@@ -1507,7 +1510,7 @@ class PortableContact
                                        $timeframe = 30;
                                }
 
-                               $updatedSince = date(Temporal::MYSQL, time() - $timeframe * 86400);
+                               $updatedSince = date(DateTimeFormat::MYSQL, time() - $timeframe * 86400);
 
                                // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
                                $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
@@ -1526,7 +1529,7 @@ class PortableContact
                                }
                        }
 
-                       $fields = ['last_poco_query' => Temporal::convert()];
+                       $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
                        dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
 
                        return true;
@@ -1535,7 +1538,7 @@ class PortableContact
                        self::checkServer($server["url"], $server["network"], true);
 
                        // If we couldn't reach the server, we will try it some time later
-                       $fields = ['last_poco_query' => Temporal::convert()];
+                       $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
                        dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
 
                        return false;
@@ -1561,7 +1564,7 @@ class PortableContact
                        foreach ($r as $server) {
                                if (!self::checkServer($server["url"], $server["network"])) {
                                        // The server is not reachable? Okay, then we will try it later
-                                       $fields = ['last_poco_query' => Temporal::convert()];
+                                       $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
                                        dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
                                        continue;
                                }
@@ -1654,7 +1657,7 @@ class PortableContact
                        }
 
                        if (isset($entry->updated)) {
-                               $updated = date(Temporal::MYSQL, strtotime($entry->updated));
+                               $updated = date(DateTimeFormat::MYSQL, strtotime($entry->updated));
                        }
 
                        if (isset($entry->network)) {