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
26 * Search engine hook base class for Postgres
29 class SearchPostgres
extends SearchEngine
{
31 function __construct( $db ) {
36 * Perform a full text search query via tsearch2 and return a result set.
37 * Currently searches a page's current title (page.page_title) and
38 * latest revision article text (pagecontent.old_text)
40 * @param $term String: raw search term
41 * @return PostgresSearchResultSet
43 function searchTitle( $term ) {
44 $q = $this->searchQuery( $term , 'titlevector', 'page_title' );
45 $olderror = error_reporting(E_ERROR
);
46 $resultSet = $this->db
->resultObject( $this->db
->query( $q, 'SearchPostgres', true ) );
47 error_reporting($olderror);
49 // Needed for "Query requires full scan, GIN doesn't support it"
50 return new SearchResultTooMany();
52 return new PostgresSearchResultSet( $resultSet, $this->searchTerms
);
54 function searchText( $term ) {
55 $q = $this->searchQuery( $term, 'textvector', 'old_text' );
56 $olderror = error_reporting(E_ERROR
);
57 $resultSet = $this->db
->resultObject( $this->db
->query( $q, 'SearchPostgres', true ) );
58 error_reporting($olderror);
60 return new SearchResultTooMany();
62 return new PostgresSearchResultSet( $resultSet, $this->searchTerms
);
67 * Transform the user's search string into a better form for tsearch2
68 * Returns an SQL fragment consisting of quoted text to search for.
70 function parseQuery( $term ) {
72 wfDebug( "parseQuery received: $term \n" );
74 ## No backslashes allowed
75 $term = preg_replace('/\\\/', '', $term);
77 ## Collapse parens into nearby words:
78 $term = preg_replace('/\s*\(\s*/', ' (', $term);
79 $term = preg_replace('/\s*\)\s*/', ') ', $term);
81 ## Treat colons as word separators:
82 $term = preg_replace('/:/', ' ', $term);
86 if( preg_match_all('/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER
) ) {
87 foreach( $m as $terms ) {
88 if (strlen($terms[1])) {
89 $searchstring .= ' & !';
91 if (strtolower($terms[2]) === 'and') {
92 $searchstring .= ' & ';
94 else if (strtolower($terms[2]) === 'or' or $terms[2] === '|') {
95 $searchstring .= ' | ';
97 else if (strtolower($terms[2]) === 'not') {
98 $searchstring .= ' & !';
101 $searchstring .= " & $terms[2]";
106 ## Strip out leading junk
107 $searchstring = preg_replace('/^[\s\&\|]+/', '', $searchstring);
109 ## Remove any doubled-up operators
110 $searchstring = preg_replace('/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring);
112 ## Remove any non-spaced operators (e.g. "Zounds!")
113 $searchstring = preg_replace('/([^ ])[\!\&\|]/', "$1", $searchstring);
115 ## Remove any trailing whitespace or operators
116 $searchstring = preg_replace('/[\s\!\&\|]+$/', '', $searchstring);
118 ## Remove unnecessary quotes around everything
119 $searchstring = preg_replace('/^[\'"](.*)[\'"]$/', "$1", $searchstring);
121 ## Quote the whole thing
122 $searchstring = $this->db
->addQuotes($searchstring);
124 wfDebug( "parseQuery returned: $searchstring \n" );
126 return $searchstring;
131 * Construct the full SQL query to do the search.
132 * @param $filteredTerm String
133 * @param $fulltext String
135 function searchQuery( $term, $fulltext, $colname ) {
138 if ( !isset( $wgDBversion ) ) {
139 $this->db
->getServerVersion();
140 $wgDBversion = $this->db
->numeric_version
;
142 $prefix = $wgDBversion < 8.3 ?
"'default'," : '';
144 # Get the SQL fragment for the given term
145 $searchstring = $this->parseQuery( $term );
147 ## We need a separate query here so gin does not complain about empty searches
148 $SQL = "SELECT to_tsquery($prefix $searchstring)";
149 $res = $this->db
->doQuery($SQL);
151 ## TODO: Better output (example to catch: one 'two)
152 die ("Sorry, that was not a valid search string. Please go back and try again");
154 $top = pg_fetch_result($res,0,0);
156 if ($top === "") { ## e.g. if only stopwords are used XXX return something better
157 $query = "SELECT page_id, page_namespace, page_title, 0 AS score ".
158 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
159 "AND r.rev_text_id = c.old_id AND 1=0";
163 if( preg_match_all("/'([^']+)'/", $top, $m, PREG_SET_ORDER
) ) {
164 foreach( $m as $terms ) {
165 $this->searchTerms
[$terms[1]] = $terms[1];
169 $rankscore = $wgDBversion > 8.2 ?
5 : 1;
170 $rank = $wgDBversion < 8.3 ?
'rank' : 'ts_rank';
171 $query = "SELECT page_id, page_namespace, page_title, ".
172 "$rank($fulltext, to_tsquery($prefix $searchstring), $rankscore) AS score ".
173 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
174 "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery($prefix $searchstring)";
178 if (! $this->showRedirects
)
179 $query .= ' AND page_is_redirect = 0';
181 ## Namespaces - defaults to 0
182 if( !is_null($this->namespaces
) ){ // null -> search all
183 if ( count($this->namespaces
) < 1)
184 $query .= ' AND page_namespace = 0';
186 $namespaces = $this->db
->makeList( $this->namespaces
);
187 $query .= " AND page_namespace IN ($namespaces)";
191 $query .= " ORDER BY score DESC, page_id DESC";
193 $query .= $this->db
->limitResult( '', $this->limit
, $this->offset
);
195 wfDebug( "searchQuery returned: $query \n" );
200 ## Most of the work of these two functions are done automatically via triggers
202 function update( $pageid, $title, $text ) {
203 ## We don't want to index older revisions
204 $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id IN ".
205 "(SELECT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) .
206 " ORDER BY rev_text_id DESC OFFSET 1)";
207 $this->db
->doQuery($SQL);
211 function updateTitle( $id, $title ) {
215 } ## end of the SearchPostgres class
220 class PostgresSearchResult
extends SearchResult
{
221 function __construct( $row ) {
222 parent
::__construct($row);
223 $this->score
= $row->score
;
225 function getScore() {
233 class PostgresSearchResultSet
extends SearchResultSet
{
234 function __construct( $resultSet, $terms ) {
235 $this->mResultSet
= $resultSet;
236 $this->mTerms
= $terms;
239 function termMatches() {
240 return $this->mTerms
;
244 return $this->mResultSet
->numRows();
248 $row = $this->mResultSet
->fetchObject();
249 if( $row === false ) {
252 return new PostgresSearchResult( $row );