function cache_posts($query)
 {
     // soft cache
     bb_log_deprecated('class::function', __CLASS__ . '::' . __FUNCTION__, 'bb_cache_posts');
     return bb_cache_posts($query);
 }
 function generate_topic_sql($_part_of_post_query = false)
 {
     global $bbdb;
     $q =& $this->query_vars;
     $distinct = '';
     $sql_calc_found_rows = 'found_rows' === $q['count'] ? 'SQL_CALC_FOUND_ROWS' : '';
     // unfiltered
     $fields = 't.*';
     $index_hint = '';
     $join = '';
     $where = '';
     $group_by = '';
     $having = '';
     $order_by = '';
     $post_where = '';
     $post_queries = array('post_author_id', 'post_author', 'posted', 'post_status', 'position', 'post_text', 'poster_ip');
     if (!$_part_of_post_query && ($q['search'] || array_diff($post_queries, $this->not_set))) {
         $join .= " JOIN {$bbdb->posts} as p ON ( t.topic_id = p.topic_id )";
         $post_where = $this->generate_post_sql(true);
         if ($q['search']) {
             $post_where .= ' AND ( ';
             $post_where .= $this->generate_topic_title_sql($q['search']);
             $post_where .= ' OR ';
             $post_where .= $this->generate_post_text_sql($q['search']);
             $post_where .= ' )';
         }
         $group_by = 't.topic_id';
         $fields .= ", MIN(p.post_id) as post_id";
         if ($bbdb->has_cap('GROUP_CONCAT', $bbdb->posts)) {
             $fields .= ", GROUP_CONCAT(p.post_text SEPARATOR ' ') AS post_text";
         } else {
             $fields .= ", p.post_text";
         }
         if ($this->match_query) {
             $fields .= ", AVG({$this->match_query}) AS search_score";
             if (!$q['order_by']) {
                 $q['order_by'] = 'search_score';
             }
         } elseif ($q['search'] || $q['post_text']) {
             $fields .= ", 0 AS search_score";
         }
     }
     if (!$_part_of_post_query) {
         if ($q['post_id']) {
             $post_topics = $post_topics_no = array();
             $op = substr($q['post_id'], 0, 1);
             if (in_array($op, array('>', '<'))) {
                 $post_topics = $bbdb->get_col("SELECT DISTINCT topic_id FROM {$bbdb->posts} WHERE post_id {$op} '" . (int) substr($q['post_id'], 1) . "'");
             } else {
                 $posts = explode(',', $q['post_id']);
                 $get_posts = array();
                 foreach ($posts as $post_id) {
                     $post_id = (int) $post_id;
                     $_post_id = abs($post_id);
                     $get_posts[] = $_post_id;
                 }
                 bb_cache_posts($get_posts);
                 foreach ($posts as $post_id) {
                     $post = bb_get_post(abs($post_id));
                     if ($post_id < 0) {
                         $post_topics_no[] = $post->topic_id;
                     } else {
                         $post_topics[] = $post->topic_id;
                     }
                 }
             }
             if ($post_topics) {
                 $where .= " AND t.topic_id IN (" . join(',', $post_topics) . ")";
             }
             if ($post_topics_no) {
                 $where .= " AND t.topic_id NOT IN (" . join(',', $post_topics_no) . ")";
             }
         }
         if ($q['topic_id']) {
             $where .= $this->parse_value('t.topic_id', $q['topic_id']);
         } elseif ($q['topic']) {
             $q['topic'] = bb_slug_sanitize($q['topic']);
             $where .= " AND t.topic_slug = '{$q['topic']}'";
         }
         if ($q['forum_id']) {
             $where .= $this->parse_value('t.forum_id', $q['forum_id']);
         } elseif ($q['forum']) {
             if (!($q['forum_id'] = bb_get_id_from_slug('forum', $q['forum']))) {
                 $this->error('query_var:forum', 'No forum by that name');
             }
             $where .= " AND t.forum_id = {$q['forum_id']}";
         }
         if ($q['tag'] && !is_int($q['tag_id'])) {
             $q['tag_id'] = (int) bb_get_tag_id($q['tag']);
         }
         if (is_numeric($q['tag_id'])) {
             $join .= " JOIN `{$bbdb->term_relationships}` AS tr ON ( t.`topic_id` = tr.`object_id` AND tr.`term_taxonomy_id` = {$q['tag_id']} )";
         }
         if (is_numeric($q['favorites']) && ($f_user = bb_get_user($q['favorites']))) {
             $where .= $this->parse_value('t.topic_id', $f_user->favorites);
         }
     }
     // !_part_of_post_query
     if ($q['topic_title']) {
         $where .= ' AND ' . $this->generate_topic_title_sql($q['topic_title']);
     }
     if ($q['started']) {
         $where .= $this->date('t.topic_start_time', $q['started']);
     }
     if ($q['updated']) {
         $where .= $this->date('t.topic_time', $q['updated']);
     }
     if ($q['topic_author_id']) {
         $where .= $this->parse_value('t.topic_poster', $q['topic_author_id']);
     } elseif ($q['topic_author']) {
         $user = bb_get_user($q['topic_author'], array('by' => 'login'));
         if (!($q['topic_author_id'] = (int) $user->ID)) {
             $this->error('query_var:user', 'No user by that name');
         }
         $where .= " AND t.topic_poster = {$q['topic_author_id']}";
     }
     if (!$q['topic_status']) {
         $where .= " AND t.topic_status = '0'";
     } elseif (false === strpos($q['topic_status'], 'all')) {
         $stati = array('normal' => 0, 'deleted' => 1);
         $q['topic_status'] = str_replace(array_keys($stati), array_values($stati), $q['topic_status']);
         $where .= $this->parse_value('t.topic_status', $q['topic_status']);
     }
     if (false !== $q['open'] && false === strpos($q['open'], 'all')) {
         $stati = array('no' => 0, 'closed' => 0, 'yes' => 1, 'open' => 1);
         $q['open'] = str_replace(array_keys($stati), array_values($stati), $q['open']);
         $where .= $this->parse_value('t.topic_open', $q['open']);
     }
     if (false !== $q['sticky'] && false === strpos($q['sticky'], 'all')) {
         $stickies = array('no' => 0, 'normal' => 0, 'forum' => 1, 'super' => 2, 'front' => 2, 'sticky' => '-0');
         $q['sticky'] = str_replace(array_keys($stickies), array_values($stickies), $q['sticky']);
         $where .= $this->parse_value('t.topic_sticky', $q['sticky']);
     }
     if (false !== $q['post_count']) {
         $where .= $this->parse_value('t.topic_posts', $q['post_count']);
     }
     if (false !== $q['tag_count']) {
         $where .= $this->parse_value('t.tag_count', $q['tag_count']);
     }
     if ($q['meta_key'] && ($q['meta_key'] = preg_replace('|[^a-z0-9_-]|i', '', $q['meta_key']))) {
         if ('-' == substr($q['meta_key'], 0, 1)) {
             $join .= " LEFT JOIN {$bbdb->meta} AS tm ON ( tm.object_type = 'bb_topic' AND t.topic_id = tm.object_id AND tm.meta_key = '" . substr($q['meta_key'], 1) . "' )";
             $where .= " AND tm.meta_key IS NULL";
         } else {
             $join .= " JOIN {$bbdb->meta} AS tm ON ( tm.object_type = 'bb_topic' AND t.topic_id = tm.object_id AND tm.meta_key = '{$q['meta_key']}' )";
             if ($q['meta_value']) {
                 $q['meta_value'] = maybe_serialize($q['meta_value']);
                 if (strpos($q['meta_value'], 'NULL') !== false) {
                     $join = ' LEFT' . $join;
                 }
                 $where .= $this->parse_value('tm.meta_value', $q['meta_value']);
             }
         }
     }
     // Just getting topic part for inclusion in post query
     if ($_part_of_post_query) {
         return $where;
     }
     $where .= $post_where;
     if ($where) {
         // Get rid of initial " AND " (this is pre-filters)
         $where = substr($where, 5);
     }
     if ($q['index_hint']) {
         $index_hint = $q['index_hint'];
     }
     if ($q['order_by']) {
         $order_by = $q['order_by'];
     } else {
         $order_by = 't.topic_time';
     }
     $bits = compact(array('distinct', 'sql_calc_found_rows', 'fields', 'index_hint', 'join', 'where', 'group_by', 'having', 'order_by'));
     $this->request = $this->_filter_sql($bits, "{$bbdb->topics} AS t");
     return $this->request;
 }
