/** * Gather data from a subset of the WP tables * The total set is posts+comments (depending which options are enabled). The function will return this combined result as the total number of items to index * * @param integer $offset Current offset (out of total) * @param integer $count Number of items to index next * @param SearchEngine $engine Search engine to store items * @return array Number of items remaining, total number of items **/ function index($offset, $count, $engine) { global $wpdb; $total_posts = $total_comments = 0; // Calculate how many in total if ($this->have_posts()) { $total_posts = $wpdb->get_var("SELECT COUNT(DISTINCT {$wpdb->posts}.ID) FROM {$wpdb->posts} " . $this->post_sql); } if ($this->have_comments()) { $total_comments = $wpdb->get_var("SELECT COUNT(DISTINCT {$wpdb->comments}.comment_ID) FROM {$wpdb->comments} " . $this->comment_sql); } $grand_total = $total_posts + $total_comments; // What to index? We don't bother trying to span posts/comments - just go up to the limit of each if ($total_posts > 0 && $offset < $total_posts) { // More posts to index $relative = $offset; $rows = $wpdb->get_results("SELECT DISTINCT {$wpdb->posts}.*,{$wpdb->users}.user_login,{$wpdb->users}.user_nicename,{$wpdb->users}.display_name FROM {$wpdb->posts} LEFT JOIN {$wpdb->users} ON {$wpdb->posts}.post_author={$wpdb->users}.ID " . $this->post_sql . " ORDER BY {$wpdb->posts}.ID LIMIT {$relative},{$count}"); foreach ((array) $rows as $row) { $engine->store($row->ID, $this->gather_for_post($row), $row); } return array($grand_total - count($rows) - $offset, $grand_total); } elseif ($total_comments > 0 && $offset < $grand_total) { $relative = $offset - $total_posts; $rows = $wpdb->get_results("SELECT DISTINCT comment_ID,comment_post_ID,comment_author,comment_author_url,comment_content FROM {$wpdb->comments} " . $this->comment_sql . " ORDER BY comment_ID LIMIT {$relative},{$count}"); foreach ((array) $rows as $row) { $engine->store($row->comment_post_ID, $this->gather_for_comment($row), $row); } return array($grand_total - count($rows) - $offset, $grand_total); } // Nothing left return array(0, $grand_total); }