2010-09-22 05:56:30 +09:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* StatusNet - the distributed open-source microblogging tool
|
|
|
|
* Copyright (C) 2010, StatusNet, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Basic client class for Yammer's OAuth/JSON API.
|
|
|
|
*
|
|
|
|
* @package YammerImportPlugin
|
|
|
|
* @author Brion Vibber <brion@status.net>
|
|
|
|
*/
|
|
|
|
class YammerImporter
|
|
|
|
{
|
2010-09-22 06:56:20 +09:00
|
|
|
|
2010-09-22 07:24:14 +09:00
|
|
|
protected $users=array();
|
|
|
|
protected $groups=array();
|
|
|
|
protected $notices=array();
|
|
|
|
|
2010-09-22 06:56:20 +09:00
|
|
|
/**
|
|
|
|
* Load or create an imported profile from Yammer data.
|
|
|
|
*
|
|
|
|
* @param object $item loaded JSON data for Yammer importer
|
|
|
|
* @return Profile
|
|
|
|
*/
|
2010-09-22 07:24:14 +09:00
|
|
|
function importUser($item)
|
2010-09-22 06:56:20 +09:00
|
|
|
{
|
|
|
|
$data = $this->prepUser($item);
|
|
|
|
|
2010-09-22 07:24:14 +09:00
|
|
|
$profileId = $this->findImportedUser($data['orig_id']);
|
2010-09-22 06:56:20 +09:00
|
|
|
if ($profileId) {
|
|
|
|
return Profile::staticGet('id', $profileId);
|
|
|
|
} else {
|
|
|
|
$user = User::register($data['options']);
|
|
|
|
// @fixme set avatar!
|
2010-09-22 07:24:14 +09:00
|
|
|
$this->recordImportedUser($data['orig_id'], $user->id);
|
2010-09-22 06:56:20 +09:00
|
|
|
return $user->getProfile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load or create an imported group from Yammer data.
|
|
|
|
*
|
|
|
|
* @param object $item loaded JSON data for Yammer importer
|
|
|
|
* @return User_group
|
|
|
|
*/
|
|
|
|
function importGroup($item)
|
|
|
|
{
|
|
|
|
$data = $this->prepGroup($item);
|
|
|
|
|
|
|
|
$groupId = $this->findImportedGroup($data['orig_id']);
|
|
|
|
if ($groupId) {
|
|
|
|
return User_group::staticGet('id', $groupId);
|
|
|
|
} else {
|
|
|
|
$group = User_group::register($data['options']);
|
|
|
|
// @fixme set avatar!
|
2010-09-22 07:24:14 +09:00
|
|
|
$this->recordImportedGroup($data['orig_id'], $group->id);
|
2010-09-22 06:56:20 +09:00
|
|
|
return $group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load or create an imported notice from Yammer data.
|
|
|
|
*
|
|
|
|
* @param object $item loaded JSON data for Yammer importer
|
|
|
|
* @return Notice
|
|
|
|
*/
|
|
|
|
function importNotice($item)
|
|
|
|
{
|
|
|
|
$data = $this->prepNotice($item);
|
|
|
|
|
|
|
|
$noticeId = $this->findImportedNotice($data['orig_id']);
|
|
|
|
if ($noticeId) {
|
|
|
|
return Notice::staticGet('id', $noticeId);
|
|
|
|
} else {
|
|
|
|
$notice = Notice::saveNew($data['profile'],
|
|
|
|
$data['content'],
|
|
|
|
$data['source'],
|
|
|
|
$data['options']);
|
|
|
|
// @fixme attachments?
|
2010-09-22 07:24:14 +09:00
|
|
|
$this->recordImportedNotice($data['orig_id'], $notice->id);
|
2010-09-22 06:56:20 +09:00
|
|
|
return $notice;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-22 07:24:14 +09:00
|
|
|
/**
|
|
|
|
* Pull relevant info out of a Yammer data record for a user import.
|
|
|
|
*
|
|
|
|
* @param array $item
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-09-22 06:56:20 +09:00
|
|
|
function prepUser($item)
|
|
|
|
{
|
|
|
|
if ($item['type'] != 'user') {
|
|
|
|
throw new Exception('Wrong item type sent to Yammer user import processing.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$origId = $item['id'];
|
|
|
|
$origUrl = $item['url'];
|
|
|
|
|
|
|
|
// @fixme check username rules?
|
|
|
|
|
|
|
|
$options['nickname'] = $item['name'];
|
|
|
|
$options['fullname'] = trim($item['full_name']);
|
|
|
|
|
|
|
|
// We don't appear to have full bio avail here!
|
|
|
|
$options['bio'] = $item['job_title'];
|
|
|
|
|
|
|
|
// What about other data like emails?
|
|
|
|
|
|
|
|
// Avatar... this will be the "_small" variant.
|
|
|
|
// Remove that (pre-extension) suffix to get the orig-size image.
|
|
|
|
$avatar = $item['mugshot_url'];
|
|
|
|
|
|
|
|
// Warning: we don't have following state for other users?
|
|
|
|
|
|
|
|
return array('orig_id' => $origId,
|
|
|
|
'orig_url' => $origUrl,
|
|
|
|
'avatar' => $avatar,
|
|
|
|
'options' => $options);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-09-22 07:24:14 +09:00
|
|
|
/**
|
|
|
|
* Pull relevant info out of a Yammer data record for a group import.
|
|
|
|
*
|
|
|
|
* @param array $item
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-09-22 06:56:20 +09:00
|
|
|
function prepGroup($item)
|
|
|
|
{
|
|
|
|
if ($item['type'] != 'group') {
|
|
|
|
throw new Exception('Wrong item type sent to Yammer group import processing.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$origId = $item['id'];
|
|
|
|
$origUrl = $item['url'];
|
|
|
|
|
|
|
|
$privacy = $item['privacy']; // Warning! only public groups in SN so far
|
|
|
|
|
|
|
|
$options['nickname'] = $item['name'];
|
|
|
|
$options['fullname'] = $item['full_name'];
|
|
|
|
$options['created'] = $this->timestamp($item['created_at']);
|
|
|
|
|
|
|
|
$avatar = $item['mugshot_url']; // as with user profiles...
|
|
|
|
|
|
|
|
|
|
|
|
$options['mainpage'] = common_local_url('showgroup',
|
|
|
|
array('nickname' => $options['nickname']));
|
|
|
|
|
|
|
|
// @fixme what about admin user for the group?
|
|
|
|
// bio? homepage etc? aliases?
|
|
|
|
|
|
|
|
$options['local'] = true;
|
|
|
|
return array('orig_id' => $origId,
|
|
|
|
'orig_url' => $origUrl,
|
|
|
|
'options' => $options);
|
|
|
|
}
|
|
|
|
|
2010-09-22 07:24:14 +09:00
|
|
|
/**
|
|
|
|
* Pull relevant info out of a Yammer data record for a notice import.
|
|
|
|
*
|
|
|
|
* @param array $item
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-09-22 06:56:20 +09:00
|
|
|
function prepNotice($item)
|
2010-09-22 05:56:30 +09:00
|
|
|
{
|
2010-09-22 06:56:20 +09:00
|
|
|
if (isset($item['type']) && $item['type'] != 'message') {
|
|
|
|
throw new Exception('Wrong item type sent to Yammer message import processing.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$origId = $item['id'];
|
|
|
|
$origUrl = $item['url'];
|
2010-09-22 05:56:30 +09:00
|
|
|
|
2010-09-22 07:24:14 +09:00
|
|
|
$profile = $this->findImportedUser($item['sender_id']);
|
2010-09-22 06:56:20 +09:00
|
|
|
$content = $item['body']['plain'];
|
2010-09-22 05:56:30 +09:00
|
|
|
$source = 'yammer';
|
|
|
|
$options = array();
|
|
|
|
|
2010-09-22 06:56:20 +09:00
|
|
|
if ($item['replied_to_id']) {
|
|
|
|
$replyto = $this->findImportedNotice($item['replied_to_id']);
|
2010-09-22 05:56:30 +09:00
|
|
|
if ($replyto) {
|
|
|
|
$options['replyto'] = $replyto;
|
|
|
|
}
|
|
|
|
}
|
2010-09-22 06:56:20 +09:00
|
|
|
$options['created'] = $this->timestamp($item['created_at']);
|
2010-09-22 05:56:30 +09:00
|
|
|
|
|
|
|
// Parse/save rendered text?
|
|
|
|
// Save liked info?
|
|
|
|
// @todo attachments?
|
|
|
|
|
2010-09-22 06:56:20 +09:00
|
|
|
return array('orig_id' => $origId,
|
|
|
|
'orig_url' => $origUrl,
|
2010-09-22 05:56:30 +09:00
|
|
|
'profile' => $profile,
|
|
|
|
'content' => $content,
|
|
|
|
'source' => $source,
|
|
|
|
'options' => $options);
|
|
|
|
}
|
|
|
|
|
2010-09-22 07:24:14 +09:00
|
|
|
private function findImportedUser($origId)
|
|
|
|
{
|
|
|
|
if (isset($this->users[$origId])) {
|
|
|
|
return $this->users[$origId];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function findImportedGroup($origId)
|
|
|
|
{
|
|
|
|
if (isset($this->groups[$origId])) {
|
|
|
|
return $this->groups[$origId];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function findImportedNotice($origId)
|
|
|
|
{
|
|
|
|
if (isset($this->notices[$origId])) {
|
|
|
|
return $this->notices[$origId];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function recordImportedUser($origId, $userId)
|
2010-09-22 05:56:30 +09:00
|
|
|
{
|
2010-09-22 07:24:14 +09:00
|
|
|
$this->users[$origId] = $userId;
|
2010-09-22 05:56:30 +09:00
|
|
|
}
|
|
|
|
|
2010-09-22 07:24:14 +09:00
|
|
|
private function recordImportedGroup($origId, $groupId)
|
2010-09-22 06:56:20 +09:00
|
|
|
{
|
2010-09-22 07:24:14 +09:00
|
|
|
$this->groups[$origId] = $groupId;
|
2010-09-22 06:56:20 +09:00
|
|
|
}
|
|
|
|
|
2010-09-22 07:24:14 +09:00
|
|
|
private function recordImportedNotice($origId, $noticeId)
|
2010-09-22 05:56:30 +09:00
|
|
|
{
|
2010-09-22 07:24:14 +09:00
|
|
|
$this->notices[$origId] = $noticeId;
|
2010-09-22 05:56:30 +09:00
|
|
|
}
|
2010-09-22 06:56:20 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize timestamp format.
|
|
|
|
* @param string $ts
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-09-22 07:24:14 +09:00
|
|
|
private function timestamp($ts)
|
2010-09-22 06:56:20 +09:00
|
|
|
{
|
|
|
|
return common_sql_date(strtotime($ts));
|
|
|
|
}
|
|
|
|
}
|