3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('STATUSNET')) {
25 * State machine for running through Yammer import.
27 * @package YammerImportPlugin
28 * @author Brion Vibber <brion@status.net>
37 * Normalize our singleton state and give us a YammerRunner object to play with!
39 * @return YammerRunner
41 public static function init()
43 $state = Yammer_state::staticGet('id', 1);
45 $state = self::initState();
47 return new YammerRunner($state);
50 private static function initState()
52 $state = new Yammer_state();
54 $state->state = 'init';
55 $state->created = common_sql_now();
56 $state->modified = common_sql_now();
61 private function __construct($state)
63 $this->state = $state;
65 $this->client = new SN_YammerClient(
66 common_config('yammer', 'consumer_key'),
67 common_config('yammer', 'consumer_secret'),
68 $this->state->oauth_token,
69 $this->state->oauth_secret);
71 $this->importer = new YammerImporter($this->client);
75 * Check which state we're in
79 public function state()
81 return $this->state->state;
85 * Is the import done, finished, complete, finito?
89 public function isDone()
91 $workStates = array('import-users', 'import-groups', 'fetch-messages', 'save-messages');
92 return ($this->state() == 'done');
96 * Check if we have work to do in iterate().
100 public function hasWork()
102 $workStates = array('import-users', 'import-groups', 'fetch-messages', 'save-messages');
103 return in_array($this->state(), $workStates);
107 * Blow away any current state!
109 public function reset()
111 $this->state->delete();
112 $this->state = self::initState();
116 * Start the authentication process! If all goes well, we'll get back a URL.
117 * Have the user visit that URL, log in on Yammer and verify the importer's
118 * permissions. They'll get back a verification code, which needs to be passed
119 * on to saveAuthToken().
123 public function requestAuth()
125 if ($this->state->state != 'init') {
126 throw new ServerException("Cannot request Yammer auth; already there!");
129 $data = $this->client->requestToken();
131 $old = clone($this->state);
132 $this->state->state = 'requesting-auth';
133 $this->state->oauth_token = $data['oauth_token'];
134 $this->state->oauth_secret = $data['oauth_token_secret'];
135 $this->state->modified = common_sql_now();
136 $this->state->update($old);
138 return $this->getAuthUrl();
142 * When already in requesting-auth state, grab the URL to send the user to
143 * to complete OAuth setup.
147 function getAuthUrl()
149 if ($this->state() == 'requesting-auth') {
150 return $this->client->authorizeUrl($this->state->oauth_token);
152 throw new ServerException('Cannot get Yammer auth URL when not in requesting-auth state!');
157 * Now that the user's given us this verification code from Yammer, we can
158 * request a final OAuth token/secret pair which we can use to access the
161 * After success here, we'll be ready to move on and run through iterate()
162 * until the import is complete.
164 * @param string $verifier
165 * @return boolean success
167 public function saveAuthToken($verifier)
169 if ($this->state->state != 'requesting-auth') {
170 throw new ServerException("Cannot save auth token in Yammer import state {$this->state->state}");
173 $data = $this->client->accessToken($verifier);
175 $old = clone($this->state);
176 $this->state->state = 'import-users';
177 $this->state->oauth_token = $data['oauth_token'];
178 $this->state->oauth_secret = $data['oauth_token_secret'];
179 $this->state->modified = common_sql_now();
180 $this->state->update($old);
186 * Once authentication is complete, we need to call iterate() a bunch of times
187 * until state() returns 'done'.
189 * @return boolean success
191 public function iterate()
193 switch($this->state())
196 case 'requesting-auth':
197 // Neither of these should reach our background state!
198 common_log(LOG_ERR, "Non-background YammerImport state '$state->state' during import run!");
201 return $this->iterateUsers();
202 case 'import-groups':
203 return $this->iterateGroups();
204 case 'fetch-messages':
205 return $this->iterateFetchMessages();
206 case 'save-messages':
207 return $this->iterateSaveMessages();
209 common_log(LOG_ERR, "Invalid YammerImport state '$state->state' during import run!");
215 * Trundle through one 'page' return of up to 50 user accounts retrieved
216 * from the Yammer API, importing them as we go.
218 * When we run out of users, move on to groups.
220 * @return boolean success
222 private function iterateUsers()
224 $old = clone($this->state);
226 $page = intval($this->state->users_page) + 1;
227 $data = $this->client->users(array('page' => $page));
229 if (count($data) == 0) {
230 common_log(LOG_INFO, "Finished importing Yammer users; moving on to groups.");
231 $this->state->state = 'import-groups';
233 foreach ($data as $item) {
234 $user = $this->importer->importUser($item);
235 common_log(LOG_INFO, "Imported Yammer user " . $item['id'] . " as $user->nickname ($user->id)");
237 $this->state->users_page = $page;
239 $this->state->modified = common_sql_now();
240 $this->state->update($old);
245 * Trundle through one 'page' return of up to 20 user groups retrieved
246 * from the Yammer API, importing them as we go.
248 * When we run out of groups, move on to messages.
250 * @return boolean success
252 private function iterateGroups()
254 $old = clone($this->state);
256 $page = intval($this->state->groups_page) + 1;
257 $data = $this->client->groups(array('page' => $page));
259 if (count($data) == 0) {
260 common_log(LOG_INFO, "Finished importing Yammer groups; moving on to messages.");
261 $this->state->state = 'fetch-messages';
263 foreach ($data as $item) {
264 $group = $this->importer->importGroup($item);
265 common_log(LOG_INFO, "Imported Yammer group " . $item['id'] . " as $group->nickname ($group->id)");
267 $this->state->groups_page = $page;
269 $this->state->modified = common_sql_now();
270 $this->state->update($old);
275 * Trundle through one 'page' return of up to 20 public messages retrieved
276 * from the Yammer API, saving them to our stub table for future import in
277 * correct chronological order.
279 * When we run out of messages to fetch, move on to saving the messages.
281 * @return boolean success
283 private function iterateFetchMessages()
285 $old = clone($this->state);
287 $oldest = intval($this->state->messages_oldest);
289 $params = array('older_than' => $oldest);
293 $data = $this->client->messages($params);
294 $messages = $data['messages'];
296 if (count($messages) == 0) {
297 common_log(LOG_INFO, "Finished fetching Yammer messages; moving on to save messages.");
298 $this->state->state = 'save-messages';
300 foreach ($messages as $item) {
301 Yammer_notice_stub::record($item['id'], $item);
302 $oldest = $item['id'];
304 $this->state->messages_oldest = $oldest;
306 $this->state->modified = common_sql_now();
307 $this->state->update($old);
311 private function iterateSaveMessages()
313 $old = clone($this->state);
315 $newest = intval($this->state->messages_newest);
317 $stub = new Yammer_notice_stub();
319 $stub->whereAdd('id > ' . $newest);
322 $stub->orderBy('id');
326 common_log(LOG_INFO, "Finished saving Yammer messages; import complete!");
327 $this->state->state = 'done';
329 while ($stub->fetch()) {
330 $item = $stub->getData();
331 $notice = $this->importer->importNotice($item);
332 common_log(LOG_INFO, "Imported Yammer notice " . $item['id'] . " as $notice->id");
333 $newest = $item['id'];
335 $this->state->messages_newest = $newest;
337 $this->state->modified = common_sql_now();
338 $this->state->update($old);
343 * Count the number of Yammer users we've mapped into our system!
347 public function countUsers()
349 $map = new Yammer_user();
350 return $map->count();
355 * Count the number of Yammer groups we've mapped into our system!
359 public function countGroups()
361 $map = new Yammer_group();
362 return $map->count();
367 * Count the number of Yammer notices we've pulled down for pending import...
371 public function countFetchedNotices()
373 $map = new Yammer_notice_stub();
374 return $map->count();
379 * Count the number of Yammer notices we've mapped into our system!
383 public function countSavedNotices()
385 $map = new Yammer_notice();
386 return $map->count();
390 * Start running import work in the background queues...
392 public function startBackgroundImport()
394 $qm = QueueManager::get();
395 $qm->enqueue('YammerImport', 'yammer');