* Some enhancements to live preview
[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 * @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 $this->searchTerms = array();
59
60 # FIXME: This doesn't handle parenthetical expressions.
61 $m = array();
62 if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
63 $filteredText, $m, PREG_SET_ORDER ) ) {
64 foreach( $m as $terms ) {
65 if( $searchon !== '' ) $searchon .= ' ';
66 if($terms[1] == '') {
67 $terms[1] = '+';
68 }
69 $searchon .= $terms[1] . $wgContLang->stripForSearch( $terms[2] );
70 if( !empty( $terms[3] ) ) {
71 $regexp = preg_quote( $terms[3], '/' );
72 if( $terms[4] ) $regexp .= "[0-9A-Za-z_]+";
73 } else {
74 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
75 }
76 $this->searchTerms[] = $regexp;
77 }
78 wfDebug( "Would search with '$searchon'\n" );
79 wfDebug( 'Match with /\b' . implode( '\b|\b', $this->searchTerms ) . "\b/\n" );
80 } else {
81 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
82 }
83
84 $searchon = preg_replace('/!/','\\!',$searchon);
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 "rank(titlevector, to_tsquery('default','$match')) AS rnk ".
102 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
103 "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery('default','$match')";
104
105 ## Redirects
106 if (! $this->showRedirects)
107 $query .= ' AND page_is_redirect = 0'; ## IS FALSE
108
109 ## Namespaces - defaults to 0
110 if ( count($this->namespaces) < 1)
111 $query .= ' AND page_namespace = 0';
112 else {
113 $namespaces = implode( ',', $this->namespaces );
114 $query .= " AND page_namespace IN ($namespaces)";
115 }
116
117 $query .= " ORDER BY rnk DESC, page_id DESC";
118
119 $query .= $this->db->limitResult( '', $this->limit, $this->offset );
120
121 return $query;
122 }
123
124 ## These two functions are done automatically via triggers
125
126 function update( $pageid, $title, $text ) {
127 $dbw = wfGetDB( DB_MASTER );
128 ## We don't want to index older revisions
129 $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id = ".
130 "(SELECT rev_text_id FROM revision WHERE rev_page = $pageid ".
131 "ORDER BY rev_text_id DESC LIMIT 1 OFFSET 1)";
132 $dbw->doQuery($SQL);
133 return true;
134 }
135 function updateTitle( $id, $title ) { return true; }
136
137 } ## end of the SearchPostgres class
138
139
140 class PostgresSearchResultSet extends SearchResultSet {
141 function PostgresSearchResultSet( $resultSet, $terms ) {
142 $this->mResultSet = $resultSet;
143 $this->mTerms = $terms;
144 }
145
146 function termMatches() {
147 return $this->mTerms;
148 }
149
150 function numRows() {
151 return $this->mResultSet->numRows();
152 }
153
154 function next() {
155 $row = $this->mResultSet->fetchObject();
156 if( $row === false ) {
157 return false;
158 } else {
159 return new SearchResult( $row );
160 }
161 }
162 }
163
164 ?>