123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- <?php
- if (!defined('STATUSNET')) {
- exit(1);
- }
- class YammerRunner
- {
- private $state;
- private $client;
- private $importer;
-
- public static function init()
- {
- $state = Yammer_state::getKV('id', 1);
- if (!$state) {
- $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;
- $this->client = new SNYammerClient(
- common_config('yammer', 'consumer_key'),
- common_config('yammer', 'consumer_secret'),
- $this->state->oauth_token,
- $this->state->oauth_secret);
- $this->importer = new YammerImporter($this->client);
- }
-
- public function state()
- {
- return $this->state->state;
- }
-
- public function isDone()
- {
- $workStates = array('import-users', 'import-groups', 'fetch-messages', 'save-messages');
- return ($this->state() == 'done');
- }
-
- public function hasWork()
- {
- $workStates = array('import-users', 'import-groups', 'fetch-messages', 'save-messages');
- return in_array($this->state(), $workStates);
- }
-
- public function reset()
- {
- $this->state->delete();
- $this->state = self::initState();
- }
-
- public function requestAuth()
- {
- if ($this->state->state != 'init') {
-
- throw new ServerException(_m('Cannot request Yammer auth; already there!'));
- }
- $data = $this->client->requestToken();
- $old = clone($this->state);
- $this->state->state = 'requesting-auth';
- $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->getAuthUrl();
- }
-
- function getAuthUrl()
- {
- if ($this->state() == 'requesting-auth') {
- return $this->client->authorizeUrl($this->state->oauth_token);
- } else {
-
- throw new ServerException(_m('Cannot get Yammer auth URL when not in requesting-auth state!'));
- }
- }
-
- public function saveAuthToken($verifier)
- {
- if ($this->state->state != 'requesting-auth') {
-
-
- throw new ServerException(_m('Cannot save auth token in Yammer import state %s.',$this->state->state));
- }
- $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;
- }
-
- public function iterate()
- {
- switch($this->state())
- {
- case 'init':
- case 'requesting-auth':
-
- common_log(LOG_ERR, "Non-background YammerImport state '$state->state' during import run!");
- return false;
- case 'import-users':
- return $this->iterateUsers();
- case 'import-groups':
- return $this->iterateGroups();
- case 'fetch-messages':
- return $this->iterateFetchMessages();
- case 'save-messages':
- return $this->iterateSaveMessages();
- default:
- common_log(LOG_ERR, "Invalid YammerImport state '$state->state' during import run!");
- return false;
- }
- }
-
- private function iterateUsers()
- {
- $old = clone($this->state);
- $page = intval($this->state->users_page) + 1;
- $data = $this->client->users(array('page' => $page));
- if (count($data) == 0) {
- common_log(LOG_INFO, "Finished importing Yammer users; moving on to groups.");
- $this->state->state = 'import-groups';
- } else {
- foreach ($data as $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;
- }
-
- private function iterateGroups()
- {
- $old = clone($this->state);
- $page = intval($this->state->groups_page) + 1;
- $data = $this->client->groups(array('page' => $page));
- if (count($data) == 0) {
- common_log(LOG_INFO, "Finished importing Yammer groups; moving on to messages.");
- $this->state->state = 'fetch-messages';
- } else {
- foreach ($data as $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;
- }
-
- private function iterateFetchMessages()
- {
- $old = clone($this->state);
- $oldest = intval($this->state->messages_oldest);
- if ($oldest) {
- $params = array('older_than' => $oldest);
- } else {
- $params = array();
- }
- $data = $this->client->messages($params);
- $messages = $data['messages'];
- if (count($messages) == 0) {
- common_log(LOG_INFO, "Finished fetching Yammer messages; moving on to save messages.");
- $this->state->state = 'save-messages';
- } else {
- foreach ($messages as $item) {
- $stub = Yammer_notice_stub::getKV($item['id']);
- if (!$stub) {
- 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;
- }
- private function iterateSaveMessages()
- {
- $old = clone($this->state);
- $newest = intval($this->state->messages_newest);
- $stub = new Yammer_notice_stub();
- if ($newest) {
- $stub->whereAdd('id > ' . $newest);
- }
- $stub->limit(20);
- $stub->orderBy('id');
- $stub->find();
- if ($stub->N == 0) {
- common_log(LOG_INFO, "Finished saving Yammer messages; import complete!");
- $this->state->state = 'done';
- } else {
- while ($stub->fetch()) {
- $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;
- }
-
- public function countUsers()
- {
- $map = new Yammer_user();
- return $map->count();
- }
-
- public function countGroups()
- {
- $map = new Yammer_group();
- return $map->count();
- }
-
- public function countFetchedNotices()
- {
- $map = new Yammer_notice_stub();
- return $map->count();
- }
-
- public function countSavedNotices()
- {
- $map = new Yammer_notice();
- return $map->count();
- }
-
- public function startBackgroundImport()
- {
- $qm = QueueManager::get();
- $qm->enqueue('YammerImport', 'yammer');
- }
-
- public function recordError($msg)
- {
-
- try {
- $temp = new Yammer_state();
- $temp->query('ROLLBACK');
- } catch (Exception $e) {
- common_log(LOG_ERR, 'Exception while confirming rollback while recording error: ' . $e->getMessage());
- }
- $old = clone($this->state);
- $this->state->last_error = $msg;
- $this->state->update($old);
- }
-
- public function clearError()
- {
- $this->recordError('');
- }
-
- public function lastError()
- {
- return $this->state->last_error;
- }
- }
|