Move Modern and CologneBlue out of core
[mediawiki.git] / includes / search / SearchPostgres.php
blob42b67f36799b738a10f970666b7383a68a2044ca
1 <?php
2 /**
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
23 * @file
24 * @ingroup Search
27 /**
28 * Search engine hook base class for Postgres
29 * @ingroup Search
31 class SearchPostgres extends SearchDatabase {
32 /**
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 PostgresSearchResultSet
40 function searchTitle( $term ) {
41 $q = $this->searchQuery( $term, 'titlevector', 'page_title' );
42 $olderror = error_reporting( E_ERROR );
43 $resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) );
44 error_reporting( $olderror );
45 if ( !$resultSet ) {
46 // Needed for "Query requires full scan, GIN doesn't support it"
47 return new SearchResultTooMany();
49 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
52 function searchText( $term ) {
53 $q = $this->searchQuery( $term, 'textvector', 'old_text' );
54 $olderror = error_reporting( E_ERROR );
55 $resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) );
56 error_reporting( $olderror );
57 if ( !$resultSet ) {
58 return new SearchResultTooMany();
60 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
63 /**
64 * Transform the user's search string into a better form for tsearch2
65 * Returns an SQL fragment consisting of quoted text to search for.
67 * @param string $term
69 * @return string
71 function parseQuery( $term ) {
73 wfDebug( "parseQuery received: $term \n" );
75 ## No backslashes allowed
76 $term = preg_replace( '/\\\/', '', $term );
78 ## Collapse parens into nearby words:
79 $term = preg_replace( '/\s*\(\s*/', ' (', $term );
80 $term = preg_replace( '/\s*\)\s*/', ') ', $term );
82 ## Treat colons as word separators:
83 $term = preg_replace( '/:/', ' ', $term );
85 $searchstring = '';
86 $m = array();
87 if ( preg_match_all( '/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
88 foreach ( $m as $terms ) {
89 if ( strlen( $terms[1] ) ) {
90 $searchstring .= ' & !';
92 if ( strtolower( $terms[2] ) === 'and' ) {
93 $searchstring .= ' & ';
95 elseif ( strtolower( $terms[2] ) === 'or' or $terms[2] === '|' ) {
96 $searchstring .= ' | ';
98 elseif ( strtolower( $terms[2] ) === 'not' ) {
99 $searchstring .= ' & !';
101 else {
102 $searchstring .= " & $terms[2]";
107 ## Strip out leading junk
108 $searchstring = preg_replace( '/^[\s\&\|]+/', '', $searchstring );
110 ## Remove any doubled-up operators
111 $searchstring = preg_replace( '/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring );
113 ## Remove any non-spaced operators (e.g. "Zounds!")
114 $searchstring = preg_replace( '/([^ ])[\!\&\|]/', "$1", $searchstring );
116 ## Remove any trailing whitespace or operators
117 $searchstring = preg_replace( '/[\s\!\&\|]+$/', '', $searchstring );
119 ## Remove unnecessary quotes around everything
120 $searchstring = preg_replace( '/^[\'"](.*)[\'"]$/', "$1", $searchstring );
122 ## Quote the whole thing
123 $searchstring = $this->db->addQuotes( $searchstring );
125 wfDebug( "parseQuery returned: $searchstring \n" );
127 return $searchstring;
132 * Construct the full SQL query to do the search.
133 * @param string $term
134 * @param string $fulltext
135 * @param string $colname
136 * @return string
138 function searchQuery( $term, $fulltext, $colname ) {
139 # Get the SQL fragment for the given term
140 $searchstring = $this->parseQuery( $term );
142 ## We need a separate query here so gin does not complain about empty searches
143 $sql = "SELECT to_tsquery($searchstring)";
144 $res = $this->db->query( $sql );
145 if ( !$res ) {
146 ## TODO: Better output (example to catch: one 'two)
147 die( "Sorry, that was not a valid search string. Please go back and try again" );
149 $top = $res->fetchRow();
150 $top = $top[0];
152 if ( $top === "" ) { ## e.g. if only stopwords are used XXX return something better
153 $query = "SELECT page_id, page_namespace, page_title, 0 AS score " .
154 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
155 "AND r.rev_text_id = c.old_id AND 1=0";
157 else {
158 $m = array();
159 if ( preg_match_all( "/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
160 foreach ( $m as $terms ) {
161 $this->searchTerms[$terms[1]] = $terms[1];
165 $query = "SELECT page_id, page_namespace, page_title, " .
166 "ts_rank($fulltext, to_tsquery($searchstring), 5) 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 $fulltext @@ to_tsquery($searchstring)";
171 ## Namespaces - defaults to 0
172 if ( !is_null( $this->namespaces ) ) { // null -> search all
173 if ( count( $this->namespaces ) < 1 ) {
174 $query .= ' AND page_namespace = 0';
175 } else {
176 $namespaces = $this->db->makeList( $this->namespaces );
177 $query .= " AND page_namespace IN ($namespaces)";
181 $query .= " ORDER BY score DESC, page_id DESC";
183 $query .= $this->db->limitResult( '', $this->limit, $this->offset );
185 wfDebug( "searchQuery returned: $query \n" );
187 return $query;
190 ## Most of the work of these two functions are done automatically via triggers
192 function update( $pageid, $title, $text ) {
193 ## We don't want to index older revisions
194 $sql = "UPDATE pagecontent SET textvector = NULL WHERE textvector IS NOT NULL and old_id IN " .
195 "(SELECT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) .
196 " ORDER BY rev_text_id DESC OFFSET 1)";
197 $this->db->query( $sql );
198 return true;
201 function updateTitle( $id, $title ) {
202 return true;
205 } ## end of the SearchPostgres class
208 * @ingroup Search
210 class PostgresSearchResult extends SearchResult {
211 function __construct( $row ) {
212 parent::__construct( $row );
213 $this->score = $row->score;
216 function getScore() {
217 return $this->score;
222 * @ingroup Search
224 class PostgresSearchResultSet extends SqlSearchResultSet {
225 function next() {
226 $row = $this->resultSet->fetchObject();
227 if ( $row === false ) {
228 return false;
229 } else {
230 return new PostgresSearchResult( $row );