Changing URLs of mediawiki.org in scripts to the SSL-based website
[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 * https://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 SearchDatabase {
32 /**
33 * Perform a full text search query via tsearch2 and return a result set.
34 * Currently searches a page's current title (page.page_title) and
35 * latest revision article text (pagecontent.old_text)
36 *
37 * @param string $term raw search term
38 * @return PostgresSearchResultSet
39 */
40 function searchTitle( $term ) {
41 $q = $this->searchQuery( $term, 'titlevector', 'page_title' );
42 $olderror = error_reporting( E_ERROR );
43 $resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) );
44 error_reporting( $olderror );
45 if ( !$resultSet ) {
46 // Needed for "Query requires full scan, GIN doesn't support it"
47 return new SearchResultTooMany();
48 }
49 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
50 }
51
52 function searchText( $term ) {
53 $q = $this->searchQuery( $term, 'textvector', 'old_text' );
54 $olderror = error_reporting( E_ERROR );
55 $resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) );
56 error_reporting( $olderror );
57 if ( !$resultSet ) {
58 return new SearchResultTooMany();
59 }
60 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
61 }
62
63 /**
64 * Transform the user's search string into a better form for tsearch2
65 * Returns an SQL fragment consisting of quoted text to search for.
66 *
67 * @param $term string
68 *
69 * @return string
70 */
71 function parseQuery( $term ) {
72
73 wfDebug( "parseQuery received: $term \n" );
74
75 ## No backslashes allowed
76 $term = preg_replace( '/\\\/', '', $term );
77
78 ## Collapse parens into nearby words:
79 $term = preg_replace( '/\s*\(\s*/', ' (', $term );
80 $term = preg_replace( '/\s*\)\s*/', ') ', $term );
81
82 ## Treat colons as word separators:
83 $term = preg_replace( '/:/', ' ', $term );
84
85 $searchstring = '';
86 $m = array();
87 if ( preg_match_all( '/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
88 foreach ( $m as $terms ) {
89 if ( strlen( $terms[1] ) ) {
90 $searchstring .= ' & !';
91 }
92 if ( strtolower( $terms[2] ) === 'and' ) {
93 $searchstring .= ' & ';
94 }
95 elseif ( strtolower( $terms[2] ) === 'or' or $terms[2] === '|' ) {
96 $searchstring .= ' | ';
97 }
98 elseif ( strtolower( $terms[2] ) === 'not' ) {
99 $searchstring .= ' & !';
100 }
101 else {
102 $searchstring .= " & $terms[2]";
103 }
104 }
105 }
106
107 ## Strip out leading junk
108 $searchstring = preg_replace( '/^[\s\&\|]+/', '', $searchstring );
109
110 ## Remove any doubled-up operators
111 $searchstring = preg_replace( '/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring );
112
113 ## Remove any non-spaced operators (e.g. "Zounds!")
114 $searchstring = preg_replace( '/([^ ])[\!\&\|]/', "$1", $searchstring );
115
116 ## Remove any trailing whitespace or operators
117 $searchstring = preg_replace( '/[\s\!\&\|]+$/', '', $searchstring );
118
119 ## Remove unnecessary quotes around everything
120 $searchstring = preg_replace( '/^[\'"](.*)[\'"]$/', "$1", $searchstring );
121
122 ## Quote the whole thing
123 $searchstring = $this->db->addQuotes( $searchstring );
124
125 wfDebug( "parseQuery returned: $searchstring \n" );
126
127 return $searchstring;
128
129 }
130
131 /**
132 * Construct the full SQL query to do the search.
133 * @param $term String
134 * @param $fulltext String
135 * @param $colname
136 * @return string
137 */
138 function searchQuery( $term, $fulltext, $colname ) {
139 # Get the SQL fragment for the given term
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($searchstring)";
144 $res = $this->db->query( $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 = $res->fetchRow();
150 $top = $top[0];
151
152 if ( $top === "" ) { ## e.g. if only stopwords are used XXX return something better
153 $query = "SELECT page_id, page_namespace, page_title, 0 AS score " .
154 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
155 "AND r.rev_text_id = c.old_id AND 1=0";
156 }
157 else {
158 $m = array();
159 if ( preg_match_all( "/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
160 foreach ( $m as $terms ) {
161 $this->searchTerms[$terms[1]] = $terms[1];
162 }
163 }
164
165 $query = "SELECT page_id, page_namespace, page_title, " .
166 "ts_rank($fulltext, to_tsquery($searchstring), 5) 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 $fulltext @@ to_tsquery($searchstring)";
169 }
170
171 ## Redirects
172 if ( !$this->showRedirects ) {
173 $query .= ' AND page_is_redirect = 0';
174 }
175
176 ## Namespaces - defaults to 0
177 if ( !is_null( $this->namespaces ) ) { // null -> search all
178 if ( count( $this->namespaces ) < 1 ) {
179 $query .= ' AND page_namespace = 0';
180 } else {
181 $namespaces = $this->db->makeList( $this->namespaces );
182 $query .= " AND page_namespace IN ($namespaces)";
183 }
184 }
185
186 $query .= " ORDER BY score DESC, page_id DESC";
187
188 $query .= $this->db->limitResult( '', $this->limit, $this->offset );
189
190 wfDebug( "searchQuery returned: $query \n" );
191
192 return $query;
193 }
194
195 ## Most of the work of these two functions are done automatically via triggers
196
197 function update( $pageid, $title, $text ) {
198 ## We don't want to index older revisions
199 $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id IN " .
200 "(SELECT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) .
201 " ORDER BY rev_text_id DESC OFFSET 1)";
202 $this->db->query( $SQL );
203 return true;
204 }
205
206 function updateTitle( $id, $title ) {
207 return true;
208 }
209
210 } ## end of the SearchPostgres class
211
212 /**
213 * @ingroup Search
214 */
215 class PostgresSearchResult extends SearchResult {
216 function __construct( $row ) {
217 parent::__construct( $row );
218 $this->score = $row->score;
219 }
220 function getScore() {
221 return $this->score;
222 }
223 }
224
225 /**
226 * @ingroup Search
227 */
228 class PostgresSearchResultSet extends SqlSearchResultSet {
229 function __construct( $resultSet, $terms ) {
230 parent::__construct( $resultSet, $terms );
231 }
232
233 function next() {
234 $row = $this->mResultSet->fetchObject();
235 if ( $row === false ) {
236 return false;
237 } else {
238 return new PostgresSearchResult( $row );
239 }
240 }
241 }