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

pwestbro at devel.lifetype.net pwestbro at devel.lifetype.net
Sat May 26 05:13:47 EDT 2007


Author: pwestbro
Date: 2007-05-26 05:13:47 -0400 (Sat, 26 May 2007)
New Revision: 5447

Added:
   plugins/branches/lifetype-1.2/related/
   plugins/branches/lifetype-1.2/related/Readme.txt
   plugins/branches/lifetype-1.2/related/class/
   plugins/branches/lifetype-1.2/related/class/action/
   plugins/branches/lifetype-1.2/related/class/action/pluginrelatedconfigaction.class.php
   plugins/branches/lifetype-1.2/related/class/action/pluginrelatedupdateconfigaction.class.php
   plugins/branches/lifetype-1.2/related/class/view/
   plugins/branches/lifetype-1.2/related/class/view/pluginrelatedconfigview.class.php
   plugins/branches/lifetype-1.2/related/locale/
   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/
   plugins/branches/lifetype-1.2/related/templates/related.template
Log:
Checkpoint for new "related" plugin.  Ths will return a list of related
posts to a given post.

The user can configure the maximum number of posts to be returned, and the
minimum size of keyword to be used.

There are two things that I still want to do with this plugin:

1) Implement a better caching.  Currently when LifeType's cache is
deleted/cleared, the list of related posts is recalculated.  This is very
heavy, especially since the cache is blown away when most plugin modify a
setting, or a new post is added.

I was thinking that the array of related post ids could be serialized and
saved to the file system.  Then when displaying the list of related
articles, the searches don't have to be run

2) Implement a better way to get keywords from a post.  Currently the words
in the post title are use as the keywords of the post.  This works since,
generally, the title should be a description of the post.  Also the title
doesn't have html tags, so it is easy to parse.

Ideally this would find the keyword from the body and title of the post.  I
think that this will require the caching described above, as the could make
generating list of related posts more expensive.


Added: plugins/branches/lifetype-1.2/related/Readme.txt
===================================================================
--- plugins/branches/lifetype-1.2/related/Readme.txt	                        (rev 0)
+++ plugins/branches/lifetype-1.2/related/Readme.txt	2007-05-26 09:13:47 UTC (rev 5447)
@@ -0,0 +1,35 @@
+Plugin: Related Posts
+Author: Paul Westbrook
+Release Date: 05/25/2007
+Version: 20070525
+
+The Related Post plugin returns a list of posts related to a specified one.
+
+Usage:
+
+1) Enable the plugin
+2) Add the following code to the postandcomments.template
+
+<!--TagCloud-->
+		{if $related && $related->isEnabled() }
+            {assign var=postid value=$post->getId()}
+            {assign var=recentsrelatedposts value=$related->relatedArticles($postid)}
+            {if count($recentsrelatedposts) > 0 }
+                {$locale->tr("related_plugin_display")}:
+                <ul>
+                {foreach from=$recentsrelatedposts item=relatedpost}
+                    <li>
+                    <a title='{$locale->tr("permalink_title")}: {$relatedpost->getTopic()}' href="{$url->postLink($relatedpost)}">
+                    {$relatedpost->getTopic()}
+                    </a>
+                    </li>
+                {/foreach}
+                </ul>
+            {/if}
+        {/if}
+
+
+TODO :
+
+Add better caching
+Changes to generate better keywords from a given article
\ No newline at end of file

