[pLog-svn] r3008 - in plog/trunk/class: action/admin dao

oscar at devel.lifetype.net oscar at devel.lifetype.net
Fri Mar 3 10:36:37 GMT 2006


Author: oscar
Date: 2006-03-03 10:36:36 +0000 (Fri, 03 Mar 2006)
New Revision: 3008

Modified:
   plog/trunk/class/action/admin/adminchangecommentsstatusaction.class.php
   plog/trunk/class/action/admin/adminchangetrackbacksstatusaction.class.php
   plog/trunk/class/action/admin/adminmarkcommentaction.class.php
   plog/trunk/class/action/admin/adminmarktrackbackaction.class.php
   plog/trunk/class/dao/articlecomments.class.php
   plog/trunk/class/dao/commentscommon.class.php
Log:
when updating trackbacks and comments, caches and counters in the articles and blogs table are now being updated correctly. I have also simplified the logic fo rupdating 'counter'-style fields and now instead of having some complicated logic to compare the previous version and the current one, and then determining what to do, the counters are always updated based on the contents of the db. This makes update operations a bit slower but those are less common than SELECT operations.


Modified: plog/trunk/class/action/admin/adminchangecommentsstatusaction.class.php
===================================================================
--- plog/trunk/class/action/admin/adminchangecommentsstatusaction.class.php	2006-03-03 10:34:35 UTC (rev 3007)
+++ plog/trunk/class/action/admin/adminchangecommentsstatusaction.class.php	2006-03-03 10:36:36 UTC (rev 3008)
@@ -96,7 +96,8 @@
 						$errorMessage .= $this->_locale->pr("error_deleting_comment", $comment->getTopic())."<br/>";
 					}
 					else {
-						if( !$comments->updateCommentStatus( $commentId, $this->_commentStatus ))
+						$comment->setStatus( $this->_commentStatus );
+						if( !$comments->updateComment( $comment ))
 							$errorMessage .= $this->_locale->pr("error_updating_comment", $comment->getTopic())."<br/>";
 						else {
 							$totalOk++;

Modified: plog/trunk/class/action/admin/adminchangetrackbacksstatusaction.class.php
===================================================================
--- plog/trunk/class/action/admin/adminchangetrackbacksstatusaction.class.php	2006-03-03 10:34:35 UTC (rev 3007)
+++ plog/trunk/class/action/admin/adminchangetrackbacksstatusaction.class.php	2006-03-03 10:36:36 UTC (rev 3008)
@@ -96,7 +96,8 @@
 						$errorMessage .= $this->_locale->pr("error_updating_trackback", $trackback->getExcerpt())."<br/>";
 					}
 					else {
-						if( !$trackbacks->updateCommentStatus( $trackbackId, $this->_trackbackStatus ))
+						$trackback->setStatus( $this->_trackbackStatus );
+						if( !$trackbacks->updateComment( $trackback ))
 							$errorMessage .= $this->_locale->pr("error_updating_trackback", $trackback->getExcerpt())."<br/>";
 						else {
 							$totalOk++;

Modified: plog/trunk/class/action/admin/adminmarkcommentaction.class.php
===================================================================
--- plog/trunk/class/action/admin/adminmarkcommentaction.class.php	2006-03-03 10:34:35 UTC (rev 3007)
+++ plog/trunk/class/action/admin/adminmarkcommentaction.class.php	2006-03-03 10:36:36 UTC (rev 3008)
@@ -78,43 +78,48 @@
 				$this->_view = new AdminArticleCommentsListView( $this->_blogInfo, Array( "article" => $this->_article ));			
 			
         	$comments = new ArticleComments();
-            if( !$comments->updateCommentStatus( $this->_commentId, COMMENT_STATUS_SPAM )) {
-                $this->_view->setErrorMessage( $this->_locale->tr("error_marking_comment_as_spam" ));
-                $this->setCommonData();
-				
-				$res = false;
-            }
-            else {
-                $this->_view->setSuccessMessage( $this->_locale->tr("comment_marked_as_spam_ok" ));				
-                $this->setCommonData();
-				
-                $res = true;
+        	$comment = $comments->getComment( $this->_commentId );
+        	if( $comment ) {
+	        	$comment->setStatus( COMMENT_STATUS_SPAM );
+	            if(($comment->getBlogId() != $this->_blogInfo->getId()) || 
+	                (!$comments->updateComment( $comment ))) {
+	                $this->_view->setErrorMessage( $this->_locale->tr("error_marking_comment_as_spam" ));
+	                $this->setCommonData();
+					
+					$res = false;
+	            }
+	            else {
+	                $this->_view->setSuccessMessage( $this->_locale->tr("comment_marked_as_spam_ok" ));				
+	                $this->setCommonData();
+					
+	                $res = true;
+	
+	                // before exiting, we should get the comment and train the filter
+	                // to recognize this as spam...
+	                $comment = $comments->getComment( $this->_commentId );
+	                $bayesian = new BayesianFilterCore();
+	
+	                $bayesian->untrain( $this->_blogInfo->getId(),
+		                                $comment->getTopic(),
+		                                $comment->getText(),
+		                                $comment->getUserName(),
+		                                $comment->getUserEmail(),
+		                                $comment->getUserUrl(),
+		                                false );
+	                                  
+	                $bayesian->train( $this->_blogInfo->getId(),
+	                                  $comment->getTopic(),
+	                                  $comment->getText(),
+	                                  $comment->getUserName(),
+	                                  $comment->getUserEmail(),
+	                                  $comment->getUserUrl(),
+	                                  true );
+									  
+					// throw the post-event if everythign went fine
+					$this->notifyEvent( EVENT_POST_MARK_SPAM_COMMENT, Array( "commentId" => $this->_commentId ));								  
+	            }	        	
+        	}        	
 
-                // before exiting, we should get the comment and train the filter
-                // to recognize this as spam...
-                $comment = $comments->getComment( $this->_commentId );
-                $bayesian = new BayesianFilterCore();
-
-                $bayesian->untrain( $this->_blogInfo->getId(),
-	                                $comment->getTopic(),
-	                                $comment->getText(),
-	                                $comment->getUserName(),
-	                                $comment->getUserEmail(),
-	                                $comment->getUserUrl(),
-	                                false );
-                                  
-                $bayesian->train( $this->_blogInfo->getId(),
-                                  $comment->getTopic(),
-                                  $comment->getText(),
-                                  $comment->getUserName(),
-                                  $comment->getUserEmail(),
-                                  $comment->getUserUrl(),
-                                  true );
-								  
-				// throw the post-event if everythign went fine
-				$this->notifyEvent( EVENT_POST_MARK_SPAM_COMMENT, Array( "commentId" => $this->_commentId ));								  
-            }
-
             return $res;
         }
 
@@ -132,42 +137,48 @@
 				$this->_view = new AdminArticleCommentsListView( $this->_blogInfo, Array( "article" => $this->_article ));			
 		
         	$comments = new ArticleComments();
-            if( !$comments->updateCommentStatus( $this->_commentId, COMMENT_STATUS_NONSPAM )) {
-                $this->_view->setErrorMessage( $this->_locale->tr("error_marking_comment_as_nonspam" ));
-                $this->setCommonData();
+        	$comment = $comments->getComment( $this->_commentId );
+        	if( $comment ) {
+	        	$comment->setStatus( COMMENT_STATUS_NONSPAM );
+	        	if(( $comment->getBlogId() != $this->_blogInfo->getId()) ||
+	        	    (!$comments->updateComment( $comment ))) {	
+
+	                $this->_view->setErrorMessage( $this->_locale->tr("error_marking_comment_as_nonspam" ));
+    	            $this->setCommonData();
 				
 				$res = false;
-            }
-            else {
-                $this->_view->setSuccessMessage( $this->_locale->tr("comment_marked_as_nonspam_ok" ));				
-                $this->setCommonData();
+            	}
+            	else {
+                	$this->_view->setSuccessMessage( $this->_locale->tr("comment_marked_as_nonspam_ok" ));				
+                	$this->setCommonData();
 				
-                $res = true;
+                	$res = true;
 
-                // before exiting, we should get the comment and train the filter
-                // to recognize this as spam...
-                $comment = $comments->getComment( $this->_commentId );
-                $bayesian = new BayesianFilterCore();
+	                // before exiting, we should get the comment and train the filter
+    	            // to recognize this as spam...
+        	        $comment = $comments->getComment( $this->_commentId );
+            	    $bayesian = new BayesianFilterCore();
                 
-                $bayesian->untrain( $this->_blogInfo->getId(),
-	                                $comment->getTopic(),
-	                                $comment->getText(),
-	                                $comment->getUserName(),
-	                                $comment->getUserEmail(),
-	                                $comment->getUserUrl(),
-	                                true );
-                                  
-                $bayesian->train( $this->_blogInfo->getId(),
-                                  $comment->getTopic(),
-                                  $comment->getText(),
-                                  $comment->getUserName(),
-                                  $comment->getUserEmail(),
-                                  $comment->getUserUrl(),
-                                  false );
-								  
-				// throw the post-event if everythign went fine
-				$this->notifyEvent( EVENT_POST_MARK_NO_SPAM_COMMENT, Array( "commentId" => $this->_commentId ));
-            }
+	                $bayesian->untrain( $this->_blogInfo->getId(),
+		                                $comment->getTopic(),
+		                                $comment->getText(),
+		                                $comment->getUserName(),
+		                                $comment->getUserEmail(),
+		                                $comment->getUserUrl(),
+		                                true );
+	                                  
+	                $bayesian->train( $this->_blogInfo->getId(),
+	                                  $comment->getTopic(),
+	                                  $comment->getText(),
+	                                  $comment->getUserName(),
+	                                  $comment->getUserEmail(),
+	                                  $comment->getUserUrl(),
+	                                  false );
+									  
+					// throw the post-event if everythign went fine
+					$this->notifyEvent( EVENT_POST_MARK_NO_SPAM_COMMENT, Array( "commentId" => $this->_commentId ));
+	            }
+           }
 
             return $res;
         }

Modified: plog/trunk/class/action/admin/adminmarktrackbackaction.class.php
===================================================================
--- plog/trunk/class/action/admin/adminmarktrackbackaction.class.php	2006-03-03 10:34:35 UTC (rev 3007)
+++ plog/trunk/class/action/admin/adminmarktrackbackaction.class.php	2006-03-03 10:36:36 UTC (rev 3008)
@@ -75,7 +75,8 @@
            	$this->_view = new AdminArticleTrackbacksListview( $this->_blogInfo, Array( "article" => $this->_article ));
 			
         	$trackbacks = new Trackbacks();
-            if( !$trackbacks->updateCommentStatus( $this->_trackback->getId(), COMMENT_STATUS_SPAM )) {
+        	$this->_trackback->setStatus( COMMENT_STATUS_SPAM );
+            if( !$trackbacks->updateComment( $this->_trackback )) {
                 $this->_view->setErrorMessage( $this->_locale->tr("error_marking_trackback_as_spam" ));
                 $this->setCommonData();
 				
@@ -126,7 +127,8 @@
            	$this->_view = new AdminArticleTrackbacksListView( $this->_blogInfo, Array( "article" => $this->_article ));
 		
         	$trackbacks = new Trackbacks();
-            if( !$trackbacks->updateCommentStatus( $this->_trackbackId, COMMENT_STATUS_NONSPAM )) {
+        	$this->_trackback->setStatus( COMMENT_STATUS_NONSPAM );
+            if( !$trackbacks->updateComment( $this->_trackback )) {
                 $this->_view->setErrorMessage( $this->_locale->tr("error_marking_trackback_as_nonspam" ));
                 $this->setCommonData();
 				

Modified: plog/trunk/class/dao/articlecomments.class.php
===================================================================
--- plog/trunk/class/dao/articlecomments.class.php	2006-03-03 10:34:35 UTC (rev 3007)
+++ plog/trunk/class/dao/articlecomments.class.php	2006-03-03 10:36:36 UTC (rev 3008)
@@ -61,18 +61,6 @@
         }
 
         /**
-         * updates the status of a comment, regarding its spam status...
-         *
-         * @param commentId
-         * @param status
-         * @return true or false, depending...
-         */
-        function updateCommentStatus( $commentId, $status )
-        {
-			return( CommentsCommon::updateCommentStatus( $commentId, $status, COMMENT_TYPE_COMMENT ));
-        }
-
-        /**
          * removes all comments marked as spam from the database
          */
         function purgeSpamComments()

Modified: plog/trunk/class/dao/commentscommon.class.php
===================================================================
--- plog/trunk/class/dao/commentscommon.class.php	2006-03-03 10:34:35 UTC (rev 3007)
+++ plog/trunk/class/dao/commentscommon.class.php	2006-03-03 10:36:36 UTC (rev 3008)
@@ -66,18 +66,22 @@
 				$article = $comment->getArticle();
 				$blog = $article->getBlogInfo();
 				if( $comment->getType() == COMMENT_TYPE_COMMENT ) {
-					$article->setTotalComments( $article->getTotalComments() + 1 );
-					if( $comment->getStatus() == COMMENT_STATUS_NONSPAM ) {
-						$article->setNumComments( $article->getNumComments() +1 );
-					}
-					$blog->setTotalComments( $blog->getTotalComments() + 1 );
+        			$article->setNumComments( $this->getNumItems( $this->getPrefix().'articles_comments', 
+        		    	                                          'article_id = '.$article->getId().' AND status = '.COMMENT_STATUS_NONSPAM.
+        		        	                                      ' AND type = '.$comment->getType()));
+					$totalComments = $this->getNumItems( $this->getPrefix().'articles_comments', 
+        		    	                                 'article_id = '.$article->getId().' AND type = '.$comment->getType());
+        			$article->setTotalComments( $totalComments );
+        		   	$blog->setTotalComments( $totalComments );
 				}
 				else {
-					$article->setTotalTrackbacks( $article->getTotalTrackbacks() + 1 );
-					if( $comment->getStatus() == COMMENT_STATUS_NONSPAM ) {
-						$article->setNumTrackbacks( $article->getNumTrackbacks() +1 );
-					}
-					$blog->setTotalTrackbacks( $blog->getTotalTrackbacks() + 1 );					
+        			$article->setNumTrackbacks( $this->getNumItems( $this->getPrefix().'articles_comments', 
+        		    	                                          'article_id = '.$article->getId().' AND status = '.COMMENT_STATUS_NONSPAM.
+        		        	                                      ' AND type = '.$comment->getType()));
+					$totalTrackbacks = $this->getNumItems( $this->getPrefix().'articles_comments', 
+        		    	                                 'article_id = '.$article->getId().' AND type = '.$comment->getType());
+        			$article->setTotalTrackbacks( $totalTrackbacks );
+        		   	$blog->setTotalTrackbacks( $totalTrackbacks );
 				}
 				$articles = new Articles();
 				$articles->updateArticle( $article );
@@ -301,26 +305,38 @@
 		}		
 
         /**
-         * updates the status of a comment, regarding its spam status...
+         * Updates a comment. It also takes into account status changes and updates counters in
+         * the blogs and articles table accordingly.
          *
-         * @param commentId
-         * @param status
-		 * @param type
-         * @return true or false, depending...
+         * @param comment An UserComment object
+         * @return true if update successful or false otherwise.
          */
-        function updateCommentStatus( $commentId, $status, $type = COMMENT_TYPE_ANY )
-        {
-        	$comment = $this->getComment( $commentId );
-        	if( !$comment )
-        		return false;
-        	
-        	if( $type != COMMENT_TYPE_ANY )
-        		if( $comment->getType() != $type )
-        			return false;
-        			
-        	$comment->setStatus( $status );
+        function updateComment( $comment )
+        {    
         	if(($result = $this->update( $comment ))) {
+	        	// reset the cache
         		$this->_cache->removeData( $comment->getArticleId(), CACHE_ARTICLE_COMMENTS_BYARTICLE );
+        		// update counters in the articles table according to the status
+        		include_once( PLOG_CLASS_PATH."class/dao/articles.class.php" );
+        		$articles = new Articles();
+        		// load the article
+        		$article = $comment->getArticle();
+        		if( $comment->getType() == COMMENT_TYPE_COMMENT ) {
+        			$article->setNumComments( $this->getNumItems( $this->getPrefix().'articles_comments', 
+        		    	                                          'article_id = '.$article->getId().' AND status = '.COMMENT_STATUS_NONSPAM.
+        		        	                                      ' AND type = '.$comment->getType()));
+        			$article->setTotalComments( $this->getNumItems( $this->getPrefix().'articles_comments', 
+        		    	                                          'article_id = '.$article->getId().' AND type = '.$comment->getType()));
+        		    
+    			}
+    			elseif( $comment->getType() == COMMENT_TYPE_TRACKBACK ) {
+        			$article->setNumTrackbacks( $this->getNumItems( $this->getPrefix().'articles_comments', 
+        		    	                                          'article_id = '.$article->getId().' AND status = '.COMMENT_STATUS_NONSPAM.
+        		        	                                      ' AND type = '.$comment->getType()));
+        			$article->setTotalTrackbacks( $this->getNumItems( $this->getPrefix().'articles_comments', 
+        		    	                                          'article_id = '.$article->getId().' AND type = '.$comment->getType()));
+    			}
+    			$articles->updateArticle( $article );
         	}
         	
         	return( $result );



More information about the pLog-svn mailing list