Index: core/filter_api.php
===================================================================
RCS file: /cvsroot/mantisbt/mantisbt/core/filter_api.php,v
retrieving revision 1.163
diff -u -r1.163 filter_api.php
--- core/filter_api.php	15 Sep 2007 21:48:26 -0000	1.163
+++ core/filter_api.php	28 Sep 2007 11:46:27 -0000
@@ -1245,47 +1245,26 @@
 			$t_where	= '';
 		}
 
-		# Possibly do two passes. First time, grab the IDs of issues that match the filters. Second time, grab the IDs of issues that
-		# have bugnotes that match the text search if necessary.
-		$t_id_array = array();
-		for ( $i = 0; $i < 2; $i++ ) {
-			$t_id_where = $t_where;
-			$t_id_join = $t_join;
-			if ( $i == 0 ) {
-				if ( !is_blank( $t_id_where ) && !is_blank( $t_textsearch_where_clause ) ) {
-					$t_id_where = $t_id_where . ' AND ' . $t_textsearch_where_clause;
-				}
-			} else if ( !is_blank( $t_textsearch_wherejoin_clause ) ) {
-				$t_id_where = $t_id_where . ' AND ' . $t_textsearch_wherejoin_clause;
-				$t_id_join = $t_id_join . " INNER JOIN $t_bugnote_table ON $t_bugnote_table.bug_id = $t_bug_table.id";
-				$t_id_join = $t_id_join . " INNER JOIN $t_bugnote_text_table ON $t_bugnote_text_table.id = $t_bugnote_table.bugnote_text_id";
-			}
-			$query  = "SELECT DISTINCT $t_bug_table.id AS id
-						$t_from
-						$t_id_join
-						$t_id_where";
-			if ( ( $i == 0 ) || ( !is_blank( $t_textsearch_wherejoin_clause ) ) ) {
-				$result = db_query( $query );
-				$row_count = db_num_rows( $result );
-
-				for ( $j=0; $j < $row_count; $j++ ) {
-					$row = db_fetch_array( $result );
-					$t_id_array[] = db_prepare_int ( $row['id'] );
-				}
-			}
-		}
 
-		$t_id_array = array_unique( $t_id_array );
+		$t_id_where = $t_where;
+		$t_id_join = $t_join;
+		if ( !is_blank( $t_textsearch_wherejoin_clause ) ) {
+			$t_id_where = $t_id_where . ' AND ' . $t_textsearch_wherejoin_clause;
+			$t_id_join .= " LEFT JOIN $t_bugnote_table ON $t_bugnote_table.bug_id = $t_bug_table.id";
+			$t_id_join .= " LEFT JOIN $t_bugnote_text_table ON $t_bugnote_text_table.id = $t_bugnote_table.bugnote_text_id";
+		}
+		$query  = "SELECT DISTINCT $t_bug_table.id AS id
+			$t_from
+			$t_id_join
+			$t_id_where";
+		$result = db_query( $query );
 
 		# Get the total number of bugs that meet the criteria.
-		$bug_count = count( $t_id_array );
-
-		$rows = array();
-
+		$bug_count = db_num_rows( $result );
 		if ( $bug_count > 0 ) {
-			$t_where = "WHERE $t_bug_table.id in (" . implode( ", ", $t_id_array ) . ")";
+			$t_where = "WHERE $t_bug_table.id in (" .$query . ")";
 		} else {
-			return $rows;
+			return array();
 		}
 
 		$t_from = 'FROM ' . $t_bug_table;
@@ -1402,40 +1381,34 @@
 		$result2 = db_query( $query2, $c_per_page, $t_offset );
 
 		$row_count = db_num_rows( $result2 );
-
-		$t_id_array_lastmod = array();
 		
+		$t_filtered_rows = array();
 		for ( $i=0 ; $i < $row_count ; $i++ ) {
 			$row = db_fetch_array( $result2 );
-			$t_id_array_lastmod[] = db_prepare_int ( $row['id'] );
 			
 			$row['date_submitted'] = db_unixtimestamp ( $row['date_submitted'] );
 			$row['last_updated'] = db_unixtimestamp ( $row['last_updated'] );
 					
-			array_push( $rows, $row );
+			array_push( $t_filtered_rows, $row );
 		}
 
-		$t_id_array_lastmod = array_unique( $t_id_array_lastmod );
-		
-		// paulr: it should be impossible for t_id_array_lastmod to be array():
-		// that would imply that $t_id_array is null which aborts this function early
-		//if ( count( $t_id_array_lastmod ) > 0 ) {
-		$t_where = "WHERE $t_bugnote_table.bug_id in (" . implode( ", ", $t_id_array_lastmod ) . ")";
-		
-		$query3 = "SELECT DISTINCT bug_id,MAX(last_modified) as last_modified, COUNT(last_modified) as count FROM $t_bugnote_table $t_where GROUP BY bug_id";
+
+		#####
+		# Third query
+		# Collect the number of notes associated to each bug
+		$t_where = "WHERE $t_bugnote_table.bug_id IN (" . $query . ")";
+		$query3 = "SELECT DISTINCT bug_id, MAX(last_modified) AS last_modified, COUNT(last_modified) AS count FROM $t_bugnote_table $t_where GROUP BY bug_id";
 
 		# perform query
 		$result3 = db_query( $query3 );
-
 		$row_count = db_num_rows( $result3 );
 
 		for ( $i=0 ; $i < $row_count ; $i++ ) {
 			$row = db_fetch_array( $result3 );
-			
 			$t_stats[ $row['bug_id'] ] = $row;
 		}
 
-		foreach($rows as $row) {
+		foreach($t_filtered_rows as $row) {
 			if( !isset( $t_stats[ $row['id'] ] ) ) {
 				bug_cache_database_result( $row, false );
 			} else {
@@ -1443,7 +1416,7 @@
 			}
 		}
 
-		return $rows;
+		return $t_filtered_rows;
 	}
 
 	# --------------------
