a7c7c3da6b8bbfc925ead3b76ea4249401f89732
[lhc/web/wiklou.git] / includes / SearchPostgres.php
1 <?php
2 # Copyright (C) 2006-2007 Greg Sabino Mullane <greg@turnstep.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 * Search engine hook base class for Postgres
22 * @addtogroup Search
23 */
24 class SearchPostgres extends SearchEngine {
25
26 function SearchPostgres( $db ) {
27 $this->db = $db;
28 }
29
30 /**
31 * Perform a full text search query via tsearch2 and return a result set.
32 * Currently searches a page's current title (page.page_title) and
33 * latest revision article text (pagecontent.old_text)
34 *
35 * @param string $term - Raw search term
36 * @return PostgresSearchResultSet
37 * @access public
38 */
39 function searchTitle( $term ) {
40 $resultSet = $this->db->resultObject( $this->db->query( $this->searchQuery( $term , 'titlevector', 'page_title' )));
41 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
42 }
43 function searchText( $term ) {
44 $resultSet = $this->db->resultObject( $this->db->query( $this->searchQuery( $term, 'textvector', 'old_text' )));
45 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
46 }
47
48
49 /*
50 * Transform the user's search string into a better form for tsearch2
51 */
52 function parseQuery( $term ) {
53
54 wfDebug( "parseQuery received: $term" );
55
56 ## No backslashes allowed
57 $term = preg_replace('/\\\/', '', $term);
58
59 ## Collapse parens into nearby words:
60 $term = preg_replace('/\s*\(\s*/', ' (', $term);
61 $term = preg_replace('/\s*\)\s*/', ') ', $term);
62
63 ## Treat colons as word separators:
64 $term = preg_replace('/:/', ' ', $term);
65
66 $searchstring = '';
67 $m = array();
68 if( preg_match_all('/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
69 foreach( $m as $terms ) {
70 if (strlen($terms[1])) {
71 $searchstring .= ' & !';
72 }
73 if (strtolower($terms[2]) === 'and') {
74 $searchstring .= ' & ';
75 }
76 else if (strtolower($terms[2]) === 'or' or $terms[2] === '|') {
77 $searchstring .= ' | ';
78 }
79 else if (strtolower($terms[2]) === 'not') {
80 $searchstring .= ' & !';
81 }
82 else {
83 $searchstring .= " & $terms[2]";
84 }
85 }
86 }
87
88 ## Strip out leading junk
89 $searchstring = preg_replace('/^[\s\&\|]+/', '', $searchstring);
90
91 ## Remove any doubled-up operators
92 $searchstring = preg_replace('/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring);
93
94 ## Remove any non-spaced operators (e.g. "Zounds!")
95 $searchstring = preg_replace('/([^ ])[\!\&\|]/', "$1", $searchstring);
96
97 ## Remove any trailing whitespace or operators
98 $searchstring = preg_replace('/[\s\!\&\|]+$/', '', $searchstring);
99
100 ## Remove unnecessary quotes around everything
101 $searchstring = preg_replace('/^[\'"](.*)[\'"]$/', "$1", $searchstring);
102
103 ## Quote the whole thing
104 $searchstring = $this->db->addQuotes($searchstring);
105
106 wfDebug( "parseQuery returned: $searchstring" );
107
108 return $searchstring;
109
110 }
111
112 /**
113 * Construct the full SQL query to do the search.
114 * @param string $filteredTerm
115 * @param string $fulltext
116 * @private
117 */
118 function searchQuery( $term, $fulltext, $colname ) {
119
120 $searchstring = $this->parseQuery( $term );
121
122 ## We need a separate query here so gin does not complain about empty searches
123 $SQL = "SELECT to_tsquery('default',$searchstring)";
124 $res = $this->db->doQuery($SQL);
125 if (!$res) {
126 ## TODO: Better output (example to catch: one 'two)
127 die ("Sorry, that was not a valid search string. Please go back and try again");
128 }
129 $top = pg_fetch_result($res,0,0);
130
131 if ($top === "") { ## e.g. if only stopwords are used XXX return something better
132 $query = "SELECT page_id, page_namespace, page_title, 0 AS score ".
133 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
134 "AND r.rev_text_id = c.old_id AND 1=0";
135 }
136 else {
137 $m = array();
138 if( preg_match_all("/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
139 foreach( $m as $terms ) {
140 $this->searchTerms[$terms[1]] = $terms[1];
141 }
142 }
143
144 $query = "SELECT page_id, page_namespace, page_title, ".
145 "rank($fulltext, to_tsquery('default',$searchstring),5) AS score ".
146 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
147 "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery('default',$searchstring)";
148 }
149
150 ## Redirects
151 if (! $this->showRedirects)
152 $query .= ' AND page_is_redirect = 0'; ## IS FALSE
153
154 ## Namespaces - defaults to 0
155 if ( count($this->namespaces) < 1)
156 $query .= ' AND page_namespace = 0';
157 else {
158 $namespaces = implode( ',', $this->namespaces );
159 $query .= " AND page_namespace IN ($namespaces)";
160 }
161
162 $query .= " ORDER BY score DESC, page_id DESC";
163
164 $query .= $this->db->limitResult( '', $this->limit, $this->offset );
165
166 wfDebug( "searchQuery returned: $query" );
167
168 return $query;
169 }
170
171 ## Most of the work of these two functions are done automatically via triggers
172
173 function update( $pageid, $title, $text ) {
174 ## We don't want to index older revisions
175 $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id = ".
176 "(SELECT rev_text_id FROM revision WHERE rev_page = $pageid ".
177 "ORDER BY rev_text_id DESC LIMIT 1 OFFSET 1)";
178 $this->db->doQuery($SQL);
179 return true;
180 }
181
182 function updateTitle( $id, $title ) {
183 return true;
184 }
185
186 } ## end of the SearchPostgres class
187
188 /**
189 * @addtogroup Search
190 */
191 class PostgresSearchResult extends SearchResult {
192 function PostgresSearchResult( $row ) {
193 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
194 $this->score = $row->score;
195 }
196 function getScore() {
197 return $this->score;
198 }
199 }
200
201 /**
202 * @addtogroup Search
203 */
204 class PostgresSearchResultSet extends SearchResultSet {
205 function PostgresSearchResultSet( $resultSet, $terms ) {
206 $this->mResultSet = $resultSet;
207 $this->mTerms = $terms;
208 }
209
210 function termMatches() {
211 return $this->mTerms;
212 }
213
214 function numRows() {
215 return $this->mResultSet->numRows();
216 }
217
218 function next() {
219 $row = $this->mResultSet->fetchObject();
220 if( $row === false ) {
221 return false;
222 } else {
223 return new PostgresSearchResult( $row );
224 }
225 }
226 }
227
228
229 ?>