3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Importer for feeds of activities
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2010 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Importer for feeds of activities
40 * Takes an XML file representing a feed of activities and imports each
41 * activity to the user in question.
45 * @author Evan Prodromou <evan@status.net>
46 * @copyright 2010 StatusNet, Inc.
47 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
48 * @link http://status.net/
51 class FeedImporter extends QueueHandler
54 * Transport identifier
56 * @return string identifier for this queue handler
59 public function transport()
64 function handle($data)
66 list($user, $xml, $trusted) = $data;
69 $doc = DOMDocument::loadXML($xml);
71 $feed = $doc->documentElement;
73 if ($feed->namespaceURI != Activity::ATOM ||
74 $feed->localName != 'feed') {
75 throw new ClientException(_("Not an atom feed."));
79 $author = ActivityUtils::getFeedAuthor($feed);
82 throw new ClientException(_("No author in the feed."));
87 $user = $this->userFromAuthor($author);
89 throw new ClientException(_("Can't import without a user."));
93 $activities = $this->getActivities($feed);
95 $qm = QueueManager::get();
97 foreach ($activities as $activity) {
98 $qm->enqueue(array($user, $author, $activity, $trusted), 'actimp');
100 } catch (ClientException $ce) {
101 common_log(LOG_WARNING, $ce->getMessage());
103 } catch (ServerException $se) {
104 common_log(LOG_ERR, $ce->getMessage());
106 } catch (Exception $e) {
107 common_log(LOG_ERR, $ce->getMessage());
112 function getActivities($feed)
114 $entries = $feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
116 $activities = array();
118 for ($i = 0; $i < $entries->length; $i++) {
119 $activities[] = new Activity($entries->item($i));
122 usort($activities, array("FeedImporter", "activitySort"));
128 * Sort activities oldest-first
131 static function activitySort($a, $b)
133 if ($a->time == $b->time) {
135 } else if ($a->time < $b->time) {
142 function userFromAuthor($author)
144 $user = User::staticGet('uri', $author->id);
148 array('nickname' => Ostatus_profile::getActivityObjectNickname($author),
149 'uri' => $author->id);
151 $user = User::register($attrs);
154 $profile = $user->getProfile();
155 Ostatus_profile::updateProfile($profile, $author);
157 // FIXME: Update avatar