BUGFIX: limit the query results for pagination

This commit is contained in:
Mateusz Uzdowski 2010-09-01 04:06:36 +00:00
parent 2aff1a9196
commit 2b5f25455f

View File

@ -1228,9 +1228,8 @@ class MSSQLDatabase extends SS_Database {
*/ */
public function searchEngine($classesToSearch, $keywords, $start, $pageLength, $sortBy = "Relevance DESC", $extraFilter = "", $booleanSearch = false, $alternativeFileFilter = "", $invertedMatch = false) { public function searchEngine($classesToSearch, $keywords, $start, $pageLength, $sortBy = "Relevance DESC", $extraFilter = "", $booleanSearch = false, $alternativeFileFilter = "", $invertedMatch = false) {
$results = new DataObjectSet(); $results = new DataObjectSet();
if(!$this->fullTextEnabled()) { if(!$this->fullTextEnabled()) return $results;
return $results; if (substr($sortBy, 0, 9)!='Relevance') user_error("Non-relevance sort not supported.", E_USER_ERROR);
}
//Get a list of all the tables and columns we'll be searching on: //Get a list of all the tables and columns we'll be searching on:
$fulltextColumns = DB::query('EXEC sp_help_fulltext_columns'); $fulltextColumns = DB::query('EXEC sp_help_fulltext_columns');
@ -1266,7 +1265,7 @@ class MSSQLDatabase extends SS_Database {
$queries[$tableName]->orderby = null; $queries[$tableName]->orderby = null;
} }
// Generate SQL and count totals // Generate SQL
$querySQLs = array(); $querySQLs = array();
foreach($queries as $query) { foreach($queries as $query) {
$querySQLs[] = $query->sql(); $querySQLs[] = $query->sql();
@ -1278,14 +1277,18 @@ class MSSQLDatabase extends SS_Database {
// Perform the search // Perform the search
$result = DB::query($fullQuery); $result = DB::query($fullQuery);
// Regenerate the DataObjects, apply security // Regenerate DataObjectSet
$totalCount = 0; $totalCount = $result->numRecords();
$current = -1;
$results = new DataObjectSet();
foreach ($result as $row) { foreach ($result as $row) {
$record = DataObject::get_by_id($row['Source'], $row['ID']); $current++;
if($record->canView()) {
$results->push($record); // Select a subset for paging
$totalCount++; if ($current>=$start+$pageLength) break;
} if ($current<$start) continue;
$results->push(DataObject::get_by_id($row['Source'], $row['ID']));
} }
$results->setPageLimits($start, $pageLength, $totalCount); $results->setPageLimits($start, $pageLength, $totalCount);