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
{
30 function __construct($db) {
35 * Perform a full text search query and return a result set.
37 * @param string $term - Raw search term
38 * @return OracleSearchResultSet
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
);
47 * Perform a title-only search query and return a result set.
49 * @param string $term - Raw search term
50 * @return ORacleSearchResultSet
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
);
60 * Return a partial WHERE clause to exclude redirects, if so set
64 function queryRedirect() {
65 if ($this->showRedirects
) {
68 return 'AND page_is_redirect=0';
73 * Return a partial WHERE clause to limit the search to the given namespaces
77 function queryNamespaces() {
78 if( is_null($this->namespaces
) )
80 $namespaces = implode(',', $this->namespaces
);
81 if ($namespaces == '') {
84 return 'AND page_namespace IN (' . $namespaces . ')';
88 * Return a LIMIT clause to limit results on the query.
92 function queryLimit($sql) {
93 return $this->db
->limitResult($sql, $this->limit
, $this->offset
);
97 * Does not do anything for generic search engine
98 * subclasses may define this though
102 function queryRanking($filteredTerm, $fulltext) {
103 return ' ORDER BY score(1)';
107 * Construct the full SQL query to do the search.
108 * The guts shoulds be constructed in queryMain()
109 * @param string $filteredTerm
110 * @param bool $fulltext
113 function getQuery( $filteredTerm, $fulltext ) {
114 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
115 $this->queryRedirect() . ' ' .
116 $this->queryNamespaces() . ' ' .
117 $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
122 * Picks which field to index on, depending on what type of query.
123 * @param bool $fulltext
126 function getIndexField($fulltext) {
127 return $fulltext ?
'si_text' : 'si_title';
131 * Get the base part of the search query.
133 * @param string $filteredTerm
134 * @param bool $fulltext
138 function queryMain( $filteredTerm, $fulltext ) {
139 $match = $this->parseQuery($filteredTerm, $fulltext);
140 $page = $this->db
->tableName('page');
141 $searchindex = $this->db
->tableName('searchindex');
142 return 'SELECT page_id, page_namespace, page_title ' .
143 "FROM $page,$searchindex " .
144 'WHERE page_id=si_page AND ' . $match;
147 /** @todo document */
148 function parseQuery($filteredText, $fulltext) {
150 $lc = SearchEngine
::legalSearchChars();
151 $this->searchTerms
= array();
153 # FIXME: This doesn't handle parenthetical expressions.
157 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
158 $filteredText, $m, PREG_SET_ORDER
)) {
159 foreach($m as $terms) {
160 $q[] = $terms[1] . $wgContLang->stripForSearch($terms[2]);
162 if (!empty($terms[3])) {
163 $regexp = preg_quote( $terms[3], '/' );
165 $regexp .= "[0-9A-Za-z_]+";
167 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
169 $this->searchTerms
[] = $regexp;
173 $searchon = $this->db
->strencode(join(',', $q));
174 $field = $this->getIndexField($fulltext);
175 return " CONTAINS($field, '$searchon', 1) > 0 ";
179 * Create or update the search index record for the given page.
180 * Title and text should be pre-processed.
183 * @param string $title
184 * @param string $text
186 function update($id, $title, $text) {
187 $dbw = wfGetDB(DB_MASTER
);
188 $dbw->replace('searchindex',
192 'si_title' => $title,
194 ), 'SearchOracle::update' );
195 $dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
196 $dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
200 * Update a search index record's title only.
201 * Title should be pre-processed.
204 * @param string $title
206 function updateTitle($id, $title) {
207 $dbw = wfGetDB(DB_MASTER
);
209 $dbw->update('searchindex',
210 array('si_title' => $title),
211 array('si_page' => $id),
212 'SearchOracle::updateTitle',
220 class OracleSearchResultSet
extends SearchResultSet
{
221 function __construct($resultSet, $terms) {
222 $this->mResultSet
= $resultSet;
223 $this->mTerms
= $terms;
226 function termMatches() {
227 return $this->mTerms
;
231 return $this->mResultSet
->numRows();
235 $row = $this->mResultSet
->fetchObject();
238 return new SearchResult($row);