Localisation updates for core messages from Betawiki (2008-03-14 18:02 CET)
[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 $q = $this->searchQuery( $term , 'titlevector', 'page_title' );
41 $olderror = error_reporting(E_ERROR);
42 $resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) );
43 error_reporting($olderror);
44 if (!$resultSet) {
45 // Needed for "Query requires full scan, GIN doesn't support it"
46 return new SearchResultTooMany();
47 }
48 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
49 }
50 function searchText( $term ) {
51 $q = $this->searchQuery( $term, 'textvector', 'old_text' );
52 $olderror = error_reporting(E_ERROR);
53 $resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) );
54 error_reporting($olderror);
55 if (!$resultSet) {
56 return new SearchResultTooMany();
57 }
58 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
59 }
60
61
62 /*
63 * Transform the user's search string into a better form for tsearch2
64 */
65 function parseQuery( $term ) {
66
67 wfDebug( "parseQuery received: $term" );
68
69 ## No backslashes allowed
70 $term = preg_replace('/\\\/', '', $term);
71
72 ## Collapse parens into nearby words:
73 $term = preg_replace('/\s*\(\s*/', ' (', $term);
74 $term = preg_replace('/\s*\)\s*/', ') ', $term);
75
76 ## Treat colons as word separators:
77 $term = preg_replace('/:/', ' ', $term);
78
79 $searchstring = '';
80 $m = array();
81 if( preg_match_all('/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
82 foreach( $m as $terms ) {
83 if (strlen($terms[1])) {
84 $searchstring .= ' & !';
85 }
86 if (strtolower($terms[2]) === 'and') {
87 $searchstring .= ' & ';
88 }
89 else if (strtolower($terms[2]) === 'or' or $terms[2] === '|') {
90 $searchstring .= ' | ';
91 }
92 else if (strtolower($terms[2]) === 'not') {
93 $searchstring .= ' & !';
94 }
95 else {
96 $searchstring .= " & $terms[2]";
97 }
98 }
99 }
100
101 ## Strip out leading junk
102 $searchstring = preg_replace('/^[\s\&\|]+/', '', $searchstring);
103
104 ## Remove any doubled-up operators
105 $searchstring = preg_replace('/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring);
106
107 ## Remove any non-spaced operators (e.g. "Zounds!")
108 $searchstring = preg_replace('/([^ ])[\!\&\|]/', "$1", $searchstring);
109
110 ## Remove any trailing whitespace or operators
111 $searchstring = preg_replace('/[\s\!\&\|]+$/', '', $searchstring);
112
113 ## Remove unnecessary quotes around everything
114 $searchstring = preg_replace('/^[\'"](.*)[\'"]$/', "$1", $searchstring);
115
116 ## Quote the whole thing
117 $searchstring = $this->db->addQuotes($searchstring);
118
119 wfDebug( "parseQuery returned: $searchstring" );
120
121 return $searchstring;
122
123 }
124
125 /**
126 * Construct the full SQL query to do the search.
127 * @param string $filteredTerm
128 * @param string $fulltext
129 * @private
130 */
131 function searchQuery( $term, $fulltext, $colname ) {
132 global $wgDBversion;
133
134 if ( !isset( $wgDBversion ) ) {
135 $this->db->getServerVersion();
136 $wgDBversion = $this->db->numeric_version;
137 }
138 $prefix = $wgDBversion < 8.3 ? "'default'," : '';
139
140 $searchstring = $this->parseQuery( $term );
141
142 ## We need a separate query here so gin does not complain about empty searches
143 $SQL = "SELECT to_tsquery($prefix $searchstring)";
144 $res = $this->db->doQuery($SQL);
145 if (!$res) {
146 ## TODO: Better output (example to catch: one 'two)
147 die ("Sorry, that was not a valid search string. Please go back and try again");
148 }
149 $top = pg_fetch_result($res,0,0);
150
151 if ($top === "") { ## e.g. if only stopwords are used XXX return something better
152 $query = "SELECT page_id, page_namespace, page_title, 0 AS score ".
153 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
154 "AND r.rev_text_id = c.old_id AND 1=0";
155 }
156 else {
157 $m = array();
158 if( preg_match_all("/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
159 foreach( $m as $terms ) {
160 $this->searchTerms[$terms[1]] = $terms[1];
161 }
162 }
163
164 $rankscore = $wgDBversion > 8.2 ? 5 : 1;
165 $rank = $wgDBversion < 8.3 ? 'rank' : 'ts_rank';
166 $query = "SELECT page_id, page_namespace, page_title, ".
167 "$rank($fulltext, to_tsquery($prefix $searchstring), $rankscore) AS score ".
168 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
169 "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery($prefix $searchstring)";
170 }
171
172 ## Redirects
173 if (! $this->showRedirects)
174 $query .= ' AND page_is_redirect = 0';
175
176 ## Namespaces - defaults to 0
177 if ( count($this->namespaces) < 1)
178 $query .= ' AND page_namespace = 0';
179 else {
180 $namespaces = implode( ',', $this->namespaces );
181 $query .= " AND page_namespace IN ($namespaces)";
182 }
183
184 $query .= " ORDER BY score DESC, page_id DESC";
185
186 $query .= $this->db->limitResult( '', $this->limit, $this->offset );
187
188 wfDebug( "searchQuery returned: $query" );
189
190 return $query;
191 }
192
193 ## Most of the work of these two functions are done automatically via triggers
194
195 function update( $pageid, $title, $text ) {
196 ## We don't want to index older revisions
197 $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id = ".
198 "(SELECT rev_text_id FROM revision WHERE rev_page = $pageid ".
199 "ORDER BY rev_text_id DESC LIMIT 1 OFFSET 1)";
200 $this->db->doQuery($SQL);
201 return true;
202 }
203
204 function updateTitle( $id, $title ) {
205 return true;
206 }
207
208 } ## end of the SearchPostgres class
209
210 /**
211 * @addtogroup Search
212 */
213 class PostgresSearchResult extends SearchResult {
214 function PostgresSearchResult( $row ) {
215 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
216 $this->score = $row->score;
217 }
218 function getScore() {
219 return $this->score;
220 }
221 }
222
223 /**
224 * @addtogroup Search
225 */
226 class PostgresSearchResultSet extends SearchResultSet {
227 function PostgresSearchResultSet( $resultSet, $terms ) {
228 $this->mResultSet = $resultSet;
229 $this->mTerms = $terms;
230 }
231
232 function termMatches() {
233 return $this->mTerms;
234 }
235
236 function numRows() {
237 return $this->mResultSet->numRows();
238 }
239
240 function next() {
241 $row = $this->mResultSet->fetchObject();
242 if( $row === false ) {
243 return false;
244 } else {
245 return new PostgresSearchResult( $row );
246 }
247 }
248 }
249
250
251