]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/feedimporter.php
Merge branch 'master' into testing
[quix0rs-gnu-social.git] / lib / feedimporter.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Importer for feeds of activities
7  * 
8  * PHP version 5
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  * @category  Account
24  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Importer for feeds of activities
39  *
40  * Takes an XML file representing a feed of activities and imports each
41  * activity to the user in question.
42  *
43  * @category  Account
44  * @package   StatusNet
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/
49  */
50
51 class FeedImporter extends QueueHandler
52 {
53     /**
54      * Transport identifier
55      *
56      * @return string identifier for this queue handler
57      */
58
59     public function transport()
60     {
61         return 'feedimp';
62     }
63
64     function handle($data)
65     {
66         list($user, $xml, $trusted) = $data;
67
68         try {
69             $doc = DOMDocument::loadXML($xml);
70
71             $feed = $doc->documentElement;
72
73             if ($feed->namespaceURI != Activity::ATOM ||
74                 $feed->localName != 'feed') {
75                 throw new ClientException(_("Not an atom feed."));
76             }
77
78
79             $author = ActivityUtils::getFeedAuthor($feed);
80
81             if (empty($author)) {
82                 throw new ClientException(_("No author in the feed."));
83             }
84
85             if (empty($user)) {
86                 if ($trusted) {
87                     $user = $this->userFromAuthor($author);
88                 } else {
89                     throw new ClientException(_("Can't import without a user."));
90                 }
91             }
92
93             $activities = $this->getActivities($feed);
94
95             $qm = QueueManager::get();
96
97             foreach ($activities as $activity) {
98                 $qm->enqueue(array($user, $author, $activity, $trusted), 'actimp');
99             }
100         } catch (ClientException $ce) {
101             common_log(LOG_WARNING, $ce->getMessage());
102             return true;
103         } catch (ServerException $se) {
104             common_log(LOG_ERR, $ce->getMessage());
105             return false;
106         } catch (Exception $e) {
107             common_log(LOG_ERR, $ce->getMessage());
108             return false;
109         }
110     }
111
112     function getActivities($feed)
113     {
114         $entries = $feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
115
116         $activities = array();
117
118         for ($i = 0; $i < $entries->length; $i++) {
119             $activities[] = new Activity($entries->item($i));
120         }
121
122         usort($activities, array("FeedImporter", "activitySort"));
123
124         return $activities;
125     }
126
127     /**
128      * Sort activities oldest-first
129      */
130
131     static function activitySort($a, $b)
132     {
133         if ($a->time == $b->time) {
134             return 0;
135         } else if ($a->time < $b->time) {
136             return -1;
137         } else {
138             return 1;
139         }
140     }
141
142     function userFromAuthor($author)
143     {
144         $user = User::staticGet('uri', $author->id);
145
146         if (empty($user)) {
147             $attrs =
148                 array('nickname' => Ostatus_profile::getActivityObjectNickname($author),
149                       'uri' => $author->id);
150
151             $user = User::register($attrs);
152         }
153
154         $profile = $user->getProfile();
155         Ostatus_profile::updateProfile($profile, $author);
156
157         // FIXME: Update avatar
158         return $user;
159     }
160 }