c202f911e60b1dd20db12eba0effcc93fcd1601f
[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 $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] = '+';
70 }
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] ), '/' );
77 }
78 $this->searchTerms[] = $regexp;
79 }
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" );
84 }
85
86 $searchon = preg_replace('/(\s+)/','&',$searchon);
87 $searchon = $this->db->strencode( $searchon );
88 return $searchon;
89 }
90
91 /**
92 * Construct the full SQL query to do the search.
93 * @param string $filteredTerm
94 * @param string $fulltext
95 * @private
96 */
97 function searchQuery( $filteredTerm, $fulltext ) {
98
99 $match = $this->parseQuery( $filteredTerm, $fulltext );
100
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')";
105
106 ## Redirects
107 if (! $this->showRedirects)
108 $query .= ' AND page_is_redirect = 0'; ## IS FALSE
109
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)";
116 }
117
118 $query .= " ORDER BY rnk DESC, page_id DESC";
119
120 $query .= $this->db->limitResult( '', $this->limit, $this->offset );
121
122 return $query;
123 }
124
125 ## These two functions are done automatically via triggers
126
127 function update( $pageid, $title, $text ) {
128 $dbw =& wfGetDB( DB_MASTER );
129 ## We don't want to index older revisions
130 $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id = ".
131 "(SELECT rev_text_id FROM revision WHERE rev_page = $pageid ".
132 "ORDER BY rev_text_id DESC LIMIT 1 OFFSET 1)";
133 $dbw->doQuery($SQL);
134 return true;
135 }
136 function updateTitle( $id, $title ) { return true; }
137
138 } ## end of the SearchPostgres class
139
140
141 /** @package MediaWiki */
142 class PostgresSearchResultSet extends SearchResultSet {
143 function PostgresSearchResultSet( $resultSet, $terms ) {
144 $this->mResultSet = $resultSet;
145 $this->mTerms = $terms;
146 }
147
148 function termMatches() {
149 return $this->mTerms;
150 }
151
152 function numRows() {
153 return $this->mResultSet->numRows();
154 }
155
156 function next() {
157 $row = $this->mResultSet->fetchObject();
158 if( $row === false ) {
159 return false;
160 } else {
161 return new SearchResult( $row );
162 }
163 }
164 }
165
166 ?>