Skip to content

Commit

Permalink
fix limit calculation for prod server
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras committed Dec 1, 2022
1 parent b9edf70 commit 227e886
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
24 changes: 7 additions & 17 deletions lib/database/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,37 @@ function performSearch(int $searchType, string $searchQuery, int $offset, int $c
{
sanitize_sql_inputs($searchQuery, $offset, $count);

$found_rows = 'SQL_CALC_FOUND_ROWS';
$parts = [];
if ($searchType == SearchType::Game || $searchType == SearchType::All) {
$parts[] = "(
SELECT $found_rows 'Game' AS Type, gd.ID, CONCAT( '/game/', gd.ID ) AS Target, CONCAT(gd.Title, ' (', c.Name, ')') as Title FROM GameData AS gd
SELECT 'Game' AS Type, gd.ID, CONCAT( '/game/', gd.ID ) AS Target, CONCAT(gd.Title, ' (', c.Name, ')') as Title FROM GameData AS gd
LEFT JOIN Achievements AS ach ON ach.GameID = gd.ID AND ach.Flags = 3
LEFT JOIN Console AS c ON gd.ConsoleID = c.ID
WHERE gd.Title LIKE '%$searchQuery%'
GROUP BY gd.ID, gd.Title
ORDER BY gd.Title)";

$found_rows = '';
}

if ($searchType == SearchType::Achievement || $searchType == SearchType::All) {
$parts[] = "(
SELECT $found_rows 'Achievement' AS Type, ach.ID, CONCAT( '/achievement/', ach.ID ) AS Target, ach.Title FROM Achievements AS ach
SELECT 'Achievement' AS Type, ach.ID, CONCAT( '/achievement/', ach.ID ) AS Target, ach.Title FROM Achievements AS ach
WHERE ach.Flags = 3 AND ach.Title LIKE '%$searchQuery%' ORDER BY ach.Title)";

$found_rows = '';
}

if ($searchType == SearchType::User || $searchType == SearchType::All) {
$parts[] = "(
SELECT $found_rows 'User' AS Type,
SELECT 'User' AS Type,
ua.User AS ID,
CONCAT( '/user/', ua.User ) AS Target,
ua.User AS Title
FROM UserAccounts AS ua
WHERE ua.User LIKE '%$searchQuery%' AND ua.Permissions >= 0 AND ua.Deleted IS NULL
ORDER BY ua.User)";

$found_rows = '';
}

if ($searchType == SearchType::Forum || $searchType == SearchType::All) {
$parts[] = "(
SELECT $found_rows 'Forum Comment' AS Type,
SELECT 'Forum Comment' AS Type,
ua.User AS ID,
CONCAT( '/viewtopic.php?t=', ftc.ForumTopicID, '&c=', ftc.ID, '#', ftc.ID ) AS Target,
CASE WHEN CHAR_LENGTH(ftc.Payload) <= 64 THEN ftc.Payload ELSE
Expand All @@ -56,13 +49,11 @@ function performSearch(int $searchType, string $searchQuery, int $offset, int $c
AND ft.RequiredPermissions <= '$permissions'
GROUP BY ID, ftc.ID
ORDER BY DateModified DESC)";

$found_rows = '';
}

if ($searchType == SearchType::Comment || $searchType == SearchType::All) {
$parts[] = "(
SELECT $found_rows 'Comment' AS Type, cua.User AS ID,
SELECT 'Comment' AS Type, cua.User AS ID,
CASE
WHEN c.articletype=1 THEN CONCAT( '/game/', c.ArticleID )
Expand All @@ -85,11 +76,10 @@ function performSearch(int $searchType, string $searchQuery, int $offset, int $c
AND ua.UserWallActive AND ua.Deleted IS NULL
AND c.articletype IN (1,2,3,5,7)
ORDER BY c.Submitted DESC)";

$found_rows = '';
}

$query = implode(' UNION ALL ', $parts) . " LIMIT $offset, $count";
$query = "SELECT SQL_CALC_FOUND_ROWS * FROM (" .
implode(' UNION ALL ', $parts) . ") AS results LIMIT $offset, $count";

$dbResult = s_mysql_sanitized_query($query);
if (!$dbResult) {
Expand Down
1 change: 1 addition & 0 deletions public/request/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
if ($numFound >= $maxResults) {
break;
}
$maxResults -= $numFound;
}

$dataOut = [];
Expand Down

0 comments on commit 227e886

Please sign in to comment.