]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/feedimporter.php
Make restoreuser use new FeedImporter queue handler
[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             if ($doc->documentElement->namespaceURI != Activity::ATOM ||
72                 $doc->documentElement->localName != 'feed') {
73                 throw new ClientException(_("Not an atom feed."));
74             }
75
76             $feed = $doc->documentElement;
77
78             $author = ActivityUtils::getFeedAuthor($feed);
79
80             if (empty($author)) {
81                 throw new ClientException(_("No author in the feed."));
82             }
83
84             if (empty($user)) {
85                 if ($trusted) {
86                     $user = $this->userFromAuthor($author);
87                 } else {
88                     throw new ClientException(_("Can't import without a user."));
89                 }
90             }
91
92             $activities = $this->getActivities($feed);
93
94             $qm = QueueManager::get();
95
96             foreach ($activities as $activity) {
97                 $qm->enqueue(array($user, $author, $activity, $trusted), 'actimp');
98             }
99         } catch (ClientException $ce) {
100             common_log(LOG_WARNING, $ce->getMessage());
101             return true;
102         } catch (ServerException $se) {
103             common_log(LOG_ERR, $ce->getMessage());
104             return false;
105         } catch (Exception $e) {
106             common_log(LOG_ERR, $ce->getMessage());
107             return false;
108         }
109     }
110
111     function getActivities($feed)
112     {
113         $entries = $feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
114
115         $activities = array();
116
117         for ($i = 0; $i < $entries->length; $i++) {
118             $activities[] = new Activity($entries->item($i));
119         }
120
121         usort($activities, array("FeedImporter", "activitySort"));
122
123         return $activities;
124     }
125
126     /**
127      * Sort activities oldest-first
128      */
129
130     static function activitySort($a, $b)
131     {
132         if ($a->time == $b->time) {
133             return 0;
134         } else if ($a->time < $b->time) {
135             return -1;
136         } else {
137             return 1;
138         }
139     }
140
141     function userFromAuthor($author)
142     {
143         $user = User::staticGet('uri', $author->id);
144
145         if (empty($user)) {
146             $attrs =
147                 array('nickname' => Ostatus_profile::getActivityObjectNickname($author),
148                       'uri' => $author->id);
149
150             $user = User::register($attrs);
151         }
152
153         $profile = $user->getProfile();
154         Ostatus_profile::updateProfile($profile, $author);
155
156         // FIXME: Update avatar
157         return $user;
158     }
159 }