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