2 # Copyright (C) 2006 Greg Sabino Mullane <greg@turnstep.com>
3 # http://www.mediawiki.org/
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.
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.
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
20 ## XXX Better catching of SELECT to_tsquery('the')
23 * Search engine hook base class for Postgres
27 class SearchPostgres
extends SearchEngine
{
29 function SearchPostgres( $db ) {
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)
37 * @param string $term - Raw search term
38 * @return PostgresSearchResultSet
41 function searchText( $term ) {
42 $resultSet = $this->db
->resultObject( $this->db
->query( $this->searchQuery( $term, 'textvector' ) ) );
43 return new PostgresSearchResultSet( $resultSet, $this->searchTerms
);
45 function searchTitle( $term ) {
46 $resultSet = $this->db
->resultObject( $this->db
->query( $this->searchQuery( $term , 'titlevector' ) ) );
47 return new PostgresSearchResultSet( $resultSet, $this->searchTerms
);
52 * Transform the user's search string into a better form for tsearch2
54 function parseQuery( $filteredText, $fulltext ) {
56 $lc = SearchEngine
::legalSearchChars();
58 $this->searchTerms
= array();
60 # FIXME: This doesn't handle parenthetical expressions.
62 if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
63 $filteredText, $m, PREG_SET_ORDER
) ) {
64 foreach( $m as $terms ) {
65 if( $searchon !== '' ) $searchon .= ' ';
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_]+";
74 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
76 $this->searchTerms
[] = $regexp;
78 wfDebug( "Would search with '$searchon'\n" );
79 wfDebug( 'Match with /\b' . implode( '\b|\b', $this->searchTerms
) . "\b/\n" );
81 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
84 $searchon = preg_replace('/!/','\\!',$searchon);
85 $searchon = preg_replace('/(\s+)/','&',$searchon);
86 $searchon = $this->db
->strencode( $searchon );
91 * Construct the full SQL query to do the search.
92 * @param string $filteredTerm
93 * @param string $fulltext
96 function searchQuery( $filteredTerm, $fulltext ) {
98 $match = $this->parseQuery( $filteredTerm, $fulltext );
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')";
106 if (! $this->showRedirects
)
107 $query .= ' AND page_is_redirect = 0'; ## IS FALSE
109 ## Namespaces - defaults to 0
110 if ( count($this->namespaces
) < 1)
111 $query .= ' AND page_namespace = 0';
113 $namespaces = implode( ',', $this->namespaces
);
114 $query .= " AND page_namespace IN ($namespaces)";
117 $query .= " ORDER BY rnk DESC, page_id DESC";
119 $query .= $this->db
->limitResult( '', $this->limit
, $this->offset
);
124 ## These two functions are done automatically via triggers
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)";
135 function updateTitle( $id, $title ) { return true; }
137 } ## end of the SearchPostgres class
140 class PostgresSearchResultSet
extends SearchResultSet
{
141 function PostgresSearchResultSet( $resultSet, $terms ) {
142 $this->mResultSet
= $resultSet;
143 $this->mTerms
= $terms;
146 function termMatches() {
147 return $this->mTerms
;
151 return $this->mResultSet
->numRows();
155 $row = $this->mResultSet
->fetchObject();
156 if( $row === false ) {
159 return new SearchResult( $row );