protected function initComments($page) { // get comments for this page $this->comments = false; if (isset($this->app->memcache)) { $key = $this->getCommentsCacheKey(); $this->comments = $this->app->memcache->getNs('posts', $key); /* * Note: The limit of comments per page is somewhat important here. * In extreme cases, we could run over the 1M size limit for cached * values. This would occur when every comment is close to the * maximum size in bodytext (8K) and the associated posts also have * a very large bodytext (about 8K each). In these rare cases, * caching will fail. */ } if ($this->comments === false) { $sql = sprintf('select BlorgComment.* from BlorgComment %s where %s order by BlorgComment.createdate desc', $this->getJoinClause(), $this->getWhereClause()); $offset = ($page - 1) * $this->getPageSize(); $this->app->db->setLimit($this->getPageSize(), $offset); $wrapper = SwatDBClassMap::get('BlorgCommentWrapper'); $this->comments = SwatDB::query($this->app->db, $sql, $wrapper); // efficiently load posts $post_wrapper = SwatDBClassMap::get('BlorgPostWrapper'); $post_sql = 'select id, title, shortname, bodytext, publish_date from BlorgPost where id in (%s)'; $this->comments->loadAllSubDataObjects('post', $this->app->db, $post_sql, $post_wrapper); // efficiently load authors $author_wrapper = SwatDBClassMap::get('BlorgAuthorWrapper'); $author_sql = 'select id, name, shortname, email, visible from BlorgAuthor where id in (%s)'; $this->comments->loadAllSubDataObjects('author', $this->app->db, $author_sql, $author_wrapper); if (isset($this->app->memcache)) { $this->app->memcache->setNs('posts', $key, $this->comments); } } else { $this->comments->setDatabase($this->app->db); } // if we're not on the first page and there are no comments, 404 if ($page > 1 && count($this->comments) === 0) { throw new SiteNotFoundException('Page not found.'); } // get total number of comments $this->total_count = false; if (isset($this->app->memcache)) { $total_key = $this->getTotalCountCacheKey(); $this->total_count = $this->app->memcache->getNs('posts', $total_key); } if ($this->total_count === false) { $sql = sprintf('select count(1) from BlorgComment %s where %s', $this->getJoinClause(), $this->getWhereClause()); $this->total_count = SwatDB::queryOne($this->app->db, $sql); if (isset($this->app->memcache)) { $this->app->memcache->setNs('posts', $total_key, $this->total_count); } } }
protected function initComments($year, $month_name, $shortname, $page) { if (!array_key_exists($month_name, BlorgPageFactory::$months_by_name)) { throw new SiteNotFoundException(Blorg::_('Page not found.')); } // Date parsed from URL is in locale time. $date = new SwatDate(); $date->setTZ($this->app->default_time_zone); $date->setDate($year, BlorgPageFactory::$months_by_name[$month_name], 1); $date->setTime(0, 0, 0); $memcache = isset($this->app->memcache) ? $this->app->memcache : null; $loader = new BlorgPostLoader($this->app->db, $this->app->getInstance(), $memcache); $loader->addSelectField('title'); $loader->addSelectField('bodytext'); $loader->addSelectField('shortname'); $loader->addSelectField('publish_date'); $loader->addSelectField('author'); $loader->addSelectField('visible_comment_count'); $loader->setWhereClause(sprintf('enabled = %s', $this->app->db->quote(true, 'boolean'))); $this->post = $loader->getPostByDateAndShortname($date, $shortname); if ($this->post === null) { throw new SiteNotFoundException('Post not found.'); } $this->total_count = $this->post->getVisibleCommentCount(); $this->comments = false; if (isset($this->app->memcache)) { $key = $this->getCommentsCacheKey(); $this->comments = $this->app->memcache->getNs('posts', $key); } if ($this->comments === false) { $offset = ($page - 1) * $this->getPageSize(); $this->comments = $this->post->getVisibleComments($this->getPageSize(), $offset); if (isset($this->app->memcache)) { $this->app->memcache->setNs('posts', $key, $this->comments); } } else { $this->comments->setDatabase($this->app->db); } if ($page > 1 && count($this->comments) === 0) { throw new SiteNotFoundException(Blorg::_('Page not found.')); } }