]> git.mxchange.org Git - friendica.git/blobdiff - src/Worker/OnePoll.php
Merge pull request #5294 from annando/use-gravity
[friendica.git] / src / Worker / OnePoll.php
index 6e69bc3aa8570e1519bf63a50fbfb53f636b5066..eaef5f252d4404a01217a7618abe3f64e924a498 100644 (file)
@@ -4,17 +4,17 @@
  */
 namespace Friendica\Worker;
 
+use Friendica\Content\Text\BBCode;
 use Friendica\Core\Config;
 use Friendica\Core\PConfig;
 use Friendica\Database\DBM;
 use Friendica\Model\Contact;
 use Friendica\Model\Item;
-use Friendica\Content\Text\BBCode;
 use Friendica\Protocol\Email;
 use Friendica\Protocol\PortableContact;
+use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
 use Friendica\Util\XML;
-use Friendica\Util\Temporal;
 use dba;
 
 require_once 'include/dba.php';
@@ -24,7 +24,6 @@ class OnePoll
        public static function execute($contact_id = 0, $command = '') {
                global $a;
 
-               require_once 'include/datetime.php';
                require_once 'include/items.php';
 
                logger('start');
@@ -44,7 +43,7 @@ class OnePoll
                        return;
                }
 
-               $d = Temporal::utcNow();
+               $d = DateTimeFormat::utcNow();
 
                $contact = dba::selectFirst('contact', [], ['id' => $contact_id]);
                if (!DBM::is_result($contact)) {
@@ -70,7 +69,7 @@ class OnePoll
                // Diaspora users, archived users and followers are only checked if they still exist.
                if ($contact['archive'] || ($contact["network"] == NETWORK_DIASPORA) || ($contact["rel"] == CONTACT_IS_FOLLOWER)) {
                        $last_updated = PortableContact::lastUpdated($contact["url"], true);
-                       $updated = Temporal::utcNow();
+                       $updated = DateTimeFormat::utcNow();
                        if ($last_updated) {
                                logger('Contact '.$contact['id'].' had last update on '.$last_updated, LOGGER_DEBUG);
 
@@ -79,7 +78,7 @@ class OnePoll
                                        $last_updated = $contact['last-item'];
                                }
 
-                               $fields = ['last-item' => $last_updated, 'last-update' => $updated, 'success_update' => $updated];
+                               $fields = ['last-item' => DateTimeFormat::utc($last_updated), 'last-update' => $updated, 'success_update' => $updated];
                                self::updateContact($contact, $fields);
                                Contact::unmarkForArchival($contact);
                        } else {
@@ -99,7 +98,7 @@ class OnePoll
                        $contact['priority'] = intval($poll_interval);
                        $hub_update = false;
 
-                       if (Temporal::utcNow() > Temporal::convert($t . " + 1 day")) {
+                       if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 day")) {
                                $hub_update = true;
                        }
                } else {
@@ -107,8 +106,8 @@ class OnePoll
                }
 
                $last_update = (($contact['last-update'] <= NULL_DATE)
-                       ? Temporal::convert('now - 7 days', 'UTC', 'UTC', Temporal::ATOM)
-                       : Temporal::convert($contact['last-update'], 'UTC', 'UTC', Temporal::ATOM)
+                       ? DateTimeFormat::utc('now - 7 days', DateTimeFormat::ATOM)
+                       : DateTimeFormat::utc($contact['last-update'], DateTimeFormat::ATOM)
                );
 
                // Update the contact entry
@@ -117,7 +116,7 @@ class OnePoll
                                logger("Skipping probably dead contact ".$contact['url']);
 
                                // set the last-update so we don't keep polling
-                               dba::update('contact', ['last-update' => Temporal::utcNow()], ['id' => $contact['id']]);
+                               dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
                                return;
                        }
 
@@ -126,7 +125,7 @@ class OnePoll
                                logger('Contact is marked dead');
 
                                // set the last-update so we don't keep polling
