Move things along the DROP TABLE.
[mediawiki.git] / includes / search / SearchPostgres.php
blobc9a2c0bde067285bd4b222b50aa3d9c25351289b
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 function __construct( $db ) {
34 $this->db = $db;
37 /**
38 * Perform a full text search query via tsearch2 and return a result set.
39 * Currently searches a page's current title (page.page_title) and
40 * latest revision article text (pagecontent.old_text)
42 * @param $term String: raw search term
43 * @return PostgresSearchResultSet
45 function searchTitle( $term ) {
46 $q = $this->searchQuery( $term , 'titlevector', 'page_title' );
47 $olderror = error_reporting(E_ERROR);
48 $resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) );
49 error_reporting($olderror);
50 if (!$resultSet) {
51 // Needed for "Query requires full scan, GIN doesn't support it"
52 return new SearchResultTooMany();
54 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
56 function searchText( $term ) {
57 $q = $this->searchQuery( $term, 'textvector', 'old_text' );
58 $olderror = error_reporting(E_ERROR);
59 $resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) );
60 error_reporting($olderror);
61 if (!$resultSet) {
62 return new SearchResultTooMany();
64 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
69 * Transform the user's search string into a better form for tsearch2
70 * Returns an SQL fragment consisting of quoted text to search for.
72 function parseQuery( $term ) {
74 wfDebug( "parseQuery received: $term \n" );
76 ## No backslashes allowed
77 $term = preg_replace('/\\\/', '', $term);
79 ## Collapse parens into nearby words:
80 $term = preg_replace('/\s*\(\s*/', ' (', $term);
81 $term = preg_replace('/\s*\)\s*/', ') ', $term);
83 ## Treat colons as word separators:
84 $term = preg_replace('/:/', ' ', $term);
86 $searchstring = '';
87 $m = array();
88 if( preg_match_all('/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
89 foreach( $m as $terms ) {
90 if (strlen($terms[1])) {
91 $searchstring .= ' & !';
93 if (strtolower($terms[2]) === 'and') {
94 $searchstring .= ' & ';
96 else if (strtolower($terms[2]) === 'or' or $terms[2] === '|') {
97 $searchstring .= ' | ';
99 else if (strtolower($terms[2]) === 'not') {
100 $searchstring .= ' & !';
102 else {
103 $searchstring .= " & $terms[2]";
108 ## Strip out leading junk
109 $searchstring = preg_replace('/^[\s\&\|]+/', '', $searchstring);
111 ## Remove any doubled-up operators
112 $searchstring = preg_replace('/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring);
114 ## Remove any non-spaced operators (e.g. "Zounds!")
115 $searchstring = preg_replace('/([^ ])[\!\&\|]/', "$1", $searchstring);
117 ## Remove any trailing whitespace or operators
118 $searchstring = preg_replace('/[\s\!\&\|]+$/', '', $searchstring);
120 ## Remove unnecessary quotes around everything
121 $searchstring = preg_replace('/^[\'"](.*)[\'"]$/', "$1", $searchstring);
123 ## Quote the whole thing
124 $searchstring = $this->db->addQuotes($searchstring);
126 wfDebug( "parseQuery returned: $searchstring \n" );
128 return $searchstring;
133 * Construct the full SQL query to do the search.
134 * @param $term String
135 * @param $fulltext String
136 * @param $colname
138 function searchQuery( $term, $fulltext, $colname ) {
139 $postgresVersion = $this->db->getServerVersion();
141 $prefix = $postgresVersion < 8.3 ? "'default'," : '';
143 # Get the SQL fragment for the given term
144 $searchstring = $this->parseQuery( $term );
146 ## We need a separate query here so gin does not complain about empty searches
147 $SQL = "SELECT to_tsquery($prefix $searchstring)";
148 $res = $this->db->doQuery($SQL);
149 if (!$res) {
150 ## TODO: Better output (example to catch: one 'two)
151 die ("Sorry, that was not a valid search string. Please go back and try again");
153 $top = pg_fetch_result($res,0,0);
155 if ($top === "") { ## e.g. if only stopwords are used XXX return something better
156 $query = "SELECT page_id, page_namespace, page_title, 0 AS score ".
157 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
158 "AND r.rev_text_id = c.old_id AND 1=0";
160 else {
161 $m = array();
162 if( preg_match_all("/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
163 foreach( $m as $terms ) {
164 $this->searchTerms[$terms[1]] = $terms[1];
168 $rankscore = $postgresVersion > 8.2 ? 5 : 1;
169 $rank = $postgresVersion < 8.3 ? 'rank' : 'ts_rank';
170 $query = "SELECT page_id, page_namespace, page_title, ".
171 "$rank($fulltext, to_tsquery($prefix $searchstring), $rankscore) AS score ".
172 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
173 "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery($prefix $searchstring)";
176 ## Redirects
177 if (! $this->showRedirects)
178 $query .= ' AND page_is_redirect = 0';
180 ## Namespaces - defaults to 0
181 if( !is_null($this->namespaces) ){ // null -> search all
182 if ( count($this->namespaces) < 1)
183 $query .= ' AND page_namespace = 0';
184 else {
185 $namespaces = $this->db->makeList( $this->namespaces );
186 $query .= " AND page_namespace IN ($namespaces)";
190 $query .= " ORDER BY score DESC, page_id DESC";
192 $query .= $this->db->limitResult( '', $this->limit, $this->offset );
194 wfDebug( "searchQuery returned: $query \n" );
196 return $query;
199 ## Most of the work of these two functions are done automatically via triggers
201 function update( $pageid, $title, $text ) {
202 ## We don't want to index older revisions
203 $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id IN ".
204 "(SELECT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) .
205 " ORDER BY rev_text_id DESC OFFSET 1)";
206 $this->db->doQuery($SQL);
207 return true;
210 function updateTitle( $id, $title ) {
211 return true;
214 } ## end of the SearchPostgres class
217 * @ingroup Search
219 class PostgresSearchResult extends SearchResult {
220 function __construct( $row ) {
221 parent::__construct($row);
222 $this->score = $row->score;
224 function getScore() {
225 return $this->score;
230 * @ingroup Search
232 class PostgresSearchResultSet extends SqlSearchResultSet {
233 function __construct( $resultSet, $terms ) {
234 parent::__construct( $resultSet, $terms );
237 function next() {
238 $row = $this->mResultSet->fetchObject();
239 if( $row === false ) {
240 return false;
241 } else {
242 return new PostgresSearchResult( $row );