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

ork at devel.plogworld.net ork at devel.plogworld.net
Tue May 31 15:29:31 GMT 2005


Author: ork
Date: 2005-05-31 15:29:30 +0000 (Tue, 31 May 2005)
New Revision: 2138

Modified:
   plog/branches/plog-1.1-ben/class/dao/articles.class.php
Log:
the basic caching is enabled, i guess the cache behave the way it should be.


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-31 13:55:51 UTC (rev 2137)
+++ plog/branches/plog-1.1-ben/class/dao/articles.class.php	2005-05-31 15:29:30 UTC (rev 2138)
@@ -363,19 +363,21 @@
 		}
 
         /**
-         * Returns all the articles for a given blog, according to the conditions specified in the call. If
-         * this function is too cumbersome to use (I reckon it might be, too many parameters that have been
-         * added over time due to new requirements) you may want to have a look at getBlogArticlesByQuery below.
-         * It's some kind of straight-through method that allows free-form SQL queries to be passed.
+         * Returns all the articles for a given blog, according to the conditions specified in 
+         * the call. If this function is too cumbersome to use (I reckon it might be, 
+         * too many parameters that have been added over time due to new requirements).
          *
          * @param blogId Identifier of the blog from where we want to fetch the articles
          * @param date date in MySQL TIMESTAMP(14) format
          * @param amount The maximum amount of posts that we would like to be returned.
-         * @param categoryId A category identifier. If specified, only the posts of the given category will be returned
+         * @param categoryId A category identifier. If specified, only the posts of 
+                             the given category will be returned
          * @param status If specified, only the posts with given status will be returned.
-         * @param userId If specified, only the posts that belong to the specified user will be returned
+         * @param userId If specified, only the posts that belong to the specified user 
+                         will be returned
          * @param maxDate a date in MySQL TIMESTAMP(14)
-		 * @param searchTerms in case we would like to further refine the filtering, we can also use search features
+		 * @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
          */
 
@@ -393,12 +395,15 @@
             $requestedArticles = $blogArticles->getArticleIdsByStatus( $status );
 
             if( $categoryId > 0 ) 
-                $requestedArticles = $blogArticles->getArticleIdsByCategory( $categoryId, $requestedArticles );
-
+                $requestedArticles = $blogArticles->getArticleIdsByCategory( $categoryId, 
+                                                                             $requestedArticles );
+            if( $userId > 0 )
+                $requestedArticles = $blogArticles->getArticleIdsByUserId( $userId,
+                                                                           $requestedArticles );
             if( $date != -1 )
-                $requestedArticles = $blogArticles->getArticleIdsByDate( $date, $requestedArticles );
+                $requestedArticles = $blogArticles->getArticleIdsByDate( $date, 
+                                                                         $requestedArticles );
 
-
             if( $maxDate != 0 )
                 $requestedArticles = $blogArticles->getArticleIdsBefore( $maxDate, 
                                                                          $requestedArticles );
@@ -409,10 +414,6 @@
             if( ($amount > 0) && ($date == -1) && ($page == -1 ) )
                 $requestedArticles = array_slice( $requestedArticles, 0, $amount );
 
