Commits

Jean-Christian Denis committed ad4942b

Enhance mass posts actions by reducing SQL queries, addresses #943

Comments (0)

Files changed (4)

admin/categories.php

 		}
 		
 		# 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']);

admin/posts_actions.php

 	
 	$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);
 	
 		
 		try
 		{
-			while ($posts->fetch()) {
-				$core->blog->updPostStatus($posts->post_id,$status);
-			}
+			$core->blog->updPostsStatus($posts_ids,$status);
 			
 			http::redirect($redir);
 		}
 	{
 		try
 		{
-			while ($posts->fetch()) {
-				$core->blog->updPostSelected($posts->post_id,$action == 'selected');
-			}
+			$core->blog->updPostsSelected($posts_ids,$action == 'selected');
 			
 			http::redirect($redir);
 		}
 	{
 		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)
 	{
 		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)
 				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);
 		}

inc/core/class.dc.blog.php

 	*/
 	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');
 		$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');
 		$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();
 	}
 	
 	*/
 	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');
 		$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();
 	}
 	
 	@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'));
 	*/
 	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();
 	}

inc/core/class.dc.utils.php

 		
 		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;
+	}
 }
 
 ?>