diff options
Diffstat (limited to 'plugins/YammerImport/lib')
-rw-r--r-- | plugins/YammerImport/lib/sn_yammerclient.php | 7 | ||||
-rw-r--r-- | plugins/YammerImport/lib/yammerimporter.php | 11 | ||||
-rw-r--r-- | plugins/YammerImport/lib/yammerrunner.php | 77 |
3 files changed, 67 insertions, 28 deletions
diff --git a/plugins/YammerImport/lib/sn_yammerclient.php b/plugins/YammerImport/lib/sn_yammerclient.php index 830f9dabb..5da1cc5e7 100644 --- a/plugins/YammerImport/lib/sn_yammerclient.php +++ b/plugins/YammerImport/lib/sn_yammerclient.php @@ -104,7 +104,8 @@ class SN_YammerClient { $body = $this->fetchApi("api/v1/$method.json", $params); $data = json_decode($body, true); - if (!$data) { + if ($data === null) { + common_log(LOG_ERR, "Invalid JSON response from Yammer API: " . $body); throw new Exception("Invalid JSON response from Yammer API"); } return $data; @@ -161,7 +162,7 @@ class SN_YammerClient if ($this->token || $this->tokenSecret) { throw new Exception("Requesting a token, but already set up with a token"); } - $data = $this->fetch('oauth/request_token'); + $data = $this->fetchApi('oauth/request_token'); $arr = array(); parse_str($data, $arr); return $arr; @@ -176,7 +177,7 @@ class SN_YammerClient public function accessToken($verifier) { $this->verifier = $verifier; - $data = $this->fetch('oauth/access_token'); + $data = $this->fetchApi('oauth/access_token'); $this->verifier = null; $arr = array(); parse_str($data, $arr); diff --git a/plugins/YammerImport/lib/yammerimporter.php b/plugins/YammerImport/lib/yammerimporter.php index b1d2815b9..0425b8b04 100644 --- a/plugins/YammerImport/lib/yammerimporter.php +++ b/plugins/YammerImport/lib/yammerimporter.php @@ -327,17 +327,20 @@ class YammerImporter private function findImportedUser($origId) { - return Yammer_user::staticGet('id', $origId); + $map = Yammer_user::staticGet('id', $origId); + return $map ? $map->user_id : null; } private function findImportedGroup($origId) { - return Yammer_group::staticGet('id', $origId); + $map = Yammer_group::staticGet('id', $origId); + return $map ? $map->group_id : null; } private function findImportedNotice($origId) { - return Yammer_notice::staticGet('id', $origId); + $map = Yammer_notice::staticGet('id', $origId); + return $map ? $map->notice_id : null; } private function recordImportedUser($origId, $userId) @@ -370,7 +373,7 @@ class YammerImporter // Blaaaaaarf! $known = array('Pacific Time (US & Canada)' => 'America/Los_Angeles', 'Eastern Time (US & Canada)' => 'America/New_York'); - if (array_key_exists($known, $tz)) { + if (array_key_exists($tz, $known)) { return $known[$tz]; } else { return false; diff --git a/plugins/YammerImport/lib/yammerrunner.php b/plugins/YammerImport/lib/yammerrunner.php index 95ff78371..c4db48399 100644 --- a/plugins/YammerImport/lib/yammerrunner.php +++ b/plugins/YammerImport/lib/yammerrunner.php @@ -33,18 +33,31 @@ class YammerRunner private $client; private $importer; + /** + * Normalize our singleton state and give us a YammerRunner object to play with! + * + * @return YammerRunner + */ public static function init() { $state = Yammer_state::staticGet('id', 1); if (!$state) { - $state = new Yammer_state(); - $state->id = 1; - $state->state = 'init'; - $state->insert(); + $state = self::initState(); } return new YammerRunner($state); } + private static function initState() + { + $state = new Yammer_state(); + $state->id = 1; + $state->state = 'init'; + $state->created = common_sql_now(); + $state->modified = common_sql_now(); + $state->insert(); + return $state; + } + private function __construct($state) { $this->state = $state; @@ -55,7 +68,7 @@ class YammerRunner $this->state->oauth_token, $this->state->oauth_secret); - $this->importer = new YammerImporter($client); + $this->importer = new YammerImporter($this->client); } /** @@ -81,6 +94,8 @@ class YammerRunner /** * Check if we have work to do in iterate(). + * + * @return boolean */ public function hasWork() { @@ -89,6 +104,15 @@ class YammerRunner } /** + * Blow away any current state! + */ + public function reset() + { + $this->state->delete(); + $this->state = self::initState(); + } + + /** * Start the authentication process! If all goes well, we'll get back a URL. * Have the user visit that URL, log in on Yammer and verify the importer's * permissions. They'll get back a verification code, which needs to be passed @@ -102,12 +126,16 @@ class YammerRunner throw ServerError("Cannot request Yammer auth; already there!"); } + $data = $this->client->requestToken(); + $old = clone($this->state); $this->state->state = 'requesting-auth'; - $this->state->request_token = $client->requestToken(); + $this->state->oauth_token = $data['oauth_token']; + $this->state->oauth_secret = $data['oauth_token_secret']; + $this->state->modified = common_sql_now(); $this->state->update($old); - return $this->client->authorizeUrl($this->state->request_token); + return $this->client->authorizeUrl($this->state->oauth_token); } /** @@ -127,12 +155,13 @@ class YammerRunner throw ServerError("Cannot save auth token in Yammer import state {$this->state->state}"); } - $old = clone($this->state); - list($token, $secret) = $this->client->getAuthToken($verifier); - $this->state->verifier = ''; - $this->state->oauth_token = $token; - $this->state->oauth_secret = $secret; + $data = $this->client->accessToken($verifier); + $old = clone($this->state); + $this->state->state = 'import-users'; + $this->state->oauth_token = $data['oauth_token']; + $this->state->oauth_secret = $data['oauth_token_secret']; + $this->state->modified = common_sql_now(); $this->state->update($old); return true; @@ -146,8 +175,7 @@ class YammerRunner */ public function iterate() { - - switch($state->state) + switch($this->state()) { case 'init': case 'requesting-auth': @@ -188,11 +216,12 @@ class YammerRunner $this->state->state = 'import-groups'; } else { foreach ($data as $item) { - $user = $imp->importUser($item); + $user = $this->importer->importUser($item); common_log(LOG_INFO, "Imported Yammer user " . $item['id'] . " as $user->nickname ($user->id)"); } $this->state->users_page = $page; } + $this->state->modified = common_sql_now(); $this->state->update($old); return true; } @@ -214,14 +243,15 @@ class YammerRunner if (count($data) == 0) { common_log(LOG_INFO, "Finished importing Yammer groups; moving on to messages."); - $this->state->state = 'import-messages'; + $this->state->state = 'fetch-messages'; } else { foreach ($data as $item) { - $group = $imp->importGroup($item); + $group = $this->importer->importGroup($item); common_log(LOG_INFO, "Imported Yammer group " . $item['id'] . " as $group->nickname ($group->id)"); } $this->state->groups_page = $page; } + $this->state->modified = common_sql_now(); $this->state->update($old); return true; } @@ -248,16 +278,17 @@ class YammerRunner $data = $this->client->messages($params); $messages = $data['messages']; - if (count($data) == 0) { + if (count($messages) == 0) { common_log(LOG_INFO, "Finished fetching Yammer messages; moving on to save messages."); $this->state->state = 'save-messages'; } else { - foreach ($data as $item) { + foreach ($messages as $item) { Yammer_notice_stub::record($item['id'], $item); $oldest = $item['id']; } $this->state->messages_oldest = $oldest; } + $this->state->modified = common_sql_now(); $this->state->update($old); return true; } @@ -267,10 +298,13 @@ class YammerRunner $old = clone($this->state); $newest = intval($this->state->messages_newest); + + $stub = new Yammer_notice_stub(); if ($newest) { - $stub->addWhere('id > ' . $newest); + $stub->whereAdd('id > ' . $newest); } $stub->limit(20); + $stub->orderBy('id'); $stub->find(); if ($stub->N == 0) { @@ -278,13 +312,14 @@ class YammerRunner $this->state->state = 'done'; } else { while ($stub->fetch()) { - $item = json_decode($stub->json_data); + $item = $stub->getData(); $notice = $this->importer->importNotice($item); common_log(LOG_INFO, "Imported Yammer notice " . $item['id'] . " as $notice->id"); $newest = $item['id']; } $this->state->messages_newest = $newest; } + $this->state->modified = common_sql_now(); $this->state->update($old); return true; } |