Skip to content
Merged
5 changes: 3 additions & 2 deletions www/components/ifdb-recommends.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ function sortBySortorder($a, $b)
$sortby = "ratu"; // Sort the highly rated games to the top of the results.
$maxpicks = 12; // Get the first twelve results. (We want extras so we're not always displaying the same games.)
$limit = "limit 0, $maxpicks";
$browse = 0;
$browse = 1;
$count_all_possible_rows = false;


// run the search for highly-rated games
list($recs, $rowcnt, $sortList, $errMsg, $summaryDesc, $badges,
$specials, $specialsUsed, $orderBy) =
doSearch($db, $term, $searchType, $sortby, $limit, $browse);
doSearch($db, $term, $searchType, $sortby, $limit, $browse, $count_all_possible_rows);


// show some recommendations
Expand Down
4 changes: 3 additions & 1 deletion www/editgame
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ if (isset($_REQUEST['search']))
if ($searchFor)
{
// run the search
$count_all_possible_rows = false;
list($rows, $rowcnt, $sortList, $errMsg) =
doSearch($db, $searchFor, "game", "rel", "limit 0, 10", false);
doSearch($db, $searchFor, "game", "rel", "limit 0, 10", false,
$count_all_possible_rows);

// if the top result matches the title exactly, consider it a
// one-row match, even if we found other less relevant titles
Expand Down
3 changes: 2 additions & 1 deletion www/review
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ if (isset($_REQUEST['browse']))
if ($searchFor)
{
// run the search
$count_all_possible_rows = false;
list($rows, $rowcnt, $sortList, $errMsg) =
doSearch($db, $searchFor, "game", "rel", "limit 0, 10", false);
doSearch($db, $searchFor, "game", "rel", "limit 0, 10", false, $count_all_possible_rows);

// show the results
$term = htmlspecialcharx($searchFor);
Expand Down
7 changes: 6 additions & 1 deletion www/search
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,16 @@ if ($pg == 'all') {

// if we have a search term, find it
if ($term || $browse) {

// We want a count of all possible results, regardless of
// limit clause, so we can display that number
$count_all_possible_rows = true;

// run the search
list($rows, $rowcnt, $sortList, $errMsg, $summaryDesc, $badges,
$specials, $specialsUsed, $orderBy) =
doSearch($db, $term, $searchType, $sortby, $limit, $browse);
doSearch($db, $term, $searchType, $sortby, $limit, $browse,
$count_all_possible_rows);

// adjust our page limits to include the whole result set if desired
if ($pgAll) {
Expand Down
75 changes: 21 additions & 54 deletions www/searchutil.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function convertTimeStringToMinutes($h_m_string) {
}


function doSearch($db, $term, $searchType, $sortby, $limit, $browse)
function doSearch($db, $term, $searchType, $sortby, $limit, $browse, $count_all_possible_rows = false)
{
// we need the current user for some types of queries
checkPersistentLogin();
Expand Down Expand Up @@ -653,91 +653,50 @@ function doSearch($db, $term, $searchType, $sortby, $limit, $browse)
if (!$curuser) {
break;
}
// need to join the playedgames table to do this query
if (!isset($extraJoins[$col])) {
$extraJoins[$col] = true;
$tableList .= " left join playedgames as pg "
. "on games.id = pg.gameid "
. "and pg.userid = '$curuser'";
}

// we need yes=not-null/no=null game ids
$op = (preg_match("/^y.*/i", $txt) ? "is not" : "is");
$expr = "pg.gameid $op null";
$not = (preg_match("/^y.*/i", $txt) ? "" : "not");
$expr = "gameid $not in (select gameid from playedgames where userid = '$curuser')";
break;

case 'willplay':
// Only use this query when the user is logged in
if (!$curuser) {
break;
}
// need to join the wishlists table to do this query
if (!isset($extraJoins[$col])) {
$extraJoins[$col] = true;
$tableList .= " left join wishlists as wl "
. "on games.id = wl.gameid "
. "and wl.userid = '$curuser'";
}

// we need yes=not-null/no=null game ids
$op = (preg_match("/^y.*/i", $txt) ? "is not" : "is");
$expr = "wl.gameid $op null";
$not = (preg_match("/^y.*/i", $txt) ? "" : "not");
$expr = "gameid $not in (select gameid from wishlists where userid = '$curuser')";
break;

case 'wontplay':
// Only use this query when the user is logged in
if (!$curuser) {
break;
}
// need to join the unwishlists table to do this query
if (!isset($extraJoins[$col])) {
$extraJoins[$col] = true;
$tableList .= " left join unwishlists as ul "
. "on games.id = ul.gameid "
. "and ul.userid = '$curuser'";
}

// we need yes=not-null/no=null game ids
$op = (preg_match("/^y.*/i", $txt) ? "is not" : "is");
$expr = "ul.gameid $op null";
$not = (preg_match("/^y.*/i", $txt) ? "" : "not");
$expr = "gameid $not in (select gameid from unwishlists where userid = '$curuser')";
break;


case 'reviewed':
// Only use this query when the user is logged in
if (!$curuser) {
break;
}
// need to join the reviews table to do this query
if (!isset($extraJoins[$col])) {
$extraJoins[$col] = true;
$tableList .= " left join reviews as reviewed "
. "on games.id = reviewed.gameid "
. "and reviewed.review is not null "
. "and reviewed.userid = '$curuser'";
}

// we need yes=not-null/no=null game ids
$op = (preg_match("/^y.*/i", $txt) ? "is not" : "is");
$expr = "reviewed.gameid $op null";
$not = (preg_match("/^y.*/i", $txt) ? "" : "not");
$expr = "gameid $not in (select gameid from reviews where review is not null and userid = '$curuser')";
break;

case 'rated':
// Only use this query when the user is logged in
if (!$curuser) {
break;
}
// need to join the reviews table to do this query
if (!isset($extraJoins[$col])) {
$extraJoins[$col] = true;
$tableList .= " left join reviews as rated "
. "on games.id = rated.gameid "
. "and rated.rating is not null "
. "and rated.userid = '$curuser'";
}

// we need yes=not-null/no=null game ids
$op = (preg_match("/^y.*/i", $txt) ? "is not" : "is");
$expr = "rated.gameid $op null";
$not = (preg_match("/^y.*/i", $txt) ? "" : "not");
$expr = "gameid $not in (select gameid from reviews where rating is not null and userid = '$curuser')";
break;

case 'author':
Expand Down Expand Up @@ -1118,6 +1077,14 @@ function doSearch($db, $term, $searchType, $sortby, $limit, $browse)
// But when browsing for all games, we can do a fast `count(*)` query instead
$sql_calc_found_rows = "";
}
if (!$count_all_possible_rows) {
// `sql_calc_found_rows` forces the query to ignore the `limit` clause
// in order to count all possible results, which means a slower full
// table scan. If the total number of rows is not needed, we can skip
// `sql_calc_found_rows` to speed up the query.
$sql_calc_found_rows = "";
}


// build the SELECT statement
$sql = "select $sql_calc_found_rows
Expand Down Expand Up @@ -1170,7 +1137,7 @@ function doSearch($db, $term, $searchType, $sortby, $limit, $browse)
$result = mysql_query("select count(*) from games", $db);
[$rowcnt] = mysql_fetch_row($result);
} else {
$rowcnt = length($rows);
$rowcnt = count($rows);
}

} else {
Expand Down