From c9733ad7195995a9f28ecbbc8aa3e94a05527114 Mon Sep 17 00:00:00 2001 From: rxu Date: Sun, 18 Dec 2011 16:28:35 +0800 Subject: [PATCH 1/6] [ticket/10532] Fix $start out of range for pre-made searches PHPBB3-10532 --- phpBB/search.php | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/phpBB/search.php b/phpBB/search.php index 2aa61401cf..07be438ab4 100644 --- a/phpBB/search.php +++ b/phpBB/search.php @@ -471,26 +471,33 @@ if ($keywords || $author || $author_id || $search_id || $submit) if ($search_id) { - if ($sql) + if ($sql || $search_id == 'unreadposts') { - // only return up to 1000 ids (the last one will be removed later) - $result = $db->sql_query_limit($sql, 1001 - $start, $start); - - while ($row = $db->sql_fetchrow($result)) + if ($sql) { - $id_ary[] = (int) $row[$field]; + // only return up to 1000 ids (the last one will be removed later) + $result = $db->sql_query_limit($sql, 1001); + + while ($row = $db->sql_fetchrow($result)) + { + $id_ary[] = (int) $row[$field]; + } + $db->sql_freeresult($result); + } + else if ($search_id == 'unreadposts') + { + $id_ary = array_keys(get_unread_topics($user->data['user_id'], $sql_where, $sql_sort, 1001)); } - $db->sql_freeresult($result); - $total_match_count = sizeof($id_ary) + $start; - $id_ary = array_slice($id_ary, 0, $per_page); - } - else if ($search_id == 'unreadposts') - { - $id_ary = array_keys(get_unread_topics($user->data['user_id'], $sql_where, $sql_sort, 1001 - $start, $start)); - - $total_match_count = sizeof($id_ary) + $start; - $id_ary = array_slice($id_ary, 0, $per_page); + if ($total_match_count = sizeof($id_ary)) + { + // Make sure $start is set to the last page if it exceeds the amount + if ($start < 0 || $start >= $total_match_count) + { + $start = ($start < 0) ? 0 : floor(($total_match_count - 1) / $per_page) * $per_page; + } + $id_ary = array_slice($id_ary, $start, $per_page); + } } else { From cb7bb31129a38cbdbeb682e869e51b1199b43bfb Mon Sep 17 00:00:00 2001 From: rxu Date: Sat, 14 Jan 2012 15:50:47 +0800 Subject: [PATCH 2/6] [ticket/10532] Adjust total match count and limit Set a variable for the limit of total matches count. Adjust total match count to limit to provide proper $start value calculation. Adjust $start value if no matches were found. PHPBB3-10532 --- phpBB/search.php | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/phpBB/search.php b/phpBB/search.php index 07be438ab4..3956478371 100644 --- a/phpBB/search.php +++ b/phpBB/search.php @@ -469,14 +469,18 @@ if ($keywords || $author || $author_id || $search_id || $submit) $per_page = ($show_results == 'posts') ? $config['posts_per_page'] : $config['topics_per_page']; $total_match_count = 0; + // Set limit for the $total_match_count to reduce server load + $total_matches_limit = 1000; + $found_more_search_matches = false; + if ($search_id) { if ($sql || $search_id == 'unreadposts') { if ($sql) { - // only return up to 1000 ids (the last one will be removed later) - $result = $db->sql_query_limit($sql, 1001); + // Only return up to $total_matches_limit+1 ids (the last one will be removed later) + $result = $db->sql_query_limit($sql, ($total_matches_limit + 1)); while ($row = $db->sql_fetchrow($result)) { @@ -486,11 +490,19 @@ if ($keywords || $author || $author_id || $search_id || $submit) } else if ($search_id == 'unreadposts') { - $id_ary = array_keys(get_unread_topics($user->data['user_id'], $sql_where, $sql_sort, 1001)); + // Only return up to $total_matches_limit+1 ids (the last one will be removed later) + $id_ary = array_keys(get_unread_topics($user->data['user_id'], $sql_where, $sql_sort, ($total_matches_limit + 1))); } if ($total_match_count = sizeof($id_ary)) { + // Limit the number to $total_matches_limit for pre-made searches + if ($total_match_count > $total_matches_limit) + { + $found_more_search_matches = true; + $total_match_count = $total_matches_limit; + } + // Make sure $start is set to the last page if it exceeds the amount if ($start < 0 || $start >= $total_match_count) { @@ -498,6 +510,11 @@ if ($keywords || $author || $author_id || $search_id || $submit) } $id_ary = array_slice($id_ary, $start, $per_page); } + else + { + // Set $start to 0 if no matches were found + $start = 0; + } } else { @@ -550,10 +567,8 @@ if ($keywords || $author || $author_id || $search_id || $submit) $icons = $cache->obtain_icons(); // Output header - if ($search_id && ($total_match_count > 1000)) + if ($found_more_search_matches) { - // limit the number to 1000 for pre-made searches - $total_match_count--; $l_search_matches = sprintf($user->lang['FOUND_MORE_SEARCH_MATCHES'], $total_match_count); } else From 8f3fba885859e00646531632558d75b609ecf813 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 26 Feb 2012 01:58:17 +0100 Subject: [PATCH 3/6] [ticket/10532] Put $total_match_count assignment onto its own line. PHPBB3-10532 --- phpBB/search.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phpBB/search.php b/phpBB/search.php index 3956478371..87e40832a5 100644 --- a/phpBB/search.php +++ b/phpBB/search.php @@ -494,7 +494,8 @@ if ($keywords || $author || $author_id || $search_id || $submit) $id_ary = array_keys(get_unread_topics($user->data['user_id'], $sql_where, $sql_sort, ($total_matches_limit + 1))); } - if ($total_match_count = sizeof($id_ary)) + $total_match_count = sizeof($id_ary); + if ($total_match_count) { // Limit the number to $total_matches_limit for pre-made searches if ($total_match_count > $total_matches_limit) From 459e8dc09522d8e54ce82182a99ebddf2362a737 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 26 Feb 2012 02:15:39 +0100 Subject: [PATCH 4/6] [ticket/10532] Get rid of inline calculation of $start, remove duplicated check PHPBB3-10532 --- phpBB/search.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/phpBB/search.php b/phpBB/search.php index 87e40832a5..3006c0e5d9 100644 --- a/phpBB/search.php +++ b/phpBB/search.php @@ -505,10 +505,15 @@ if ($keywords || $author || $author_id || $search_id || $submit) } // Make sure $start is set to the last page if it exceeds the amount - if ($start < 0 || $start >= $total_match_count) + if ($start < 0) { - $start = ($start < 0) ? 0 : floor(($total_match_count - 1) / $per_page) * $per_page; + $start = 0; } + else if ($start >= $total_match_count) + { + $start = floor(($total_match_count - 1) / $per_page) * $per_page; + } + $id_ary = array_slice($id_ary, $start, $per_page); } else From 6548a3094f2981a19ff001e67596befab4159ecd Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 26 Feb 2012 02:19:12 +0100 Subject: [PATCH 5/6] [ticket/10532] Remove one unnecessary level of if block nesting. PHPBB3-10532 --- phpBB/search.php | 81 +++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 42 deletions(-) diff --git a/phpBB/search.php b/phpBB/search.php index 3006c0e5d9..d89bc79225 100644 --- a/phpBB/search.php +++ b/phpBB/search.php @@ -475,57 +475,54 @@ if ($keywords || $author || $author_id || $search_id || $submit) if ($search_id) { - if ($sql || $search_id == 'unreadposts') + if ($sql) { - if ($sql) - { - // Only return up to $total_matches_limit+1 ids (the last one will be removed later) - $result = $db->sql_query_limit($sql, ($total_matches_limit + 1)); + // Only return up to $total_matches_limit+1 ids (the last one will be removed later) + $result = $db->sql_query_limit($sql, ($total_matches_limit + 1)); - while ($row = $db->sql_fetchrow($result)) - { - $id_ary[] = (int) $row[$field]; - } - $db->sql_freeresult($result); - } - else if ($search_id == 'unreadposts') + while ($row = $db->sql_fetchrow($result)) { - // Only return up to $total_matches_limit+1 ids (the last one will be removed later) - $id_ary = array_keys(get_unread_topics($user->data['user_id'], $sql_where, $sql_sort, ($total_matches_limit + 1))); - } - - $total_match_count = sizeof($id_ary); - if ($total_match_count) - { - // Limit the number to $total_matches_limit for pre-made searches - if ($total_match_count > $total_matches_limit) - { - $found_more_search_matches = true; - $total_match_count = $total_matches_limit; - } - - // Make sure $start is set to the last page if it exceeds the amount - if ($start < 0) - { - $start = 0; - } - else if ($start >= $total_match_count) - { - $start = floor(($total_match_count - 1) / $per_page) * $per_page; - } - - $id_ary = array_slice($id_ary, $start, $per_page); - } - else - { - // Set $start to 0 if no matches were found - $start = 0; + $id_ary[] = (int) $row[$field]; } + $db->sql_freeresult($result); + } + else if ($search_id == 'unreadposts') + { + // Only return up to $total_matches_limit+1 ids (the last one will be removed later) + $id_ary = array_keys(get_unread_topics($user->data['user_id'], $sql_where, $sql_sort, ($total_matches_limit + 1))); } else { $search_id = ''; } + + $total_match_count = sizeof($id_ary); + if ($total_match_count) + { + // Limit the number to $total_matches_limit for pre-made searches + if ($total_match_count > $total_matches_limit) + { + $found_more_search_matches = true; + $total_match_count = $total_matches_limit; + } + + // Make sure $start is set to the last page if it exceeds the amount + if ($start < 0) + { + $start = 0; + } + else if ($start >= $total_match_count) + { + $start = floor(($total_match_count - 1) / $per_page) * $per_page; + } + + $id_ary = array_slice($id_ary, $start, $per_page); + } + else + { + // Set $start to 0 if no matches were found + $start = 0; + } } // make sure that some arrays are always in the same order From 7a061cfc6ea52f86d623e496d7355413739b6254 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 26 Feb 2012 02:29:49 +0100 Subject: [PATCH 6/6] [ticket/10532] Remove unnecessary parentheses around calculations of addition. PHPBB3-10532 --- phpBB/search.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/search.php b/phpBB/search.php index d89bc79225..8cb2020630 100644 --- a/phpBB/search.php +++ b/phpBB/search.php @@ -478,7 +478,7 @@ if ($keywords || $author || $author_id || $search_id || $submit) if ($sql) { // Only return up to $total_matches_limit+1 ids (the last one will be removed later) - $result = $db->sql_query_limit($sql, ($total_matches_limit + 1)); + $result = $db->sql_query_limit($sql, $total_matches_limit + 1); while ($row = $db->sql_fetchrow($result)) { @@ -489,7 +489,7 @@ if ($keywords || $author || $author_id || $search_id || $submit) else if ($search_id == 'unreadposts') { // Only return up to $total_matches_limit+1 ids (the last one will be removed later) - $id_ary = array_keys(get_unread_topics($user->data['user_id'], $sql_where, $sql_sort, ($total_matches_limit + 1))); + $id_ary = array_keys(get_unread_topics($user->data['user_id'], $sql_where, $sql_sort, $total_matches_limit + 1)); } else {