abb4e99e0e98604f9b38c82a3817b52865ad891f
2 # Copyright (C) 2006-2007 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();
59 $this->searchTerms
= array();
61 # FIXME: This doesn't handle parenthetical expressions.
63 if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
64 $filteredText, $m, PREG_SET_ORDER
) ) {
65 foreach( $m as $terms ) {
67 case '~': // negate but do not exclude like "-" for now, simply negate
68 case '-': $searchconst.= '&!' . $wgContLang->stripForSearch( $terms[2] );
70 case '+': $searchconst.= '&' . $wgContLang->stripForSearch( $terms[2] );
72 case '<': // decrease priority of word - not implemented
73 case '>': // increase priority of word - not implemented
74 default : $searchon.= '|' . $wgContLang->stripForSearch( $terms[2] );
76 if( !empty( $terms[3] ) ) {
77 $regexp = preg_quote( $terms[3], '/' );
78 if( $terms[4] ) $regexp .= "[0-9A-Za-z_]+";
80 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
82 $this->searchTerms
[] = $regexp;
84 wfDebug( "Would search with '$searchon'\n" );
85 wfDebug( 'Match with /\b' . implode( '\b|\b', $this->searchTerms
) . "\b/\n" );
87 wfDebug( "Can't understand search query '{$this->filteredText}'\n" );
90 if (substr_count($searchon,'|')==1) {
91 $searchon = str_replace ('|','&',$searchon);
93 $searchon = substr($searchconst . $searchon, 1) ;
94 $searchon = preg_replace('/(\s+)/','&',$searchon);
95 $searchon = $this->db
->strencode( $searchon );
100 * Construct the full SQL query to do the search.
101 * @param string $filteredTerm
102 * @param string $fulltext
105 function searchQuery( $filteredTerm, $fulltext ) {
107 $match = $this->parseQuery( $filteredTerm, $fulltext );
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')";
115 if (! $this->showRedirects
)
116 $query .= ' AND page_is_redirect = 0'; ## IS FALSE
118 ## Namespaces - defaults to 0
119 if ( count($this->namespaces
) < 1)
120 $query .= ' AND page_namespace = 0';
122 $namespaces = implode( ',', $this->namespaces
);
123 $query .= " AND page_namespace IN ($namespaces)";
126 $query .= " ORDER BY rnk DESC, page_id DESC";
128 $query .= $this->db
->limitResult( '', $this->limit
, $this->offset
);
133 ## These two functions are done automatically via triggers
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)";
144 function updateTitle( $id, $title ) { return true; }
146 } ## end of the SearchPostgres class
149 class PostgresSearchResultSet
extends SearchResultSet
{
150 function PostgresSearchResultSet( $resultSet, $terms ) {
151 $this->mResultSet
= $resultSet;
152 $this->mTerms
= $terms;
155 function termMatches() {
156 return $this->mTerms
;
160 return $this->mResultSet
->numRows();
164 $row = $this->mResultSet
->fetchObject();
165 if( $row === false ) {
168 return new SearchResult( $row );