コード例 #1
0
    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);
            }
        }
    }