Added: plugins/branches/lifetype-1.2/related/class/action/pluginrelatedconfigaction.class.php
===================================================================
--- plugins/branches/lifetype-1.2/related/class/action/pluginrelatedconfigaction.class.php	                        (rev 0)
+++ plugins/branches/lifetype-1.2/related/class/action/pluginrelatedconfigaction.class.php	2007-05-26 09:13:47 UTC (rev 5447)
@@ -0,0 +1,49 @@
+<?php
+
+    /*
+    Copyright 2006 Paul Westbrook (paul at westbrooks.org)
+    
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+    
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+    
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+    */
+
+	lt_include( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
+    lt_include( PLOG_CLASS_PATH."plugins/related/class/view/pluginrelatedconfigview.class.php" ); 
+
+    class PluginRelatedConfigAction extends AdminAction 
+	{
+
+    	/**
+         * Constructor. If nothing else, it also has to call the constructor of the parent
+         * class, BlogAction with the same parameters
+         */
+        function PluginRelatedConfigAction( $actionInfo, $request )
+        {
+        	$this->AdminAction( $actionInfo, $request );
+        }
+
+        /**
+         * Carries out the specified action
+         */
+        function perform()
+        {
+			$this->_view = new PluginRelatedConfigView( $this->_blogInfo );
+
+            $this->setCommonData();
+
+            // better to return true if everything fine
+            return true;
+        }
+    }
+?>

Added: plugins/branches/lifetype-1.2/related/class/action/pluginrelatedupdateconfigaction.class.php
===================================================================
--- plugins/branches/lifetype-1.2/related/class/action/pluginrelatedupdateconfigaction.class.php	                        (rev 0)
+++ plugins/branches/lifetype-1.2/related/class/action/pluginrelatedupdateconfigaction.class.php	2007-05-26 09:13:47 UTC (rev 5447)
@@ -0,0 +1,139 @@
+<?php
+
+    /*
+    Copyright 2006 Paul Westbrook (paul at westbrooks.org)
+    
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+    
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+    
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+    */
+
+	lt_include( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
+    lt_include( PLOG_CLASS_PATH."plugins/related/class/view/pluginrelatedconfigview.class.php" ); 
+
+    $apiKeyValid = true;
+    
+    class PluginRelatedUpdateConfigAction extends AdminAction 
+	{
+	
+		var $_pluginEnabled;
+		var $_numRelatedArticles;
+		var $_minWordLength;
+		var $_refreshInterval;
+
+
+    	/**
+         * Constructor. If nothing else, it also has to call the constructor of the parent
+         * class, BlogAction with the same parameters
+         */
+        function PluginRelatedUpdateConfigAction( $actionInfo, $request )
+        {
+        	$this->AdminAction( $actionInfo, $request );
+        }
+		
+		function validate()
+		{
+
+            $this->_pluginEnabled = $this->_request->getValue( "pluginEnabled" );
+            $this->_pluginEnabled = ($this->_pluginEnabled != "" );			
+        
+
+            $this->_numRelatedArticles = $this->_request->getValue( "numArticles" );
+            $this->_minWordLength = $this->_request->getValue( "minWordLength" );
+            $this->_refreshInterval = $this->_request->getValue( "refreshInterval" );
+            
+            lt_include( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );
+                
+            if( $this->_pluginEnabled == true ) {
+                if( $this->_numRelatedArticles == "" ) {
+                    $this->_view = new PluginRelatedConfigView( $this->_blogInfo );
+                    $this->_view->setErrorMessage( $this->_locale->tr("related_missing_num_articles"));
+                    $this->setCommonData();
+    
+                    return false;    
+                }
+                else {
+                    $val1 = new IntegerValidator();
+                    if( !$val1->validate( $this->_numRelatedArticles )) {
+                        $this->_view = new PluginRelatedConfigView( $this->_blogInfo );
+                        $this->_view->setErrorMessage( $this->_locale->tr("related_invalid_num_articles"));
+                        $this->setCommonData();
+        
+                        return false;    
+                    }
+
+                }
+
+                
+                if( $this->_minWordLength == "" ) {
+                    $this->_view = new PluginRelatedConfigView( $this->_blogInfo );
+                    $this->_view->setErrorMessage( $this->_locale->tr("related_missing_min_length"));
+                    $this->setCommonData();
+    
+                    return false;                
+                }
+                else {
+                    $val2 = new IntegerValidator();
+                    if( !$val2->validate( $this->_minWordLength )) {
+                        $this->_view = new PluginRelatedConfigView( $this->_blogInfo );
+                        $this->_view->setErrorMessage( $this->_locale->tr("related_invalid_min_length"));
+                        $this->setCommonData();
+        
+                        return false;    
+                    }
+                }
+            }
+			return true;
+		}
+		
+        /**
+         * Carries out the specified action
+         */
+        function perform()
+        {
+            // update the plugin configurations to blog setting
+			$blogSettings = $this->_blogInfo->getSettings();
+            $blogSettings->setValue( "plugin_related_enabled", $this->_pluginEnabled );
+            $blogSettings->setValue( "plugin_related_num_articles", $this->_numRelatedArticles );
+            $blogSettings->setValue( "plugin_related_min_word_length", $this->_minWordLength );
+            $blogSettings->setValue( "plugin_related_refresh_interval", $this->_refreshInterval );
+
+            $this->_blogInfo->setSettings( $blogSettings ); 
+		
+			// save the blogs settings
+			$blogs = new Blogs();
+            if( !$blogs->updateBlog( $this->_blogInfo )) {
+                $this->_view = new PluginRelatedConfigView( $this->_blogInfo );
+                $this->_view->setErrorMessage( $this->_locale->tr("error_updating_settings"));
+                $this->setCommonData();
+
+                return false;                       
+            }
+			
+			// if everything went ok...
+            $this->_blogInfo->setSettings( $blogSettings );
+            $this->_session->setValue( "blogInfo", $this->_blogInfo );
+            $this->saveSession();
+			
+			$this->_view = new PluginRelatedConfigView( $this->_blogInfo );
+			$this->_view->setSuccessMessage( $this->_locale->tr("related_settings_saved_ok"));			
+			$this->setCommonData();
+			
+			// clear the cache
+			CacheControl::resetBlogCache( $this->_blogInfo->getId());					
+            
+            return true;		
+        }
+    }
+    
+?>

Added: plugins/branches/lifetype-1.2/related/class/view/pluginrelatedconfigview.class.php
===================================================================
--- plugins/branches/lifetype-1.2/related/class/view/pluginrelatedconfigview.class.php	                        (rev 0)
+++ plugins/branches/lifetype-1.2/related/class/view/pluginrelatedconfigview.class.php	2007-05-26 09:13:47 UTC (rev 5447)
@@ -0,0 +1,53 @@
+<?php
+	
+    /*
+    Copyright 2006 Paul Westbrook (paul at westbrooks.org)
+    
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+    
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+    
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+    */
+
+	lt_include( PLOG_CLASS_PATH."class/view/admin/adminplugintemplatedview.class.php" );
+
+	/**
+	 * implements the main view of the validate trackback plugin
+	 */
+	class PluginRelatedConfigView extends AdminPluginTemplatedView
+	{
+
+		function PluginRelatedConfigView( $blogInfo )
+		{
+			$this->AdminPluginTemplatedView( $blogInfo, "related", "related" );
+		}
+		
+		function render()
+		{
+			// load some configuration settings
+			$blogSettings = $this->_blogInfo->getSettings();
+			$pluginEnabled = $blogSettings->getValue( "plugin_related_enabled" );			
+			$numArticles = $blogSettings->getValue( "plugin_related_num_articles" );			
+			$minWordLength = $blogSettings->getValue( "plugin_related_min_word_length" );			
+			$refreshInterval = $blogSettings->getValue( "plugin_related_refresh_interval" );			
+			
+
+			// create a view and export the settings to the template
+			$this->setValue( "pluginEnabled", $pluginEnabled );
+			$this->setValue( "numArticles", $numArticles );
+			$this->setValue( "minWordLength", $minWordLength );
+			$this->setValue( "refreshInterval", $refreshInterval );
+
+			parent::render();
+		}
+	}
+?>
\ No newline at end of file

Added: plugins/branches/lifetype-1.2/related/locale/locale_en_UK.php
===================================================================
--- plugins/branches/lifetype-1.2/related/locale/locale_en_UK.php	                        (rev 0)
+++ plugins/branches/lifetype-1.2/related/locale/locale_en_UK.php	2007-05-26 09:13:47 UTC (rev 5447)
@@ -0,0 +1,25 @@
+<?php
+$messages["manageAppearancePlugins"] = "Appearance Management";
+$messages["relatedPluginSettings"] = "Related Posts";
+$messages["related"] = "Related Posts";
+
+$messages["related_plugin_enabled"] = "Enable this plugin";
+$messages["related_plugin"] = "Related Posts Plugin";
+$messages["related_plugin_display"] = "Related Articles";
+
+$messages["related_settings_saved_ok"] = "Related Posts settings saved successfully!";
+$messages["related_missing_num_articles"] = "Number of articles needs to be specified";
+$messages["related_invalid_num_articles"] = "Number of articles needs to be an integer";
+$messages["related_missing_min_length"] = "Minimum keyword length needs to be specified";
+$messages["related_invalid_min_length"] = "Minumum keyword length needs to be an integer";
+
+$messages["related_articles"] = "Number of related articles to return.";
+$messages["related_word_length"] = "Minimum length of keyword used to generate related article.";
+
+$messages["label_configuration"] = "Configuration";
+$messages["label_enable"] = "Enable";
+
+$messages["related_max_articles"] = "Number Articles";
+$messages["related_min_word_length"] = "Minimum Keyword Length";
+
+?>
\ No newline at end of file

Added: plugins/branches/lifetype-1.2/related/pluginrelated.class.php
===================================================================
--- plugins/branches/lifetype-1.2/related/pluginrelated.class.php	                        (rev 0)
+++ plugins/branches/lifetype-1.2/related/pluginrelated.class.php	2007-05-26 09:13:47 UTC (rev 5447)
@@ -0,0 +1,159 @@
+<?php
+
+    /*
+    Copyright 2006 Paul Westbrook (paul at westbrooks.org)
+    
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+    
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+    
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+    */
+
+	lt_include( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );
+
+	class PluginRelated extends PluginBase
+	{
+		var $pluginEnabled;
+		var $numRelatedArticles;
+		var $minWordLength;
+		var $refreshInterval;
+		
+		function PluginRelated( $source = "" )
+		{
+			$this->PluginBase($source);
+
+			$this->id      = "related";
+			$this->desc    = "The Related plugin will generate a list of related posts.";
+			$this->author  = "Paul Westbrook";
+			$this->locales = Array( "en_UK" );
+            $this->version = "20070525";
+
+
+			if( $source == "admin" )
+				$this->initAdmin();
+		}
+
+		function initAdmin()
+		{
+            $this->registerAdminAction( "related", "PluginRelatedConfigAction" );
+			$this->registerAdminAction( "updateRelatedConfig", "PluginRelatedUpdateConfigAction" );
+			
+			$menu =& Menu::getMenu();
+			if( !$menu->entryExists( "/menu/controlCenter/manageAppearancePlugins" ))						
+				$this->addMenuEntry( "/menu/controlCenter", "manageAppearancePlugins", "", "", true, false );			
+            $this->addMenuEntry( "/menu/controlCenter/manageAppearancePlugins", "related", "?op=related", "" );            
+		}
+
+		function register()
+		{
+		    $blogSettings = $this->blogInfo->getSettings();
+			$this->pluginEnabled = $blogSettings->getValue( "plugin_related_enabled" );
+			$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" );
+		}
+
+	    function isEnabled()
+	    {
+	        return $this->pluginEnabled;
+	    }
+	    
+	    
+	    function relatedArticles($articleId)
+	    {
+	        $relatedArticles = Array();
+	        
+	        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
+
+                    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);
+
+            $numArticles = 0;
+            // Now return the list of articles described by the entries in this array
+            foreach( $tempList as $id=>$score) {
+                if ($numArticles > $this->numRelatedArticles) {
+                    break;
+                }
+                
+                $relatedArticle = $articles->getArticle( $id );
+                if ($relatedArticle) {
+                    array_push( $relatedArticles, $relatedArticle);
+                    $numArticles += 1;
+                }
+            }
+
+            return $relatedArticles;
+	    }
+	    
+	    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" ),
+			));
+		}
+		
+		
+
+	}
+?>
\ No newline at end of file

