3 * PostgreSQL search engine
5 * Copyright © 2006-2007 Greg Sabino Mullane <greg@turnstep.com>
6 * https://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 SearchDatabase
{
33 * Perform a full text search query via tsearch2 and return a result set.
34 * Currently searches a page's current title (page.page_title) and
35 * latest revision article text (pagecontent.old_text)
37 * @param string $term Raw search term
38 * @return SqlSearchResultSet
40 function searchTitle( $term ) {
41 $q = $this->searchQuery( $term, 'titlevector', 'page_title' );
42 $olderror = error_reporting( E_ERROR
);
43 $resultSet = $this->db
->query( $q, 'SearchPostgres', true );
44 error_reporting( $olderror );
45 return new SqlSearchResultSet( $resultSet, $this->searchTerms
);
48 function searchText( $term ) {
49 $q = $this->searchQuery( $term, 'textvector', 'old_text' );
50 $olderror = error_reporting( E_ERROR
);
51 $resultSet = $this->db
->query( $q, 'SearchPostgres', true );
52 error_reporting( $olderror );
53 return new SqlSearchResultSet( $resultSet, $this->searchTerms
);
57 * Transform the user's search string into a better form for tsearch2
58 * Returns an SQL fragment consisting of quoted text to search for.
64 function parseQuery( $term ) {
66 wfDebug( "parseQuery received: $term \n" );
68 # # No backslashes allowed
69 $term = preg_replace( '/\\\/', '', $term );
71 # # Collapse parens into nearby words:
72 $term = preg_replace( '/\s*\(\s*/', ' (', $term );
73 $term = preg_replace( '/\s*\)\s*/', ') ', $term );
75 # # Treat colons as word separators:
76 $term = preg_replace( '/:/', ' ', $term );
80 if ( preg_match_all( '/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER
) ) {
81 foreach ( $m as $terms ) {
82 if ( strlen( $terms[1] ) ) {
83 $searchstring .= ' & !';
85 if ( strtolower( $terms[2] ) === 'and' ) {
86 $searchstring .= ' & ';
87 } elseif ( strtolower( $terms[2] ) === 'or' ||
$terms[2] === '|' ) {
88 $searchstring .= ' | ';
89 } elseif ( strtolower( $terms[2] ) === 'not' ) {
90 $searchstring .= ' & !';
92 $searchstring .= " & $terms[2]";
97 # # Strip out leading junk
98 $searchstring = preg_replace( '/^[\s\&\|]+/', '', $searchstring );
100 # # Remove any doubled-up operators
101 $searchstring = preg_replace( '/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring );
103 # # Remove any non-spaced operators (e.g. "Zounds!")
104 $searchstring = preg_replace( '/([^ ])[\!\&\|]/', "$1", $searchstring );
106 # # Remove any trailing whitespace or operators
107 $searchstring = preg_replace( '/[\s\!\&\|]+$/', '', $searchstring );
109 # # Remove unnecessary quotes around everything
110 $searchstring = preg_replace( '/^[\'"](.*)[\'"]$/', "$1", $searchstring );
112 # # Quote the whole thing
113 $searchstring = $this->db
->addQuotes( $searchstring );
115 wfDebug( "parseQuery returned: $searchstring \n" );
117 return $searchstring;
122 * Construct the full SQL query to do the search.
123 * @param string $term
124 * @param string $fulltext
125 * @param string $colname
128 function searchQuery( $term, $fulltext, $colname ) {
129 # Get the SQL fragment for the given term
130 $searchstring = $this->parseQuery( $term );
132 # # We need a separate query here so gin does not complain about empty searches
133 $sql = "SELECT to_tsquery($searchstring)";
134 $res = $this->db
->query( $sql );
136 # # TODO: Better output (example to catch: one 'two)
137 die( "Sorry, that was not a valid search string. Please go back and try again" );
139 $top = $res->fetchRow()[0];
141 $this->searchTerms
= [];
142 if ( $top === "" ) { # # e.g. if only stopwords are used XXX return something better
143 $query = "SELECT page_id, page_namespace, page_title, 0 AS score " .
144 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
145 "AND r.rev_text_id = c.old_id AND 1=0";
148 if ( preg_match_all( "/'([^']+)'/", $top, $m, PREG_SET_ORDER
) ) {
149 foreach ( $m as $terms ) {
150 $this->searchTerms
[$terms[1]] = $terms[1];
154 $query = "SELECT page_id, page_namespace, page_title, " .
155 "ts_rank($fulltext, to_tsquery($searchstring), 5) AS score " .
156 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
157 "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery($searchstring)";
160 # # Namespaces - defaults to 0
161 if ( !is_null( $this->namespaces
) ) { // null -> search all
162 if ( count( $this->namespaces
) < 1 ) {
163 $query .= ' AND page_namespace = 0';
165 $namespaces = $this->db
->makeList( $this->namespaces
);
166 $query .= " AND page_namespace IN ($namespaces)";
170 $query .= " ORDER BY score DESC, page_id DESC";
172 $query .= $this->db
->limitResult( '', $this->limit
, $this->offset
);
174 wfDebug( "searchQuery returned: $query \n" );
179 # # Most of the work of these two functions are done automatically via triggers
181 function update( $pageid, $title, $text ) {
182 # # We don't want to index older revisions
183 $sql = "UPDATE pagecontent SET textvector = NULL WHERE textvector IS NOT NULL and old_id IN " .
184 "(SELECT DISTINCT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) .
185 " ORDER BY rev_text_id DESC OFFSET 1)";
186 $this->db
->query( $sql );
190 function updateTitle( $id, $title ) {