Change reserved word table names "user" and "text"
[lhc/web/wiklou.git] / includes / SearchPostgres.php
1 <?php
2 # Copyright (C) 2006 Greg Sabino Mullane <greg@turnstep.com>
3 # http://www.mediawiki.org/
4 #
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.
9 #
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.
14 #
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
19
20 ## XXX Better catching of SELECT to_tsquery('the')
21
22 /**
23 * Search engine hook base class for Postgres
24 * @package MediaWiki
25 * @subpackage Search
26 */
27
28 /** @package MediaWiki */
29 class SearchPostgres extends SearchEngine {
30
31 function SearchPostgres( &$db ) {
32 $this->db =& $db;
33 }
34
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)
38 *
39 * @param string $term - Raw search term
40 * @return PostgresSearchResultSet
41 * @access public
42 */
43 function searchText( $term ) {
44 $resultSet = $this->db->resultObject( $this->db->query( $this->searchQuery( $term, 'textvector' ) ) );
45 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
46 }
47 function searchTitle( $term ) {
48 $resultSet = $this->db->resultObject( $this->db->query( $this->searchQuery( $term , 'titlevector' ) ) );
49 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
50 }
51
52
53 /*
54 * Transform the user's search string into a better form for tsearch2
55 */
56 function parseQuery( $filteredText, $fulltext ) {
57 global $wgContLang;
58 $lc = SearchEngine::legalSearchChars();
59 $searchon = '';
60 $this->searchTerms = array();
61
62 # FIXME: This doesn't handle parenthetical expressions.
63 if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
64 $filteredText, $m, PREG_SET_ORDER ) ) {
65 foreach( $m as $terms ) {
66 if( $searchon !== '' ) $searchon .= ' ';
67 if($terms[1] == '') {
68 $terms[1] = '+';
69 }
70 $searchon .= $terms[1] . $wgContLang->stripForSearch( $terms[2] );
71 if( !empty( $terms[3] ) ) {
72 $regexp = preg_quote( $terms[3], '/' );
73 if( $terms[4] ) $regexp .= "[0-9A-Za-z_]+";
74 } else {
75 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
76 }
77 $this->searchTerms[] = $regexp;
78 }
79 wfDebug( "Would search with '$searchon'\n" );
80 wfDebug( "Match with /\b" . implode( '\b|\b', $this->searchTerms ) . "\b/\n" );
81 } else {
82 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
83 }
84
85 $searchon = preg_replace('/(\s+)/','&',$searchon);
86 $searchon = $this->db->strencode( $searchon );
87 return $searchon;
88 }
89
90 /**
91 * Construct the full SQL query to do the search.
92 * @param string $filteredTerm
93 * @param string $fulltext
94 * @private
95 */
96 function searchQuery( $filteredTerm, $fulltext ) {
97
98 $match = $this->parseQuery( $filteredTerm, $fulltext );
99
100 $query = "SELECT page_id, page_namespace, page_title, old_text AS page_text ".
101 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
102 "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery('$match')";
103
104 ## Redirects
105 if (! $this->showRedirects)
106 $query .= ' AND page_is_redirect = 0'; ## IS FALSE
107
108 ## Namespaces - defaults to 0
109 if ( count($this->namespaces) < 1)
110 $query .= ' AND page_namespace = 0';
111 else {
112 $namespaces = implode( ',', $this->namespaces );
113 $query .= " AND page_namespace IN ($namespaces)";
114 }
115
116 $query .= " ORDER BY rank($fulltext, to_tsquery('$fulltext')) DESC";
117
118 $query .= $this->db->limitResult( '', $this->limit, $this->offset );
119
120 return $query;
121 }
122
123 ## These two functions are done automatically via triggers
124
125 function update( $id, $title, $text ) { return true; }
126 function updateTitle( $id, $title ) { return true; }
127
128 } ## end of the SearchPostgres class
129
130
131 /** @package MediaWiki */
132 class PostgresSearchResultSet extends SearchResultSet {
133 function PostgresSearchResultSet( $resultSet, $terms ) {
134 $this->mResultSet = $resultSet;
135 $this->mTerms = $terms;
136 }
137
138 function termMatches() {
139 return $this->mTerms;
140 }
141
142 function numRows() {
143 return $this->mResultSet->numRows();
144 }
145
146 function next() {
147 $row = $this->mResultSet->fetchObject();
148 if( $row === false ) {
149 return false;
150 } else {
151 return new SearchResult( $row );
152 }
153 }
154 }
155
156 ?>