[pLog-svn] r3348 - in plugins/trunk/contentfilter/class: dao security view

oscar at devel.lifetype.net oscar at devel.lifetype.net
Sat May 6 13:34:43 GMT 2006


Author: oscar
Date: 2006-05-06 13:34:43 +0000 (Sat, 06 May 2006)
New Revision: 3348

Modified:
   plugins/trunk/contentfilter/class/dao/filteredcontent.class.php
   plugins/trunk/contentfilter/class/dao/filteredcontents.class.php
   plugins/trunk/contentfilter/class/security/contentfilter.class.php
   plugins/trunk/contentfilter/class/view/adminfilteredcontentview.class.php
Log:
reimplemented the contentfilter plugin using the caching framework, no more unnecessary queries again :)


Modified: plugins/trunk/contentfilter/class/dao/filteredcontent.class.php
===================================================================
--- plugins/trunk/contentfilter/class/dao/filteredcontent.class.php	2006-05-06 12:33:05 UTC (rev 3347)
+++ plugins/trunk/contentfilter/class/dao/filteredcontent.class.php	2006-05-06 13:34:43 UTC (rev 3348)
@@ -1,6 +1,6 @@
 <?php
 
-	
+	include_once( PLOG_CLASS_PATH."class/database/dbobject.class.php" );
 
     /**
      * Represents a record form the plog_filtered_content table
@@ -8,7 +8,7 @@
      * The key of this class is the regexp that will be used to match
      * words against it.
      */