Example #3
0
function bb_cache_last_posts($_topics = false, $author_cache = true)
{
    global $topics, $bbdb;
    if (!$_topics) {
        $_topics =& $topics;
    }
    if (!is_array($_topics)) {
        return false;
    }
    $last_post_ids = array();
    $topic_ids = array();
    foreach ($_topics as $topic) {
        if (is_object($topic)) {
            $last_post_ids[] = (int) $topic->topic_last_post_id;
        } else {
            if (is_numeric($topic) && false !== ($cached_topic = nxt_cache_get($topic, 'bb_topic'))) {
                $last_post_ids[] = (int) $cached_topic->topic_last_post_id;
            } else {
                if (is_numeric($topic)) {
                    $topic_ids[] = (int) $topic;
                }
            }
        }
    }
    if (!empty($last_post_ids)) {
        $_last_post_ids = join(',', $last_post_ids);
        $posts = (array) bb_cache_posts("SELECT post_id FROM {$bbdb->posts} WHERE post_id IN ({$_last_post_ids}) AND post_status = 0", true);
        if ($author_cache) {
            bb_post_author_cache($posts);
        }
    }
    if (!empty($topic_ids)) {
        $_topic_ids = join(',', $topic_ids);
        $posts = (array) bb_cache_posts("SELECT p.post_id FROM {$bbdb->topics} AS t LEFT JOIN {$bbdb->posts} AS p ON ( t.topic_last_post_id = p.post_id ) WHERE t.topic_id IN ({$_topic_ids}) AND p.post_status = 0", true);
        if ($author_cache) {
            bb_post_author_cache($posts);
        }
    }
}