-                               dba::update('contact', ['last-update' => Temporal::utcNow()], ['id' => $contact['id']]);
+                               dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
                                return;
                        } else {
                                Contact::unmarkForArchival($contact);
@@ -137,7 +136,7 @@ class OnePoll
                        logger('Ignore public contacts');
 
                        // set the last-update so we don't keep polling
-                       dba::update('contact', ['last-update' => Temporal::utcNow()], ['id' => $contact['id']]);
+                       dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
                        return;
                }
 
@@ -149,11 +148,12 @@ class OnePoll
                        logger('No self contact for user '.$importer_uid);
 
                        // set the last-update so we don't keep polling
-                       dba::update('contact', ['last-update' => Temporal::utcNow()], ['id' => $contact['id']]);
+                       dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
                        return;
                }
 
                $importer = $r[0];
+               $url = '';
 
                logger("poll: ({$contact['network']}-{$contact['id']}) IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
 
@@ -185,7 +185,7 @@ class OnePoll
 
                        if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
                                // set the last-update so we don't keep polling
-                               dba::update('contact', ['last-update' => Temporal::utcNow()], ['id' => $contact['id']]);
+                               dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
                                Contact::markForArchival($contact);
                                return;
                        }
@@ -207,7 +207,7 @@ class OnePoll
                                Contact::markForArchival($contact);
 
                                // set the last-update so we don't keep polling
-                               $fields = ['last-update' => Temporal::utcNow(), 'failure_update' => Temporal::utcNow()];
+                               $fields = ['last-update' => DateTimeFormat::utcNow(), 'failure_update' => DateTimeFormat::utcNow()];
                                self::updateContact($contact, $fields);
                                return;
                        }
@@ -217,7 +217,7 @@ class OnePoll
 
                                Contact::markForArchival($contact);
 
-                               $fields = ['last-update' => Temporal::utcNow(), 'failure_update' => Temporal::utcNow()];
+                               $fields = ['last-update' => DateTimeFormat::utcNow(), 'failure_update' => DateTimeFormat::utcNow()];
                                self::updateContact($contact, $fields);
                                return;
                        }
@@ -230,7 +230,7 @@ class OnePoll
 
                                // we may not be friends anymore. Will keep trying for one month.
                                // set the last-update so we don't keep polling
-                               $fields = ['last-update' => Temporal::utcNow(), 'failure_update' => Temporal::utcNow()];
+                               $fields = ['last-update' => DateTimeFormat::utcNow(), 'failure_update' => DateTimeFormat::utcNow()];
                                self::updateContact($contact, $fields);
 
                                Contact::markForArchival($contact);
@@ -241,7 +241,7 @@ class OnePoll
 
                        if ((intval($res->status) != 0) || !strlen($res->challenge) || !strlen($res->dfrn_id)) {
                                // set the last-update so we don't keep polling
-                               dba::update('contact', ['last-update' => Temporal::utcNow()], ['id' => $contact['id']]);
+                               dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
                                return;
                        }
 
@@ -276,7 +276,7 @@ class OnePoll
                                logger('ID did not decode: ' . $contact['id'] . ' orig: ' . $orig_id . ' final: ' . $final_dfrn_id);
 
                                // set the last-update so we don't keep polling
-                               dba::update('contact', ['last-update' => Temporal::utcNow()], ['id' => $contact['id']]);
+                               dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
                                Contact::markForArchival($contact);
                                return;
                        }
@@ -309,9 +309,9 @@ class OnePoll
 
                        // Are we allowed to import from this person?
 
-                       if ($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['blocked'] || $contact['readonly']) {
+                       if ($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['blocked']) {
                                // set the last-update so we don't keep polling
-                               dba::update('contact', ['last-update' => Temporal::utcNow()], ['id' => $contact['id']]);
+                               dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
                                return;
                        }
 
@@ -321,7 +321,7 @@ class OnePoll
 
                        if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) {
                                // set the last-update so we don't keep polling
-                               dba::update('contact', ['last-update' => Temporal::utcNow()], ['id' => $contact['id']]);
+                               dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
                                Contact::markForArchival($contact);
                                return;
                        }
