]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/SearchOracle.php
MediaWiki 1.15.5
[autoinstallsdev/mediawiki.git] / includes / SearchOracle.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.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 Oracle (ConText).
27  * @ingroup Search
28  */
29 class SearchOracle extends SearchEngine {
30         function __construct($db) {
31                 $this->db = $db;
32         }
33
34         /**
35          * Perform a full text search query and return a result set.
36          *
37          * @param string $term - Raw search term
38          * @return OracleSearchResultSet
39          * @access public
40          */
41         function searchText( $term ) {
42                 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), true)));
43                 return new OracleSearchResultSet($resultSet, $this->searchTerms);
44         }
45
46         /**
47          * Perform a title-only search query and return a result set.
48          *
49          * @param string $term - Raw search term
50          * @return ORacleSearchResultSet
51          * @access public
52          */
53         function searchTitle($term) {
54                 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), false)));
55                 return new MySQLSearchResultSet($resultSet, $this->searchTerms);
56         }
57
58
59         /**
60          * Return a partial WHERE clause to exclude redirects, if so set
61          * @return string
62          * @private
63          */
64         function queryRedirect() {
65                 if ($this->showRedirects) {
66                         return '';
67                 } else {
68                         return 'AND page_is_redirect=0';
69                 }
70         }
71
72         /**
73          * Return a partial WHERE clause to limit the search to the given namespaces
74          * @return string
75          * @private
76          */
77         function queryNamespaces() {
78                 if( is_null($this->namespaces) )
79                         return '';
80                 if ( !count( $this->namespaces ) ) {
81                         $namespaces = '0';
82                 } else {
83                         $namespaces = $this->db->makeList( $this->namespaces );
84                 }
85                 return 'AND page_namespace IN (' . $namespaces . ')';
86         }
87
88         /**
89          * Return a LIMIT clause to limit results on the query.
90          * @return string
91          * @private
92          */
93         function queryLimit($sql) {
94                 return $this->db->limitResult($sql, $this->limit, $this->offset);
95         }
96
97         /**
98          * Does not do anything for generic search engine
99          * subclasses may define this though
100          * @return string
101          * @private
102          */
103         function queryRanking($filteredTerm, $fulltext) {
104                 return ' ORDER BY score(1)';
105         }
106
107         /**
108          * Construct the full SQL query to do the search.
109          * The guts shoulds be constructed in queryMain()
110          * @param string $filteredTerm
111          * @param bool $fulltext
112          * @private
113          */
114         function getQuery( $filteredTerm, $fulltext ) {
115                 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
116                         $this->queryRedirect() . ' ' .
117                         $this->queryNamespaces() . ' ' .
118                         $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
119         }
120
121
122         /**
123          * Picks which field to index on, depending on what type of query.
124          * @param bool $fulltext
125          * @return string
126          */
127         function getIndexField($fulltext) {
128                 return $fulltext ? 'si_text' : 'si_title';
129         }
130
131         /**
132          * Get the base part of the search query.
133          *
134          * @param string $filteredTerm
135          * @param bool $fulltext
136          * @return string
137          * @private
138          */
139         function queryMain( $filteredTerm, $fulltext ) {
140                 $match = $this->parseQuery($filteredTerm, $fulltext);
141                 $page        = $this->db->tableName('page');
142                 $searchindex = $this->db->tableName('searchindex');
143                 return 'SELECT page_id, page_namespace, page_title ' .
144                         "FROM $page,$searchindex " .
145                         'WHERE page_id=si_page AND ' . $match;
146         }
147
148         /** 
149          * Parse a user input search string, and return an SQL fragment to be used 
150          * as part of a WHERE clause
151          */
152         function parseQuery($filteredText, $fulltext) {
153                 global $wgContLang;
154                 $lc = SearchEngine::legalSearchChars();
155                 $this->searchTerms = array();
156
157                 # FIXME: This doesn't handle parenthetical expressions.
158                 $m = array();
159                 $q = array();
160
161                 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
162                           $filteredText, $m, PREG_SET_ORDER)) {
163                         foreach($m as $terms) {
164                                 $q[] = $terms[1] . $wgContLang->stripForSearch($terms[2]);
165
166                                 if (!empty($terms[3])) {
167                                         $regexp = preg_quote( $terms[3], '/' );
168                                         if ($terms[4])
169                                                 $regexp .= "[0-9A-Za-z_]+";
170                                 } else {
171                                         $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
172                                 }
173                                 $this->searchTerms[] = $regexp;
174                         }
175                 }
176
177                 $searchon = $this->db->addQuotes(join(',', $q));
178                 $field = $this->getIndexField($fulltext);
179                 return " CONTAINS($field, $searchon, 1) > 0 ";
180         }
181
182         /**
183          * Create or update the search index record for the given page.
184          * Title and text should be pre-processed.
185          *
186          * @param int $id
187          * @param string $title
188          * @param string $text
189          */
190         function update($id, $title, $text) {
191                 $dbw = wfGetDB(DB_MASTER);
192                 $dbw->replace('searchindex',
193                         array('si_page'),
194                         array(
195                                 'si_page' => $id,
196                                 'si_title' => $title,
197                                 'si_text' => $text
198                         ), 'SearchOracle::update' );
199                 $dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
200                 $dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
201         }
202
203         /**
204          * Update a search index record's title only.
205          * Title should be pre-processed.
206          *
207          * @param int $id
208          * @param string $title
209          */
210         function updateTitle($id, $title) {
211                 $dbw = wfGetDB(DB_MASTER);
212
213                 $dbw->update('searchindex',
214                         array('si_title' => $title),
215                         array('si_page'  => $id),
216                         'SearchOracle::updateTitle',
217                         array());
218         }
219 }
220
221 /**
222  * @ingroup Search
223  */
224 class OracleSearchResultSet extends SearchResultSet {
225         function __construct($resultSet, $terms) {
226                 $this->mResultSet = $resultSet;
227                 $this->mTerms = $terms;
228         }
229
230         function termMatches() {
231                 return $this->mTerms;
232         }
233
234         function numRows() {
235                 return $this->mResultSet->numRows();
236         }
237
238         function next() {
239                 $row = $this->mResultSet->fetchObject();
240                 if ($row === false)
241                         return false;
242                 return new SearchResult($row);
243         }
244 }