5 * Copyright © 2004 Brion Vibber <brion@pobox.com>
6 * http://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
28 * Search engine hook base class for Oracle (ConText).
31 class SearchOracle
extends SearchEngine
{
33 private $reservedWords = array ('ABOUT' => 1,
61 * Creates an instance of this class
62 * @param $db DatabasePostgres: database object
64 function __construct($db) {
65 parent
::__construct( $db );
69 * Perform a full text search query and return a result set.
71 * @param $term String: raw search term
72 * @return SqlSearchResultSet
74 function searchText( $term ) {
76 return new SqlSearchResultSet(false, '');
78 $resultSet = $this->db
->resultObject($this->db
->query($this->getQuery($this->filter($term), true)));
79 return new SqlSearchResultSet($resultSet, $this->searchTerms
);
83 * Perform a title-only search query and return a result set.
85 * @param $term String: raw search term
86 * @return SqlSearchResultSet
88 function searchTitle($term) {
90 return new SqlSearchResultSet(false, '');
92 $resultSet = $this->db
->resultObject($this->db
->query($this->getQuery($this->filter($term), false)));
93 return new MySQLSearchResultSet($resultSet, $this->searchTerms
);
98 * Return a partial WHERE clause to exclude redirects, if so set
101 function queryRedirect() {
102 if ($this->showRedirects
) {
105 return 'AND page_is_redirect=0';
110 * Return a partial WHERE clause to limit the search to the given namespaces
113 function queryNamespaces() {
114 if( is_null($this->namespaces
) )
116 if ( !count( $this->namespaces
) ) {
119 $namespaces = $this->db
->makeList( $this->namespaces
);
121 return 'AND page_namespace IN (' . $namespaces . ')';
125 * Return a LIMIT clause to limit results on the query.
128 function queryLimit($sql) {
129 return $this->db
->limitResult($sql, $this->limit
, $this->offset
);
133 * Does not do anything for generic search engine
134 * subclasses may define this though
137 function queryRanking($filteredTerm, $fulltext) {
138 return ' ORDER BY score(1)';
142 * Construct the full SQL query to do the search.
143 * The guts shoulds be constructed in queryMain()
144 * @param $filteredTerm String
145 * @param $fulltext Boolean
147 function getQuery( $filteredTerm, $fulltext ) {
148 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
149 $this->queryRedirect() . ' ' .
150 $this->queryNamespaces() . ' ' .
151 $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
156 * Picks which field to index on, depending on what type of query.
157 * @param $fulltext Boolean
160 function getIndexField($fulltext) {
161 return $fulltext ?
'si_text' : 'si_title';
165 * Get the base part of the search query.
167 * @param $filteredTerm String
168 * @param $fulltext Boolean
171 function queryMain( $filteredTerm, $fulltext ) {
172 $match = $this->parseQuery($filteredTerm, $fulltext);
173 $page = $this->db
->tableName('page');
174 $searchindex = $this->db
->tableName('searchindex');
175 return 'SELECT page_id, page_namespace, page_title ' .
176 "FROM $page,$searchindex " .
177 'WHERE page_id=si_page AND ' . $match;
181 * Parse a user input search string, and return an SQL fragment to be used
182 * as part of a WHERE clause
184 function parseQuery($filteredText, $fulltext) {
186 $lc = SearchEngine
::legalSearchChars();
187 $this->searchTerms
= array();
189 # FIXME: This doesn't handle parenthetical expressions.
192 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
193 $filteredText, $m, PREG_SET_ORDER
)) {
194 foreach($m as $terms) {
195 // Search terms in all variant forms, only
196 // apply on wiki with LanguageConverter
197 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
198 if( is_array( $temp_terms )) {
199 $temp_terms = array_unique( array_values( $temp_terms ));
200 foreach( $temp_terms as $t ) {
201 $searchon .= ($terms[1] == '-' ?
' ~' : ' & ') . $this->escapeTerm( $t );
205 $searchon .= ($terms[1] == '-' ?
' ~' : ' & ') . $this->escapeTerm( $terms[2] );
207 if (!empty($terms[3])) {
208 $regexp = preg_quote( $terms[3], '/' );
210 $regexp .= "[0-9A-Za-z_]+";
212 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
214 $this->searchTerms
[] = $regexp;
219 $searchon = $this->db
->addQuotes(ltrim($searchon, ' &'));
220 $field = $this->getIndexField($fulltext);
221 return " CONTAINS($field, $searchon, 1) > 0 ";
224 private function escapeTerm($t) {
226 $t = $wgContLang->normalizeForSearch($t);
227 $t = isset($this->reservedWords
[strtoupper($t)]) ?
'{'.$t.'}' : $t;
228 $t = preg_replace('/^"(.*)"$/', '($1)', $t);
229 $t = preg_replace('/([-&|])/', '\\\\$1', $t);
233 * Create or update the search index record for the given page.
234 * Title and text should be pre-processed.
237 * @param $title String
238 * @param $text String
240 function update($id, $title, $text) {
241 $dbw = wfGetDB(DB_MASTER
);
242 $dbw->replace('searchindex',
246 'si_title' => $title,
248 ), 'SearchOracle::update' );
249 $dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
250 $dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
254 * Update a search index record's title only.
255 * Title should be pre-processed.
258 * @param $title String
260 function updateTitle($id, $title) {
261 $dbw = wfGetDB(DB_MASTER
);
263 $dbw->update('searchindex',
264 array('si_title' => $title),
265 array('si_page' => $id),
266 'SearchOracle::updateTitle',
271 public static function legalSearchChars() {
272 return "\"" . parent
::legalSearchChars();