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
21 * Search engine hook base class for Oracle (ConText).
25 class SearchOracle
extends SearchEngine
{
26 function __construct($db) {
31 * Perform a full text search query and return a result set.
33 * @param string $term - Raw search term
34 * @return OracleSearchResultSet
37 function searchText( $term ) {
38 $resultSet = $this->db
->resultObject($this->db
->query($this->getQuery($this->filter($term), true)));
39 return new OracleSearchResultSet($resultSet, $this->searchTerms
);
43 * Perform a title-only search query and return a result set.
45 * @param string $term - Raw search term
46 * @return ORacleSearchResultSet
49 function searchTitle($term) {
50 $resultSet = $this->db
->resultObject($this->db
->query($this->getQuery($this->filter($term), false)));
51 return new MySQLSearchResultSet($resultSet, $this->searchTerms
);
56 * Return a partial WHERE clause to exclude redirects, if so set
60 function queryRedirect() {
61 if ($this->showRedirects
) {
64 return 'AND page_is_redirect=0';
69 * Return a partial WHERE clause to limit the search to the given namespaces
73 function queryNamespaces() {
74 $namespaces = implode(',', $this->namespaces
);
75 if ($namespaces == '') {
78 return 'AND page_namespace IN (' . $namespaces . ')';
82 * Return a LIMIT clause to limit results on the query.
86 function queryLimit($sql) {
87 return $this->db
->limitResult($sql, $this->limit
, $this->offset
);
91 * Does not do anything for generic search engine
92 * subclasses may define this though
96 function queryRanking($filteredTerm, $fulltext) {
97 return ' ORDER BY score(1)';
101 * Construct the full SQL query to do the search.
102 * The guts shoulds be constructed in queryMain()
103 * @param string $filteredTerm
104 * @param bool $fulltext
107 function getQuery( $filteredTerm, $fulltext ) {
108 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
109 $this->queryRedirect() . ' ' .
110 $this->queryNamespaces() . ' ' .
111 $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
116 * Picks which field to index on, depending on what type of query.
117 * @param bool $fulltext
120 function getIndexField($fulltext) {
121 return $fulltext ?
'si_text' : 'si_title';
125 * Get the base part of the search query.
127 * @param string $filteredTerm
128 * @param bool $fulltext
132 function queryMain( $filteredTerm, $fulltext ) {
133 $match = $this->parseQuery($filteredTerm, $fulltext);
134 $page = $this->db
->tableName('page');
135 $searchindex = $this->db
->tableName('searchindex');
136 return 'SELECT page_id, page_namespace, page_title ' .
137 "FROM $page,$searchindex " .
138 'WHERE page_id=si_page AND ' . $match;
141 /** @todo document */
142 function parseQuery($filteredText, $fulltext) {
144 $lc = SearchEngine
::legalSearchChars();
145 $this->searchTerms
= array();
147 # FIXME: This doesn't handle parenthetical expressions.
151 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
152 $filteredText, $m, PREG_SET_ORDER
)) {
153 foreach($m as $terms) {
154 $q[] = $terms[1] . $wgContLang->stripForSearch($terms[2]);
156 if (!empty($terms[3])) {
157 $regexp = preg_quote( $terms[3], '/' );
159 $regexp .= "[0-9A-Za-z_]+";
161 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
163 $this->searchTerms
[] = $regexp;
167 $searchon = $this->db
->strencode(join(',', $q));
168 $field = $this->getIndexField($fulltext);
169 return " CONTAINS($field, '$searchon', 1) > 0 ";
173 * Create or update the search index record for the given page.
174 * Title and text should be pre-processed.
177 * @param string $title
178 * @param string $text
180 function update($id, $title, $text) {
181 $dbw = wfGetDB(DB_MASTER
);
182 $dbw->replace('searchindex',
186 'si_title' => $title,
188 ), 'SearchOracle::update' );
189 $dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
190 $dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
194 * Update a search index record's title only.
195 * Title should be pre-processed.
198 * @param string $title
200 function updateTitle($id, $title) {
201 $dbw = wfGetDB(DB_MASTER
);
203 $dbw->update('searchindex',
204 array('si_title' => $title),
205 array('si_page' => $id),
206 'SearchOracle::updateTitle',
211 class OracleSearchResultSet
extends SearchResultSet
{
212 function __construct($resultSet, $terms) {
213 $this->mResultSet
= $resultSet;
214 $this->mTerms
= $terms;
217 function termMatches() {
218 return $this->mTerms
;
222 return $this->mResultSet
->numRows();
226 $row = $this->mResultSet
->fetchObject();
229 return new SearchResult($row);