Skip to content

Commit

Permalink
tweaks and other commands from other forks
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Feb 28, 2015
1 parent 616211c commit e96c38b
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 24 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# discourse-api-php

This is a composer packaged version of the PHP library for accessing the Discourse API as published by DiscourseHosting - https://github.com/discoursehosting/discourse-api-php

Changes may be added from the forks of that original source. No guarentee that this will remain stable, use with care.
This is a composer packaged version of the PHP library for accessing the Discourse API as published by DiscourseHosting
https://github.com/discoursehosting/discourse-api-php

Some changes may be added from the forks of that original source. Sorry, not done with proper merging..

Also some changes from
https://github.com/sspssp/discourse-api-php
https://github.com/kazad/discourse-api-php
https://github.com/timolaine/discourse-api-php

128 changes: 107 additions & 21 deletions src/DiscourseAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private function _getRequest($reqString, $paramArray = null, $apiUser = 'system'

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$body = curl_exec($ch);
$rc = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Expand Down Expand Up @@ -94,42 +95,40 @@ private function _putpostRequest($reqString, $paramArray, $apiUser = 'system', $
return $resObj;
}

/**
/**
* group
*
* @param string $groupname name of group
* @param string $usernames users to add to group
* @param string $groupname name of group to be created
* @param string $usernames users in the group
*
* @return mixed HTTP return code and API return object
*/

function group($groupname, $usernames = array())
{
$obj = $this->_getRequest("/admin/groups.json");
if ($obj->http_code != 200) {
$groupId = $this->getGroupIdByGroupName($groupname);
if ($groupId) {
return false;
}

foreach($obj->apiresult as $group) {
if($group->name === $groupname) {
$groupId = $group->id;
break;
}
$groupId = false;
}

$params = array(
'group' => array(
'name' => $groupname,
'usernames' => implode(',', $usernames)
'usernames' => implode(',', $usernames),
'alias_level' => '0'
)
);
return $this->_postRequest('/admin/groups', $params);
}

if($groupId) {
return $this->_putRequest('/admin/groups/' . $groupId, $params);
} else {
return $this->_postRequest('/admin/groups', $params);
}

/**
* getCategories
*
* @return mixed HTTP return code and API return object
*/

function getCategories()
{
return $this->_getRequest("/categories.json");
}

/**
Expand Down Expand Up @@ -276,6 +275,20 @@ function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId
return $this->_postRequest('/posts', $params, $userName);
}


function getCategory($categoryName) {
return $this->_getRequest("/c/{$categoryName}.json");
}

/**
* getTopic
*
*/

function getTopic($topicId) {
return $this->_getRequest("/t/{$topicId}.json");
}

/**
* createPost
*
Expand Down Expand Up @@ -307,5 +320,78 @@ function changeSiteSetting($siteSetting, $value)
$params = array($siteSetting => $value);
return $this->_putRequest('/admin/site_settings/' . $siteSetting, $params);
}

# these are mostly from timolaine

/**
* getUserByEmail
*
* @param string $email email of user
*
* @return mixed user object
*/

function getUserByEmail($email)
{
$users = $this->_getRequest("/admin/users/list/active.json");
foreach($users->apiresult as $user) {
if(strtolower($user->email) === strtolower($email)) {
return $user;
}
}

return false;
}

/*
* getGroupIdByGroupName
*
* @param string $groupname name of group
*
* @return mixed id of the group, or false if nonexistent
*/

function getGroupIdByGroupName($groupname)
{
$obj = $this->getGroups();
if ($obj->http_code != 200) {
return false;
}

foreach($obj->apiresult as $group) {
if($group->name === $groupname) {
$groupId = intval($group->id);
break;
}
$groupId = false;
}

return $groupId;
}

/**
* addUserToGroup
*
* @param string $groupname name of group
* @param string $username user to add to the group
*
* @return mixed HTTP return code and API return object
*/

function addUserToGroup($groupname, $username)
{
$groupId = $this->getGroupIdByGroupName($groupname);
if (!$groupId) {
$this->group($groupname, array($username));
} else {
$user = $this->getUserByUserName($username)->apiresult->user;
$params = array(
'group_id' => $groupId
);
return $this->_postRequest('/admin/users/' . $user->id . '/groups', $params);
}
}


}

0 comments on commit e96c38b

Please sign in to comment.