Skip to content

Commit e96c38b

Browse files
author
root
committed
tweaks and other commands from other forks
1 parent 616211c commit e96c38b

File tree

2 files changed

+114
-24
lines changed

2 files changed

+114
-24
lines changed

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# discourse-api-php
22

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

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

8+
Also some changes from
9+
https://github.com/sspssp/discourse-api-php
10+
https://github.com/kazad/discourse-api-php
11+
https://github.com/timolaine/discourse-api-php
812

src/DiscourseAPI.php

+107-21
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ private function _getRequest($reqString, $paramArray = null, $apiUser = 'system'
4747

4848
curl_setopt($ch, CURLOPT_URL, $url);
4949
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
50+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
5051
$body = curl_exec($ch);
5152
$rc = curl_getinfo($ch, CURLINFO_HTTP_CODE);
5253
curl_close($ch);
@@ -94,42 +95,40 @@ private function _putpostRequest($reqString, $paramArray, $apiUser = 'system', $
9495
return $resObj;
9596
}
9697

97-
/**
98+
/**
9899
* group
99100
*
100-
* @param string $groupname name of group
101-
* @param string $usernames users to add to group
101+
* @param string $groupname name of group to be created
102+
* @param string $usernames users in the group
102103
*
103104
* @return mixed HTTP return code and API return object
104105
*/
105-
106106
function group($groupname, $usernames = array())
107107
{
108-
$obj = $this->_getRequest("/admin/groups.json");
109-
if ($obj->http_code != 200) {
108+
$groupId = $this->getGroupIdByGroupName($groupname);
109+
if ($groupId) {
110110
return false;
111111
}
112-
113-
foreach($obj->apiresult as $group) {
114-
if($group->name === $groupname) {
115-
$groupId = $group->id;
116-
break;
117-
}
118-
$groupId = false;
119-
}
120-
121112
$params = array(
122113
'group' => array(
123114
'name' => $groupname,
124-
'usernames' => implode(',', $usernames)
115+
'usernames' => implode(',', $usernames),
116+
'alias_level' => '0'
125117
)
126118
);
119+
return $this->_postRequest('/admin/groups', $params);
120+
}
127121

128-
if($groupId) {
129-
return $this->_putRequest('/admin/groups/' . $groupId, $params);
130-
} else {
131-
return $this->_postRequest('/admin/groups', $params);
132-
}
122+
123+
/**
124+
* getCategories
125+
*
126+
* @return mixed HTTP return code and API return object
127+
*/
128+
129+
function getCategories()
130+
{
131+
return $this->_getRequest("/categories.json");
133132
}
134133

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

278+
279+
function getCategory($categoryName) {
280+
return $this->_getRequest("/c/{$categoryName}.json");
281+
}
282+
283+
/**
284+
* getTopic
285+
*
286+
*/
287+
288+
function getTopic($topicId) {
289+
return $this->_getRequest("/t/{$topicId}.json");
290+
}
291+
279292
/**
280293
* createPost
281294
*
@@ -307,5 +320,78 @@ function changeSiteSetting($siteSetting, $value)
307320
$params = array($siteSetting => $value);
308321
return $this->_putRequest('/admin/site_settings/' . $siteSetting, $params);
309322
}
323+
324+
# these are mostly from timolaine
325+
326+
/**
327+
* getUserByEmail
328+
*
329+
* @param string $email email of user
330+
*
331+
* @return mixed user object
332+
*/
333+
334+
function getUserByEmail($email)
335+
{
336+
$users = $this->_getRequest("/admin/users/list/active.json");
337+
foreach($users->apiresult as $user) {
338+
if(strtolower($user->email) === strtolower($email)) {
339+
return $user;
340+
}
341+
}
342+
343+
return false;
344+
}
345+
346+
/*
347+
* getGroupIdByGroupName
348+
*
349+
* @param string $groupname name of group
350+
*
351+
* @return mixed id of the group, or false if nonexistent
352+
*/
353+
354+
function getGroupIdByGroupName($groupname)
355+
{
356+
$obj = $this->getGroups();
357+
if ($obj->http_code != 200) {
358+
return false;
359+
}
360+
361+
foreach($obj->apiresult as $group) {
362+
if($group->name === $groupname) {
363+
$groupId = intval($group->id);
364+
break;
365+
}
366+
$groupId = false;
367+
}
368+
369+
return $groupId;
370+
}
371+
372+
/**
373+
* addUserToGroup
374+
*
375+
* @param string $groupname name of group
376+
* @param string $username user to add to the group
377+
*
378+
* @return mixed HTTP return code and API return object
379+
*/
380+
381+
function addUserToGroup($groupname, $username)
382+
{
383+
$groupId = $this->getGroupIdByGroupName($groupname);
384+
if (!$groupId) {
385+
$this->group($groupname, array($username));
386+
} else {
387+
$user = $this->getUserByUserName($username)->apiresult->user;
388+
$params = array(
389+
'group_id' => $groupId
390+
);
391+
return $this->_postRequest('/admin/users/' . $user->id . '/groups', $params);
392+
}
393+
}
394+
395+
310396
}
311397

0 commit comments

Comments
 (0)