3 * FusionForge search engine
5 * Copyright 2004, Dominik Haas
7 * This file is part of FusionForge.
9 * FusionForge is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published
11 * by the Free Software Foundation; either version 2 of the License,
12 * or (at your option) any later version.
14 * FusionForge is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with FusionForge; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 require_once $gfcommon.'search/SearchQuery.class.php';
27 class FrsSearchQuery extends SearchQuery {
37 * flag if non public items are returned
39 * @var boolean $showNonPublic
46 * @param string $words words we are searching for
47 * @param int $offset offset
48 * @param boolean $isExact if we want to search for all the words or if only one matching the query is sufficient
49 * @param int $groupId group id
50 * @param array $sections sections to search in
52 function FrsSearchQuery($words, $offset, $isExact, $groupId, $sections=SEARCH__ALL_SECTIONS, $showNonPublic=false) {
53 $this->groupId = $groupId;
54 $this->showNonPublic = $showNonPublic;
56 $this->SearchQuery($words, $offset, $isExact);
58 $this->setSections($sections);
62 * getQuery - get the sql query built to get the search results
64 * @return string sql query to execute
69 if(count($this->words)) {
70 $tsquery0 = "headline(frs_package.name, q) AS package_name, headline(frs_release.name, q) as release_name";
71 $tsquery = ", to_tsquery('".$this->getFormattedWords()."') AS q, frs_release_idx r, frs_file_idx f";
72 $tsmatch = "(f.vectors @@ q OR r.vectors @@ q)";
74 $tsjoin = 'AND r.release_id = frs_release.release_id AND f.file_id = frs_file.file_id';
75 $orderBy = "ORDER BY frs_package.name, frs_release.name";
76 $phraseOp = $this->getOperator();
78 $tsquery0 = "frs_package.name as package_name, frs_release.name as release_name";
83 $orderBy = "ORDER BY frs_package.name, frs_release.name";
87 if(count($this->phrases)) {
88 $phraseCond .= $phraseOp.'(('.$this->getMatchCond('frs_release.changes', $this->phrases).')'
89 . ' OR ('.$this->getMatchCond('frs_release.notes', $this->phrases).')'
90 . ' OR ('.$this->getMatchCond('frs_release.name', $this->phrases).')'
91 . ' OR ('.$this->getMatchCond('frs_file.filename', $this->phrases).'))';
93 $sql = 'SELECT '.$tsquery0.', frs_release.release_date, frs_release.release_id, users.realname'
94 . ' FROM frs_file, frs_release, users, frs_package'.$tsquery
95 . ' WHERE frs_release.released_by = users.user_id'
97 . ' AND frs_package.package_id = frs_release.package_id'
98 . ' AND frs_file.release_id=frs_release.release_id'
99 . ' AND frs_package.group_id='.$this->groupId;
100 if ($this->sections != SEARCH__ALL_SECTIONS) {
101 $sections = $this->sections;
102 $sql .= ' AND frs_package.package_id IN ('.$this->sections.') ';
104 if(!$this->showNonPublic) {
105 $sql .= ' AND is_public=1';
108 $sql .= ' AND ( '.$tsmatch.' '.$phraseCond.') '.$orderBy;
110 $sql = 'SELECT frs_package.name as package_name, frs_release.name as release_name, frs_release.release_date, frs_release.release_id, users.realname'
111 . ' FROM frs_file, frs_release, users, frs_package'
112 . ' WHERE frs_release.released_by = users.user_id'
113 . ' AND frs_package.package_id = frs_release.package_id'
114 . ' AND frs_file.release_id=frs_release.release_id'
115 . ' AND frs_package.group_id='.$this->groupId;
117 if ($this->sections != SEARCH__ALL_SECTIONS) {
118 $sql .= ' AND frs_package.package_id IN ('.$this->sections.') ';
120 if(!$this->showNonPublic) {
121 $sql .= ' AND is_public=1';
124 $sql .= ' AND (('.$this->getIlikeCondition('frs_release.changes', $this->words).')'
125 . ' OR ('.$this->getIlikeCondition('frs_release.notes', $this->words).')'
126 . ' OR ('.$this->getIlikeCondition('frs_release.name', $this->words).')'
127 . ' OR ('.$this->getIlikeCondition('frs_file.filename', $this->words).'))'
128 . ' ORDER BY frs_package.name, frs_release.name';
134 * getSections - returns the list of available forums
136 * @param $groupId int group id
137 * @param $showNonPublic boolean if we should consider non public sections
139 function getSections($groupId, $showNonPublic) {
140 $sql = 'SELECT package_id, name FROM frs_package WHERE group_id = \''.$groupId.'\' ORDER BY name';
142 if(!$showNonPublic) {
143 $sql .= ' AND is_public=1';
145 $sql .= ' ORDER BY name';
148 $res = db_query($sql);
149 while($data = db_fetch_array($res)) {
150 $sections[$data['package_id']] = $data['name'];
158 // c-file-style: "bsd"