function SearchItems($projectList, $search_text, $fields, $searchType, $limitAuthor, $undocOnly, $ResultsLimit = 50) { if ($search_text == "" && $limitAuthor == "" && !$undocOnly) { return "Nothing to search for. Please enter a sentence to search for."; } if ($ResultsLimit == "") { $ResultsLimit = 50; } StartAccessToDB(); $allFields = false; if ($fields == "") { $allFields = true; $fields = "Summary, Extras, Description, ReturnValue, SeeAlsoUndefined, Parameters, Name"; } if ($searchType == "") { $searchType = 0; } $MySQLVersion = mysql_get_server_info(); switch ($searchType) { case 1: // Boolean Mode is only available from 4.0.1 if (strcmp(substr($MySQLVersion, 0, 5), "4.0.1") >= 0) { $SQLSearchType = " IN BOOLEAN MODE"; } else { $SQLSearchType = ""; echo "Warning: Your version of MySQL ({$MySQLVersion}) does not support Boolean Mode, it is supported from version 4.0.1"; } break; case 2: // Query expansions is only available from 4.1.1 if (strcmp(substr($MySQLVersion, 0, 5), "4.1.1") >= 0) { $SQLSearchType = " WITH QUERY EXPANSION"; } else { $SQLSearchType = ""; echo "Warning: Your version of MySQL ({$MySQLVersion}) does not support Query Expansion, it is supported from version 4.1.1"; } break; default: $SQLSearchType = ""; break; } // Add the items that may match (only if the $fields specifier is not Author) if ($fields != "Author") { $request = "SELECT Id FROM jh_units WHERE ProjectId IN (" . $projectList . ") "; if ($limitAuthor != "") { $request .= "AND Author LIKE \"%" . $limitAuthor . "%\" "; } $reqResult = mysql_query($request); if (!$reqResult) { $result = $request . ": " . mysql_error(); } else { while ($item = mysql_fetch_array($reqResult)) { $unitsIdArray[] = $item["Id"]; } if (is_array($unitsIdArray)) { $unitsList = implode(",", $unitsIdArray); $request = "SELECT Id, UnitId, Name, Summary, Extras, Description, MATCH(" . $fields . ") AGAINST ('" . $search_text . "') AS Relevance " . "FROM jh_items " . "WHERE UnitId IN (" . $unitsList . ") "; if ($undocOnly) { $request .= "AND (Summary LIKE \"Write here a Summary (1 line)\" OR " . " Description LIKE \"Write here a description\") "; } if ($search_text != "") { $request .= "AND MATCH(" . $fields . ") AGAINST ('" . $search_text . "'" . $SQLSearchType . ") " . "ORDER BY Relevance DESC "; } $request .= "LIMIT 0, " . $ResultsLimit; $reqResult = mysql_query($request); if (!$reqResult) { $result = $request . ": " . mysql_error(); } else { $result = array(); while ($item = mysql_fetch_array($reqResult)) { $result[] = $item; } } } } } // Now, add the units that may match if ($allFields) { $fields = "Name, Description, Author"; } else { $fieldsList = explode(",", $fields); $fields = ""; foreach ($fieldsList as $field) { $field = trim($field); if ($field == "Name" || $field == "Description" || $field == "Author") { if ($fields == "") { $fields = $field; } else { $fields .= ", " . $field; } } } } $request = "SELECT Id, Name, Description, MATCH(" . $fields . ") AGAINST ('" . $search_text . "') AS Relevance " . "FROM jh_units " . "WHERE ProjectId IN (" . $projectList . ") "; if ($limitAuthor != "") { $request .= "AND Author LIKE \"%" . $limitAuthor . "%\" "; } if ($undocOnly) { $request .= "AND Description LIKE \"Write here a description\" "; } if ($search_text != "") { $request .= "AND MATCH(" . $fields . ") AGAINST ('" . $search_text . "'" . $SQLSearchType . ") " . "ORDER BY Relevance DESC "; } $request .= "LIMIT 0, " . $ResultsLimit; $reqResult = mysql_query($request); if (!$reqResult) { $result = $request . ": " . mysql_error(); } else { while ($item = mysql_fetch_array($reqResult)) { $result[] = $item; } } // Finally, sort all this by Relevance FieldQSort($result, "Relevance"); // And limit to the actual number of results required if (is_array($result)) { $result = array_slice($result, 0, $ResultsLimit); } EndAccessToDB(); return $result; }
function FieldQSort(&$array, $fieldName, $firstElement = null, $lastElement = null) { if (!is_array($array)) { return false; } if (is_null($firstElement)) { $firstElement = 0; } if (is_null($lastElement)) { $lastElement = count($array) - 1; } // echo $array[$firstElement][$fieldName]."<br>"; if ($array[$firstElement][$fieldName] < $array[$lastElement][$fieldName]) { $middleElement = floor(($firstElement + $lastElement) / 2); $compareElement = $array[$middleElement]; $fromLeft = $firstElement; $fromRight = $lastElement; while ($fromLeft <= $fromRight) { while ($array[$fromLeft] < $compareElement) { $fromLeft++; } while ($array[$fromRight] > $compareElement) { $fromRight--; } if ($fromLeft <= $fromRight) { $tmp = $array[$fromLeft]; $array[$fromLeft] = $array[$fromRight]; $array[$fromRight] = $tmp; $fromLeft++; $fromRight--; } } FieldQSort($array, $fieldName, $firstElement, $fromRight); FieldQSort($array, $fieldName, $fromLeft, $lastElement); } return true; }