Merge "Code style cleanup for ApiQuerySiteinfo.php."
[mediawiki.git] / includes / search / SearchPostgres.php
blob7f19ed13ff8485f05f8b6fe588bde54469ac5863
1 <?php
2 /**
3 * PostgreSQL search engine
5 * Copyright © 2006-2007 Greg Sabino Mullane <greg@turnstep.com>
6 * http://www.mediawiki.org/
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.
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.
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
23 * @file
24 * @ingroup Search
27 /**
28 * Search engine hook base class for Postgres
29 * @ingroup Search
31 class SearchPostgres extends SearchEngine {
33 /**
34 * @var DatabasePostgres
36 protected $db;
37 /**
38 * Creates an instance of this class
39 * @param $db DatabaseSqlite: database object
41 function __construct( $db ) {
42 parent::__construct( $db );
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)
50 * @param string $term raw search term
51 * @return PostgresSearchResultSet
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();
62 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
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();
73 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
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.
80 * @param $term string
82 * @return string
84 function parseQuery( $term ) {
86 wfDebug( "parseQuery received: $term \n" );
88 ## No backslashes allowed
89 $term = preg_replace( '/\\\/', '', $term );
91 ## Collapse parens into nearby words:
92 $term = preg_replace( '/\s*\(\s*/', ' (', $term );
93 $term = preg_replace( '/\s*\)\s*/', ') ', $term );
95 ## Treat colons as word separators:
96 $term = preg_replace( '/:/', ' ', $term );
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 .= ' & !';
105 if ( strtolower( $terms[2] ) === 'and' ) {
106 $searchstring .= ' & ';
108 elseif ( strtolower( $terms[2] ) === 'or' or $terms[2] === '|' ) {
109 $searchstring .= ' | ';
111 elseif ( strtolower( $terms[2] ) === 'not' ) {
112 $searchstring .= ' & !';
114 else {
115 $searchstring .= " & $terms[2]";
120 ## Strip out leading junk
121 $searchstring = preg_replace( '/^[\s\&\|]+/', '', $searchstring );
123 ## Remove any doubled-up operators
124 $searchstring = preg_replace( '/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring );
126 ## Remove any non-spaced operators (e.g. "Zounds!")
127 $searchstring = preg_replace( '/([^ ])[\!\&\|]/', "$1", $searchstring );
129 ## Remove any trailing whitespace or operators
130 $searchstring = preg_replace( '/[\s\!\&\|]+$/', '', $searchstring );
132 ## Remove unnecessary quotes around everything
133 $searchstring = preg_replace( '/^[\'"](.*)[\'"]$/', "$1", $searchstring );
135 ## Quote the whole thing
136 $searchstring = $this->db->addQuotes( $searchstring );
138 wfDebug( "parseQuery returned: $searchstring \n" );
140 return $searchstring;
145 * Construct the full SQL query to do the search.
146 * @param $term String
147 * @param $fulltext String
148 * @param $colname
149 * @return string
151 function searchQuery( $term, $fulltext, $colname ) {
152 # Get the SQL fragment for the given term
153 $searchstring = $this->parseQuery( $term );
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" );
162 $top = $res->fetchRow();
163 $top = $top[0];
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";
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];
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)";
184 ## Redirects
185 if ( !$this->showRedirects ) {
186 $query .= ' AND page_is_redirect = 0';
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)";
199 $query .= " ORDER BY score DESC, page_id DESC";
201 $query .= $this->db->limitResult( '', $this->limit, $this->offset );
203 wfDebug( "searchQuery returned: $query \n" );
205 return $query;
208 ## Most of the work of these two functions are done automatically via triggers
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;
219 function updateTitle( $id, $title ) {
220 return true;
223 } ## end of the SearchPostgres class
226 * @ingroup Search
228 class PostgresSearchResult extends SearchResult {
229 function __construct( $row ) {
230 parent::__construct( $row );
231 $this->score = $row->score;
233 function getScore() {
234 return $this->score;
239 * @ingroup Search
241 class PostgresSearchResultSet extends SqlSearchResultSet {
242 function __construct( $resultSet, $terms ) {
243 parent::__construct( $resultSet, $terms );
246 function next() {
247 $row = $this->mResultSet->fetchObject();
248 if ( $row === false ) {
249 return false;
250 } else {
251 return new PostgresSearchResult( $row );