Fixed static member invocation, was broken since inception.
[lhc/web/wiklou.git] / includes / SearchPostgres.php
1 <?php
2 # Copyright (C) 2006-2007 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 * @addtogroup Search
25 */
26
27 class SearchPostgres extends SearchEngine {
28
29 function SearchPostgres( $db ) {
30 $this->db = $db;
31 }
32
33 /**
34 * Perform a full text search query via tsearch2 and return a result set.
35 * Currently searches a page's current title (p.page_title) and text (t.old_text)
36 *
37 * @param string $term - Raw search term
38 * @return PostgresSearchResultSet
39 * @access public
40 */
41 function searchText( $term ) {
42 $resultSet = $this->db->resultObject( $this->db->query( $this->searchQuery( $term, 'textvector' ) ) );
43 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
44 }
45 function searchTitle( $term ) {
46 $resultSet = $this->db->resultObject( $this->db->query( $this->searchQuery( $term , 'titlevector' ) ) );
47 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
48 }
49
50
51 /*
52 * Transform the user's search string into a better form for tsearch2
53 */
54 function parseQuery( $filteredText, $fulltext ) {
55 global $wgContLang;
56 $lc = SearchEngine::legalSearchChars();
57 $searchon = '';
58 $searchconst = '';
59 $this->searchTerms = array();
60
61 # FIXME: This doesn't handle parenthetical expressions.
62 $m = array();
63 if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
64 $filteredText, $m, PREG_SET_ORDER ) ) {
65 foreach( $m as $terms ) {
66 switch ($terms[1]) {
67 case '~': // negate but do not exclude like "-" for now, simply negate
68 case '-': $searchconst.= '&!' . $wgContLang->stripForSearch( $terms[2] );
69 break;
70 case '+': $searchconst.= '&' . $wgContLang->stripForSearch( $terms[2] );
71 break;
72 case '<': // decrease priority of word - not implemented
73 case '>': // increase priority of word - not implemented
74 default : $searchon.= '|' . $wgContLang->stripForSearch( $terms[2] );
75 }
76 if( !empty( $terms[3] ) ) {
77 $regexp = preg_quote( $terms[3], '/' );
78 if( $terms[4] ) $regexp .= "[0-9A-Za-z_]+";
79 } else {
80 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
81 }
82 $this->searchTerms[] = $regexp;
83 }
84 wfDebug( "Would search with '$searchon'\n" );
85 wfDebug( 'Match with /\b' . implode( '\b|\b', $this->searchTerms ) . "\b/\n" );
86 } else {
87 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
88 }
89
90 if (substr_count($searchon,'|')==1) {
91 $searchon = str_replace ('|','&',$searchon);
92 }
93 $searchon = substr($searchconst . $searchon, 1) ;
94 $searchon = preg_replace('/(\s+)/','&',$searchon);
95 $searchon = $this->db->strencode( $searchon );
96 return $searchon;
97 }
98
99 /**
100 * Construct the full SQL query to do the search.
101 * @param string $filteredTerm
102 * @param string $fulltext
103 * @private
104 */
105 function searchQuery( $filteredTerm, $fulltext ) {
106
107 $match = $this->parseQuery( $filteredTerm, $fulltext );
108
109 $query = "SELECT page_id, page_namespace, page_title, old_text AS page_text, ".
110 "rank(titlevector, to_tsquery('default','$match')) AS rnk ".
111 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
112 "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery('default','$match')";
113
114 ## Redirects
115 if (! $this->showRedirects)
116 $query .= ' AND page_is_redirect = 0'; ## IS FALSE
117
118 ## Namespaces - defaults to 0
119 if ( count($this->namespaces) < 1)
120 $query .= ' AND page_namespace = 0';
121 else {
122 $namespaces = implode( ',', $this->namespaces );
123 $query .= " AND page_namespace IN ($namespaces)";
124 }
125
126 $query .= " ORDER BY rnk DESC, page_id DESC";
127
128 $query .= $this->db->limitResult( '', $this->limit, $this->offset );
129
130 return $query;
131 }
132
133 ## These two functions are done automatically via triggers
134
135 function update( $pageid, $title, $text ) {
136 $dbw = wfGetDB( DB_MASTER );
137 ## We don't want to index older revisions
138 $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id = ".
139 "(SELECT rev_text_id FROM revision WHERE rev_page = $pageid ".
140 "ORDER BY rev_text_id DESC LIMIT 1 OFFSET 1)";
141 $dbw->doQuery($SQL);
142 return true;
143 }
144 function updateTitle( $id, $title ) { return true; }
145
146 } ## end of the SearchPostgres class
147
148
149 class PostgresSearchResultSet extends SearchResultSet {
150 function PostgresSearchResultSet( $resultSet, $terms ) {
151 $this->mResultSet = $resultSet;
152 $this->mTerms = $terms;
153 }
154
155 function termMatches() {
156 return $this->mTerms;
157 }
158
159 function numRows() {
160 return $this->mResultSet->numRows();
161 }
162
163 function next() {
164 $row = $this->mResultSet->fetchObject();
165 if( $row === false ) {
166 return false;
167 } else {
168 return new SearchResult( $row );
169 }
170 }
171 }
172
173 ?>