Dotclear


Ignore:
Timestamp:
11/26/12 00:57:48 (13 years ago)
Author:
JcDenis
Branch:
default
Message:

Enhance mass use of triggerComment() by reducing SQL queries, addresses #943

File:
1 edited

Legend:

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

    r1041 r1042  
    194194     public function triggerComment($id,$del=false) 
    195195     { 
    196           $id = (integer) $id; 
    197            
    198           $strReq = 'SELECT post_id, comment_trackback '. 
    199                     'FROM '.$this->prefix.'comment '. 
    200                     'WHERE comment_id = '.$id.' '; 
     196          $this->triggerComments($id,$del); 
     197     } 
     198      
     199     /** 
     200     Updates comments and trackbacks counters in post table. Should be called 
     201     every time comments or trackbacks are added, removed or changed there status. 
     202      
     203     @param    ids       <b>mixed</b>        Comment(s) ID(s) 
     204     @param    del       <b>boolean</b>      If comment is delete, set this to true 
     205     */ 
     206     public function triggerComments($ids,$del=false) 
     207     { 
     208          $co_ids = dcUtils::cleanIds($ids); 
     209           
     210          # a) Retrieve posts affected by comments edition 
     211          $strReq =  
     212               'SELECT post_id, comment_trackback '. 
     213               'FROM '.$this->prefix.'comment '. 
     214               'WHERE comment_id'.$this->con->in($co_ids). 
     215               'GROUP BY post_id,comment_trackback'; 
    201216           
    202217          $rs = $this->con->select($strReq); 
    203218           
    204           $post_id = $rs->post_id; 
    205           $tb = (boolean) $rs->comment_trackback; 
    206            
    207           $strReq = 'SELECT COUNT(post_id) '. 
    208                     'FROM '.$this->prefix.'comment '. 
    209                     'WHERE post_id = '.(integer) $post_id.' '. 
    210                     'AND comment_trackback = '.(integer) $tb.' '. 
    211                     'AND comment_status = 1 '; 
     219          $a_ids = $a_tbs = array(); 
     220          while ($rs->fetch()) { 
     221               $a_ids[] = (integer) $rs->post_id; 
     222               $a_tbs[] = (integer) $rs->comment_trackback; 
     223          } 
     224           
     225          # b) Count comments of each posts previously retrieved 
     226          # Note that this does not return posts without comment 
     227          $strReq =  
     228               'SELECT post_id, COUNT(post_id) AS nb_comment,comment_trackback '. 
     229               'FROM '.$this->prefix.'comment '. 
     230               'WHERE post_id'.$this->con->in($a_ids). 
     231               'AND comment_status = 1 '; 
    212232           
    213233          if ($del) { 
    214                $strReq .= 'AND comment_id <> '.$id.' '; 
    215           } 
     234               $strReq .=  
     235                    'AND comment_id NOT'.$this->con->in($co_ids); 
     236          } 
     237           
     238          $strReq .=  
     239               'GROUP BY post_id,comment_trackback'; 
    216240           
    217241          $rs = $this->con->select($strReq); 
    218242           
     243          $b_ids = $b_tbs = $b_nbs = array(); 
     244          while ($rs->fetch()) { 
     245               $b_ids[] = (integer) $rs->post_id; 
     246               $b_tbs[] = (integer) $rs->comment_trackback; 
     247               $b_nbs[] = (integer) $rs->nb_comment; 
     248          } 
     249           
     250          # c) Update comments numbers on posts 
     251          # This compare previous requests to update also posts without comment 
    219252          $cur = $this->con->openCursor($this->prefix.'post'); 
    220253           
    221           if ($rs->isEmpty()) { 
    222                return; 
    223           } 
    224            
    225           if ($tb) { 
    226                $cur->nb_trackback = (integer) $rs->f(0); 
    227           } else { 
    228                $cur->nb_comment = (integer) $rs->f(0); 
    229           } 
    230            
    231           $cur->update('WHERE post_id = '.(integer) $post_id); 
     254          foreach($a_ids as $a_key => $a_id) 
     255          { 
     256               $nb_comment = $nb_trackback = 0; 
     257               foreach($b_ids as $b_key => $b_id) 
     258               { 
     259                    if ($a_id != $b_id || $a_tbs[$a_key] != $b_tbs[$b_key]) { 
     260                         continue; 
     261                    } 
     262                     
     263                    if ($b_tbs[$b_key]) { 
     264                         $nb_trackback = $b_nbs[$b_key]; 
     265                    } else { 
     266                         $nb_comment = $b_nbs[$b_key]; 
     267                    } 
     268               } 
     269                
     270               if ($a_tbs[$a_key]) { 
     271                    $cur->nb_trackback = $nb_trackback; 
     272               } else { 
     273                    $cur->nb_comment = $nb_comment; 
     274               } 
     275               $cur->update('WHERE post_id = '.$a_id); 
     276          } 
    232277     } 
    233278     //@} 
     
    21092154           
    21102155          $this->con->execute($strReq); 
    2111            
    2112           foreach($co_ids as $id) { 
    2113                $this->triggerComment($id); 
    2114           } 
     2156          $this->triggerComments($co_ids); 
    21152157          $this->triggerBlog(); 
    21162158     } 
     
    21702212           
    21712213          $this->con->execute($strReq); 
    2172            
    2173           foreach($co_ids as $id) { 
    2174                $this->triggerComment($id,true); 
    2175           } 
     2214          $this->triggerComments($co_ids,true); 
    21762215          $this->triggerBlog(); 
    21772216     } 
Note: See TracChangeset for help on using the changeset viewer.

Sites map