Revert the attempt to autodetect CGI configs with working PATHINFO, since it turns...
[mediawiki.git] / includes / SearchPostgres.php
blob457636b481754c5808683c865fe880c34962ce5d
1 <?php
2 # Copyright (C) 2006 Greg Sabino Mullane <greg@turnstep.com>
3 # http://www.mediawiki.org/
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.
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.
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
20 ## XXX Better catching of SELECT to_tsquery('the')
22 /**
23 * Search engine hook base class for Postgres
24 * @package MediaWiki
25 * @subpackage Search
28 /** @package MediaWiki */
29 class SearchPostgres extends SearchEngine {
31 function SearchPostgres( &$db ) {
32 $this->db =& $db;
35 /**
36 * Perform a full text search query via tsearch2 and return a result set.
37 * Currently searches a page's current title (p.page_title) and text (t.old_text)
39 * @param string $term - Raw search term
40 * @return PostgresSearchResultSet
41 * @access public
43 function searchText( $term ) {
44 $resultSet = $this->db->resultObject( $this->db->query( $this->searchQuery( $term, 'textvector' ) ) );
45 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
47 function searchTitle( $term ) {
48 $resultSet = $this->db->resultObject( $this->db->query( $this->searchQuery( $term , 'titlevector' ) ) );
49 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
54 * Transform the user's search string into a better form for tsearch2
56 function parseQuery( $filteredText, $fulltext ) {
57 global $wgContLang;
58 $lc = SearchEngine::legalSearchChars();
59 $searchon = '';
60 $this->searchTerms = array();
62 # FIXME: This doesn't handle parenthetical expressions.
63 $m = array();
64 if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
65 $filteredText, $m, PREG_SET_ORDER ) ) {
66 foreach( $m as $terms ) {
67 if( $searchon !== '' ) $searchon .= ' ';
68 if($terms[1] == '') {
69 $terms[1] = '+';
71 $searchon .= $terms[1] . $wgContLang->stripForSearch( $terms[2] );
72 if( !empty( $terms[3] ) ) {
73 $regexp = preg_quote( $terms[3], '/' );
74 if( $terms[4] ) $regexp .= "[0-9A-Za-z_]+";
75 } else {
76 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
78 $this->searchTerms[] = $regexp;
80 wfDebug( "Would search with '$searchon'\n" );
81 wfDebug( 'Match with /\b' . implode( '\b|\b', $this->searchTerms ) . "\b/\n" );
82 } else {
83 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
86 $searchon = preg_replace('/(\s+)/','&',$searchon);
87 $searchon = $this->db->strencode( $searchon );
88 return $searchon;
91 /**
92 * Construct the full SQL query to do the search.
93 * @param string $filteredTerm
94 * @param string $fulltext
95 * @private
97 function searchQuery( $filteredTerm, $fulltext ) {
99 $match = $this->parseQuery( $filteredTerm, $fulltext );
101 $query = "SELECT page_id, page_namespace, page_title, old_text AS page_text, ".
102 "rank(titlevector, to_tsquery('default','$match')) AS rnk ".
103 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
104 "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery('default','$match')";
106 ## Redirects
107 if (! $this->showRedirects)
108 $query .= ' AND page_is_redirect = 0'; ## IS FALSE
110 ## Namespaces - defaults to 0
111 if ( count($this->namespaces) < 1)
112 $query .= ' AND page_namespace = 0';
113 else {
114 $namespaces = implode( ',', $this->namespaces );
115 $query .= " AND page_namespace IN ($namespaces)";
118 $query .= " ORDER BY rnk DESC, page_id DESC";
120 $query .= $this->db->limitResult( '', $this->limit, $this->offset );
122 return $query;
125 ## These two functions are done automatically via triggers
127 function update( $id, $title, $text ) { return true; }
128 function updateTitle( $id, $title ) { return true; }
130 } ## end of the SearchPostgres class
133 /** @package MediaWiki */
134 class PostgresSearchResultSet extends SearchResultSet {
135 function PostgresSearchResultSet( $resultSet, $terms ) {
136 $this->mResultSet = $resultSet;
137 $this->mTerms = $terms;
140 function termMatches() {
141 return $this->mTerms;
144 function numRows() {
145 return $this->mResultSet->numRows();
148 function next() {
149 $row = $this->mResultSet->fetchObject();
150 if( $row === false ) {
151 return false;
152 } else {
153 return new SearchResult( $row );