]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/feedimporter.php
move activity importing code to two different queuehandler classes
[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                 }
88             }
89
90             $entries = $feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
91
92             $activities = $this->entriesToActivities($entries, $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 userFromAuthor($author)
112     {
113         $user = User::staticGet('uri', $author->id);
114
115         if (empty($user)) {
116             $attrs =
117                 array('nickname' => Ostatus_profile::getActivityObjectNickname($author),
118                       'uri' => $author->id);
119
120             $user = User::register($attrs);
121         }
122
123         $profile = $user->getProfile();
124         Ostatus_profile::updateProfile($profile, $author);
125
126         // FIXME: Update avatar
127         return $user;
128     }
129 }