]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/search/SearchPostgres.php
MediaWiki 1.16.1-scripts
[autoinstalls/mediawiki.git] / includes / search / SearchPostgres.php
1 <?php
2 # Copyright (C) 2006-2007 Greg Sabino Mullane <greg@turnstep.com>
3 # http://www.mediawiki.org/
4 #
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.
9 #
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.
14 #
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
19
20 /**
21  * @file
22  * @ingroup Search
23  */
24
25 /**
26  * Search engine hook base class for Postgres
27  * @ingroup Search
28  */
29 class SearchPostgres extends SearchEngine {
30
31         function __construct( $db ) {
32                 $this->db = $db;
33         }
34
35         /**
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)
39          *
40          * @param $term String: raw search term
41          * @return PostgresSearchResultSet
42          */
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);
48                 if (!$resultSet) {
49                         // Needed for "Query requires full scan, GIN doesn't support it"
50                         return new SearchResultTooMany();
51                 }
52                 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
53         }
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);
59                 if (!$resultSet) {
60                         return new SearchResultTooMany();
61                 }
62                 return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
63         }
64
65
66         /*
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.
69         */
70         function parseQuery( $term ) {
71
72                 wfDebug( "parseQuery received: $term \n" );
73
74                 ## No backslashes allowed
75                 $term = preg_replace('/\\\/', '', $term);
76
77                 ## Collapse parens into nearby words:
78                 $term = preg_replace('/\s*\(\s*/', ' (', $term);
79                 $term = preg_replace('/\s*\)\s*/', ') ', $term);
80
81                 ## Treat colons as word separators:
82                 $term = preg_replace('/:/', ' ', $term);
83
84                 $searchstring = '';
85                 $m = array();
86                 if( preg_match_all('/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
87                         foreach( $m as $terms ) {
88                                 if (strlen($terms[1])) {
89                                         $searchstring .= ' & !';
90                                 }
91                                 if (strtolower($terms[2]) === 'and') {
92                                         $searchstring .= ' & ';
93                                 }
94                                 else if (strtolower($terms[2]) === 'or' or $terms[2] === '|') {
95                                         $searchstring .= ' | ';
96                                 }
97                                 else if (strtolower($terms[2]) === 'not') {
98                                         $searchstring .= ' & !';
99                                 }
100                                 else {
101                                         $searchstring .= " & $terms[2]";
102                                 }
103                         }
104                 }
105
106                 ## Strip out leading junk
107                 $searchstring = preg_replace('/^[\s\&\|]+/', '', $searchstring);
108
109                 ## Remove any doubled-up operators
110                 $searchstring = preg_replace('/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring);
111
112                 ## Remove any non-spaced operators (e.g. "Zounds!")
113                 $searchstring = preg_replace('/([^ ])[\!\&\|]/', "$1", $searchstring);
114
115                 ## Remove any trailing whitespace or operators
116                 $searchstring = preg_replace('/[\s\!\&\|]+$/', '', $searchstring);
117
118                 ## Remove unnecessary quotes around everything
119                 $searchstring = preg_replace('/^[\'"](.*)[\'"]$/', "$1", $searchstring);
120
121                 ## Quote the whole thing
122                 $searchstring = $this->db->addQuotes($searchstring);
123
124                 wfDebug( "parseQuery returned: $searchstring \n" );
125
126                 return $searchstring;
127
128         }
129
130         /**
131          * Construct the full SQL query to do the search.
132          * @param $filteredTerm String
133          * @param $fulltext String
134          */
135         function searchQuery( $term, $fulltext, $colname ) {
136                 global $wgDBversion;
137
138                 if ( !isset( $wgDBversion ) ) {
139                         $this->db->getServerVersion();
140                         $wgDBversion = $this->db->numeric_version;
141                 }
142                 $prefix = $wgDBversion < 8.3 ? "'default'," : '';
143
144                 # Get the SQL fragment for the given term
145                 $searchstring = $this->parseQuery( $term );
146
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);
150                 if (!$res) {
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");
153                 }
154                 $top = pg_fetch_result($res,0,0);
155
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";
160                 }
161                 else {
162                         $m = array();
163                         if( preg_match_all("/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
164                                 foreach( $m as $terms ) {
165                                         $this->searchTerms[$terms[1]] = $terms[1];
166                                 }
167                         }
168
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)";
175                 }
176
177                 ## Redirects
178                 if (! $this->showRedirects)
179                         $query .= ' AND page_is_redirect = 0';
180
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';
185                         else {
186                                 $namespaces = $this->db->makeList( $this->namespaces );
187                                 $query .= " AND page_namespace IN ($namespaces)";
188                         }
189                 }
190
191                 $query .= " ORDER BY score DESC, page_id DESC";
192
193                 $query .= $this->db->limitResult( '', $this->limit, $this->offset );
194
195                 wfDebug( "searchQuery returned: $query \n" );
196
197                 return $query;
198         }
199
200         ## Most of the work of these two functions are done automatically via triggers
201
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);
208                 return true;
209         }
210
211         function updateTitle( $id, $title ) {
212                 return true;
213         }
214
215 } ## end of the SearchPostgres class
216
217 /**
218  * @ingroup Search
219  */
220 class PostgresSearchResult extends SearchResult {
221         function __construct( $row ) {
222                 parent::__construct($row);
223                 $this->score = $row->score;
224         }
225         function getScore() {
226                 return $this->score;
227         }
228 }
229
230 /**
231  * @ingroup Search
232  */
233 class PostgresSearchResultSet extends SqlSearchResultSet {
234         function __construct( $resultSet, $terms ) {
235                 parent::__construct( $resultSet, $terms );
236         }
237
238         function next() {
239                 $row = $this->mResultSet->fetchObject();
240                 if( $row === false ) {
241                         return false;
242                 } else {
243                         return new PostgresSearchResult( $row );
244                 }
245         }
246 }