[pLog-svn] r2101 - plog/branches/plog-1.1-ben/class/dao

ork at devel.plogworld.net ork at devel.plogworld.net
Sun May 29 19:22:40 GMT 2005


Author: ork
Date: 2005-05-29 19:22:40 +0000 (Sun, 29 May 2005)
New Revision: 2101

Modified:
   plog/branches/plog-1.1-ben/class/dao/articles.class.php
Log:
tried to clean up a bit .. this is an unstable version, as the cache will not be cleaned yet. 
please dont use this version on a productive environment.


Modified: plog/branches/plog-1.1-ben/class/dao/articles.class.php
===================================================================
--- plog/branches/plog-1.1-ben/class/dao/articles.class.php	2005-05-29 19:17:33 UTC (rev 2100)
+++ plog/branches/plog-1.1-ben/class/dao/articles.class.php	2005-05-29 19:22:40 UTC (rev 2101)
@@ -1,7 +1,6 @@
 <?php
 
     include_once( PLOG_CLASS_PATH.'class/dao/model.class.php' );
-//  include_once( PLOG_CLASS_PATH.'class/dao/articlecommentstatus.class.php' );	
     include_once( PLOG_CLASS_PATH.'class/config/config.class.php' );
 
     /**
@@ -11,30 +10,59 @@
      */
     class Articles extends Model
     {
-
         // DAO objects that we keep for later, so that we don't have
         // to create them eeeeevery time!
-        var $categories = null;
-        var $comments   = null;
-        var $trackbacks = null;
-        var $users      = null;
-        var $blogs      = null;
-		var $customfields = null;
-		var $_blogInfo  = null;
+        var $categories    = null;
+        var $comments      = null;
+        var $trackbacks    = null;
+        var $users         = null;
+        var $blogs         = null;
+		var $customfields  = null;
+		var $_blogInfo     = null;
 		var $_blogSettings = null;
-		var $_timeDiff  = null;
+		var $_timeDiff     = null;
+        var $_memoryCache  = Array();
 
         function Articles()
         {
             $this->Model();
 			// fine, this is not very nice but it helps a lot, specially if all the classes
 			// that need to load articles share the same instance		
-			$this->cache      = Array();	
-			$this->_blogInfo  = null;
-			$this->_timeDiff  = 0;
+			$this->_memoryCache  = Array();	
+			$this->_blogInfo     = null;
+			$this->_timeDiff     = 0;
 			$this->_blogsettings = null;
         }
 
+        function getArticle( $articleId )
+        {
+            $this->log->info('called getArticle with id: ' . $articleId );
+
+            $article = $this->_cache->getData( $articleId, CACHE_ARTICLES );
+
+            if ( !$article ) {
+                include_once( PLOG_CLASS_PATH."class/database/db.class.php" );
+
+                $query = Db::buildSelectQuery( ARTICLES_TABLENAME,
+                                               array(),
+                                               'id',
+                                               $articleId );
+
+                // we send the query and then fetch the first array with the result
+                $result = $this->Execute( $query );
+
+                if ( $result->RecordCount() == 0)
+                    return false;
+
+                $article = $this->_fillArticleInformation( $result->FetchRow( $result ) );
+
+                // update the cache
+                // TBD: the cache will never be erased currently
+                $this->_cache->setData( $article->getId(), CACHE_ARTICLES, $article );
+            }
+            return $article;
+        }
+
         /**
          * Gets an article from the database, given its id
          *
@@ -45,21 +73,18 @@
          */
         function getUserArticle( $artId, $userId = -1 )
         {
-            $query = "SELECT * FROM ".$this->getPrefix()."articles WHERE id = '".Db::qstr($artId)."'";
-            if( $userId != -1 )
-                $query .= " AND user_id = '".Db::qstr($userId)."'";
-            $query .= ";";
-
-            // we send the query and then fetch the first array with the result
-            $result = $this->Execute( $query );
-
-            if ( $result->RecordCount() == 0)
+            $article =  $this->getArticle( $artId );
+            if( !$article )
                 return false;
 
-            $row = $result->FetchRow( $result );
+            if ( $userId != -1 ) {
+                $article      = $this->getArticle( $artId );
+                $articleOwner = $article->getUser();
 
-            $article = $this->_fillArticleInformation( $row );
-
+                if( $articleOwner->getId() != $userId ) {
+                    return false;
+                }
+            }
             return $article;
         }
 
@@ -72,23 +97,19 @@
          */
         function getBlogArticle( $artId, $blogId = -1 )
         {
-            // TBD: remove me
-            // function getBlogArticle( $artId, $blogId = -1, $includeHiddenFields = true, $date = -1, $categoryId = -1, $userId = -1, $status = null )
-            // $this->log->info("called getBlogArticle with: $artId, $blogId, $includeHiddenFields, $date, $categoryId, $userId, $status");
-			$prefix = $this->getPrefix();
-            $query = "SELECT a.id, a.date,
-			                 a.user_id,a.blog_id,a.status,a.properties,
-							 a.num_reads, a.slug FROM {$prefix}articles a ";
-			// thanks jon for the tip :) You're right that the amount of rows will be too big 
-            // if we don't really need these fields!
-			
-			$query .= "WHERE a.id = ".Db::qstr($artId);
-					 
+            $article = $this->getArticle( $artId );
+
+            if( !$article )
+                return false;
+
             if( $blogId != -1 ) {
-                $query .= " AND a.blog_id = ".Db::qstr($blogId);
+                $article     = $this->getArticle( $artId );
+
+                if( $article->getBlogId() != $blogId ) {
+                    return false;
+                }
             }
-
-            return $this->_getBlogArticleFromQuery( $query, $includeHiddenFields );
+            return $article;
         }
         
         /**
@@ -316,17 +337,63 @@
 		 * @param searchTerms in case we would like to further refine the filtering, we can also use search features
          * @return Returns an array with all the articles from the given blog
          */
-        function getBlogArticles( $blogid, $date = -1, $amount = -1, $categoryId = 0, $status = 0, $userId = 0, 
-                                  $maxDate = 0, $searchTerms = "", $page = -1 )
+
+        function getBlogArticles( $blogid, 
+                                  $date = -1, 
+                                  $amount = -1, 
+                                  $categoryId = 0, 
+                                  $status = POST_STATUS_PUBLISHED, 
+                                  $userId = 0, 
+                                  $maxDate = 0, 
+                                  $searchTerms = "", 
+                                  $page = -1 )
         {
             include_once( PLOG_CLASS_PATH.'class/dao/articlecategories.class.php' );
-            // $this->log->info("getBlogArticles $blogid, $date, $amount, $categoryId, $status, $userId, $maxDate, $searchTerms, $page");
+            include_once( PLOG_CLASS_PATH.'class/database/db.class.php' );
+
+
+
+
+            $this->log->info("getBlogArticles $blogid, $date, $amount, $categoryId, $status, $userId, $maxDate, $searchTerms, $page");
+
+            $whereConditions = array();
+
+            $whereConditions['blog_id'] = $blogid;
+
+            // check the parameters to adjust the whereConditions accordingly
+            if( $date != -1 ) {
+            }
+
+            if( $categoryId != 0 ) {
+            }
+
+            if( $status != POST_STATUS_PUBLISHED ) {
+                $whereConditions['status'] = $status;
+            }
+
+            if( $userId != 0 ) {
+            }
+
+            if( $maxDate != 0 ) {
+            }
+
+            if( $searchTerms != '' ) {
+            }
+
+            if( $page != -1 ) {
+            }
+
+            $query = Db::buildSelectQuery( ARTICLES_TABLENAME,
+                                           array('id'),
+                                           $whereConditions );
+
+            $this->log->info('new query: ' . $query );
+
             // build the query
             // the query gets quite complicated to build because we have to take care of plenty
             // of conditions, such as the maximum date, the amount, the category,
             // wether the category has to be shown in the main page or not, etc...
             $postStatus = $status;
-		    $prefix = $this->getPrefix();
 		    $where = $this->buildWhere( $blogid, $date, $amount, $categoryId, $status, $userId, $maxDate, $searchTerms, $page );
             $query = "SELECT a.id as id, a.id, a.date,
                              a.user_id,a.blog_id,a.status,a.properties,
@@ -358,6 +425,7 @@
 				$query .= " LIMIT $start, $amount";   
             }
                                                      
+            $this->log->info('old query: ' . $query );
 				
 			// execute the query
             $result = $this->Execute( $query );
@@ -407,7 +475,7 @@
 				//$article->setFields( $fields[$lastArticleId] );
 
 				$fullarticles[]=$article;
-				$this->cache[$lastArticleId] = $article;       
+				$this->_memoryCache[$lastArticleId] = $article;       
             }
             
             return $fullarticles;
@@ -1066,8 +1134,8 @@
             include_once( PLOG_CLASS_PATH."class/dao/bloginfo.class.php" );
 
 			$id = $query_result['id'];
-			if( isset($this->cache[$id])) {
-				return($this->cache[$id]);
+			if( isset($this->_memoryCache[$id])) {
+				return($this->_memoryCache[$id]);
 			}
 
             // this is a little dirty trick or otherwise the old
@@ -1130,7 +1198,7 @@
 			$article->setUserInfo( $this->users->getUserInfoFromId( $query_result['user_id'] ));			
 
 			// fill in the cache with the result we just loaded
-			$this->cache[$query_result['id']] = $article;
+			$this->_memoryCache[$query_result['id']] = $article;
 
             return $article;
         }
@@ -1146,8 +1214,8 @@
             include_once( PLOG_CLASS_PATH.'class/dao/blogs.class.php' );
 
 			$id = $query_result['id'];
-			if( isset($this->cache[$id])) {
-				return($this->cache[$id]);
+			if( isset($this->_memoryCache[$id])) {
+				return($this->_memoryCache[$id]);
 			}
 
             // this is a little dirty trick or otherwise the old




More information about the pLog-svn mailing list