Revision: 1387
Author: magike.net
Date: Mon Mar 15 23:27:51 2010
Log: fix issue 382
修正评论翻页打断的问题,建议侧边栏的最近评论显示少一点,递归查询的开销比较大
我们会在近期的版本中考虑缓存的加入
http://code.google.com/p/typecho/source/detail?r=1387
Modified:
/trunk/var/Widget/Abstract/Comments.php
/trunk/var/Widget/Comments/Admin.php
/trunk/var/Widget/Comments/Archive.php
/trunk/var/Widget/Comments/Recent.php
=======================================
--- /trunk/var/Widget/Abstract/Comments.php Sun Mar 7 06:52:54 2010
+++ /trunk/var/Widget/Abstract/Comments.php Mon Mar 15 23:27:51 2010
@@ -51,23 +51,34 @@
{
if ($this->options->commentsPageBreak) {
-
- $select =$this->db->select(array('COUNT(coid)' => 'num'))
- ->from('table.comments')->where('cid = ? AND status = ?',
$this->parentContent['cid'], 'approved')
- ->where('coid ' . ('DESC' ==
$this->options->commentsOrder ? '>=' : '<=') . ' ?', $this->coid);
+
+ $coid = $this->coid;
+ $parent = $this->parent;
+
+ while ($parent > 0) {
+ $coid = $parent;
+ $parent =
$this->db->fetchObject($this->db->select('parent')->from('table.comments')
+ ->where('coid = ? AND status = ?',
$parent, 'approved'))->parent;
+ }
+
+
+ $select = $this->db->select(array('COUNT(coid)' => 'num'))
+ ->from('table.comments')->where('cid = ? AND parent = ? AND
status = ?',
+ $this->parentContent['cid'], 0, 'approved')
+ ->where('coid ' . ('DESC' ==
$this->options->commentsOrder ? '>=' : '<=') . ' ?', $coid);
if ($this->options->commentsShowCommentOnly) {
$select->where('type = ?', 'comment');
}
$currentPage = ceil($this->db->fetchObject($select)->num /
$this->options->commentsPageSize);
+
$pageRow = array('permalink' =>
$this->parentContent['pathinfo'], 'commentPage' => $currentPage);
-
return Typecho_Router::url('comment_page',
$pageRow, $this->options->index) . '#' .
$this->theId;
- } else {
- return $this->parentContent['permalink'] . '#' . $this->theId;
- }
+ }
+
+ return $this->parentContent['permalink'] . '#' . $this->theId;
}
/**
=======================================
--- /trunk/var/Widget/Comments/Admin.php Sun Feb 7 05:30:09 2010
+++ /trunk/var/Widget/Comments/Admin.php Mon Mar 15 23:27:51 2010
@@ -117,7 +117,7 @@
$this->_countSql = clone $select;
- $select->order('table.comments.created', Typecho_Db::SORT_DESC)
+ $select->order('table.comments.coid', Typecho_Db::SORT_DESC)
->page($this->_currentPage, $this->parameter->pageSize);
$this->db->fetchAll($select, array($this, 'push'));
=======================================
--- /trunk/var/Widget/Comments/Archive.php Mon Mar 15 02:35:30 2010
+++ /trunk/var/Widget/Comments/Archive.php Mon Mar 15 23:27:51 2010
@@ -66,6 +66,14 @@
* @var mixed
*/
private $_customThreadedCommentsCallback = false;
+
+ /**
+ * 第一条评论的id
+ *
+ * @access private
+ * @var integer
+ */
+ private $_firstCommentId = 0;
/**
* 构造函数,初始化组件
@@ -135,15 +143,35 @@
$singleCommentOptions->afterDate(); ?></a>
</div>
<?php $this->content(); ?>
+ <?php if ($this->children) { ?>
<div class="comment-children">
<?php $this->threadedComments($before, $after,
$singleCommentOptions); ?>
</div>
+ <?php } ?>
<div class="comment-reply">
<?php $this->reply($singleCommentOptions->replyWord); ?>
</div>
</li>
<?php
}
+
+ /**
+ * 获取当前评论链接
+ *
+ * @access protected
+ * @return string
+ */
+ protected function ___permalink()
+ {
+
+ if ($this->options->commentsPageBreak) {
+ $pageRow = array('permalink' =>
$this->parentContent['pathinfo'], 'commentPage' => $this->_currentPage);
+ return Typecho_Router::url('comment_page',
+ $pageRow, $this->options->index) . '#' .
$this->theId;
+ }
+
+ return $this->parentContent['permalink'] . '#' . $this->theId;
+ }
/**
* 子评论
@@ -153,22 +181,8 @@
*/
protected function ___children()
{
- $result = array();
-
- if (isset($this->_threadedComments[$this->coid])) {
- //深度清零
- if (!$this->parent) {
- $this->_deep = 0;
- }
-
- $threadedComments = $this->_threadedComments[$this->coid];
- foreach ($threadedComments as $coid) {
- $result[] = $this->stack[$coid];
- unset($this->stack[$coid]);
- }
- }
-
- return $result;
+ return $this->options->commentsThreaded && !$this->isTopLevel &&
isset($this->_threadedComments[$this->coid])
+ ? $this->_threadedComments[$this->coid] : array();
}
/**
@@ -203,34 +217,6 @@
{
return $this->parameter->parentContent;
}
-
- /**
- * 返回堆栈每一行的值
- *
- * @return array
- */
- public function next()
- {
- if ($this->stack) {
-
- // fix issue 379
- do {
- $this->row = &$this->stack[key($this->stack)];
- next($this->stack);
- } while ($this->options->commentsThreaded && $this->row
- && 0 != $this->row['parent'] &&
isset($this->stack[$this->row['parent']]));
-
- $this->sequence ++;
- }
-
- if (!$this->row) {
- reset($this->stack);
- $this->sequence = 0;
- return false;
- }
-
- return $this->row;
- }
/**
* 输出文章评论数
@@ -270,10 +256,16 @@
$select = $this->select()->where('table.comments.status
= ?', 'approved')
->where('table.comments.cid = ?', $this->parameter->parentId);
-
+ $threadedSelect = NULL;
+
if ($this->options->commentsShowCommentOnly) {
$select->where('table.comments.type = ?', 'comment');
}
+
+ if ($this->options->commentsPageBreak) {
+ $threadedSelect = clone $select;
+ $select->where('table.comments.parent = ?', 0);
+ }
$this->_countSql = clone $select;
@@ -289,8 +281,20 @@
$select->page($this->_currentPage,
$this->options->commentsPageSize);
}
- $select->order('table.comments.created',
$this->options->commentsOrder);
+ $select->order('table.comments.coid',
$this->options->commentsOrder);
$this->db->fetchAll($select, array($this, 'push'));
+
+ if ($threadedSelect) {
+ $threadedSelect->where('table.comments.parent <> ? AND
table.comments.coid '
+ . ('DESC' == $this->options->commentsOrder ? '<' : '>')
+ . ' ?', 0, $this->_firstCommentId)
+ ->order('table.comments.coid', $this->options->commentsOrder);
+ $threadedComments = $this->db->fetchAll($threadedSelect,
array($this, 'filter'));
+
+ foreach ($threadedComments as $comment) {
+
$this->_threadedComments[$comment['parent']][$comment['coid']] = $comment;
+ }
+ }
}
/**
@@ -305,8 +309,8 @@
$value = $this->filter($value);
//存储子父级关系
- if ($value['parent']) {
- $this->_threadedComments[$value['parent']][] = $value['coid'];
+ if (0 == $this->_firstCommentId) {
+ $this->_firstCommentId = $value['coid'];
}
//将行数据按顺序置位
@@ -355,10 +359,6 @@
*/
public function threadedComments($before = '', $after = '',
$singleCommentOptions = NULL)
{
- if (!$this->options->commentsThreaded || $this->isTopLevel) {
- return;
- }
-
$children = $this->children;
if ($children) {
//缓存变量便于还原
=======================================
--- /trunk/var/Widget/Comments/Recent.php Sun Feb 7 05:30:09 2010
+++ /trunk/var/Widget/Comments/Recent.php Mon Mar 15 23:27:51 2010
@@ -42,7 +42,7 @@
{
$select = $this->select()->limit($this->parameter->pageSize)
->where('table.comments.status = ?', 'approved')
- ->order('table.comments.created', Typecho_Db::SORT_DESC);
+ ->order('table.comments.coid', Typecho_Db::SORT_DESC);
if ($this->parameter->parentId) {
$select->where('cid = ?', $this->parameter->parentId);
_______________________________________________
announce mailing list
[email protected]
http://lists.typecho.org/mailman/listinfo/announce