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