Overhaul.
[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 /**
21 * Search engine hook base class for Postgres
22 * @addtogroup Search
23 */
24
25 class SearchPostgres extends SearchEngine {
26
27 function SearchPostgres( $db ) {
28 $this->db = $db;
29 }
30
31 /**
32 * Perform a full text search query via tsearch2 and return a result set.
33 * Currently searches a page's current title (page.page_title) and
34 * latest revision article text (pagecontent.old_text)
35 *
36 * @param string $term - Raw search term
37 * @return PostgresSearchResultSet
38 * @access public
39 */
40 function searchTitle( $term ) {
41 $resultSet = $this->db->resultObject( $this->db->query( $this->searchQuery( $term , 'titlevector', 'page_title' )));
42 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
43 }
44 function searchText( $term ) {
45 $resultSet = $this->db->resultObject( $this->db->query( $this->searchQuery( $term, 'textvector', 'old_text' )));
46 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
47 }
48
49
50 /*
51 * Transform the user's search string into a better form for tsearch2
52 */
53 function parseQuery( $term ) {
54
55 wfDebug( "parseQuery received: $term" );
56
57 ## No backslashes allowed
58 $term = preg_replace('/\\\/', '', $term);
59
60 ## Collapse parens into nearby words:
61 $term = preg_replace('/\s*\(\s*/', ' (', $term);
62 $term = preg_replace('/\s*\)\s*/', ') ', $term);
63
64 $this->searchTerms = array();
65 $m = array();
66 $searchstring = '';
67 if( preg_match_all('/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
68 foreach( $m as $terms ) {
69 if (strlen($terms[1])) {
70 $searchstring .= ' & !';
71 }
72 if (strtolower($terms[2]) === 'and') {
73 $searchstring .= ' & ';
74 }
75 else if (strtolower($terms[2]) === 'or' or $terms[2] === '|') {
76 $searchstring .= ' | ';
77 }
78 else if (strtolower($terms[2]) === 'not') {
79 $searchstring .= ' & !';
80 }
81 else {
82 $searchstring .= " & $terms[2]";
83 array_push( $this->searchTerms, $terms[2] );
84 }
85 }
86 }
87
88 ## Strip out leading junk
89 $searchstring = preg_replace('/^[\s\&\|]+/', '', $searchstring);
90
91 ## Remove any doubled-up operators
92 $searchstring = preg_replace('/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring);
93
94 ## Quote the whole thing
95 $searchstring = $this->db->addQuotes($searchstring);
96
97 wfDebug( "parseQuery returned: $searchstring" );
98
99 return $searchstring;
100
101 }
102
103 /**
104 * Construct the full SQL query to do the search.
105 * @param string $filteredTerm
106 * @param string $fulltext
107 * @private
108 */
109 function searchQuery( $term, $fulltext, $colname ) {
110
111 $searchstring = $this->parseQuery( $term );
112
113 ## We need a separate query here so gin does not complain about empty searches
114 $SQL = "SELECT to_tsquery('default',$searchstring)";
115 $res = $this->db->doQuery($SQL);
116 if (!$res) {
117 ## TODO: Better output (example to catch: one 'two)
118 die ("Sorry, that was not a valid search string. Please go back and try again");
119 }
120 $top = pg_fetch_result($res,0,0);
121
122 if ($top === "") { ## e.g. if only stopwords are used XXX return something better
123 $query = "SELECT page_id, page_namespace, page_title, 0 AS score ".
124 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
125 "AND r.rev_text_id = c.old_id AND 1=0";
126 }
127 else {
128 $query = "SELECT page_id, page_namespace, page_title, ".
129 "rank($fulltext, to_tsquery('default',$searchstring),5) AS score ".
130 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
131 "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery('default',$searchstring)";
132 }
133
134 ## Redirects
135 if (! $this->showRedirects)
136 $query .= ' AND page_is_redirect = 0'; ## IS FALSE
137
138 ## Namespaces - defaults to 0
139 if ( count($this->namespaces) < 1)
140 $query .= ' AND page_namespace = 0';
141 else {
142 $namespaces = implode( ',', $this->namespaces );
143 $query .= " AND page_namespace IN ($namespaces)";
144 }
145
146 $query .= " ORDER BY score DESC, page_id DESC";
147
148 $query .= $this->db->limitResult( '', $this->limit, $this->offset );
149
150 wfDebug( "searchQuery returned: $query" );
151
152 return $query;
153 }
154
155 ## Most of the work of these two functions are done automatically via triggers
156
157 function update( $pageid, $title, $text ) {
158 ## We don't want to index older revisions
159 $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id = ".
160 "(SELECT rev_text_id FROM revision WHERE rev_page = $pageid ".
161 "ORDER BY rev_text_id DESC LIMIT 1 OFFSET 1)";
162 $this->db->doQuery($SQL);
163 return true;
164 }
165
166 function updateTitle( $id, $title ) {
167 return true;
168 }
169
170 } ## end of the SearchPostgres class
171
172
173 class PostgresSearchResult extends SearchResult {
174 function PostgresSearchResult( $row ) {
175 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
176 $this->score = $row->score;
177 }
178 function getScore() {
179 return $this->score;
180 }
181 }
182
183 class PostgresSearchResultSet extends SearchResultSet {
184 function PostgresSearchResultSet( $resultSet, $terms ) {
185 $this->mResultSet = $resultSet;
186 $this->mTerms = $terms;
187 }
188
189 function termMatches() {
190 return $this->mTerms;
191 }
192
193 function numRows() {
194 return $this->mResultSet->numRows();
195 }
196
197 function next() {
198 $row = $this->mResultSet->fetchObject();
199 if( $row === false ) {
200 return false;
201 } else {
202 return new PostgresSearchResult( $row );
203 }
204 }
205 }
206
207
208 ?>