[pLog-svn] r5454 - in plugins/branches/lifetype-1.2/related: . class/action class/view locale templates

pwestbro at devel.lifetype.net pwestbro at devel.lifetype.net
Mon May 28 03:54:07 EDT 2007


Author: pwestbro
Date: 2007-05-28 03:54:07 -0400 (Mon, 28 May 2007)
New Revision: 5454

Modified:
   plugins/branches/lifetype-1.2/related/class/action/pluginrelatedupdateconfigaction.class.php
   plugins/branches/lifetype-1.2/related/class/view/pluginrelatedconfigview.class.php
   plugins/branches/lifetype-1.2/related/locale/locale_en_UK.php
   plugins/branches/lifetype-1.2/related/pluginrelated.class.php
   plugins/branches/lifetype-1.2/related/templates/related.template
Log:
Added better caching to the related post plugin.  Now when the LifeType
cache is rebuilt, the calculations for the list of related posts doesn't
have to be performed again.


Modified: plugins/branches/lifetype-1.2/related/class/action/pluginrelatedupdateconfigaction.class.php
===================================================================
--- plugins/branches/lifetype-1.2/related/class/action/pluginrelatedupdateconfigaction.class.php	2007-05-27 21:19:45 UTC (rev 5453)
+++ plugins/branches/lifetype-1.2/related/class/action/pluginrelatedupdateconfigaction.class.php	2007-05-28 07:54:07 UTC (rev 5454)
@@ -50,7 +50,7 @@
 
             $this->_numRelatedArticles = $this->_request->getValue( "numArticles" );
             $this->_minWordLength = $this->_request->getValue( "minWordLength" );
-            $this->_refreshInterval = $this->_request->getValue( "refreshInterval" );
+            $this->_refreshInterval = $this->_request->getValue( "interval" );
             
             lt_include( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );
                 

Modified: plugins/branches/lifetype-1.2/related/class/view/pluginrelatedconfigview.class.php
===================================================================
--- plugins/branches/lifetype-1.2/related/class/view/pluginrelatedconfigview.class.php	2007-05-27 21:19:45 UTC (rev 5453)
+++ plugins/branches/lifetype-1.2/related/class/view/pluginrelatedconfigview.class.php	2007-05-28 07:54:07 UTC (rev 5454)
@@ -45,7 +45,7 @@
 			$this->setValue( "pluginEnabled", $pluginEnabled );
 			$this->setValue( "numArticles", $numArticles );
 			$this->setValue( "minWordLength", $minWordLength );
-			$this->setValue( "refreshInterval", $refreshInterval );
+			$this->setValue( "interval", $refreshInterval );
 
 			parent::render();
 		}

Modified: plugins/branches/lifetype-1.2/related/locale/locale_en_UK.php
===================================================================
--- plugins/branches/lifetype-1.2/related/locale/locale_en_UK.php	2007-05-27 21:19:45 UTC (rev 5453)
+++ plugins/branches/lifetype-1.2/related/locale/locale_en_UK.php	2007-05-28 07:54:07 UTC (rev 5454)
@@ -15,11 +15,13 @@
 
 $messages["related_articles"] = "Number of related articles to return.";
 $messages["related_word_length"] = "Minimum length of keyword used to generate related article.";
+$messages["related_cache"] = "Lifetime for the related article cache.";
 
 $messages["label_configuration"] = "Configuration";
 $messages["label_enable"] = "Enable";
 
 $messages["related_max_articles"] = "Number Articles";
 $messages["related_min_word_length"] = "Minimum Keyword Length";
+$messages["related_cache_lifetime"] = "Cache Lifetime";
 
 ?>
\ No newline at end of file

Modified: plugins/branches/lifetype-1.2/related/pluginrelated.class.php
===================================================================
--- plugins/branches/lifetype-1.2/related/pluginrelated.class.php	2007-05-27 21:19:45 UTC (rev 5453)
+++ plugins/branches/lifetype-1.2/related/pluginrelated.class.php	2007-05-28 07:54:07 UTC (rev 5454)
@@ -26,6 +26,7 @@
 		var $numRelatedArticles;
 		var $minWordLength;
 		var $refreshInterval;
+        var $cacheFolder;
 		
 		function PluginRelated( $source = "" )
 		{
@@ -35,7 +36,7 @@
 			$this->desc    = "The Related plugin will generate a list of related posts.";
 			$this->author  = "Paul Westbrook";
 			$this->locales = Array( "en_UK" );
-            $this->version = "20070525";
+            $this->version = "20070528";
 
 
 			if( $source == "admin" )
@@ -60,6 +61,17 @@
 			$this->numRelatedArticles = $blogSettings->getValue( "plugin_related_num_articles" );
 			$this->minWordLength = $blogSettings->getValue( "plugin_related_min_word_length" );
 			$this->refreshInterval = $blogSettings->getValue( "plugin_related_refresh_interval" );
+			
+           if(!$this->isEnabled())
+                return;
+            
+            $config =& Config::getConfig();
+            $this->cacheFolder = $config->getValue('temp_folder');
+            $this->cacheFolder = $this->cacheFolder.'/related/'.$this->blogInfo->getId();
+            if( !File::exists( $this->cacheFolder )) {
+                File::createDir( $this->cacheFolder, 0755 );
+            }
+
 		}
 
 	    function isEnabled()
