]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/feedimporter.php
Misses this file to merge. I like the comments.
[quix0rs-gnu-social.git] / lib / feedimporter.php
index e2c9df72fd9f3e69515779e45e1870ce8b1d76df..b5807ee8b45d82384fe98e355a4f9cba864387dd 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (C) 2010, StatusNet, Inc.
  *
  * Importer for feeds of activities
- * 
+ *
  * PHP version 5
  *
  * This program is free software: you can redistribute it and/or modify
@@ -47,7 +47,6 @@ if (!defined('STATUSNET')) {
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  * @link      http://status.net/
  */
-
 class FeedImporter extends QueueHandler
 {
     /**
@@ -55,7 +54,6 @@ class FeedImporter extends QueueHandler
      *
      * @return string identifier for this queue handler
      */
-
     public function transport()
     {
         return 'feedimp';
@@ -68,28 +66,33 @@ class FeedImporter extends QueueHandler
         try {
             $doc = DOMDocument::loadXML($xml);
 
-            if ($doc->documentElement->namespaceURI != Activity::ATOM ||
-                $doc->documentElement->localName != 'feed') {
-                throw new ClientException(_("Not an atom feed."));
+            $feed = $doc->documentElement;
+
+            if ($feed->namespaceURI != Activity::ATOM ||
+                $feed->localName != 'feed') {
+                // TRANS: Client exception thrown when an imported feed is not an Atom feed.
+                throw new ClientException(_("Not an Atom feed."));
             }
 
-            $feed = $doc->documentElement;
 
             $author = ActivityUtils::getFeedAuthor($feed);
 
             if (empty($author)) {
+                // TRANS: Client exception thrown when an imported feed does not have an author.
                 throw new ClientException(_("No author in the feed."));
             }
 
             if (empty($user)) {
                 if ($trusted) {
                     $user = $this->userFromAuthor($author);
+                } else {
+                    // TRANS: Client exception thrown when an imported feed does not have an author that
+                    // TRANS: can be associated with a user.
+                    throw new ClientException(_("Cannot import without a user."));
                 }
             }
 
-            $entries = $feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
-
-            $activities = $this->entriesToActivities($entries, $feed);
+            $activities = $this->getActivities($feed);
 
             $qm = QueueManager::get();
 
@@ -108,9 +111,38 @@ class FeedImporter extends QueueHandler
         }
     }
 
+    function getActivities($feed)
+    {
+        $entries = $feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
+
+        $activities = array();
+
+        for ($i = 0; $i < $entries->length; $i++) {
+            $activities[] = new Activity($entries->item($i));
+        }
+
+        usort($activities, array("FeedImporter", "activitySort"));
+
+        return $activities;
+    }
+
+    /**
+     * Sort activities oldest-first
+     */
+    static function activitySort($a, $b)
+    {
+        if ($a->time == $b->time) {
+            return 0;
+        } else if ($a->time < $b->time) {
+            return -1;
+        } else {
+            return 1;
+        }
+    }
+
     function userFromAuthor($author)
     {
-        $user = User::staticGet('uri', $author->id);
+        $user = User::getKV('uri', $author->id);
 
         if (empty($user)) {
             $attrs =
@@ -123,7 +155,7 @@ class FeedImporter extends QueueHandler
         $profile = $user->getProfile();
         Ostatus_profile::updateProfile($profile, $author);
 
-        // FIXME: Update avatar
+        // @todo FIXME: Update avatar
         return $user;
     }
 }