/** * Construct the full SQL query to do the search. * @param $term String * @param $fulltext String * @param $colname */ function searchQuery($term, $fulltext, $colname) { # Get the SQL fragment for the given term $searchstring = $this->parseQuery($term); ## We need a separate query here so gin does not complain about empty searches $SQL = "SELECT to_tsquery({$searchstring})"; $res = $this->db->query($SQL); if (!$res) { ## TODO: Better output (example to catch: one 'two) die("Sorry, that was not a valid search string. Please go back and try again"); } $top = $res->fetchRow(); $top = $top[0]; if ($top === "") { ## e.g. if only stopwords are used XXX return something better $query = "SELECT page_id, page_namespace, page_title, 0 AS score " . "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " . "AND r.rev_text_id = c.old_id AND 1=0"; } else { $m = array(); if (preg_match_all("/'([^']+)'/", $top, $m, PREG_SET_ORDER)) { foreach ($m as $terms) { $this->searchTerms[$terms[1]] = $terms[1]; } } $query = "SELECT page_id, page_namespace, page_title, " . "ts_rank({$fulltext}, to_tsquery({$searchstring}), 5) AS score " . "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " . "AND r.rev_text_id = c.old_id AND {$fulltext} @@ to_tsquery({$searchstring})"; } ## Redirects if (!$this->showRedirects) { $query .= ' AND page_is_redirect = 0'; } ## Namespaces - defaults to 0 if (!is_null($this->namespaces)) { // null -> search all if (count($this->namespaces) < 1) { $query .= ' AND page_namespace = 0'; } else { $namespaces = $this->db->makeList($this->namespaces); $query .= " AND page_namespace IN ({$namespaces})"; } } $query .= " ORDER BY score DESC, page_id DESC"; $query .= $this->db->limitResult('', $this->limit, $this->offset); wfDebug("searchQuery returned: {$query} \n"); return $query; }