/
EDisqusComments.php
251 lines (226 loc) · 7.87 KB
/
EDisqusComments.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
/**
* Class EDisqusComments
* @property string $apiKey
* @property string $shortName
* @property integer $commentsLimit
* @property bool $autoUpdateMap
*/
class EDisqusComments extends CApplicationComponent
{
/* Count of comments that we want to receive from API for one request. max = 100*/
public $commentsLimit = 100;
/* The API Key that you can receive by registering your application on https://disqus.com/api/applications/ */
public $apiKey;
/* The Disqus shortname of your site. Register it on https://disqus.com/admin/create/ */
public $shortName;
/* true if you want to create url map automatically by using the widget. false otherwise */
public $autoUpdateMap = true;
/* Id of cache component you want to use */
public $cacheId = 'cache';
/* Duration of caching in seconds */
public $cacheDuration = 3600;
/* Duration of query caching in seconds */
public $queryCacheDuration = 300;
public function init()
{
Yii::setPathOfAlias('disqusComments', dirname(__FILE__));
Yii::import('disqusComments.models.*');
Yii::import('disqusComments.helpers.*');
Yii::import('disqusComments.actions.*');
parent::init();
}
/**
* Getting comments from Disqus API for website page using cursor for results pagination
* @param string $pageUrl
* @param string|null $cursor
* @return stdClass[]
*/
public function loadCommentsByUrl($pageUrl, $cursor = null)
{
$disqusApiUrl = 'https://disqus.com/api/3.0/threads/listPosts.json';
$disqusApiUrl .= '?api_key=' . $this->apiKey;
$disqusApiUrl .= '&forum=' . $this->shortName;
$disqusApiUrl .= '&limit=' . $this->commentsLimit;
$disqusApiUrl .= '&order=desc';
$disqusApiUrl .= '&thread:link=' . $pageUrl;
if (isset($cursor)) {
$disqusApiUrl .= '&cursor=' . $cursor;
}
$decodedData = self::sendRequest($disqusApiUrl);
$result = $decodedData->response;
if ($decodedData->cursor->hasNext) {
$result = array_merge($result, $this->loadCommentsByUrl($pageUrl, $decodedData->cursor->next));
}
return $result;
}
/**
* Get from Disqus API all your thread URLs
* @param string|null $cursor
* @return string[]
*/
public function loadUrls($cursor = null)
{
$disqusApiUrl = 'https://disqus.com/api/3.0/threads/list.json';
$disqusApiUrl .= '?api_key=' . $this->apiKey;
$disqusApiUrl .= '&forum=' . $this->shortName;
$disqusApiUrl .= '&limit=' . $this->commentsLimit;
if (isset($cursor)) {
$disqusApiUrl .= '&cursor=' . $cursor;
}
$data = self::sendRequest($disqusApiUrl);
$result = array();
if (is_array($data->response) && !empty($data->response)) {
foreach ($data->response as $threadFromApi) {
$result[] = $threadFromApi->link;
}
}
if ($data->cursor->hasNext) {
$result = array_merge($result, $this->loadUrls($data->cursor->next));
}
return $result;
}
/**
* Get from Disqus API the threads having new comments in recent interval
* @param string $interval
* @param string|null $cursor
* @return string[]
*/
public function loadRecentThreads($interval, $cursor = null)
{
$disqusApiUrl = 'https://disqus.com/api/3.0/threads/listPopular.json';
$disqusApiUrl .= '?api_key=' . $this->apiKey;
$disqusApiUrl .= '&forum=' . $this->shortName;
$disqusApiUrl .= '&limit=' . $this->commentsLimit;
$disqusApiUrl .= '&interval=' . $interval;
if (isset($cursor)) {
$disqusApiUrl .= '&cursor=' . $cursor;
}
$data = self::sendRequest($disqusApiUrl);
$result = array();
if (is_array($data->response) && !empty($data->response)) {
foreach ($data->response as $threadFromApi) {
$result[] = $threadFromApi->link;
}
}
if ($data->cursor->hasNext) {
$result = array_merge($result, $this->loadRecentThreads($interval, $data->cursor->next));
}
return $result;
}
/**
* Get stored value from cache
* @param string $id
* @return bool|mixed
*/
public function getCache($id)
{
$value = false;
if ($this->cacheId !== false && ($cache = Yii::app()->getComponent($this->cacheId)) !== null) {
/** @var CCache $cache */
$value = $cache->get($id);
}
return $value;
}
/**
* Set value to cache
* @param string $id
* @param mixed $value
*/
public function setCache($id, $value)
{
if ($this->cacheId !== false && ($cache = Yii::app()->getComponent($this->cacheId)) !== null) {
/** @var CCache $cache */
$dependency = new \CGlobalStateCacheDependency('DisqusComments');
$cache->set($id, $value, $this->cacheDuration, $dependency);
}
}
/**
* Generates HTML code for comments block from Disqus API request results
* @param stdClass[] $comments
* @return string
*/
public static function createCommentsHTML($comments)
{
$commentsHTML = '';
if (is_array($comments)) {
$commentsHTML = CHtml::openTag('ul');
foreach ($comments as $comment) {
$commentsHTML .= CHtml::openTag('li');
$commentsHTML .= CHtml::tag('span', array(), $comment->author);
$commentsHTML .= $comment->text;
$commentsHTML .= CHtml::closeTag('li');
}
$commentsHTML .= CHtml::closeTag('ul');
}
return $commentsHTML;
}
public static function createCommentsJSON($comments)
{
$commentsForJSON = array();
foreach ($comments as $comment) {
$commentsForJSON[] = array(
'author' => $comment->author,
'date' => $comment->date,
'text' => $comment->text,
);
}
return json_encode($commentsForJSON);
}
/**
* @param array $comments
* @param integer $parentId
* @return array
*/
public static function sortCommentsByHierarchy($comments, $parentId = null)
{
$sortedComments = array();
foreach ($comments as $comment) {
if ($comment->parent == $parentId) {
$sortedComments[] = $comment;
$sortedComments = array_merge($sortedComments, self::sortCommentsByHierarchy($comments, (integer)$comment->id));
}
}
return $sortedComments;
}
/**
* Format comment objects for storing in DB
* @param stdClass[] $commentsFromApi
* @return stdClass[]
*/
public static function formatComments($commentsFromApi)
{
$comments = array();
foreach ($commentsFromApi as $commentFromApi) {
$comment = new stdClass();
$comment->author = $commentFromApi->author->username;
$comment->date = self::formatDate($commentFromApi->createdAt);
$comment->text = $commentFromApi->message;
$comment->id = $commentFromApi->id;
$comment->parent = $commentFromApi->parent;
$comments[] = $comment;
}
return $comments;
}
/**
* @param $date
* @return bool|string
*/
public static function formatDate($date)
{
return date_format(date_create($date), 'Y-m-d H:i:s');
}
/**
* Send CUrl request and return decoded data
* @param string $url
* @return stdClass
*/
protected static function sendRequest($url)
{
$connection = curl_init($url);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($connection);
curl_close($connection);
return json_decode($data);
}
}