-    class FilteredContent 
+    class FilteredContent extends DbObject
 	{
 
     	var $_regExp;
@@ -27,9 +27,7 @@
          * @param id Identifier of this rule
          */
     	function FilteredContent( $regExp, $blogId, $reason, $date = null, $id = -1 )
-        {
-        	
-
+        {        	
         	$this->_regExp = $regExp;
             $this->_blogId = $blogId;
             $this->_reason = $reason;
@@ -40,12 +38,25 @@
 			else
 				$this->_date   = $date;
             $this->_id     = $id;
+            
+            $this->_fields = Array(
+                "id" => "getId",
+                "reg_exp" => "getRegExp",
+                "blog_id" => "getBlogId",
+                "reason" => "getReason",
+                "date" => "getDateObject"
+            );
         }
 
         function getId()
         {
         	return $this->_id;
         }
+        
+        function setId( $id )
+        {
+            $this->_id = $id;
+        }
 
         /**
          * Returns the regular expression that was assigned to this

Modified: plugins/trunk/contentfilter/class/dao/filteredcontents.class.php
===================================================================
--- plugins/trunk/contentfilter/class/dao/filteredcontents.class.php	2006-05-06 12:33:05 UTC (rev 3347)
+++ plugins/trunk/contentfilter/class/dao/filteredcontents.class.php	2006-05-06 13:34:43 UTC (rev 3348)
@@ -2,6 +2,12 @@
 
 	include_once( PLOG_CLASS_PATH."class/dao/model.class.php" );
     include_once( PLOG_CLASS_PATH."plugins/contentfilter/class/dao/filteredcontent.class.php" );
+    
+    /**
+     * ids of the caches being used
+     */
+    define( "CACHE_FILTERED_CONTENT", "cache_filtered_content" );
+    define( "CACHE_FILTERED_CONTENT_BYBLOG", "cache_filtered_content_byblog" );
 
     /**
      * Takes care of dealing with fetching filtered contents from the database
@@ -12,6 +18,7 @@
     	function FilteredContents()
         {
         	$this->Model();
+        	$this->table = $this->getPrefix()."filtered_content";
         }
 
         /**
@@ -19,14 +26,20 @@
          * has been set up by this blog.
          *
          * @param blogId The blog
+         * @param includeGlobal Whether to return globally-filtered content or not
          * @return An array filled with FilteredContent objects, or an empty array if
          * none was found
          */
-        function getBlogFilteredContents( $blogId )
+        function getBlogFilteredContents( $blogId, $includeGlobal = false )
         {
-        	$query = "SELECT * FROM ".$this->getPrefix()."filtered_content WHERE blog_id = $blogId";
-
-            return $this->getFilteredContentFromQuery( $query );
+            $data = $this->getMany( "blog_id", $blogId, CACHE_FILTERED_CONTENT_BYBLOG );
+            if( $includeGlobal )
+                $data = array_merge( $data, $this->getBlogFilteredContents( "0", false ));
+                
+            if( !$data )
+                $data = Array();
+                
+            return( $data );
         }
 
         /**
@@ -34,19 +47,14 @@
          */
         function getBlogFilteredContent( $ruleId, $blogId )
         {
-        	$query = "SELECT * FROM ".$this->getPrefix()."filtered_content WHERE id = $ruleId";
-            if( $blogId != 0 )
-            	$query .= " AND blog_id = $blogId";
-
-            $result = $this->Execute( $query );
-
-            if( !$result )
-            	return false;
-
-            if( $result->RecordCount() == 0 )
-            	return false;
-
-            return $this->_mapRowToObject( $result->FetchRow());
+            $rule = $this->get( "id", $ruleId, CACHE_FILTERED_CONTENT );
+            if( !$rule )
+                return false;
+                
+            if( $rule->getBlogId() == $blogId )
+                return( $rule );
+            else
+                return( false );
         }
 
         /**
@@ -58,43 +66,10 @@
          */
         function getGlobalFilteredContents()
         {
-        	$query = "SELECT * FROM ".$this->getPrefix()."filtered_content WHERE blog_id = 0";
-
-            return $this->getFilteredContentFromQuery( $query );
+            return( $this->getBlogFilteredContents( "0" ));
         }
 
         /**
-         * Returns all the contents that have been blocked in the site
-         *
-         * @return Array of FilteredContent objects
-         */
-        function getAllFilteredContents()
-        {
-        	$query = "SELECT * FROM ".$this->getPrefix()."filtered_content";
-
-            return $this->getFilteredContentFromQuery( $query );
-        }
-
-        /**
-         * @private
-         * Given a query, fetches the content
-         */
-        function getFilteredContentFromQuery( $query )
-        {
-            $result = $this->Execute( $query );
-
-            if( !$result )	// return an empty array if error
-            	return Array();
-
-            $filteredContents = Array();
-            while( $row = $result->FetchRow()) {
-            	array_push( $filteredContents, $this->_mapRowToObject( $row ));
-            }
-
-            return $filteredContents;
-        }
-
-        /**
          * Adds a new rule to filter content.
          *
 		 * @param filteredContent A FilteredContent object
@@ -102,14 +77,11 @@
          */
         function addBlogFilteredContent( $filteredContent )
         {
-        	$query = "INSERT INTO ".$this->getPrefix()."filtered_content ".
-                     "(reg_exp, blog_id, reason) VALUES (".
-                     "'".Db::qstr($filteredContent->getRegExp())."', ".$filteredContent->getBlogId().
-					 ",'".Db::qstr($filteredContent->getReason())."')";
-
-            $result = $this->Execute( $query );
-
-			return $result;
+            if(( $result = parent::add( $filteredContent ))) {
+                $this->_cache->removeData( $filteredContent->getBlogId(), CACHE_FILTERED_CONTENT_BYBLOG );
+            }
+            
+            return( $result );
         }
 
         /**
@@ -133,10 +105,15 @@
          */
         function removeBlogFilteredContent( $ruleId, $blogId )
         {
-        	$query = "DELETE FROM ".$this->getPrefix()."filtered_content WHERE blog_id = $blogId AND id = $ruleId";
+            $rule = $this->getBlogFilteredContent( $ruleId, $blogId );
+            if( !$rule )
+                return false;
+                
+            if(( $result = $this->delete( "id", $ruleId ))) {
+                $this->_cache->removeData( $ruleId, CACHE_FILTERED_CONTENT );
+                $this->_cache->removeData( $blogId, CACHE_FILTERED_CONTENT_BYBLOG );
+            }
 
-            $result = $this->Execute( $query );
-
             return $result;
         }
 
@@ -160,29 +137,24 @@
          */
         function updateFilteredContent( $rule )
         {
-        	$query = "UPDATE ".$this->getPrefix()."filtered_content SET ".
-                     "blog_id = ".$rule->getBlogId().", ".
-                     "reg_exp = '".Db::qstr($rule->getRegExp( true ))."', ".
-                     "reason = '".Db::qstr($rule->getReason())."' ".
-					 " WHERE blog_id = ".$rule->getBlogId().
-					 " AND id = ".$rule->getId().";";
-
-            $result = $this->Execute( $query );
-
-            if( !$result )
-            	return false;
-
-            return true;
+            if(( $result = parent::update( $rule ))) {
+                $this->_cache->removeData( $rule->getId(), CACHE_FILTERED_CONTENT );
+                $this->_cache->removeData( $rule->getBlogId(), CACHE_FILTERED_CONTENT_BYBLOG );
+            }
+            
+            return( $result );
         }
 
         /**
          * @private
          * Maps a row from the database to its object
          */
-        function _mapRowToObject( $row )
+        function mapRow( $row )
         {
-        	$filteredContent = new FilteredContent( $row["reg_exp"], $row["blog_id"],
-                                                    $row["reason"], $row["date"],
+        	$filteredContent = new FilteredContent( $row["reg_exp"], 
+        	                                        $row["blog_id"],
+                                                    $row["reason"], 
+                                                    $row["date"],
                                                     $row["id"] );
 
             return $filteredContent;

Modified: plugins/trunk/contentfilter/class/security/contentfilter.class.php
===================================================================
--- plugins/trunk/contentfilter/class/security/contentfilter.class.php	2006-05-06 12:33:05 UTC (rev 3347)
+++ plugins/trunk/contentfilter/class/security/contentfilter.class.php	2006-05-06 13:34:43 UTC (rev 3348)
@@ -49,55 +49,22 @@
             // if this is already rejected, there is no reason to do anything here
             if ( $this->_pipelineRequest->getRejectedState() )
                 return new PipelineResult();
-                
-            //
-            // get the content that has been globally blocked by the admin(s)
-            //
-            $filteredContents = new FilteredContents();
-            $globalFilteredContents = $filteredContents->getGlobalFilteredContents();
+                            
             // text and topic of the comment
             $commentText = $request->getValue( "commentText" );
             $commentTopic = $request->getValue( "commentTopic" );
             $userName = $request->getValue( "userName" );
             $userEmail = $request->getValue( "userEmail" );
             $userUrl = $request->getValue( "userUrl" );
-            foreach( $globalFilteredContents as $globalFilteredContent ) {
-            	//_debug("regexp = ".$globalFilteredContent->getRegExp()."<br/>");
-            	if( preg_match( $globalFilteredContent->getRegExp(), $commentText )) {
-                	// if there is a match, we can quit and reject this request
-                    $result = new PipelineResult( false, CONTENT_FILTER_MATCH_FOUND, $globalFilteredContent->getReason());
-                    return $result;
-                }
 
-            	if( preg_match( $globalFilteredContent->getRegExp(), $commentTopic )) {
-                	// if there is a match, we can quit and reject this request
-                    $result = new PipelineResult( false, CONTENT_FILTER_MATCH_FOUND, $globalFilteredContent->getReason());
-                    return $result;
-                }
-                
-            	if( preg_match( $globalFilteredContent->getRegExp(), $userName )) {
-                	// if there is a match, we can quit and reject this request
-                    $result = new PipelineResult( false, CONTENT_FILTER_MATCH_FOUND, $globalFilteredContent->getReason());
-                    return $result;
-                }
-                
-            	if( preg_match( $globalFilteredContent->getRegExp(), $userEmail )) {
-                	// if there is a match, we can quit and reject this request
-                    $result = new PipelineResult( false, CONTENT_FILTER_MATCH_FOUND, $globalFilteredContent->getReason());
-                    return $result;
-                }
-                
-            	if( preg_match( $globalFilteredContent->getRegExp(), $userUrl )) {
-                	// if there is a match, we can quit and reject this request
-                    $result = new PipelineResult( false, CONTENT_FILTER_MATCH_FOUND, $globalFilteredContent->getReason());
-                    return $result;
-                }                                                                
-            }
-
             //
-            // get the list of filtered content for this blog
+            // get the list of filtered content for this blog, including global data
             //
-            $blogFilteredContents = $filteredContents->getBlogFilteredContents( $blogInfo->getId());
+            $filteredContents = new FilteredContents();            
+            $blogFilteredContents = $filteredContents->getBlogFilteredContents( $blogInfo->getId(), true );
+            
+            print_r($blogFilteredContents);
+            
             foreach( $blogFilteredContents as $blogFilteredContent ) {
             	//_debug("regexp = ".$blogFilteredContent->getRegExp()."<br/>");
             	if( preg_match( $blogFilteredContent->getRegExp(), $commentText )) {

Modified: plugins/trunk/contentfilter/class/view/adminfilteredcontentview.class.php
===================================================================
--- plugins/trunk/contentfilter/class/view/adminfilteredcontentview.class.php	2006-05-06 12:33:05 UTC (rev 3347)
+++ plugins/trunk/contentfilter/class/view/adminfilteredcontentview.class.php	2006-05-06 13:34:43 UTC (rev 3348)
@@ -21,8 +21,8 @@
 
         	// get the content that has been filtered by this blog
             $filteredContents = new FilteredContents();
-            $blogFilteredContents = $filteredContents->getAllFilteredContents();
-
+            $blogFilteredContents = $filteredContents->getGlobalFilteredContents();
+            
             $this->setValue( "pluginEnabled", $pluginEnabled );
             $this->setValue( "filteredcontent", $blogFilteredContents );
             



More information about the pLog-svn mailing list