3 * PostgreSQL search engine
5 * Copyright © 2006-2007 Greg Sabino Mullane <greg@turnstep.com>
6 * http://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
28 * Search engine hook base class for Postgres
31 class SearchPostgres
extends SearchEngine
{
34 * @var DatabasePostgres
38 * Creates an instance of this class
39 * @param $db DatabaseSqlite: database object
41 function __construct( $db ) {
42 parent
::__construct( $db );
46 * Perform a full text search query via tsearch2 and return a result set.
47 * Currently searches a page's current title (page.page_title) and
48 * latest revision article text (pagecontent.old_text)
50 * @param $term String: raw search term
51 * @return PostgresSearchResultSet
53 function searchTitle( $term ) {
54 $q = $this->searchQuery( $term , 'titlevector', 'page_title' );
55 $olderror = error_reporting(E_ERROR
);
56 $resultSet = $this->db
->resultObject( $this->db
->query( $q, 'SearchPostgres', true ) );
57 error_reporting($olderror);
59 // Needed for "Query requires full scan, GIN doesn't support it"
60 return new SearchResultTooMany();
62 return new PostgresSearchResultSet( $resultSet, $this->searchTerms
);
65 function searchText( $term ) {
66 $q = $this->searchQuery( $term, 'textvector', 'old_text' );
67 $olderror = error_reporting(E_ERROR
);
68 $resultSet = $this->db
->resultObject( $this->db
->query( $q, 'SearchPostgres', true ) );
69 error_reporting($olderror);
71 return new SearchResultTooMany();
73 return new PostgresSearchResultSet( $resultSet, $this->searchTerms
);
77 * Transform the user's search string into a better form for tsearch2
78 * Returns an SQL fragment consisting of quoted text to search for.
84 function parseQuery( $term ) {
86 wfDebug( "parseQuery received: $term \n" );
88 ## No backslashes allowed
89 $term = preg_replace('/\\\/', '', $term);
91 ## Collapse parens into nearby words:
92 $term = preg_replace('/\s*\(\s*/', ' (', $term);
93 $term = preg_replace('/\s*\)\s*/', ') ', $term);
95 ## Treat colons as word separators:
96 $term = preg_replace('/:/', ' ', $term);
100 if( preg_match_all('/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER
) ) {
101 foreach( $m as $terms ) {
102 if (strlen($terms[1])) {
103 $searchstring .= ' & !';
105 if (strtolower($terms[2]) === 'and') {
106 $searchstring .= ' & ';
108 elseif (strtolower($terms[2]) === 'or' or $terms[2] === '|') {
109 $searchstring .= ' | ';
111 elseif (strtolower($terms[2]) === 'not') {
112 $searchstring .= ' & !';
115 $searchstring .= " & $terms[2]";
120 ## Strip out leading junk
121 $searchstring = preg_replace('/^[\s\&\|]+/', '', $searchstring);
123 ## Remove any doubled-up operators
124 $searchstring = preg_replace('/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring);
126 ## Remove any non-spaced operators (e.g. "Zounds!")
127 $searchstring = preg_replace('/([^ ])[\!\&\|]/', "$1", $searchstring);
129 ## Remove any trailing whitespace or operators
130 $searchstring = preg_replace('/[\s\!\&\|]+$/', '', $searchstring);
132 ## Remove unnecessary quotes around everything
133 $searchstring = preg_replace('/^[\'"](.*)[\'"]$/', "$1", $searchstring);
135 ## Quote the whole thing
136 $searchstring = $this->db
->addQuotes($searchstring);
138 wfDebug( "parseQuery returned: $searchstring \n" );
140 return $searchstring;
145 * Construct the full SQL query to do the search.
146 * @param $term String
147 * @param $fulltext String
151 function searchQuery( $term, $fulltext, $colname ) {
152 # Get the SQL fragment for the given term
153 $searchstring = $this->parseQuery( $term );
155 ## We need a separate query here so gin does not complain about empty searches
156 $SQL = "SELECT to_tsquery($searchstring)";
157 $res = $this->db
->query($SQL);
159 ## TODO: Better output (example to catch: one 'two)
160 die ("Sorry, that was not a valid search string. Please go back and try again");
162 $top = $res->fetchRow();
165 if ($top === "") { ## e.g. if only stopwords are used XXX return something better
166 $query = "SELECT page_id, page_namespace, page_title, 0 AS score ".
167 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
168 "AND r.rev_text_id = c.old_id AND 1=0";
172 if( preg_match_all("/'([^']+)'/", $top, $m, PREG_SET_ORDER
) ) {
173 foreach( $m as $terms ) {
174 $this->searchTerms
[$terms[1]] = $terms[1];
178 $query = "SELECT page_id, page_namespace, page_title, ".
179 "ts_rank($fulltext, to_tsquery($searchstring), 5) AS score ".
180 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
181 "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery($searchstring)";
185 if (! $this->showRedirects
)
186 $query .= ' AND page_is_redirect = 0';
188 ## Namespaces - defaults to 0
189 if( !is_null($this->namespaces
) ){ // null -> search all
190 if ( count($this->namespaces
) < 1)
191 $query .= ' AND page_namespace = 0';
193 $namespaces = $this->db
->makeList( $this->namespaces
);
194 $query .= " AND page_namespace IN ($namespaces)";
198 $query .= " ORDER BY score DESC, page_id DESC";
200 $query .= $this->db
->limitResult( '', $this->limit
, $this->offset
);
202 wfDebug( "searchQuery returned: $query \n" );
207 ## Most of the work of these two functions are done automatically via triggers
209 function update( $pageid, $title, $text ) {
210 ## We don't want to index older revisions
211 $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id IN ".
212 "(SELECT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) .
213 " ORDER BY rev_text_id DESC OFFSET 1)";
214 $this->db
->query($SQL);
218 function updateTitle( $id, $title ) {
222 } ## end of the SearchPostgres class
227 class PostgresSearchResult
extends SearchResult
{
228 function __construct( $row ) {
229 parent
::__construct($row);
230 $this->score
= $row->score
;
232 function getScore() {
240 class PostgresSearchResultSet
extends SqlSearchResultSet
{
241 function __construct( $resultSet, $terms ) {
242 parent
::__construct( $resultSet, $terms );
246 $row = $this->mResultSet
->fetchObject();
247 if( $row === false ) {
250 return new PostgresSearchResult( $row );