[pLog-svn] r2136 - plog/branches/plog-1.1-ben/class/dao
ork at devel.plogworld.net
ork at devel.plogworld.net
Tue May 31 13:55:15 GMT 2005
Author: ork
Date: 2005-05-31 13:55:14 +0000 (Tue, 31 May 2005)
New Revision: 2136
Modified:
plog/branches/plog-1.1-ben/class/dao/articles.class.php
Log:
further refactoring
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:54:29 UTC (rev 2135)
+++ plog/branches/plog-1.1-ben/class/dao/articles.class.php 2005-05-31 13:55:14 UTC (rev 2136)
@@ -2,6 +2,7 @@
include_once( PLOG_CLASS_PATH.'class/dao/model.class.php' );
include_once( PLOG_CLASS_PATH.'class/dao/article.class.php' );
+ include_once( PLOG_CLASS_PATH.'class/dao/articlestatus.class.php' );
include_once( PLOG_CLASS_PATH.'class/config/config.class.php' );
/**
@@ -22,14 +23,10 @@
var $_blogInfo = null;
var $_blogSettings = 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->_memoryCache = Array();
$this->_blogInfo = null;
$this->_timeDiff = 0;
$this->_blogsettings = null;
@@ -44,8 +41,6 @@
*/
function getArticle( $articleId )
{
- $this->log->info('called getArticle with id: ' . $articleId );
-
$article = $this->_cache->getData( $articleId, CACHE_ARTICLES );
if ( !$article ) {
@@ -64,8 +59,6 @@
$article = $this->_fillArticleInformation( $result->FetchRow( $result ) );
- // update the cache
- // :TODO: the cache will never be erased currently
$this->_cache->setData( $article->getId(), CACHE_ARTICLES, $article );
}
return $article;
@@ -92,7 +85,7 @@
// sql statement or how to use an abstract method to build an statement
// like this one.
$query = "SELECT a.id as id, c.category_id as category_id, a.date as date,
- a.status as status
+ a.status as status, a.user_id as user_id
FROM {$prefix}article_categories_link c LEFT JOIN {$prefix}articles a
ON a.id = c.article_id
WHERE a.blog_id = $blogId
@@ -346,15 +339,7 @@
*/
function searchBlogArticles( $blogId, $searchTerms )
{
- $posts = $this->getBlogArticles( $blogId,
- -1, // no date
- -1, // no amount
- 0, // no category id
- 0, // not any status
- 0, // not belonging to any user id in particular
- 0, // no maximum date
- $searchTerms // the only thing we're taking into account: the search terms!!
- );
+ // :TODO: implement this :-)
return $posts;
}
@@ -371,14 +356,8 @@
$maxDate = 0,
$searchTerms = "" )
{
- $articles = $this->getBlogArticles( $blogId,
- $date,
- $amount,
- $categoryId,
- $status,
- $userId,
- $maxDate,
- $searchTerms );
+ $articles = $this->getBlogArticles( $blogId, $date, $amount, $categoryId, $status,
+ $userId, $maxDate, $searchTerms );
return count($articles);
}
@@ -401,40 +380,39 @@
*/
function getBlogArticles( $blogId,
- $date = -1,
- $amount = -1,
- $categoryId = 0,
- $status = POST_STATUS_PUBLISHED,
- $userId = 0,
- $maxDate = 0,
+ $date = -1,
+ $amount = -1,
+ $categoryId = 0,
+ $status = POST_STATUS_PUBLISHED,
+ $userId = 0,
+ $maxDate = 0,
$searchTerms = "",
- $page = -1 )
+ $page = -1 )
{
$blogArticles = $this->getArticles( $blogId );
$requestedArticles = $blogArticles->getArticleIdsByStatus( $status );
- if( $categoryId > 0 ) {
+ if( $categoryId > 0 )
$requestedArticles = $blogArticles->getArticleIdsByCategory( $categoryId, $requestedArticles );
- }
- if( $date != -1 ) {
+ if( $date != -1 )
$requestedArticles = $blogArticles->getArticleIdsByDate( $date, $requestedArticles );
- }
- if( $maxDate != 0 ) {
- $requestedArticles = $blogArticles->getArticleIdsBefore( $maxDate, $requestedArticles );
- }
+ if( $maxDate != 0 )
+ $requestedArticles = $blogArticles->getArticleIdsBefore( $maxDate,
+ $requestedArticles );
- if( $searchTerms != '' ) {
- // :TODO: implement this :-)
+ if( $searchTerms != '' )
$requestedArticles = $this->searchArticles( $searchTerms, $requestedArticles );
- }
- if( ($amount > 0) && ($date == -1) && ($page == -1 ) ) {
+ 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 );
@@ -447,35 +425,6 @@
return $articles;
}
- /**
- * returns the text of a bunch of articles, given their ids
- *
- * @param articleIds
- * @return an array, where the key is the article id and the value is an another associative array with the
- * text and topics of the articles
- */
- function getArticlesText( $articleIds )
- {
- $prefix = $this->getPrefix();
- $query = "SELECT * FROM {$prefix}articles_text WHERE
- article_id IN (".$articleIds.")";
-
- $result = $this->Execute( $query );
-
- // it's impossible that an article has no categories, but
- // we'll bear with it...
- if( !$result )
- return Array();
-
- // otherwise, fetch them
- while ($row = $result->FetchRow()) {
- $lastArticleId=$row["article_id"];
- $postTexts[$lastArticleId]=$row;
- }
-
- return( $postTexts );
- }
-
/**
* Returns all the articles for a given blog (takes a query string as an argument).
*
@@ -608,13 +557,19 @@
*/
function addPostCategoriesLink( $articleId, $categories )
{
+ require_once( PLOG_CLASS_PATH.'class/database/db.class.php' );
+
// nothing to do if the $categories array is not ehem, an array :)
if( !is_array( $categories ))
return true;
foreach( $categories as $categoryId ) {
- $query = "INSERT INTO ".$this->getPrefix()."article_categories_link
- (article_id, category_id) VALUES ($articleId, $categoryId )";
+ $keyValuePairs = array();
+ $keyValuePairs['article_id'] = $articleId;
+ $keyValuePairs['category_id'] = $categoryId;
+
+ $query = Db::buildInsertQuery( ARTICLE_CATEGORIES_RELATIONSHIP_TABLENAME,
+ $keyValuePairs );
$this->Execute( $query );
}
@@ -627,9 +582,12 @@
*/
function deletePostCategoriesLink( $articleId )
{
- $query = "DELETE FROM ".$this->getPrefix()."article_categories_link
- WHERE article_id = $articleId";
+ require_once( PLOG_CLASS_PATH.'class/database/db.class.php' );
+ $query = Db::buildDeleteQuery( ARTICLE_CATEGORIES_RELATIONSHIP_TABLENAME,
+ 'article_id',
+ $articleId );
+
return $this->Execute( $query );
}
@@ -760,13 +718,19 @@
function updateArticleText( $article )
{
$filter = new Textfilter();
- $query = "UPDATE ".$this->getPrefix()."articles_text SET
- topic = '".Db::qstr($article->getTopic())."'".
- ", text = '".Db::qstr($article->getText(false))."'".
- ", normalized_text = '".$filter->normalizeText(Db::qstr($article->getText(false)))."'".
- ", normalized_topic = '".$filter->normalizeText(Db::qstr($article->getTopic()))."'".
- " WHERE article_id = ".$article->getId().";";
-
+
+ $keyValuePairs = array();
+
+ $keyValuePairs['topic'] = $article->getTopic();
+ $keyValuePairs['text'] = $article->getText(false);
+ $keyValuePairs['normalized_text'] = $filter->normalizeText( $article->getText(false) );
+ $keyValuePairs['normalized_topic'] = $filter->normalizeText( $article->getTopic() );
+
+ $query = Db::buildUpdateQuery( ARTICLETEXTS_TABLENAME,
+ $keyValuePairs,
+ 'article_id',
+ $article->getId() );
+
$this->_cache->removeData( $article->getId(), CACHE_ARTICLETEXT );
return($this->Execute( $query ));
@@ -785,7 +749,8 @@
$customFields = new CustomFieldsValues();
foreach( $fields as $field ) {
- $customFields->addCustomFieldValue( $field->getFieldId(), $field->getValue(), $artId, $blogId );
+ $customFields->addCustomFieldValue( $field->getFieldId(), $field->getValue(),
+ $artId, $blogId );
}
return true;
@@ -805,15 +770,19 @@
$articleDate = Timestamp::getDateWithOffset( $article->getDate(), -($article->getTimeOffset()));
$filter = new Textfilter();
- $query = "UPDATE ".$this->getPrefix()."articles SET id = ".$article->getId().
- ", user_id = ".$article->getUser().
- ", date = ".$articleDate.
- ", blog_id = ".$article->getBlog().
- ", status = '".$article->getStatus()."'".
- ", num_reads = ".$article->getNumReads().
- ", properties = '".serialize($article->getProperties())."'".
- ", slug = '".$article->getPostSlug()."'".
- " WHERE id = ".$article->getId().";";
+ $keyValuePairs = array();
+ $keyValuePairs['user_id'] = $article->getUser();
+ $keyValuePairs['date'] = $articleDate;
+ $keyValuePairs['blog_id'] = $article->getBlogId();
+ $keyValuePairs['status'] = $article->getStatus();
+ $keyValuePairs['num_reads'] = $article->getNumReads();
+ $keyValuePairs['properties'] = serialize( $article->getProperties() );
+ $keyValuePairs['slug'] = $article->gePostSlug();
+
+ $query = Db::buildUpdateQuery( ARTICLES_TABLENAME,
+ $keyValuePairs,
+ 'id',
+ $article->getId() );
$result = $this->Execute( $query );
@@ -869,14 +838,27 @@
*/
function updateArticleNumReads( $articleId )
{
- // we have to build up the query, which will be pretty long...
- $query = "UPDATE ".$this->getPrefix()."articles SET ".
- " num_reads = num_reads+1 ".
- ", date = date ".
- " WHERE id = '".Db::qstr($articleId)."'";
+ require_once( PLOG_CLASS_PATH . 'class/database/db.class.php' );
+ $article = $this->getArticle( $articleId );
+ $numReads = $article->getNumReads() + 1;
+
+ $article->setNumReads( $numReads );
+
+ $keyValuePairs = array();
+ $keyValuePairs['num_reads'] = $numReads;
+ $keyValuePairs['date'] = '@date';
+
+ $query = Db::buildUpdateQuery( ARTICLES_TABLENAME,
+ $keyValuePairs,
+ 'id',
+ $articleId );
+
$result = $this->Execute( $query );
+ if( $result )
+ $this->_cache->setData( $articleId, CACHE_ARTICLES, $article );
+
return $result;
}
@@ -922,6 +904,7 @@
function deleteArticle( $artId, $userId, $blogId, $forever = false )
{
include_once( PLOG_CLASS_PATH.'class/dao/articlecomments.class.php' );
+ include_once( PLOG_CLASS_PATH.'class/database/db.class.php' );
if( $forever ) {
$query = "DELETE FROM ".$this->getPrefix()."articles WHERE id = ".$artId." AND user_id = ".$userId." AND blog_id = ".$blogId.";";
@@ -941,7 +924,14 @@
$customFields->removeArticleCustomFields( $artId );
}
else {
- $query = "UPDATE ".$this->getPrefix()."articles SET date = date, status = 3 WHERE id = ".$artId." AND user_id = ".$userId." AND blog_id = ".$blogId.";";
+ $keyValuePairs = array();
+ $keyValuePairs['date'] = '@date';
+ $keyValuePairs['status'] = POST_STATUS_DELETED;
+
+ $query = Db::buildUpdateQuery( ARTICLES_TABLENAME,
+ $keyValuePairs,
+ 'id',
+ $artId );
}
$result = $this->Execute( $query );
@@ -1048,9 +1038,13 @@
*/
function purgePosts()
{
- $query = "SELECT * FROM ".$this->getPrefix()."articles WHERE status = 3";
+ $query = Db::buildSelectQuery( ARTICLES_TABLENAME,
+ array('id', 'user_id', 'blog_id'),
+ 'status',
+ POST_STATUS_DELETED );
$result = $this->Execute( $query );
+
if( !$result )
return false;
@@ -1070,31 +1064,22 @@
if( $this->categories == null )
$this->categories = new ArticleCategories();
+
return $this->categories->getArticleCategories( $articleId, $blogId );
}
/**
- * returns all the categories that an article has been assigned to
+ * sets a time difference that will be added to each
+ * date and time
+ *
+ * @param timeDiff The time difference. IT should be between +12 and -12
+ * @return Always returns true.
*/
- function getArticleCategoryIds( $articleId )
+ function setTimeDiff( $timeDiff )
{
- $query = "SELECT category_id FROM ".$this->getPrefix()."article_categories_link
- WHERE article_id = $articleId";
+ $this->_timeDiff = $timeDiff;
- $result = $this->Execute( $query );
-
- // it's impossible that an article has no categories, but
- // we'll bear with it...
- if( !$result )
- return Array();
-
- // otherwise, fetch them
- $categories = Array();
- while( $row = $result->FetchRow()) {
- $categories[] = $row['category_id'];
- }
-
- return $categories;
+ return true;
}
/**
@@ -1109,12 +1094,8 @@
include_once( PLOG_CLASS_PATH.'class/data/timestamp.class.php' );
include_once( PLOG_CLASS_PATH.'class/dao/users.class.php' );
include_once( PLOG_CLASS_PATH.'class/dao/blogs.class.php' );
- include_once( PLOG_CLASS_PATH."class/dao/bloginfo.class.php" );
$id = $query_result['id'];
- if( isset($this->_memoryCache[$id])) {
- return($this->_memoryCache[$id]);
- }
// this is a little dirty trick or otherwise the old
// that don't have the 'properties' field will not work
@@ -1124,29 +1105,23 @@
$query_result['properties'] = serialize($tmpArray);
}
- // ---
- // this, i do not like... but I couldn't find a more
- // "elegant" way to arrange it! This makes this method
- // totally dependant on the blog configuration so it basically
- // means an additional query every time we fetch an article
- // (just in case we didn't have enough!)
- // ---
-
//
// if there's a time difference applied to all dates, then we'd better
// calculate it here!!
//
if( $this->blogs == null )
$this->blogs = new Blogs();
- $blogId = $query_result['blog_id'];
- $blogInfo = $this->blogs->getBlogInfo( $blogId );
- $blogSettings = $blogInfo->getSettings();
- $timeDiff = $blogSettings->getValue( 'time_offset' );
+ $blogId = $query_result['blog_id'];
+ $blogInfo = $this->blogs->getBlogInfo( $blogId );
+ $blogSettings = $blogInfo->getSettings();
+ $timeDiff = $blogSettings->getValue( 'time_offset' );
+
// we can use this auxiliary function to help us...
$date = Timestamp::getDateWithOffset( $query_result['date'], $timeDiff );
- $articleCategories = $this->getArticleCategories( $query_result['id'], $query_result['blog_id'] );
+ $articleCategories = $this->getArticleCategories( $query_result['id'],
+ $query_result['blog_id'] );
$categoryIds = Array();
foreach( $articleCategories as $category )
array_push( $categoryIds, $category->getId());
@@ -1175,98 +1150,7 @@
$this->users = new Users();
$article->setUserInfo( $this->users->getUserInfoFromId( $query_result['user_id'] ));
- // fill in the cache with the result we just loaded
- $this->_memoryCache[$query_result['id']] = $article;
-
return $article;
}
-
- /**
- * @private
- */
- function _fillArticleHeaderInformation( $query_result, $includeHiddenFields = true )
- {
- include_once( PLOG_CLASS_PATH.'class/data/timestamp.class.php' );
- include_once( PLOG_CLASS_PATH.'class/dao/users.class.php' );
- include_once( PLOG_CLASS_PATH.'class/dao/blogs.class.php' );
-
- $id = $query_result['id'];
- if( isset($this->_memoryCache[$id])) {
- return($this->_memoryCache[$id]);
- }
-
- // this is a little dirty trick or otherwise the old
- // that don't have the 'properties' field will not work
- // as they will appear to have comments disabled
- if( $query_result['properties'] == "" ) {
- $tmpArray = Array( 'comments_enabled' => true );
- $query_result['properties'] = serialize($tmpArray);
- }
-
- // ---
- // this, i do not like... but I couldn't find a more
- // "elegant" way to arrange it! This makes this method
- // totally dependant on the blog configuration so it basically
- // means an additional query every time we fetch an article
- // (just in case we didn't have enough!)
- // ---
-
- //
- // if there's a time difference applied to all dates, then we'd better
- // calculate it here!!
- //
- if( $this->_blogInfo == null ) {
- if( $this->blogs == null )
- $this->blogs = new Blogs();
- $blogId = $query_result['blog_id'];
- $this->_blogInfo = $this->blogs->getBlogInfo( $blogId );
- $this->_blogSettings = $this->_blogInfo->getSettings();
- $this->_timeDiff = $this->_blogSettings->getValue( 'time_offset' );
- }
-
- // we can use this auxiliary function to help us...
- $date = Timestamp::getDateWithOffset( $query_result['date'], $this->_timeDiff );
-
- // postText does not exist here.. maybe a copy/paste problem?
- // anyway.. it works without postText, so i'll just set this to
- // null. oscar, pls double check.. original code:
- // $article = new Article( $postText['topic'],
- // $postText['text'],
- // NULL,
- $article = new Article( NULL,
- NULL,
- NULL,
- $query_result['user_id'],
- $query_result['blog_id'],
- $query_result['status'],
- $query_result['num_reads'],
- unserialize($query_result['properties']),
- $query_result['slug'],
- $query_result['id'] );
-
- // and fill in all the fields with the information we just got from the db
- if( $this->users == null )
- $this->users = new Users();
- $article->setDate( $date );
- $article->setTimeOffset( $this->_timeDiff );
- $article->setBlogInfo( $this->_blogInfo );
- $article->setUserInfo( $this->users->getUserInfoFromId( $query_result['user_id'] ));
-
- return $article;
- }
-
- /**
- * sets a time difference that will be added to each
- * date and time
- *
- * @param timeDiff The time difference. IT should be between +12 and -12
- * @return Always returns true.
- */
- function setTimeDiff( $timeDiff )
- {
- $this->_timeDiff = $timeDiff;
-
- return true;
- }
}
?>
More information about the pLog-svn
mailing list