--- /root/drupal-5.2/modules/comment/comment.module	2007-09-12 21:27:42.000000000 +0900
+++ comment.module	2007-09-13 01:44:41.000000000 +0900
@@ -194,7 +194,7 @@ function comment_menu($may_cache) {
           'callback' => 'comment_reply', 'access' => node_access('view', $node), 'type' => MENU_CALLBACK);
       }
     }
-    if ((arg(0) == 'node') && is_numeric(arg(1)) && is_numeric(arg(2))) {
+    if ((arg(0) == 'node') && is_numeric(arg(1)) && (is_numeric(arg(2) || arg(2) == 'new'))) {
       $items[] = array(
         'path' => ('node/'. arg(1) .'/'. arg(2)),
         'title' => t('View'),
@@ -260,7 +260,7 @@ function comment_get_recent($number = 10
   if (!empty($nids)) {
     // From among the comments on the nodes selected in the first query,
     // find the $number most recent comments.
-    $result = db_query_range('SELECT c.nid, c.subject, c.cid, c.timestamp FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE c.nid IN ('. implode(',', $nids) .') AND n.status = 1 AND c.status = %d ORDER BY c.timestamp DESC', COMMENT_PUBLISHED, 0, $number);
+    $result = db_query_range('SELECT c.nid, c.subject, c.cid, c.timestamp, c.thread FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE c.nid IN ('. implode(',', $nids) .') AND n.status = 1 AND c.status = %d ORDER BY c.timestamp DESC', COMMENT_PUBLISHED, 0, $number); //PSS add thread
     while ($comment = db_fetch_object($result)) {
       $comments[] = $comment;
     }
@@ -277,8 +277,9 @@ function comment_get_recent($number = 10
  */
 function theme_comment_block() {
   $items = array();
+
   foreach (comment_get_recent() as $comment) {
-    $items[] = l($comment->subject, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid) .'<br />'. t('@time ago', array('@time' => format_interval(time() - $comment->timestamp)));
+    $items[] = l($comment->subject, 'node/'. $comment->nid . '/' . $comment->cid, NULL, NULL, NULL) .'<br />'. t('@time ago', array('@time' => format_interval(time() - $comment->timestamp)));
   }
   if ($items) {
     return theme('item_list', $items);
@@ -712,7 +713,7 @@ function comment_save($edit) {
         comment_invoke_comment($edit, 'update');
 
         // Add an entry to the watchdog log.
-        watchdog('content', t('Comment: updated %subject.', array('%subject' => $edit['subject'])), WATCHDOG_NOTICE, l(t('view'), 'node/'. $edit['nid'], NULL, NULL, 'comment-'. $edit['cid']));
+        watchdog('content', t('Comment: updated %subject.', array('%subject' => $edit['subject'])), WATCHDOG_NOTICE, l(t('view'), 'node/'. $edit['nid']. '/' . $edit['cid'], NULL, NULL, NULL));
       }
       else {
         // Check for duplicate comments. Note that we have to use the
@@ -792,7 +793,7 @@ function comment_save($edit) {
         comment_invoke_comment($edit, 'insert');
 
         // Add an entry to the watchdog log.
-        watchdog('content', t('Comment: added %subject.', array('%subject' => $edit['subject'])), WATCHDOG_NOTICE, l(t('view'), 'node/'. $edit['nid'], NULL, NULL, 'comment-'. $edit['cid']));
+        watchdog('content', t('Comment: added %subject.', array('%subject' => $edit['subject'])), WATCHDOG_NOTICE, l(t('view'), 'node/'. $edit['nid']. '/' . $edit['cid'], NULL, NULL, NULL));
       }
 
       // Clear the cache so an anonymous user can see his comment being added.
@@ -945,26 +946,70 @@ function comment_render($node, $cid = 0)
     $comments_per_page = _comment_get_display_setting('comments_per_page');
 
     if ($cid) {
-      // Single comment view.
-      $query = 'SELECT c.cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.picture, u.data, c.score, c.users, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d';
-      $query_args = array($cid);
-      if (!user_access('administer comments')) {
-        $query .= ' AND c.status = %d';
-        $query_args[] = COMMENT_PUBLISHED;
+      if ($cid == 'new') {
+        $timestamp = node_last_viewed($nid);
+        $timestamp = ($timestamp > NODE_NEW_LIMIT ? $timestamp : NODE_NEW_LIMIT);
+        $query = 'SELECT c.cid, c.pid, c.nid, c.timestamp, c.thread FROM {comments} c  WHERE c.nid = %d AND c.timestamp > %d ';
+        $query_args = array($nid);
+        $query_args[] = $timestamp;
+        if (!user_access('administer comments')) {
+          $query .= ' AND c.status = %d';
+          $query_args[] = COMMENT_PUBLISHED;
+        }
+        $query .= ' ORDER BY c.timestamp ASC';
+      } else {
+        // Single comment view.
+        $query = 'SELECT c.cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.picture, u.data, c.score, c.users, c.status, c.thread FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d';
+        $query_args = array($cid);
+        if (!user_access('administer comments')) {
+          $query .= ' AND c.status = %d';
+          $query_args[] = COMMENT_PUBLISHED;
+        }
       }
 
       $result = db_query($query, $query_args);
 
-      if ($comment = db_fetch_object($result)) {
-        $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
-        $links = module_invoke_all('link', 'comment', $comment, 1);
+      if ($comment = db_fetch_object($result)) {       
+        if(($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_THREAD_COLLAPSED) && $cid != 'new') {
+          $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
+          $links = module_invoke_all('link', 'comment', $comment, 1);
 
-        foreach (module_implements('link_alter') as $module) {
-          $function = $module .'_link_alter';
-          $function($node, $links);
-        }
+          foreach (module_implements('link_alter') as $module) {
+            $function = $module .'_link_alter';
+            $function($node, $links);
+          }
+
+          $output .= theme('comment_view', $comment, $links);
+        } 
+        else {
+          //PSS modified. Not single comment page, but redirect to comment  
+          if ($order == COMMENT_ORDER_NEWEST_FIRST) {
+            if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
+              $sqland = ' AND c.timestamp > %d' ;
+            }
+            else {
+              $sqland = ' AND c.thread > "%s"' ;
+            }
+          }
+          else if ($order == COMMENT_ORDER_OLDEST_FIRST) {
+            if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {
+              $sqland = ' AND c.timestamp < %d' ;          }
+            else {
+              $sqland = ' AND SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1)) < SUBSTRING("%s", 1, (LENGTH("%s") - 1))';
+            }
+          }
+          if ($mode == COMMENT_MODE_FLAT_COLLAPSED || $mode == COMMENT_MODE_FLAT_EXPANDED) {      
+            $commentresult = db_query('SELECT count(*) as cnt FROM {comments} c WHERE c.nid=' . $comment->nid . $sqland, $comment->timestamp );
+          }
+          else {
+            $commentresult = db_query('SELECT count(*) as cnt FROM {comments} c WHERE c.nid=' . $comment->nid . $sqland, $comment->thread, $comment->thread);
+          }
 
-        $output .= theme('comment_view', $comment, $links);
+          $commentcnt = db_fetch_object($commentresult);
+          $page = floor($commentcnt->cnt / _comment_get_display_setting('comments_per_page'));
+          $query = ($page > 0)?('page=' . $page) : '';
+          drupal_goto('node/' . $comment->nid, $query , 'comment-' . $comment->cid);
+        }
       }
     }
     else {
@@ -1187,7 +1232,8 @@ function comment_admin_overview($type = 
   while ($comment = db_fetch_object($result)) {
     $comments[$comment->cid] = '';
     $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
-    $form['subject'][$comment->cid] = array('#value' => l($comment->subject, 'node/'. $comment->nid, array('title' => truncate_utf8($comment->comment, 128)), NULL, 'comment-'. $comment->cid));
+    $form['subject'][$comment->cid] = array('#value' => l($comment->subject, 'node/'. $comment->nid . '/' . $comment->cid, array('title' => truncate_utf8($comment->comment, 128)), NULL, NULL));
+  
     $form['username'][$comment->cid] = array('#value' => theme('username', $comment));
     $form['timestamp'][$comment->cid] = array('#value' => format_date($comment->timestamp, 'small'));
     $form['operations'][$comment->cid] = array('#value' => l(t('edit'), 'comment/edit/'. $comment->cid, array(), $destination));
