Dotclear

Changeset 1570:f06130921ca2


Ignore:
Timestamp:
08/24/13 22:36:09 (10 years ago)
Author:
Denis Jean-Chirstian <contact@…>
Branch:
2.5
Children:
1583:cbb6d70613a0, 1584:5808766f506f
Message:

Another way to fix comments count, adresses #1401

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/core/class.dc.blog.php

    r1561 r1570  
    208208     { 
    209209          $comments_ids = dcUtils::cleanIds($ids); 
    210           $affected_posts_ids = dcUtils::cleanIds($affected_posts); 
    211           $affected_is_trackback =  
    212           $counted_posts_ids =  
    213           $counted_is_trackback =  
    214           $counted_nb_comments = array(); 
    215            
    216           # a) Retrieve posts affected by comments edition 
    217           if (empty($affected_posts_ids)) { 
     210           
     211          # Get posts affected by comments edition 
     212          if (empty($affected_posts)) { 
    218213               $strReq =  
    219                     'SELECT post_id, comment_trackback '. 
     214                    'SELECT post_id '. 
    220215                    'FROM '.$this->prefix.'comment '. 
    221216                    'WHERE comment_id'.$this->con->in($comments_ids). 
    222                     'GROUP BY post_id,comment_trackback'; 
     217                    'GROUP BY post_id'; 
    223218                
    224219               $rs = $this->con->select($strReq); 
    225220                
     221               $affected_posts = array(); 
    226222               while ($rs->fetch()) { 
    227                     $affected_posts_ids[] = (integer) $rs->post_id; 
    228                     $affected_is_trackback[] = (boolean) $rs->comment_trackback; 
    229                } 
    230           } 
    231            
    232           # b) Count comments of each posts previously retrieved 
    233           # Note that this does not return posts without comment 
     223                    $affected_posts[] = (integer) $rs->post_id; 
     224               } 
     225          } 
     226           
     227          if (!is_array($affected_posts) || empty($affected_posts)) { 
     228               return; 
     229          } 
     230           
     231          # Count number of comments if exists for affected posts 
    234232          $strReq =  
    235                'SELECT post_id, COUNT(post_id) AS nb_comment,comment_trackback '. 
     233               'SELECT post_id, COUNT(post_id) AS nb_comment, comment_trackback '. 
    236234               'FROM '.$this->prefix.'comment '. 
    237235               'WHERE comment_status = 1 '. 
    238                (count($affected_posts_ids) > 0 ? 'AND post_id'.$this->con->in($affected_posts_ids) : ' '); 
    239            
    240           if ($del) { 
    241                $strReq .=  
    242                     'AND comment_id NOT'.$this->con->in($comments_ids); 
    243           } 
    244            
    245           $strReq .=  
     236               'AND post_id'.$this->con->in($affected_posts). 
    246237               'GROUP BY post_id,comment_trackback'; 
    247238           
    248239          $rs = $this->con->select($strReq); 
    249240           
     241          $posts = array(); 
    250242          while ($rs->fetch()) { 
    251                $counted_posts_ids[] = (integer) $rs->post_id; 
    252                $counted_is_trackback[] = (boolean) $rs->comment_trackback; 
    253                $counted_nb_comments[] = (integer) $rs->nb_comment; 
    254           } 
    255            
    256           # c) Update comments numbers on posts 
    257           # This compare previous requests to update also posts without comment 
     243               if ($rs->comment_trackback) { 
     244                    $posts[$rs->post_id]['trackback'] = $rs->nb_comment; 
     245               } else { 
     246                    $posts[$rs->post_id]['comment'] = $rs->nb_comment; 
     247               } 
     248          } 
     249           
     250          # Update number of comments on affected posts 
    258251          $cur = $this->con->openCursor($this->prefix.'post'); 
    259            
    260           foreach($affected_posts_ids as $affected_key => $affected_post_id) 
    261           { 
    262                if (!array_key_exists($affected_key, $affected_is_trackback)) { 
    263                     $affected_is_trackback[$affected_key] = false; 
    264                } 
    265                $nb_comment = $nb_trackback = 0; 
    266                 
    267                foreach($counted_posts_ids as $counted_key => $counted_post_id) 
    268                { 
    269                     if ($affected_post_id != $counted_post_id  
    270                     || $affected_is_trackback[$affected_key] != $counted_is_trackback[$counted_key]) { 
    271                          continue; 
    272                     } 
    273                      
    274                     if ($counted_is_trackback[$counted_key]) { 
    275                          $nb_trackback = $counted_nb_comments[$counted_key]; 
    276                     } else { 
    277                          $nb_comment = $counted_nb_comments[$counted_key]; 
    278                     } 
    279                } 
    280                 
    281                if ($affected_is_trackback[$affected_key]) { 
    282                     $cur->nb_trackback = $nb_trackback; 
     252          foreach($affected_posts as $post_id) 
     253          { 
     254               $cur->clean(); 
     255                
     256               if (!array_key_exists($post_id,$posts)) { 
     257                    $cur->nb_trackback = 0; 
     258                    $cur->nb_comment = 0; 
    283259               } else { 
    284                     $cur->nb_comment = $nb_comment; 
    285                } 
    286  
    287                $cur->update('WHERE post_id = '.$affected_post_id); 
    288                $cur->clean(); 
     260                    $cur->nb_trackback = empty($posts[$post_id]['trackback']) ? 0 : $posts[$post_id]['trackback']; 
     261                    $cur->nb_comment = empty($posts[$post_id]['comment']) ? 0 : $posts[$post_id]['comment']; 
     262               } 
     263                
     264               $cur->update('WHERE post_id = '.$post_id); 
    289265          } 
    290266     } 
     
    22542230          $affected_posts = array(); 
    22552231          $strReq = 
    2256                'SELECT distinct(post_id) '. 
     2232               'SELECT post_id '. 
    22572233               'FROM '.$this->prefix.'comment '. 
    2258                'WHERE comment_id'.$this->con->in($co_ids); 
     2234               'WHERE comment_id'.$this->con->in($co_ids). 
     2235               'GROUP BY post_id'; 
    22592236           
    22602237          $rs = $this->con->select($strReq); 
Note: See TracChangeset for help on using the changeset viewer.

Sites map