Hello tout le monde,

Je n'ai pas fait de "pull request" (ça me gave toujours autant) donc je
met en pièce jointe ici une première approche d'une optimisation des
traitements de posts_actions.php, j'ai testé et ça fonctionne très bien
(mysql et pgsql)  mais je n'ose pas le mettre sur le HG car on perd le
message d'avertissement si quelqu'un n'a pas les droits nécessaires sur
certains billets. (mais la vérification se fait toujours)
Le patch provient du dernier commit de la branche "default", ça sera
plus simple à tester pour vous ;-)

A vous de voir si on l'intègre de suite.

Cordialement,
JC
# HG changeset patch
# User JcDenis
# Date 1353324871 -3600
# Node ID ad4942bddbdbd7fb21946f79d4900f28025755be
# Parent  a852ebcec2d02fe1fcd10bb570cc3c41468f0f07
Enhance mass posts actions by reducing SQL queries, addresses #943

diff -r a852ebcec2d0 -r ad4942bddbdb admin/categories.php
--- a/admin/categories.php      Mon Nov 19 11:28:46 2012 +0100
+++ b/admin/categories.php      Mon Nov 19 12:34:31 2012 +0100
@@ -41,7 +41,7 @@
                }
                
                # Move posts
-               $core->blog->updPostsCategory($_POST['del_cat'],$mov_cat);
+               $core->blog->changePostsCategory($_POST['del_cat'],$mov_cat);
                
                # Delete category
                $core->blog->delCategory($_POST['del_cat']);
diff -r a852ebcec2d0 -r ad4942bddbdb admin/posts_actions.php
--- a/admin/posts_actions.php   Mon Nov 19 11:28:46 2012 +0100
+++ b/admin/posts_actions.php   Mon Nov 19 12:34:31 2012 +0100
@@ -58,6 +58,11 @@
        
        $posts = $core->blog->getPosts($params);
        
+       $posts_ids = array();
+       while ($posts->fetch()) {
+               $posts_ids[] = $posts->post_id;
+       }
+       
        # --BEHAVIOR-- adminPostsActions
        $core->callBehavior('adminPostsActions',$core,$posts,$action,$redir);
        
@@ -72,9 +77,7 @@
                
                try
                {
-                       while ($posts->fetch()) {
-                               
$core->blog->updPostStatus($posts->post_id,$status);
-                       }
+                       $core->blog->updPostsStatus($posts_ids,$status);
                        
                        http::redirect($redir);
                }
