+ /**
+ * Searches videos in DB based on a search query string and returns an
+ * associative array of results.
+ * If count is zero the function only return the number of results.
+ * @param string $search_query
+ * @param int $offset
+ * @param int $count
+ * @param int $category_id if NULL, all categories are searched
+ * @return array an associative array with the same keys as that from
+ * get_videos_summary's result, but with two additional keys:
+ * description and date.
+ */
+ public function search_videos($search_query, $offset = 0, $count = 0,
+ $category_id = NULL)
+ {
+ if (! $this->is_advanced_search_query($search_query)) // if natural language mode
+ {
+ $search_cond = "MATCH (title, description, tags)
+ AGAINST ('$search_query')";
+ $relevance = "$search_cond AS relevance";
+ } // if boolean mode
+ else
+ {
+ $against = "AGAINST ('$search_query' IN BOOLEAN MODE)";
+ $search_cond = "MATCH (title, description, tags)
+ $against";
+ $relevance = "( (0.5 * (MATCH(title) $against))
+ + (0.3 * (MATCH(tags) $against))
+ + (0.2 * (MATCH(description) $against)) ) AS relevance";
+ }
+
+ if ($count === 0)
+ {
+ $selected_columns = "COUNT(*) count";
+ $order = "";
+ $limit = "";
+ }
+ else
+ {
+ $selected_columns = "id, name, title, duration, user_id, views,
+ thumbs_count, default_thumb, date, description,
+ (views + likes - dislikes) AS score,
+ $relevance";
+ $order = "ORDER BY relevance DESC, score DESC";
+ $limit = "LIMIT $offset, $count";
+ }
+
+ if ($category_id !== NULL)
+ $category_cond = "category_id = '$category_id' AND ";
+ else
+ $category_cond = "";