[pLog-svn] r2140 - plog/branches/plog-1.1-ben/class/dao
ork at devel.plogworld.net
ork at devel.plogworld.net
Tue May 31 18:16:52 GMT 2005
Author: ork
Date: 2005-05-31 18:16:51 +0000 (Tue, 31 May 2005)
New Revision: 2140
Modified:
plog/branches/plog-1.1-ben/class/dao/blogarticles.class.php
plog/branches/plog-1.1-ben/class/dao/bloginfo.class.php
plog/branches/plog-1.1-ben/class/dao/blogs.class.php
plog/branches/plog-1.1-ben/class/dao/model.class.php
Log:
some more changes to the cache..
Modified: plog/branches/plog-1.1-ben/class/dao/blogarticles.class.php
===================================================================
--- plog/branches/plog-1.1-ben/class/dao/blogarticles.class.php 2005-05-31 15:57:01 UTC (rev 2139)
+++ plog/branches/plog-1.1-ben/class/dao/blogarticles.class.php 2005-05-31 18:16:51 UTC (rev 2140)
@@ -17,6 +17,7 @@
var $_articlesByUser = array();
var $_articleStatus = array();
var $_articleDate = array();
+ var $_articleDates = array();
var $_articleCategories = array();
/**
@@ -39,6 +40,8 @@
$categoryId = $articleSqlRow['category_id'];
$userId = $articleSqlRow['user_id'];
$date = $articleSqlRow['date'];
+ $year = substr( $date, 0, 3 );
+ $month = substr( $date, 4, 5 );
$status = $articleSqlRow['status'];
$this->_articles[] = $articleId;
@@ -48,6 +51,7 @@
$this->_articlesByUser[$userId][] = $articleId;
$this->_articleStatus[$articleId] = $status;
$this->_articleDate[$articleId] = $date;
+ $this->_articleDates[$year][$month][] = $articleId;
$this->_articleCategories[$articleId][] = $categoryId;
}
}
Modified: plog/branches/plog-1.1-ben/class/dao/bloginfo.class.php
===================================================================
--- plog/branches/plog-1.1-ben/class/dao/bloginfo.class.php 2005-05-31 15:57:01 UTC (rev 2139)
+++ plog/branches/plog-1.1-ben/class/dao/bloginfo.class.php 2005-05-31 18:16:51 UTC (rev 2140)
@@ -89,6 +89,17 @@
*
* @return An integer value representing the identifier of the user who owns this blog.
*/
+ function getOwnerId()
+ {
+ return $this->_owner;
+ }
+
+ /**
+ * Returns the identifier of the user who owns this journal.
+ *
+ * @return An integer value representing the identifier of the user who owns this blog.
+ * @deprecated Use getOwnerId() instead.
+ */
function getOwner()
{
return $this->_owner;
@@ -170,8 +181,12 @@
return true;
}
- /**
- */
+ /**
+ * :TODO: I'm not sure anyone ever calls this method, maybe we can
+ * get rid of it
+ * (2005-05-31 ork at devel.plogworld.net)
+ *
+ */
function getCreateDateObject( )
{
include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
Modified: plog/branches/plog-1.1-ben/class/dao/blogs.class.php
===================================================================
--- plog/branches/plog-1.1-ben/class/dao/blogs.class.php 2005-05-31 15:57:01 UTC (rev 2139)
+++ plog/branches/plog-1.1-ben/class/dao/blogs.class.php 2005-05-31 18:16:51 UTC (rev 2140)
@@ -1,13 +1,13 @@
<?php
- include_once( PLOG_CLASS_PATH."class/dao/model.class.php" );
- include_once( PLOG_CLASS_PATH."class/config/config.class.php" );
- include_once( PLOG_CLASS_PATH."class/dao/users.class.php" );
- include_once( PLOG_CLASS_PATH."class/file/file.class.php" );
- include_once( PLOG_CLASS_PATH."class/dao/blogstatus.class.php" );
+ require_once( PLOG_CLASS_PATH . 'class/dao/model.class.php' );
+ require_once( PLOG_CLASS_PATH . 'class/config/config.class.php' );
+ require_once( PLOG_CLASS_PATH . 'class/dao/users.class.php' );
+ require_once( PLOG_CLASS_PATH . 'class/file/file.class.php' );
+ require_once( PLOG_CLASS_PATH . 'class/dao/blogstatus.class.php' );
/**
- * \ingroup DAO
+ * \ingroup DAO
* Model for the Blogs
*/
class Blogs extends Model
@@ -22,14 +22,20 @@
*/
function getBlogInfo( $blogId )
{
- require_once( PLOG_CLASS_PATH . "class/dao/blogsettings.class.php" );
+ require_once( PLOG_CLASS_PATH . 'class/dao/bloginfo.class.php' );
$blogInfo = $this->_cache->getData( $blogId, CACHE_BLOGINFOS );
if( !$blogInfo ) {
- $query = "SELECT * FROM ".$this->getPrefix()."blogs " .
- "WHERE id = '".Db::qstr($blogId)."'";
+ require_once( PLOG_CLASS_PATH."class/database/db.class.php" );
+
+ $query = Db::buildSelectQuery( BLOGS_TABLENAME,
+ array(),
+ 'id',
+ $blogId );
$blogInfo = $this->_getBlogInfoFromQuery( $query, $extendedInfo );
+
+ // save the blogInfo object in the cache.
$this->_cache->setData( $blogId, CACHE_BLOGINFOS, $blogInfo );
}
@@ -44,87 +50,55 @@
*/
function getBlogInfoByName( $blogName, $extendedInfo = false )
{
- require_once( PLOG_CLASS_PATH . "class/dao/blogsettings.class.php" );
+ $blogId = $this->_cache->getData( $blogName, CACHE_BLOGIDBYNAME );
- $blogInfo = $this->_cache->getData( $blogName, CACHE_BLOGIDBYNAME );
-
if ( !$blogId ) {
- $query = "SELECT * FROM " . $this->getPrefix() . "blogs " .
- "WHERE mangled_blog = '" . Db::qstr($blogName) . "';";
+ require_once( PLOG_CLASS_PATH."class/database/db.class.php" );
+ $query = Db::buildSelectQuery( BLOGS_TABLENAME,
+ array(),
+ 'mangled_blog',
+ $blogName );
+
$blogInfo = $this->_getBlogInfoFromQuery( $query, $extendedInfo );
+
+ // and store the blogName-to-blogId relationship in the cache
$this->_cache->setData( $blogName, CACHE_BLOGIDBYNAME, $blogInfo->getId() );
+ } else {
+ $blogInfo = $this->getBlogInfo( $blogId );
}
return $blogInfo;
}
- /**
- * @private
- */
- function _getBlogInfoFromQuery( $query, $extendedInfo = false )
- {
- $result = $this->Execute( $query );
- if( !$result )
- return false;
-
- if( $result->RecordCount() == 0 )
- return false;
-
- $row = $result->FetchRow( $result );
- $blogInfo = $this->_fillBlogInformation( $row, $extendedInfo );
- $blogSettings = $this->getBlogSettingsFromField( $row["settings"] );
- $blogInfo->setSettings( $blogSettings );
-
- return $blogInfo;
- }
-
/**
- * Private function.
- *
- * @private.
- */
- function _fillBlogInformation( $query_result, $extended = false )
- {
- // source class
- include_once( PLOG_CLASS_PATH."class/dao/bloginfo.class.php" );
-
- // create new BlogInfo
- $blogInfo = new BlogInfo( stripslashes($query_result["blog"]),
- $query_result["owner_id"],
- stripslashes($query_result["about"]),
- unserialize($query_result["settings"]),
- $query_result["id"] );
-
- // load information about the blog owner
- $blogInfo->setStatus( $query_result["status"] );
-
- return $blogInfo;
- }
-
- /**
* Retrieves the first article date timestamp
+ * :TODO: I'm not sure anyone ever calls this method, maybe we can
+ * get rid of it
+ * (2005-05-31 ork at devel.plogworld.net)
*
- * @param blogId The identifier of the blog from which we'd like to fetch the settings
+ * @param blogId The identifier of the blog from which we'd like
+ to fetch the creation date
* @return Returns an Timestamp with the first article date
*/
function getBlogCreateDate( $blogId )
{
- $query = "SELECT date FROM " . $this->getPrefix() . "articles " .
- "WHERE blog_id = " . $blogId . " ORDER BY date ASC LIMIT 0,1";
+ require_once( PLOG_CLASS_PATH . 'class/dao/articles.class.php' );
- $result = $this->Execute( $query );
+ $articles = new Articles();
+ $blogArticles = $articles->getArticles( $blogId );
+ // these are always sorted by date.
+ $allArticles = $blogArticles->getArticleIds();
- if (!$result)
- return false;
+ // there might be no article, we return 01.01.1970 then :-)
+ if( empty($allArticles) )
+ return '19700101000000';
- $row = $result->FetchRow();
+ // fetch the first article
+ $article = $articles->getArticle( $allArticles[0] );
- if (!isset($row["date"]))
- return false;
-
- return $row["date"];
+ return $article->getDate();
}
/**
@@ -135,19 +109,21 @@
*/
function getBlogUpdateDate( $blogId )
{
- $query = "SELECT date FROM " . $this->getPrefix() . "articles " .
- "WHERE blog_id = " . $blogId . " ORDER BY date DESC LIMIT 0,1";
- $result = $this->Execute( $query );
+ require_once( PLOG_CLASS_PATH . 'class/dao/articles.class.php' );
- if (!$result)
- return false;
+ $articles = new Articles();
+ $blogArticles = $articles->getArticles( $blogId );
+ // these are always sorted by date.
+ $allArticles = $blogArticles->getArticleIds();
- $row = $result->FetchRow();
+ // there might be no article, we return 01.01.1970 then :-)
+ if( empty($allArticles) )
+ return '19700101000000';
- if (!isset($row["date"]))
- return false;
+ // fetch the last article
+ $article = $articles->getArticle( $allArticles[count($allArticles) - 1] );
- return $row["date"];
+ return $article->getDate();
}
/**
@@ -182,18 +158,15 @@
*/
function getBlogTotalPosts( $blogId )
{
- $query = "SELECT COUNT(*) as total FROM ".$this->getPrefix()."articles WHERE blog_id = $blogId AND status = ".POST_STATUS_PUBLISHED;
- $result = $this->Execute( $query );
+ require_once( PLOG_CLASS_PATH . 'class/dao/articlestatus.class.php' );
+ require_once( PLOG_CLASS_PATH . 'class/dao/articles.class.php' );
- if (!$result)
- return false;
+ $articles = new Articles();
+ $blogArticles = $articles->getArticles( $blogId );
- $row = $result->FetchRow();
+ $allArticles = $blogArticles->getArticleByStatus( POST_STATUS_PUBLISHED );
- if (!isset($row["total"]))
- return false;
-
- return intval($row["total"]);
+ return count($allArticles);
}
/**
@@ -248,12 +221,14 @@
*/
function getBlogSettingsFromField( $settingsField )
{
+ require_once( PLOG_CLASS_PATH . "class/dao/blogsettings.class.php" );
+
if( $settingsField == "" || $settingsField == null ) {
$settings = new BlogSettings();
}
else {
$settings = unserialize( $settingsField );
- if( !is_object($settings)) {
+ if( !is_object($settings) ) {
// if the BlogSettings object is not valid, return a valid one
// with some of the default settings, so that at least
// the blog will keep working!
@@ -272,14 +247,9 @@
*/
function getBlogSettings( $blogId )
{
+ $blogInfo = $this->getBlogInfo( $blogId );
- $query = "SELECT settings FROM ".$this->getPrefix()."blogs WHERE id = ".$blogId;
-
- $result = $this->Execute( $query );
-
- $row = $result->FetchRow();
-
- return $this->getBlogSettingsFromField( $row["settings"] );
+ return $blogInfo->getSettings();
}
/**
@@ -287,23 +257,28 @@
*
* @param blogId Blog identifier
* @param blogInfo A BlogInfo object containing all the information of the blog
- * @param return Returns true if everything ok or false otherwise
+ * @param return Returns true if everything's ok or false otherwise
*/
function updateBlog( $blogId, $blogInfo )
{
- include_once( PLOG_CLASS_PATH."class/data/textfilter.class.php" );
+ require_once( PLOG_CLASS_PATH . 'class/data/textfilter.class.php' );
+ require_once( PLOG_CLASS_PATH . 'class/database/db.class.php' );
$blogName = TextFilter::urlize($blogInfo->getBlog());
- $query = "UPDATE ".$this->getPrefix()."blogs SET " .
- "blog = '" . Db::qstr($blogInfo->getBlog()).
- "', about = '" . Db::qstr($blogInfo->getAbout()).
- "', settings = '" . serialize($blogInfo->getSettings()).
- "', owner_id = " . $blogInfo->getOwner().
- ", mangled_blog = '" . $blogName.
- "', status = '" . Db::qstr($blogInfo->getStatus()).
- "' WHERE id = '" . Db::qstr($blogId)."';";
+ // setup an array containing the values we would like to set in the db
+ $keyValuePairs = array();
+ $keyValuePairs['blog'] = $blogInfo->getBlog();
+ $keyValuePairs['about'] = $blogInfo->getAbout();
+ $keyValuePairs['settings'] = serialize( $blogInfo->getSettings() );
+ $keyValuePairs['owner_id'] = $blogInfo->getOwnerId();
+ $keyValuePairs['mangled_blog'] = $blogName;
+ $keyValuePairs['status'] = $blogInfo->getStatus();
+ $query = Db::buildUpdateQuery( BLOGS_TABLENAME,
+ $keyValuePairs,
+ 'id',
+ $blogId );
$result = $this->Execute( $query );
$this->_cache->removeData( $blogName, CACHE_BLOGIDBYNAME );
@@ -321,10 +296,12 @@
*/
function updateBlogSettings( $blogId, $blogSettings )
{
- $query = "UPDATE ".$this->getPrefix()."blogs " .
- "SET settings = '".serialize($blogSettings) .
- "' WHERE id = ".$blogId;
+ require_once( PLOG_CLASS_PATH . 'class/database/db.class.php' );
+ $query = Db::buildUpdateQuery( BLOGS_TABLENAME,
+ array( 'settings' => serialize($blogSettings) ),
+ 'id',
+ $blogId );
$result = $this->Execute( $query );
$this->_cache->removeData( $blogId, CACHE_BLOGINFOS );
@@ -490,15 +467,17 @@
* its links, its links categories, its trackbacks and its comments
*
* @param blogId the id of the blog we'd like to delete
+ * @return boolean success of the operation
*/
function deleteBlog( $blogId )
{
// source classes
- include_once( PLOG_CLASS_PATH."class/dao/articles.class.php" );
- include_once( PLOG_CLASS_PATH."class/dao/mylinks.class.php" );
- include_once( PLOG_CLASS_PATH."class/dao/mylinkscategories.class.php" );
- include_once( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" );
-
+ require_once( PLOG_CLASS_PATH . 'class/dao/articles.class.php' );
+ require_once( PLOG_CLASS_PATH . 'class/dao/mylinks.class.php' );
+ require_once( PLOG_CLASS_PATH . 'class/dao/mylinkscategories.class.php' );
+ require_once( PLOG_CLASS_PATH . 'class/dao/userpermissions.class.php' );
+ require_once( PLOG_CLASS_PATH . 'class/database/db.class.php' );
+
// first of all, delete the posts
$articles = new Articles();
$articles->deleteBlogPosts( $blogId );
@@ -518,35 +497,86 @@
$perms->revokeBlogPermissions( $blogId );
// and finally, delete the blog
- $query = "DELETE FROM ".$this->getPrefix()."blogs WHERE id = $blogId";
-
+ $query = Db::buildDeleteQuery( BLOGS_TABLENAME,
+ 'id',
+ $blogId );
$result = $this->Execute( $query );
return $result;
}
/**
- * disables a blog
+ * disables a blog, this is done by setting the status of the blog
+ * to BLOG_STATUS_DISABLED. The blog an all posts still remain in
+ * the database.
*
* @param blogId
+ * @see deleteBlog
*/
- function disableBlog( $blogId )
- {
- $query = "UPDATE ".$this->getPrefix()."blogs
- SET status = '".BLOG_STATUS_DISABLED."'
- WHERE id = '".Db::qstr($blogId)."'";
+ function disableBlog( $blogId )
+ {
+ require_once( PLOG_CLASS_PATH."class/database/db.class.php" );
- $result = $this->Execute( $query );
+ $keyValuePairs = array();
+ $keyValuePairs['status'] = BLOG_STATUS_DISABLED;
- if( !$result )
- return false;
+ $query = Db::buildUpdateQuery( BLOGS_TABLENAME,
+ $keyValuePairs,
+ 'id',
+ $blogId );
- if( $this->_db->Affected_Rows() == 0 )
- return false;
+ $result = $this->Execute( $query );
- $this->_cache->removeData( $blogId, CACHE_BLOGINFOS );
+ if( !$result || $this->_db->Affected_Rows() == 0 )
+ return false;
- return true;
- }
- }
+ $this->_cache->removeData( $blogId, CACHE_BLOGINFOS );
+
+ return true;
+ }
+
+ /**
+ * @private
+ */
+ function _getBlogInfoFromQuery( $query, $extendedInfo = false )
+ {
+ $result = $this->Execute( $query );
+
+ if( !$result )
+ return false;
+
+ if( $result->RecordCount() == 0 )
+ return false;
+
+ $row = $result->FetchRow( $result );
+ $blogInfo = $this->_fillBlogInformation( $row, $extendedInfo );
+ $blogSettings = $this->getBlogSettingsFromField( $row["settings"] );
+ $blogInfo->setSettings( $blogSettings );
+
+ return $blogInfo;
+ }
+
+ /**
+ * Private function.
+ *
+ * @private
+ */
+ function _fillBlogInformation( $query_result, $extended = false )
+ {
+ // source class
+ include_once( PLOG_CLASS_PATH."class/dao/bloginfo.class.php" );
+
+ // create new BlogInfo
+ $blogInfo = new BlogInfo( stripslashes($query_result["blog"]),
+ $query_result["owner_id"],
+ stripslashes($query_result["about"]),
+ unserialize($query_result["settings"]),
+ $query_result["id"] );
+
+ // load information about the blog owner
+ $blogInfo->setStatus( $query_result["status"] );
+
+ return $blogInfo;
+ }
+ }
?>
Modified: plog/branches/plog-1.1-ben/class/dao/model.class.php
===================================================================
--- plog/branches/plog-1.1-ben/class/dao/model.class.php 2005-05-31 15:57:01 UTC (rev 2139)
+++ plog/branches/plog-1.1-ben/class/dao/model.class.php 2005-05-31 18:16:51 UTC (rev 2140)
@@ -54,6 +54,7 @@
/**
* the names of the tables used in pLog
*/
+ define( 'BLOGS_TABLENAME', 'blogs' );
define( 'ARTICLES_TABLENAME', 'articles' );
define( 'ARTICLETEXTS_TABLENAME', 'articles_text' );
define( 'ARTICLE_CATEGORIES_RELATIONSHIP_TABLENAME', 'article_categories_link' );
@@ -100,6 +101,7 @@
*/
function Execute( $query, $page = DEFAULT_PAGING_ENABLED, $itemsPerPage = DEFAULT_ITEMS_PER_PAGE )
{
+ $this->log->info("executing $query");
$this->_initializeDb();
// if paging is enabled...
More information about the pLog-svn
mailing list