@@ -87,9 +90,7 @@
        {
                try
                {
-                       while ($posts->fetch()) {
-                               
$core->blog->updPostSelected($posts->post_id,$action == 'selected');
-                       }
+                       $core->blog->updPostsSelected($posts_ids,$action == 
'selected');
                        
                        http::redirect($redir);
                }
@@ -102,12 +103,18 @@
        {
                try
                {
-                       while ($posts->fetch()) {
+                       // Backward compatibility
+                       foreach($posts_ids as $post_id)
+                       {
                                # --BEHAVIOR-- adminBeforePostDelete
-                               
$core->callBehavior('adminBeforePostDelete',$posts->post_id);                   
        
-                               $core->blog->delPost($posts->post_id);
+                               
$core->callBehavior('adminBeforePostDelete',(integer) $post_id);
                        }
                        
+                       # --BEHAVIOR-- adminBeforePostsDelete
+                       
$core->callBehavior('adminBeforePostsDelete',$posts_ids);
+                       
+                       $core->blog->delPosts($posts_ids);
+                       
                        http::redirect($redir);
                }
                catch (Exception $e)
@@ -120,11 +127,8 @@
        {
                try
                {
-                       while ($posts->fetch())
-                       {
-                               $new_cat_id = (integer) $_POST['new_cat_id'];
-                               
$core->blog->updPostCategory($posts->post_id,$new_cat_id);
-                       }
+                       
$core->blog->updPostsCategory($posts_ids,$_POST['new_cat_id']);
+                       
                        http::redirect($redir);
                }
                catch (Exception $e)
@@ -143,12 +147,9 @@
                                throw new Exception(__('This user does not 
exist'));
                        }
                        
-                       while ($posts->fetch())
-                       {
-                               $cur = 
$core->con->openCursor($core->prefix.'post');
-                               $cur->user_id = $new_user_id;
-                               $cur->update('WHERE post_id = '.(integer) 
$posts->post_id);
-                       }
+                       $cur = $core->con->openCursor($core->prefix.'post');
+                       $cur->user_id = $new_user_id;
+                       $cur->update('WHERE post_id 
'.$core->con->in($posts_ids));
                        
                        http::redirect($redir);
                }
diff -r a852ebcec2d0 -r ad4942bddbdb inc/core/class.dc.blog.php
--- a/inc/core/class.dc.blog.php        Mon Nov 19 11:28:46 2012 +0100
+++ b/inc/core/class.dc.blog.php        Mon Nov 19 12:34:31 2012 +0100
@@ -1222,27 +1222,31 @@
        */
        public function updPostStatus($id,$status)
        {
+               $this->updPostsStatus($id,$status);
+       }
+       
+       /**
+       Updates posts status.
+       
+       @param  ids             <b>mixed</b>            Post(s) ID(s)
+       @param  status  <b>integer</b>          Post status
+       */
+       public function updPostsStatus($ids,$status)
+       {
                if 
(!$this->core->auth->check('publish,contentadmin',$this->id)) {
                        throw new Exception(__('You are not allowed to change 
this entry status'));
                }
                
-               $id = (integer) $id;
+               $posts_ids = dcUtils::cleanIds($ids);
                $status = (integer) $status;
                
+               $strReq = "WHERE blog_id = '".$this->con->escape($this->id)."' 
".
+                               "AND post_id ".$this->con->in($posts_ids);
+               
                #If user can only publish, we need to check the post's owner
                if (!$this->core->auth->check('contentadmin',$this->id))
                {
-                       $strReq = 'SELECT post_id '.
-                                       'FROM '.$this->prefix.'post '.
-                                       'WHERE post_id = '.$id.' '.
-                                       "AND blog_id = 
'".$this->con->escape($this->id)."' ".
-                                       "AND user_id = 
'".$this->con->escape($this->core->auth->userID())."' ";
-                       
-                       $rs = $this->con->select($strReq);
-                       
-                       if ($rs->isEmpty()) {
-                               throw new Exception(__('You are not allowed to 
change this entry status'));
-                       }
+                       $strReq .= "AND user_id = 
'".$this->con->escape($this->core->auth->userID())."' ";
                }
                
                $cur = $this->con->openCursor($this->prefix.'post');
@@ -1250,36 +1254,43 @@
                $cur->post_status = $status;
                $cur->post_upddt = date('Y-m-d H:i:s');
                
-               $cur->update(
-                       'WHERE post_id = '.$id.' '.
-                       "AND blog_id = '".$this->con->escape($this->id)."' "
-                       );
+               $cur->update($strReq);
                $this->triggerBlog();
        }
        
+       /**
+       Updates post selection.
+       
+       @param  id              <b>integer</b>          Post ID
+       @param  selected        <b>integer</b>          Is selected post
+       */
        public function updPostSelected($id,$selected)
        {
+               $this->updPostsSelected($id,$selected);
+       }
+       
+       /**
+       Updates posts selection.
+       
+       @param  ids             <b>mixed</b>            Post(s) ID(s)
+       @param  selected        <b>integer</b>          Is selected post(s)
+       */
+       public function updPostsSelected($ids,$selected)
+       {
                if (!$this->core->auth->check('usage,contentadmin',$this->id)) {
                        throw new Exception(__('You are not allowed to change 
this entry category'));
                }
                
-               $id = (integer) $id;
+               $posts_ids = dcUtils::cleanIds($ids);
                $selected = (boolean) $selected;
                
+               $strReq = "WHERE blog_id = '".$this->con->escape($this->id)."' 
".
+                               "AND post_id ".$this->con->in($posts_ids);
+               
                # If user is only usage, we need to check the post's owner
                if (!$this->core->auth->check('contentadmin',$this->id))
                {
-                       $strReq = 'SELECT post_id '.
-                                       'FROM '.$this->prefix.'post '.
-                                       'WHERE post_id = '.$id.' '.
-                                       "AND blog_id = 
'".$this->con->escape($this->id)."' ".
-                                       "AND user_id = 
'".$this->con->escape($this->core->auth->userID())."' ";
-                       
-                       $rs = $this->con->select($strReq);
-                       
-                       if ($rs->isEmpty()) {
-                               throw new Exception(__('You are not allowed to 
mark this entry as selected'));
-                       }
+                       $strReq .= "AND user_id = 
'".$this->con->escape($this->core->auth->userID())."' ";
                }
                
                $cur = $this->con->openCursor($this->prefix.'post');
@@ -1287,10 +1298,7 @@
                $cur->post_selected = (integer) $selected;
                $cur->post_upddt = date('Y-m-d H:i:s');
                
-               $cur->update(
-                       'WHERE post_id = '.$id.' '.
-                       "AND blog_id = '".$this->con->escape($this->id)."' "
-               );
+               $cur->update($strReq);
                $this->triggerBlog();
        }
        
@@ -1302,27 +1310,31 @@
        */
        public function updPostCategory($id,$cat_id)
        {
+               $this->updPostsCategory($id,$cat_id);
+       }
+       
+       /**
+       Updates posts category. <var>$cat_id</var> can be null.
+       
+       @param  ids             <b>mixed</b>            Post(s) ID(s)
+       @param  cat_id  <b>integer</b>          Category ID
+       */
+       public function updPostsCategory($ids,$cat_id)
+       {
                if (!$this->core->auth->check('usage,contentadmin',$this->id)) {
                        throw new Exception(__('You are not allowed to change 
this entry category'));
                }
                
-               $id = (integer) $id;
+               $posts_ids = dcUtils::cleanIds($ids);
                $cat_id = (integer) $cat_id;
                
+               $strReq = "WHERE blog_id = '".$this->con->escape($this->id)."' 
".
+                               "AND post_id ".$this->con->in($posts_ids);
+               
                # If user is only usage, we need to check the post's owner
                if (!$this->core->auth->check('contentadmin',$this->id))
                {
-                       $strReq = 'SELECT post_id '.
-                                       'FROM '.$this->prefix.'post '.
-                                       'WHERE post_id = '.$id.' '.
-                                       "AND blog_id = 
'".$this->con->escape($this->id)."' ".
-                                       "AND user_id = 
'".$this->con->escape($this->core->auth->userID())."' ";
-                       
-                       $rs = $this->con->select($strReq);
-                       
-                       if ($rs->isEmpty()) {
-                               throw new Exception(__('You are not allowed to 
change this entry category'));
-                       }
+                       $strReq .= "AND user_id = 
'".$this->con->escape($this->core->auth->userID())."' ";
                }
                
                $cur = $this->con->openCursor($this->prefix.'post');
@@ -1330,10 +1342,7 @@
                $cur->cat_id = ($cat_id ? $cat_id : null);
                $cur->post_upddt = date('Y-m-d H:i:s');
                
-               $cur->update(
-                       'WHERE post_id = '.$id.' '.
-                       "AND blog_id = '".$this->con->escape($this->id)."' "
-               );
+               $cur->update($strReq);
                $this->triggerBlog();
        }
        
@@ -1343,7 +1352,7 @@
        @param  old_cat_id      <b>integer</b>          Old category ID
        @param  new_cat_id      <b>integer</b>          New category ID
        */
-       public function updPostsCategory($old_cat_id,$new_cat_id)
+       public function changePostsCategory($old_cat_id,$new_cat_id)
        {
                if 
(!$this->core->auth->check('contentadmin,categories',$this->id)) {
                        throw new Exception(__('You are not allowed to change 
entries category'));
@@ -1371,37 +1380,36 @@
        */
        public function delPost($id)
        {
+               $this->delPosts($id);
+       }
+       
+       /**
+       Deletes multiple posts.
+       
+       @param  ids             <b>mixed</b>            Post(s) ID(s)
+       */
+       public function delPosts($ids)
+       {
                if (!$this->core->auth->check('delete,contentadmin',$this->id)) 
{
                        throw new Exception(__('You are not allowed to delete 
entries'));
                }
                
-               $id = (integer) $id;
+               $posts_ids = dcUtils::cleanIds($ids);
                
-               if (empty($id)) {
+               if (empty($posts_ids)) {
                        throw new Exception(__('No such entry ID'));
                }
                
+               $strReq = 'DELETE FROM '.$this->prefix.'post '.
+                               "WHERE blog_id = 
'".$this->con->escape($this->id)."' ".
+                               "AND post_id ".$this->con->in($posts_ids);
+               
                #If user can only delete, we need to check the post's owner
                if (!$this->core->auth->check('contentadmin',$this->id))
                {
-                       $strReq = 'SELECT post_id '.
-                                       'FROM '.$this->prefix.'post '.
-                                       'WHERE post_id = '.$id.' '.
-                                       "AND blog_id = 
'".$this->con->escape($this->id)."' ".
-                                       "AND user_id = 
'".$this->con->escape($this->core->auth->userID())."' ";
-                       
-                       $rs = $this->con->select($strReq);
-                       
-                       if ($rs->isEmpty()) {
-                               throw new Exception(__('You are not allowed to 
delete this entry'));
-                       }
+                       $strReq .= "AND user_id = 
'".$this->con->escape($this->core->auth->userID())."' ";
                }
                
-               
-               $strReq = 'DELETE FROM '.$this->prefix.'post '.
-                               'WHERE post_id = '.$id.' '.
-                               "AND blog_id = 
'".$this->con->escape($this->id)."' ";
-               
                $this->con->execute($strReq);
                $this->triggerBlog();
        }
diff -r a852ebcec2d0 -r ad4942bddbdb inc/core/class.dc.utils.php
--- a/inc/core/class.dc.utils.php       Mon Nov 19 11:28:46 2012 +0100
+++ b/inc/core/class.dc.utils.php       Mon Nov 19 12:34:31 2012 +0100
@@ -48,6 +48,31 @@
                
                return $user_id;
        }
+       
+       /**
+       Cleanup a list of IDs
+       
+       @param  ids                     <b>mixed</b>    ID(s)
+       @return <b>array</b>
+       */
+       public static function cleanIds($ids)
+       {
+               $clean_ids = array();
+               
+               if (!is_array($ids)) {
+                       $ids = array($ids);
+               }
+               
+               foreach($ids as $id)
+               {
+                       $id = abs((integer) $id);
+                       
+                       if (!empty($id)) {
+                               $clean_ids[] = $id;
+                       }
+               }
+               return $clean_ids;
+       }
 }
 
 ?>
\ No newline at end of file
_______________________________________________
Dev mailing list
[email protected]
http://ml.dotclear.org/listinfo/dev

Répondre à