-            if( $userId > 0 )
-                $requestedArticles = $blogArticles->getArticleIdsByUserId( $userId,
-                                                                           $requestedArticles );
-
             if( $page > 0 ) {
 				$start = (($page - 1) * $amount);
                 $requestedArticles = array_slice( $requestedArticles, $start, $amount );
@@ -426,50 +427,50 @@
         }
 		
         /**
-         * Returns all the articles for a given blog (takes a query string as an argument).
-         *
-         * @param query A valid SQL query that will be used to generate a list of articles to fetch.
-         * @return Returns an Array filled with Article objects or an empty array if none could be found or
-         * the query was invalid.
-         */
-        function getBlogArticlesByQuery( $query )
-        {
-            $result = $this->Execute( $query );
-            if( !$result ) return Array();
-            $articles = Array();
-            while( $row = $result->FetchRow()) {
-                $article = $this->_fillArticleInformation( $row );
-				$articles[] = $article;
-            }
-            return $articles;
-        }
-
-        /**
          * Gets the number of posts per month per year from the database
          *
-         * @param blogId The numeric identifier of the blog from which we'd like to calculate this values
+         * @param blogId The numeric identifier of the blog from which we'd like to 
+                         calculate this values
          * @return A 2-dimensional associative array where the first index is the year and the second
          * index is the number of the month: result[2003][11] = _posts for november 2003_
          */
         function getNumberPostsPerMonth( $blogId )
         {
-            // query to get the earliest post
-            // this must be my longest and most complex SQL query ever :)
-            $blogs = new Blogs();
-            $blogSettings = $blogs->getBlogSettings( $blogId );
 
-            if( $blogSettings->getValue( "show_future_posts_in_calendar" ))
-                $numPostsPerMonthQuery = "SELECT COUNT(*) AS 'count',YEAR(date) AS 'year',MONTH(date) AS 'month',DAYOFMONTH(date) AS 'daymonth' FROM ".$this->getPrefix()."articles WHERE status = 1 AND blog_id = ".$blogId." GROUP BY YEAR(date),MONTH(date) ORDER BY YEAR(date) DESC,MONTH(date) DESC;";
-            else
-                $numPostsPerMonthQuery = "SELECT COUNT(*) AS 'count',YEAR(date) AS 'year',MONTH(date) AS 'month',DAYOFMONTH(date) AS 'daymonth' FROM ".$this->getPrefix()."articles WHERE status = 1 AND blog_id = ".$blogId." AND date <= NOW() GROUP BY YEAR(date),MONTH(date) ORDER BY YEAR(date) DESC,MONTH(date) DESC;";
+            $archives = $this->_cache->getData( $blogId, CACHE_ARTICLESPERMONTH );
 
-            $result = $this->Execute( $numPostsPerMonthQuery);
-            if( $result == false )
-                return false;
+            if( !$archives ) {
+                require_once( PLOG_CLASS_PATH . 'class/dao/blogs.class.php' );
+                $blogs = new Blogs();
+                $blogSettings = $blogs->getBlogSettings( $blogId );
 
-            $archives = Array();
-            while( $row = $result->FetchRow()) {
-                $archives[$row["year"]][$row["month"]] = $row["count"];
+                if( $blogSettings->getValue("show_future_posts_in_calendar") )
+                    $numPostsPerMonthQuery = "SELECT COUNT(*) AS 'count',
+                                                YEAR(date) AS 'year',
+                                                MONTH(date) AS 'month'
+                                              FROM ".$this->getPrefix().ARTICLES_TABLENAME." 
+                                              WHERE status = 1 AND blog_id = ".$blogId." 
+                                              GROUP BY YEAR(date),MONTH(date) 
+                                              ORDER BY YEAR(date) DESC,MONTH(date) DESC;";
+                else
+                    $numPostsPerMonthQuery = "SELECT COUNT(*) AS 'count',
+                                                YEAR(date) AS 'year',
+                                                MONTH(date) AS 'month'
+                                              FROM ".$this->getPrefix().ARTICLES_TABLENAME." 
+                                              WHERE status = 1 AND blog_id = ".$blogId." 
+                                                AND date <= NOW() 
+                                              GROUP BY YEAR(date),MONTH(date) 
+                                              ORDER BY YEAR(date) DESC,MONTH(date) DESC;";
+
+                $result = $this->Execute( $numPostsPerMonthQuery);
+                if( $result == false )
+                    return false;
+
+                $archives = Array();
+                while( $row = $result->FetchRow()) {
+                    $archives[$row["year"]][$row["month"]] = $row["count"];
+                }
+                $this->_cache->setData( $blogId, CACHE_ARTICLESPERMONTH, $archives );
             }
 
             return $archives;
@@ -649,6 +650,7 @@
 
             // and finally clear the cache :)
             $this->_cache->removeData( $newArticle->getBlogId(), CACHE_BLOGARTICLES );
+            $this->_cache->removeData( $newArticle->getBlogId(), CACHE_ARTICLESPERMONTH );
 
             return $postId;
         }
@@ -793,11 +795,14 @@
 			// update the article text
 			$this->updateArticleText( $article );
 
-            if( !$this->updatePostCategoriesLink( $article->getId(), $article->getCategoryIds()))
+            if( !$this->updatePostCategoriesLink( $article->getId(), $article->getCategoryIds()) ||
+                !$this->updateArticleCustomFields( $article->getId(), $article->getBlog(), 
+                                                   $article->getFields()) )
                 return false;
 
-            if( !$this->updateArticleCustomFields( $article->getId(), $article->getBlog(), $article->getFields()))
-                return false;
+            // clean up the cache
+            $this->_cache->removeData( $article->getBlogId(), CACHE_ARTICLESPERMONTH );
+            $this->_cache->removeData( $article->getId(), CACHE_ARTICLES );
 
             return true;
         }
@@ -821,7 +826,9 @@
                 return false;
 
             foreach( $fields as $field ) {
-                $customFields->addCustomFieldValue( $field->getFieldId(), $field->getValue(), $artId, $blogId );
+                $customFields->addCustomFieldValue( $field->getFieldId(), 
+                                                    $field->getValue(), 
+                                                    $artId, $blogId );
             }
 
             return true;
@@ -943,6 +950,7 @@
                 return false;
 
             $this->_cache->removeData( $blogId, CACHE_BLOGARTICLES );
+            $this->_cache->removeData( $blogId, CACHE_ARTICLESPERMONTH );
             $this->_cache->removeData( $artId, CACHE_ARTICLES );
 
             return true;
@@ -958,9 +966,11 @@
 		 */
 		function deleteArticleText( $articleId )
 		{
-			$query = "DELETE FROM ".$this->getPrefix()."articles_text WHERE article_id = '".Db::qstr($articleId)."'";
+            $query = Db::buildDeleteQuery( ARTICLETEXTS_TABLENAME,
+                                           'article_id',
+                                           $articleId );
 			
-			return( $this->Execute( $query ));
+			return( $this->Execute($query) );
 		}
 
         /**
@@ -1025,7 +1035,8 @@
             foreach( $blogArticles as $article ) {
                 // the deleteArticle method will also take care of removing comments and
                 // trackbacks
-                $this->deleteArticle( $article->getId(), $article->getUser(), $article->getBlog(), true );
+                $this->deleteArticle( $article->getId(), $article->getUser(), 
+                                      $article->getBlog(), true );
             }
 
             return true;




More information about the pLog-svn mailing list