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 ## We need a separate query here so gin does not complain about empty searches
110 $dbw = wfGetDB( DB_MASTER
);
111 $SQL = "SELECT to_tsquery('default','$match')";
112 $res = $dbw->doQuery($SQL);
114 ## TODO: Catch better than this e.g. "one 'two"
115 die ("Sorry, that was not a valid search string. Please go back and try again");
117 $top = $dbw->addQuotes(pg_fetch_result($res,0,0));
119 $query = "SELECT page_id, page_namespace, page_title, old_text AS page_text, ".
120 "rank(titlevector, $top) AS rnk ".
121 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
122 "AND r.rev_text_id = c.old_id AND $fulltext @@ $top";
124 if ($top === "''") { ## e.g. if only stopwords are used
125 $query = "SELECT page_id, page_namespace, page_title, old_text AS page_text, 0 AS rnk ".
126 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
127 "AND r.rev_text_id = c.old_id AND 1=0";
131 if (! $this->showRedirects
)
132 $query .= ' AND page_is_redirect = 0'; ## IS FALSE
134 ## Namespaces - defaults to 0
135 if ( count($this->namespaces
) < 1)
136 $query .= ' AND page_namespace = 0';
138 $namespaces = implode( ',', $this->namespaces
);
139 $query .= " AND page_namespace IN ($namespaces)";
142 $query .= " ORDER BY rnk DESC, page_id DESC";
144 $query .= $this->db
->limitResult( '', $this->limit
, $this->offset
);
149 ## Most of the work of these two functions are done automatically via triggers
151 function update( $pageid, $title, $text ) {
152 $dbw = wfGetDB( DB_MASTER
);
153 ## We don't want to index older revisions
154 $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id = ".
155 "(SELECT rev_text_id FROM revision WHERE rev_page = $pageid ".
156 "ORDER BY rev_text_id DESC LIMIT 1 OFFSET 1)";
160 function updateTitle( $id, $title ) { return true; }
162 } ## end of the SearchPostgres class
165 class PostgresSearchResultSet
extends SearchResultSet
{
166 function PostgresSearchResultSet( $resultSet, $terms ) {
167 $this->mResultSet
= $resultSet;
168 $this->mTerms
= $terms;
171 function termMatches() {
172 return $this->mTerms
;
176 return $this->mResultSet
->numRows();
180 $row = $this->mResultSet
->fetchObject();
181 if( $row === false ) {
184 return new SearchResult( $row );