]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/feedimporter.php
Misses this file to merge. I like the comments.
[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 class FeedImporter extends QueueHandler
51 {
52     /**
53      * Transport identifier
54      *
55      * @return string identifier for this queue handler
56      */
57     public function transport()
58     {
59         return 'feedimp';
60     }
61
62     function handle($data)
63     {
64         list($user, $xml, $trusted) = $data;
65
66         try {
67             $doc = DOMDocument::loadXML($xml);
68
69             $feed = $doc->documentElement;
70
71             if ($feed->namespaceURI != Activity::ATOM ||
72                 $feed->localName != 'feed') {
73                 // TRANS: Client exception thrown when an imported feed is not an Atom feed.
74                 throw new ClientException(_("Not an Atom feed."));
75             }
76
77
78             $author = ActivityUtils::getFeedAuthor($feed);
79
80             if (empty($author)) {
81                 // TRANS: Client exception thrown when an imported feed does not have an 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                     // TRANS: Client exception thrown when an imported feed does not have an author that
90                     // TRANS: can be associated with a user.
91                     throw new ClientException(_("Cannot import without a user."));
92                 }
93             }
94
95             $activities = $this->getActivities($feed);
96
97             $qm = QueueManager::get();
98
99             foreach ($activities as $activity) {
100                 $qm->enqueue(array($user, $author, $activity, $trusted), 'actimp');
101             }
102         } catch (ClientException $ce) {
103             common_log(LOG_WARNING, $ce->getMessage());
104             return true;
105         } catch (ServerException $se) {
106             common_log(LOG_ERR, $ce->getMessage());
107             return false;
108         } catch (Exception $e) {
109             common_log(LOG_ERR, $ce->getMessage());
110             return false;
111         }
112     }
113
114     function getActivities($feed)
115     {
116         $entries = $feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
117
118         $activities = array();
119
120         for ($i = 0; $i < $entries->length; $i++) {
121             $activities[] = new Activity($entries->item($i));
122         }
123
124         usort($activities, array("FeedImporter", "activitySort"));
125
126         return $activities;
127     }
128
129     /**
130      * Sort activities oldest-first
131      */
132     static function activitySort($a, $b)
133     {
134         if ($a->time == $b->time) {
135             return 0;
136         } else if ($a->time < $b->time) {
137             return -1;
138         } else {
139             return 1;
140         }
141     }
142
143     function userFromAuthor($author)
144     {
145         $user = User::getKV('uri', $author->id);
146
147         if (empty($user)) {
148             $attrs =
149                 array('nickname' => Ostatus_profile::getActivityObjectNickname($author),
150                       'uri' => $author->id);
151
152             $user = User::register($attrs);
153         }
154
155         $profile = $user->getProfile();
156         Ostatus_profile::updateProfile($profile, $author);
157
158         // @todo FIXME: Update avatar
159         return $user;
160     }
161 }