3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * A class for moving an account to a new server
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Moves an account from this server to another
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/
50 private $_user = null;
51 private $_profile = null;
52 private $_remote = null;
53 private $_sink = null;
55 function __construct($user, $remote, $password)
58 $this->_profile = $user->getProfile();
60 $remote = Discovery::normalize($remote);
62 $oprofile = Ostatus_profile::ensureProfileURI($remote);
64 if (empty($oprofile)) {
65 throw new Exception("Can't locate account {$remote}");
68 $this->_remote = $oprofile->localProfile();
70 list($svcDocUrl, $username) = self::getServiceDocument($remote);
72 $this->_sink = new ActivitySink($svcDocUrl, $username, $password);
75 static function getServiceDocument($remote)
77 $discovery = new Discovery();
79 $xrd = $discovery->lookup($remote);
82 throw new Exception("Can't find XRD for $remote");
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'];
104 if (empty($svcDocUrl)) {
105 throw new Exception("No AtomPub API service for $remote.");
108 return array($svcDocUrl, $username);
114 "Moving user {$this->_user->nickname} to {$this->_remote->nickname}");
116 $stream = new UserActivityStream($this->_user);
118 $acts = array_reverse($stream->activities);
121 "Got {count($acts)} activities ".
122 "for {$this->_user->nickname}");
124 // Reverse activities to run in correct chron order
126 foreach ($acts as $act) {
128 $this->_moveActivity($act);
129 } catch (Exception $e) {
131 "Error moving activity {$act->id} {$act->verb}: " .
138 "Finished moving user {$this->_user->nickname} ".
139 "to {$this->_remote->nickname}");
142 private function _moveActivity($act)
144 switch ($act->verb) {
145 case ActivityVerb::FAVORITE:
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));
158 case ActivityVerb::POST:
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)) {
169 case ActivityVerb::JOIN:
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);
179 case ActivityVerb::FOLLOW:
180 if ($act->actor->id == $this->_user->uri) {
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);
190 $otherUser = User::staticGet('uri', $act->actor->id);
191 if (!empty($otherUser)) {
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());
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}.");
212 * Add a header for our class so we know who did it.
214 * @param int $level Log level, like LOG_ERR or LOG_INFO
215 * @param string $message Message to log
220 protected function log($level, $message)
222 common_log($level, "AccountMover: " . $message);