]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/accountmover.php
considerably more logging and error checking in AccountMover
[quix0rs-gnu-social.git] / lib / accountmover.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * A class for moving an account to a new server
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  * Moves an account from this server to another
39  *
40  * @category  Account
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2010 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47
48 class AccountMover
49 {
50     private $_user    = null;
51     private $_profile = null;
52     private $_remote  = null;
53     private $_sink    = null;
54     
55     function __construct($user, $remote, $password)
56     {
57         $this->_user    = $user;
58         $this->_profile = $user->getProfile();
59
60         $remote = Discovery::normalize($remote);
61
62         $oprofile = Ostatus_profile::ensureProfileURI($remote);
63
64         if (empty($oprofile)) {
65             throw new Exception("Can't locate account {$remote}");
66         }
67
68         $this->_remote = $oprofile->localProfile();
69
70         list($svcDocUrl, $username) = self::getServiceDocument($remote);
71
72         $this->_sink = new ActivitySink($svcDocUrl, $username, $password);
73     }
74
75     static function getServiceDocument($remote)
76     {
77         $discovery = new Discovery();
78
79         $xrd = $discovery->lookup($remote);
80
81         if (empty($xrd)) {
82             throw new Exception("Can't find XRD for $remote");
83         } 
84
85         $svcDocUrl = null;
86         $username  = null;
87
88         foreach ($xrd->links as $link) {
89             if ($link['rel'] == 'http://apinamespace.org/atom' &&
90                 $link['type'] == 'application/atomsvc+xml') {
91                 $svcDocUrl = $link['href'];
92                 if (!empty($link['property'])) {
93                     foreach ($link['property'] as $property) {
94                         if ($property['type'] == 'http://apinamespace.org/atom/username') {
95                             $username = $property['value'];
96                             break;
97                         }
98                     }
99                 }
100                 break;
101             }
102         }
103
104         if (empty($svcDocUrl)) {
105             throw new Exception("No AtomPub API service for $remote.");
106         }
107
108         return array($svcDocUrl, $username);
109     }
110
111     function move()
112     {
113         $this->log(LOG_INFO, 
114                    "Moving user {$this->_user->nickname} to {$this->_remote->nickname}");
115
116         $stream = new UserActivityStream($this->_user);
117
118         $acts = array_reverse($stream->activities);
119
120         $this->log(LOG_INFO,
121                    "Got {count($acts)} activities ".
122                    "for {$this->_user->nickname}");
123
124         // Reverse activities to run in correct chron order
125
126         foreach ($acts as $act) {
127             try {
128                 $this->_moveActivity($act);
129             } catch (Exception $e) {
130                 $this->log(LOG_ERR,
131                            "Error moving activity {$act->id} {$act->verb}: " .
132                            $e->getMessage());
133                 continue;
134             }
135         }
136
137         $this->log(LOG_INFO,
138                    "Finished moving user {$this->_user->nickname} ".
139                    "to {$this->_remote->nickname}");
140     }
141
142     private function _moveActivity($act)
143     {
144         switch ($act->verb) {
145         case ActivityVerb::FAVORITE:
146             $this->log(LOG_INFO,
147                        "Moving favorite of {$act->objects[0]->id} by ".
148                        "{$act->actor->id} to {$this->_remote->nickname}.");
149             // push it, then delete local
150             $this->_sink->postActivity($act);
151             $notice = Notice::staticGet('uri', $act->objects[0]->id);
152             if (!empty($notice)) {
153                 $fave = Fave::pkeyGet(array('user_id' => $this->_user->id,
154                                             'notice_id' => $notice->id));
155                 $fave->delete();
156             }
157             break;
158         case ActivityVerb::POST:
159             $this->log(LOG_INFO,
160                        "Moving notice {$act->objects[0]->id} by ".
161                        "{$act->actor->id} to {$this->_remote->nickname}.");
162             // XXX: send a reshare, not a post
163             $this->_sink->postActivity($act);
164             $notice = Notice::staticGet('uri', $act->objects[0]->id);
165             if (!empty($notice)) {
166                 $notice->delete();
167             }
168             break;
169         case ActivityVerb::JOIN:
170             $this->log(LOG_INFO,
171                        "Moving group join of {$act->objects[0]->id} by ".
172                        "{$act->actor->id} to {$this->_remote->nickname}.");
173             $this->_sink->postActivity($act);
174             $group = User_group::staticGet('uri', $act->objects[0]->id);
175             if (!empty($group)) {
176                 Group_member::leave($group->id, $this->_user->id);
177             }
178             break;
179         case ActivityVerb::FOLLOW:
180             if ($act->actor->id == $this->_user->uri) {
181                 $this->log(LOG_INFO,
182                            "Moving subscription to {$act->objects[0]->id} by ".
183                            "{$act->actor->id} to {$this->_remote->nickname}.");
184                 $this->_sink->postActivity($act);
185                 $other = Profile::fromURI($act->objects[0]->id);
186                 if (!empty($other)) {
187                     Subscription::cancel($this->_profile, $other);
188                 }
189             } else {
190                 $otherUser = User::staticGet('uri', $act->actor->id);
191                 if (!empty($otherUser)) {
192                     $this->log(LOG_INFO,
193                                "Changing sub to {$act->objects[0]->id}".
194                                "by {$act->actor->id} to {$this->_remote->nickname}.");
195                     $otherProfile = $otherUser->getProfile();
196                     Subscription::start($otherProfile, $this->_remote);
197                     Subscription::cancel($otherProfile, $this->_user->getProfile());
198                 } else {
199                     $this->log(LOG_NOTICE,
200                                "Not changing sub to {$act->objects[0]->id}".
201                                "by remote {$act->actor->id} ".
202                                "to {$this->_remote->nickname}.");
203                 }
204             }
205             break;
206         }
207     }
208
209     /**
210      * Log some data
211      * 
212      * Add a header for our class so we know who did it.
213      *
214      * @param int    $level   Log level, like LOG_ERR or LOG_INFO
215      * @param string $message Message to log
216      *
217      * @return void
218      */
219
220     protected function log($level, $message)
221     {
222         common_log($level, "AccountMover: " . $message);
223     }
224 }