forked from xiaopanglian/icefox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment_function.php
More file actions
209 lines (180 loc) · 6.72 KB
/
comment_function.php
File metadata and controls
209 lines (180 loc) · 6.72 KB
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
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 检查评论是否与顶级评论相关(递归查找)
*
* @param int $commentId 要检查的评论ID
* @param int $topCommentId 顶级评论ID
* @param array $commentMap 评论映射表
* @return bool 是否相关
*/
function isCommentRelatedToTopComment($commentId, $topCommentId, $commentMap) {
// 如果评论不存在,返回false
if (!isset($commentMap[$commentId])) {
return false;
}
$comment = $commentMap[$commentId];
// 如果直接是顶级评论的子评论,返回true
if ($comment['parent'] == $topCommentId) {
return true;
}
// 如果没有父评论,返回false
if ($comment['parent'] == 0) {
return false;
}
// 递归检查父评论是否与顶级评论相关
return isCommentRelatedToTopComment($comment['parent'], $topCommentId, $commentMap);
}
/**
* 获取文章的最新5条顶级评论及其所有回复
*
* @param int $postId 文章ID
* @param int $limit 顶级评论数量限制
* @return array 评论数组
*/
function getPostLatestCommentsWithReplies($postId, $limit = 5) {
$db = Typecho_Db::get();
// 1. 获取最新的5条顶级评论,并 LEFT JOIN 用户表获取用户组信息
$topLevelComments = $db->fetchAll($db->select('c.*, u.`group` as userGroup')
->from('table.comments AS c')
->join('table.users AS u', 'c.authorId = u.uid', Typecho_Db::LEFT_JOIN)
->where('c.cid = ?', $postId)
->where('c.status = ?', 'approved')
->where('c.type = ?', 'comment')
->where('c.parent = ?', 0)
->order('c.created', Typecho_Db::SORT_DESC)
->limit($limit));
if (empty($topLevelComments)) {
return array();
}
// 2. 获取所有相关的子评论(按创建时间排序),并 LEFT JOIN 用户表
$allChildComments = $db->fetchAll($db->select('c.*, u.`group` as userGroup')
->from('table.comments AS c')
->join('table.users AS u', 'c.authorId = u.uid', Typecho_Db::LEFT_JOIN)
->where('c.cid = ?', $postId)
->where('c.status = ?', 'approved')
->where('c.type = ?', 'comment')
->where('c.parent > ?', 0)
->order('c.created', Typecho_Db::SORT_ASC));
// 3. 创建评论映射表(用于快速查找父评论信息)
$commentMap = array();
foreach ($topLevelComments as $comment) {
$commentMap[$comment['coid']] = $comment;
}
foreach ($allChildComments as $comment) {
$commentMap[$comment['coid']] = $comment;
}
// 4. 构建评论树
$commentTree = array();
foreach ($topLevelComments as $topComment) {
// 初始化顶级评论
$topComment['replies'] = array();
$topComment['level'] = 0;
// 查找所有与这个顶级评论相关的子评论(包括任意层级)
$relatedReplies = array();
foreach ($allChildComments as $childComment) {
// 使用递归函数检查是否与顶级评论相关
if (isCommentRelatedToTopComment($childComment['coid'], $topComment['coid'], $commentMap)) {
// 添加父评论信息
$parentComment = $commentMap[$childComment['parent']];
$childComment['parentAuthor'] = $parentComment['author'];
$childComment['parentAuthorId'] = $parentComment['authorId'] ?? '';
$childComment['parentUrl'] = $parentComment['url'] ?? '';
$childComment['parentUserGroup'] = $parentComment['userGroup'] ?? '';
$relatedReplies[] = $childComment;
}
}
// 按创建时间排序子评论
usort($relatedReplies, function($a, $b) {
return $a['created'] - $b['created'];
});
$topComment['replies'] = $relatedReplies;
$commentTree[] = $topComment;
}
return $commentTree;
}
/**
* 获取文章的最新5条顶级评论及其所有回复(简化版)
*
* @param int $postId 文章ID
* @param int $limit 顶级评论数量限制
* @return array 评论数组
*/
function getPostCommentsTree($postId, $limit = 5) {
$db = Typecho_Db::get();
// 获取顶级评论
$topComments = $db->fetchAll($db->select()
->from('table.comments')
->where('cid = ?', $postId)
->where('status = ?', 'approved')
->where('type = ?', 'comment')
->where('parent = ?', 0)
->order('created', Typecho_Db::SORT_DESC)
->limit($limit));
// 获取所有子评论
$allChildComments = $db->fetchAll($db->select()
->from('table.comments')
->where('cid = ?', $postId)
->where('status = ?', 'approved')
->where('type = ?', 'comment')
->where('parent > ?', 0)
->order('created', Typecho_Db::SORT_ASC));
// 构建评论树
$result = array();
foreach ($topComments as $topComment) {
$commentData = array(
'coid' => $topComment['coid'],
'cid' => $topComment['cid'],
'created' => $topComment['created'],
'author' => $topComment['author'],
'authorId' => $topComment['authorId'],
'mail' => $topComment['mail'],
'url' => $topComment['url'],
'text' => $topComment['text'],
'parent' => $topComment['parent'],
'level' => 0,
'replies' => array()
);
// 查找该顶级评论的所有直接回复
foreach ($allChildComments as $childComment) {
if ($childComment['parent'] == $topComment['coid']) {
$commentData['replies'][] = array(
'coid' => $childComment['coid'],
'cid' => $childComment['cid'],
'created' => $childComment['created'],
'author' => $childComment['author'],
'authorId' => $childComment['authorId'],
'mail' => $childComment['mail'],
'url' => $childComment['url'],
'text' => $childComment['text'],
'parent' => $childComment['parent'],
'level' => 1
);
}
}
$result[] = $commentData;
}
return $result;
}
/**
* 格式化评论时间
*
* @param int $timestamp 时间戳
* @return string 格式化后的时间
*/
function formatCommentTime($timestamp) {
$now = time();
$diff = $now - $timestamp;
if ($diff < 60) {
return '刚刚';
} elseif ($diff < 3600) {
return floor($diff / 60) . '分钟前';
} elseif ($diff < 86400) {
return floor($diff / 3600) . '小时前';
} elseif ($diff < 2592000) {
return floor($diff / 86400) . '天前';
} else {
return date('Y年m月d日', $timestamp);
}
}
?>