Added: plugins/branches/lifetype-1.2/related/templates/related.template
===================================================================
--- plugins/branches/lifetype-1.2/related/templates/related.template	                        (rev 0)
+++ plugins/branches/lifetype-1.2/related/templates/related.template	2007-05-26 09:13:47 UTC (rev 5447)
@@ -0,0 +1,47 @@
+{include file="$admintemplatepath/header.template"}
+{include file="$admintemplatepath/navigation.template" showOpt=related title=$locale->tr("related_plugin")}
+<form name="relatedPluginSettings" method="post">
+ <fieldset class="inputField">
+ <legend>{$locale->tr("label_configuration")}</legend>  
+  {include file="$admintemplatepath/successmessage.template"}
+  {include file="$admintemplatepath/errormessage.template"}   
+  <div class="field">
+   <label for="pluginEnabled">{$locale->tr("label_enable")}</label>
+   <div class="formHelp">   
+    <input class="checkbox" type="checkbox" name="pluginEnabled" id="pluginEnabled" {if $pluginEnabled} checked="checked" {/if} value="1" />{$locale->tr("related_plugin_enabled")}
+   </div>
+  </div>
+  
+   <div class="field">
+   <label for="width">{$locale->tr("related_max_articles")}</label>
+   <span class="required">*</span>
+   <div class="formHelp">{$locale->tr("related_articles")}</div>
+   <input class="text" type="text" name="numArticles" id="numArticles"
+          {user_cannot_override
+              key=plugin_related_num_articles}readonly="readonly"
+          {/user_cannot_override}
+          value="{$numArticles}" width="10" /> 
+  </div>
+
+  <div class="field">
+   <label for="width">{$locale->tr("related_min_word_length")}</label>
+   <span class="required">*</span>
+   <div class="formHelp">{$locale->tr("related_word_length")}</div>
+   <input class="text" type="text" name="minWordLength" id="minWordLength"
+           {user_cannot_override
+               key=plugin_related_min_word_length}readonly="readonly"
+           {/user_cannot_override}
+          value="{$minWordLength}" width="10" /> 
+  </div>
+
+  
+ </fieldset>  
+
+ <div class="buttons">
+  <input type="hidden" name="op" value="updateRelatedConfig" />
+  <input type="reset" name="{$locale->tr("reset")}" />    
+  <input type="submit" name="{$locale->tr("update_settings")}" value="{$locale->tr("update")}" />
+ </div>
+</form>
+{include file="$admintemplatepath/footernavigation.template"}
+{include file="$admintemplatepath/footer.template"}
\ No newline at end of file



More information about the pLog-svn mailing list