2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
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.
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.
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
26 * Search engine hook base class for Oracle (ConText).
29 class SearchOracle
extends SearchEngine
{
31 private $reservedWords = array ('ABOUT' => 1,
58 function __construct($db) {
63 * Perform a full text search query and return a result set.
65 * @param $term String: raw search term
66 * @return SqlSearchResultSet
68 function searchText( $term ) {
70 return new SqlSearchResultSet(false, '');
72 $resultSet = $this->db
->resultObject($this->db
->query($this->getQuery($this->filter($term), true)));
73 return new SqlSearchResultSet($resultSet, $this->searchTerms
);
77 * Perform a title-only search query and return a result set.
79 * @param $term String: raw search term
80 * @return SqlSearchResultSet
82 function searchTitle($term) {
84 return new SqlSearchResultSet(false, '');
86 $resultSet = $this->db
->resultObject($this->db
->query($this->getQuery($this->filter($term), false)));
87 return new MySQLSearchResultSet($resultSet, $this->searchTerms
);
92 * Return a partial WHERE clause to exclude redirects, if so set
95 function queryRedirect() {
96 if ($this->showRedirects
) {
99 return 'AND page_is_redirect=0';
104 * Return a partial WHERE clause to limit the search to the given namespaces
107 function queryNamespaces() {
108 if( is_null($this->namespaces
) )
110 if ( !count( $this->namespaces
) ) {
113 $namespaces = $this->db
->makeList( $this->namespaces
);
115 return 'AND page_namespace IN (' . $namespaces . ')';
119 * Return a LIMIT clause to limit results on the query.
122 function queryLimit($sql) {
123 return $this->db
->limitResult($sql, $this->limit
, $this->offset
);
127 * Does not do anything for generic search engine
128 * subclasses may define this though
131 function queryRanking($filteredTerm, $fulltext) {
132 return ' ORDER BY score(1)';
136 * Construct the full SQL query to do the search.
137 * The guts shoulds be constructed in queryMain()
138 * @param $filteredTerm String
139 * @param $fulltext Boolean
141 function getQuery( $filteredTerm, $fulltext ) {
142 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
143 $this->queryRedirect() . ' ' .
144 $this->queryNamespaces() . ' ' .
145 $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
150 * Picks which field to index on, depending on what type of query.
151 * @param $fulltext Boolean
154 function getIndexField($fulltext) {
155 return $fulltext ?
'si_text' : 'si_title';
159 * Get the base part of the search query.
161 * @param $filteredTerm String
162 * @param $fulltext Boolean
165 function queryMain( $filteredTerm, $fulltext ) {
166 $match = $this->parseQuery($filteredTerm, $fulltext);
167 $page = $this->db
->tableName('page');
168 $searchindex = $this->db
->tableName('searchindex');
169 return 'SELECT page_id, page_namespace, page_title ' .
170 "FROM $page,$searchindex " .
171 'WHERE page_id=si_page AND ' . $match;
175 * Parse a user input search string, and return an SQL fragment to be used
176 * as part of a WHERE clause
178 function parseQuery($filteredText, $fulltext) {
180 $lc = SearchEngine
::legalSearchChars();
181 $this->searchTerms
= array();
183 # FIXME: This doesn't handle parenthetical expressions.
186 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
187 $filteredText, $m, PREG_SET_ORDER
)) {
188 foreach($m as $terms) {
189 // Search terms in all variant forms, only
190 // apply on wiki with LanguageConverter
191 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
192 if( is_array( $temp_terms )) {
193 $temp_terms = array_unique( array_values( $temp_terms ));
194 foreach( $temp_terms as $t ) {
195 $searchon .= ($terms[1] == '-' ?
' ~' : ' & ') . $this->escapeTerm( $t );
199 $searchon .= ($terms[1] == '-' ?
' ~' : ' & ') . $this->escapeTerm( $terms[2] );
201 if (!empty($terms[3])) {
202 $regexp = preg_quote( $terms[3], '/' );
204 $regexp .= "[0-9A-Za-z_]+";
206 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
208 $this->searchTerms
[] = $regexp;
213 $searchon = $this->db
->addQuotes(ltrim($searchon, ' &'));
214 $field = $this->getIndexField($fulltext);
215 return " CONTAINS($field, $searchon, 1) > 0 ";
218 private function escapeTerm($t) {
220 $t = $wgContLang->normalizeForSearch($t);
221 $t = isset($this->reservedWords
[strtoupper($t)]) ?
'{'.$t.'}' : $t;
222 $t = preg_replace('/^"(.*)"$/', '($1)', $t);
223 $t = preg_replace('/([-&|])/', '\\\\$1', $t);
227 * Create or update the search index record for the given page.
228 * Title and text should be pre-processed.
231 * @param $title String
232 * @param $text String
234 function update($id, $title, $text) {
235 $dbw = wfGetDB(DB_MASTER
);
236 $dbw->replace('searchindex',
240 'si_title' => $title,
242 ), 'SearchOracle::update' );
243 $dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
244 $dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
248 * Update a search index record's title only.
249 * Title should be pre-processed.
252 * @param string $title
254 function updateTitle($id, $title) {
255 $dbw = wfGetDB(DB_MASTER
);
257 $dbw->update('searchindex',
258 array('si_title' => $title),
259 array('si_page' => $id),
260 'SearchOracle::updateTitle',
265 public static function legalSearchChars() {
266 return "\"" . parent
::legalSearchChars();