@@ -75,66 +87,78 @@
 	        if (!$this->isEnabled()) {
 	            return $relatedArticles;
 	        }
-	        
-	        // Look for the related articles in the cache
-	        
-	        // Get the article specified by the article ID
-		    lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" );
-            $articles = new Articles();	
-            $article = $articles->getArticle($articleId);
-            
-            if ($article === false) {
-	            return $relatedArticles;
-            }
 
-            // Get the title of the article
-            // XXX NOTE: There probably is a better way to get a list of 
-            // keywords from a post.  But, until then, just use the title. 
-            // (Assuming the author used a relevant title)
-
-            $title = $article->getTopic();
-            
-            // Split out the words
-            $keywords = explode(" ", $title);
-            
             $tempList = Array();
             
-            foreach($keywords as $word) {
-                if (strlen($word) >= $this->minWordLength) {
-                    // Build the list of articles that have this keyword
-                    lt_include( PLOG_CLASS_PATH."class/dao/searchengine.class.php" );
-                    lt_include( PLOG_CLASS_PATH."class/dao/articlestatus.class.php" );
-                    $searchEngine = new SearchEngine();
-                    $results = $searchEngine->search( $this->blogInfo->getId(), $word );	
-                    
-                    // Now add the article results to the internal list of related articles
+            lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" );
+            $articles = new Articles();	
 
-                    foreach( $results as $result ) {
-                        if( $result->getType() == SEARCH_RESULT_ARTICLE ) {
-                            $foundArticle = $result->getArticle();
-                            if ($foundArticle->getId() != $article->getId() ) {
-                                if (isset($tempList[$foundArticle->getId() ])) 
-                                    $tempList[$foundArticle->getId() ] += 1;
-                                else 
-                                    $tempList[$foundArticle->getId() ] = 1;
+	        
+	        if ($this->relatedArticleIdsCached($articleId)) {
+                // Look for the related articles in the cache
+	        
+	            $tempList = $this->loadArticleIds($articleId);
+	        } else {
+                
+                // Get the article specified by the article ID
+                $article = $articles->getArticle($articleId);
+                
+                if ($article === false) {
+                    return $relatedArticles;
+                }
+    
+                // Get the title of the article
+                // XXX NOTE: There probably is a better way to get a list of 
+                // keywords from a post.  But, until then, just use the title. 
+                // (Assuming the author used a relevant title)
+    
+                $title = $article->getTopic();
+                
+                // Split out the words
+                $keywords = explode(" ", $title);
+                
+                
+                foreach($keywords as $word) {
+                    if (strlen($word) >= $this->minWordLength) {
+                        // Build the list of articles that have this keyword
+                        lt_include( PLOG_CLASS_PATH."class/dao/searchengine.class.php" );
+                        lt_include( PLOG_CLASS_PATH."class/dao/articlestatus.class.php" );
+                        $searchEngine = new SearchEngine();
+                        $results = $searchEngine->search( $this->blogInfo->getId(), $word );	
+                        
+                        // Now add the article results to the internal list of related articles
+    
+                        foreach( $results as $result ) {
+                            if( $result->getType() == SEARCH_RESULT_ARTICLE ) {
+                                $foundArticle = $result->getArticle();
+                                if ($foundArticle->getId() != $article->getId() ) {
+                                    if (isset($tempList[$foundArticle->getId() ])) 
+                                        $tempList[$foundArticle->getId() ] += 1;
+                                    else 
+                                        $tempList[$foundArticle->getId() ] = 1;
+                                }
                             }
                         }
                     }
                 }
-            }
-            
-            // Now sort by score, in reverse order
-    		arsort($tempList);
+                
+                // Now sort by score, in reverse order
+                arsort($tempList);
+                
+                // Cache this list of related articles
+                $this->saveArticleIds( $articleId, $tempList);
+                
+    		}
 
             $numArticles = 0;
             // Now return the list of articles described by the entries in this array
             foreach( $tempList as $id=>$score) {
-                if ($numArticles > $this->numRelatedArticles) {
+                if ($numArticles >= $this->numRelatedArticles) {
                     break;
                 }
                 
                 $relatedArticle = $articles->getArticle( $id );
-                if ($relatedArticle) {
+                if ($relatedArticle && ($relatedArticle->getStatus() == POST_STATUS_PUBLISHED)) {
                     array_push( $relatedArticles, $relatedArticle);
                     $numArticles += 1;
                 }
@@ -143,17 +167,111 @@
             return $relatedArticles;
 	    }
 	    
+	    
+	    function saveArticleIds($articleId, $relatedArticles)
+	    {
+            // Create the serialized array
+            $saveFile = $this->cacheFolder."/".$articleId;  
+            
+            $unserializedData = Array( "timestamp" => time(),
+                                       "related" => $relatedArticles );
+
+            $serializedArray = serialize( $unserializedData );
+            $fh = fopen( $saveFile, "w");
+    
+            if ($fh) {
+                fwrite($fh, $serializedArray);
+            }
+            
+            fclose($fh);
+            File::chMod($saveFile, 0644);
+	    }
+	    
+	    
+	    function loadArticleIds($articleId)
+	    {
+            $saveFile = $this->cacheFolder."/".$articleId;  
+            
+            $articles = Array();
+    
+            if(File::isReadable($saveFile)){
+                $fh = fopen( $saveFile, "r");
+                if ($fh)
+                {
+                    $serializedData = fread($fh, filesize($saveFile));
+                    fclose($fh);
+                    
+                    $unserializedData = unserialize($serializedData);
+                    
+                    $articles = $unserializedData["related"];
+                }
+            }
+            return $articles;
+	    }
+	    
+	    //
+	    // Returns true if the list of related article Ids have been 
+	    // cached
+	    function relatedArticleIdsCached($articleId)
+	    {
+            $saveFile = $this->cacheFolder."/".$articleId;  
+	        
+            // If the file exists
+	        if( !File::exists($saveFile)) {
+	            return false;
+	        }
+	        
+	        // Make sure the file is readable
+            if( !File::isReadable($saveFile)){
+                return false;
+            }
+
+	        // If the setting states that we should never refresh the cache
+	        // we want to use the 
+	        if ($this->refreshInterval == -1)
+	        {
+	            return true;
+	        }
+
+            // If we are never supposed to use the cache, 
+            if ($this->refreshInterval == 0)
+	        {
+	            return false;
+	        }
+
+
+            $fh = fopen( $saveFile, "r");
+            if ($fh)
+            {
+                $serializedData = fread($fh, filesize($saveFile));
+                fclose($fh);
+                
+                $unserializedData = unserialize($serializedData);
+                
+                $timestamp = $unserializedData["timestamp"];
+
+                // Make sure that this data is not older that what we are allowing
+                // Get the difference in the times
+                $timeDiff = time() - $timestamp;
+                
+                if ($timeDiff > $this->refreshInterval * 60 * 60 )
+                {
+                    return false;
+                }
+            }
+            
+            return true;
+	    }
+
 	    function getPluginConfigurationKeys()
 		{			
 			return( Array(
 				Array( "name" => "plugin_related_enabled", "type" => "boolean" ),
 				Array( "name" => "plugin_related_num_articles", "type" => "integer" ),
 				Array( "name" => "plugin_related_min_word_length", "type" => "integer" ),
-				Array( "name" => "plugin_related_refresh_interval", "type" => "integer" ),
+				Array( "name" => "plugin_related_refresh_interval", "type" => "list", "options" => Array( "-1" => "-1", "0" => "0", "1" => "1", "24" => "24", "168" => "168", "720" => "720" )),
 			));
 		}
 		
-		
-
 	}
 ?>
\ No newline at end of file

Modified: plugins/branches/lifetype-1.2/related/templates/related.template
===================================================================
--- plugins/branches/lifetype-1.2/related/templates/related.template	2007-05-27 21:19:45 UTC (rev 5453)
+++ plugins/branches/lifetype-1.2/related/templates/related.template	2007-05-28 07:54:07 UTC (rev 5454)
@@ -34,6 +34,21 @@
           value="{$minWordLength}" width="10" /> 
   </div>
 
+  <div class="field">
+   <label for="size">{$locale->tr("related_cache_lifetime")}</label>
+   <span class="required">*</span>
+   <div class="formHelp">{$locale->tr("related_cache")}</div>
+   <span class="required"></span>
+   <select name="interval" id="interval" {user_cannot_override key=plugin_related_refresh_interval}disabled="disabled"{/user_cannot_override}>
+    <option value="-1" {if $interval==-1}selected="selected"{/if}>never refresh</option>
+    <option value="0" {if $interval==0}selected="selected"{/if}>don't cache</option>
+    <option value="1" {if $interval==1}selected="selected"{/if}>1 hour</option>
+    <option value="24" {if $interval==24}selected="selected"{/if}>1 day</option>
+    <option value="168" {if $interval==168}selected="selected"{/if}>1 week</option>
+    <option value="720" {if $interval==720}selected="selected"{/if}>1 month</option>
+   </select>
+ </div>
+
   
  </fieldset>  
 



More information about the pLog-svn mailing list