@@ -335,7 +335,7 @@ class OnePoll
                        $mail_disabled = ((function_exists('imap_open') && (! Config::get('system', 'imap_disabled'))) ? 0 : 1);
                        if ($mail_disabled) {
                                // set the last-update so we don't keep polling
-                               dba::update('contact', ['last-update' => Temporal::utcNow()], ['id' => $contact['id']]);
+                               dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
                                Contact::markForArchival($contact);
                                return;
                        }
@@ -355,7 +355,7 @@ class OnePoll
                                unset($password);
                                logger("Mail: Connect to " . $mailconf['user']);
                                if ($mbox) {
-                                       $fields = ['last_check' => Temporal::utcNow()];
+                                       $fields = ['last_check' => DateTimeFormat::utcNow()];
                                        dba::update('mailacct', $fields, ['id' => $mailconf['id']]);
                                        logger("Mail: Connected to " . $mailconf['user']);
                                } else {
@@ -381,6 +381,7 @@ class OnePoll
                                                        $datarray = [];
                                                        $datarray['verb'] = ACTIVITY_POST;
                                                        $datarray['object-type'] = ACTIVITY_OBJ_NOTE;
+                                                       $datarray['network'] = NETWORK_MAIL;
                                                        // $meta = Email::messageMeta($mbox, $msg_uid);
 
                                                        $datarray['uri'] = Email::msgid2iri(trim($meta->message_id, '<>'));
@@ -388,15 +389,15 @@ class OnePoll
                                                        // Have we seen it before?
                                                        $fields = ['deleted', 'id'];
                                                        $condition = ['uid' => $importer_uid, 'uri' => $datarray['uri']];
-                                                       $item = dba::selectFirst('item', $fields, $condition);
+                                                       $item = Item::selectFirst($fields, $condition);
                                                        if (DBM::is_result($item)) {
                                                                logger("Mail: Seen before ".$msg_uid." for ".$mailconf['user']." UID: ".$importer_uid." URI: ".$datarray['uri'],LOGGER_DEBUG);
 
                                                                // Only delete when mails aren't automatically moved or deleted
                                                                if (($mailconf['action'] != 1) && ($mailconf['action'] != 3))
                                                                        if ($meta->deleted && ! $item['deleted']) {
-                                                                               $fields = ['deleted' => true, 'changed' => Temporal::utcNow()];
-                                                                               dba::update('item', $fields, ['id' => $item['id']]);
+                                                                               $fields = ['deleted' => true, 'changed' => DateTimeFormat::utcNow()];
+                                                                               Item::update($fields, ['id' => $item['id']]);
                                                                        }
 
                                                                switch ($mailconf['action']) {
@@ -434,15 +435,13 @@ class OnePoll
                                                                $refs_arr = explode(' ', $raw_refs);
                                                                if (count($refs_arr)) {
                                                                        for ($x = 0; $x < count($refs_arr); $x ++) {
-                                                                               $refs_arr[$x] = "'" . Email::msgid2iri(str_replace(['<', '>', ' '],['', '', ''],dbesc($refs_arr[$x]))) . "'";
+                                                                               $refs_arr[$x] = Email::msgid2iri(str_replace(['<', '>', ' '],['', '', ''], $refs_arr[$x]));
                                                                        }
                                                                }
-                                                               $qstr = implode(',', $refs_arr);
-                                                               $r = q("SELECT `parent-uri` FROM `item` USE INDEX (`uid_uri`) WHERE `uri` IN ($qstr) AND `uid` = %d LIMIT 1",
-                                                                       intval($importer_uid)
-                                                               );
-                                                               if (DBM::is_result($r)) {
-                                                                       $datarray['parent-uri'] = $r[0]['parent-uri'];  // Set the parent as the top-level item
+                                                               $condition = ['uri' => $refs_arr, 'uid' => $importer_uid];
+                                                               $parent = Item::selectFirst(['parent-uri'], $condition);
+                                                               if (DBM::is_result($parent)) {
+                                                                       $datarray['parent-uri'] = $parent['parent-uri'];  // Set the parent as the top-level item
                                                                }
                                                        }
 
@@ -459,7 +458,7 @@ class OnePoll
                                                        $datarray['title'] = notags(trim($datarray['title']));
 
                                                        //$datarray['title'] = notags(trim($meta->subject));
-                                                       $datarray['created'] = Temporal::convert($meta->date);
+                                                       $datarray['created'] = DateTimeFormat::utc($meta->date);
 
                                                        // Is it a reply?
                                                        $reply = ((substr(strtolower($datarray['title']), 0, 3) == "re:") ||
@@ -471,12 +470,11 @@ class OnePoll
 
                                                        // If it seems to be a reply but a header couldn't be found take the last message with matching subject
                                                        if (empty($datarray['parent-uri']) && $reply) {
-                                                               $r = q("SELECT `parent-uri` FROM `item` WHERE `title` = \"%s\" AND `uid` = %d AND `network` = '%s' ORDER BY `created` DESC LIMIT 1",
-                                                                       dbesc(protect_sprintf($datarray['title'])),
-                                                                       intval($importer_uid),
-                                                                       dbesc(NETWORK_MAIL));
-                                                               if (DBM::is_result($r)) {
-                                                                       $datarray['parent-uri'] = $r[0]['parent-uri'];
+                                                               $condition = ['title' => $datarray['title'], 'uid' => importer_uid, 'network' => NETWORK_MAIL];
+                                                               $params = ['order' => ['created' => true]];
+                                                               $parent = Item::selectFirst(['parent-uri'], $condition, $params);
+                                                               if (DBM::is_result($parent)) {
+                                                                       $datarray['parent-uri'] = $parent['parent-uri'];
                                                                }
                                                        }
 
@@ -572,7 +570,7 @@ class OnePoll
                        if (!strstr($xml, '<')) {
                                logger('post_handshake: response from ' . $url . ' did not contain XML.');
 
-                               $fields = ['last-update' => Temporal::utcNow(), 'failure_update' => Temporal::utcNow()];
+                               $fields = ['last-update' => DateTimeFormat::utcNow(), 'failure_update' => DateTimeFormat::utcNow()];
                                self::updateContact($contact, $fields);
                                Contact::markForArchival($contact);
                                return;
@@ -581,14 +579,15 @@ class OnePoll
 
                        logger("Consume feed of contact ".$contact['id']);
 
-                       consume_feed($xml, $importer, $contact, $hub, 1, 1);
+                       consume_feed($xml, $importer, $contact, $hub);
 
-                       // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
-
-                       consume_feed($xml, $importer, $contact, $hub, 1, 2);
+                       // do it a second time for DFRN so that any children find their parents.
+                       if ($contact['network'] === NETWORK_DFRN) {
+                               consume_feed($xml, $importer, $contact, $hub);
+                       }
 
                        $hubmode = 'subscribe';
-                       if ($contact['network'] === NETWORK_DFRN || $contact['blocked'] || $contact['readonly']) {
+                       if ($contact['network'] === NETWORK_DFRN || $contact['blocked']) {
                                $hubmode = 'unsubscribe';
                        }
 
@@ -616,19 +615,19 @@ class OnePoll
                                }
                        }
 
-                       $updated = Temporal::utcNow();
+                       $updated = DateTimeFormat::utcNow();
 
                        self::updateContact($contact, ['last-update' => $updated, 'success_update' => $updated]);
                        dba::update('gcontact', ['last_contact' => $updated], ['nurl' => $contact['nurl']]);
                        Contact::unmarkForArchival($contact);
                } elseif (in_array($contact["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, NETWORK_FEED])) {
-                       $updated = Temporal::utcNow();
+                       $updated = DateTimeFormat::utcNow();
 
                        self::updateContact($contact, ['last-update' => $updated, 'failure_update' => $updated]);
                        dba::update('gcontact', ['last_failure' => $updated], ['nurl' => $contact['nurl']]);
                        Contact::markForArchival($contact);
                } else {
-                       $updated = Temporal::utcNow();
+                       $updated = DateTimeFormat::utcNow();
                        dba::update('contact', ['last-update' => $updated], ['id' => $contact['id']]);
                }