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).
24 class SearchOracle
extends SearchEngine
{
25 function __construct($db) {
30 * Perform a full text search query and return a result set.
32 * @param string $term - Raw search term
33 * @return OracleSearchResultSet
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
);
42 * Perform a title-only search query and return a result set.
44 * @param string $term - Raw search term
45 * @return ORacleSearchResultSet
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
);
55 * Return a partial WHERE clause to exclude redirects, if so set
59 function queryRedirect() {
60 if ($this->showRedirects
) {
63 return 'AND page_is_redirect=0';
68 * Return a partial WHERE clause to limit the search to the given namespaces
72 function queryNamespaces() {
73 if( is_null($this->namespaces
) )
75 $namespaces = implode(',', $this->namespaces
);
76 if ($namespaces == '') {
79 return 'AND page_namespace IN (' . $namespaces . ')';
83 * Return a LIMIT clause to limit results on the query.
87 function queryLimit($sql) {
88 return $this->db
->limitResult($sql, $this->limit
, $this->offset
);
92 * Does not do anything for generic search engine
93 * subclasses may define this though
97 function queryRanking($filteredTerm, $fulltext) {
98 return ' ORDER BY score(1)';
102 * Construct the full SQL query to do the search.
103 * The guts shoulds be constructed in queryMain()
104 * @param string $filteredTerm
105 * @param bool $fulltext
108 function getQuery( $filteredTerm, $fulltext ) {
109 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
110 $this->queryRedirect() . ' ' .
111 $this->queryNamespaces() . ' ' .
112 $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
117 * Picks which field to index on, depending on what type of query.
118 * @param bool $fulltext
121 function getIndexField($fulltext) {
122 return $fulltext ?
'si_text' : 'si_title';
126 * Get the base part of the search query.
128 * @param string $filteredTerm
129 * @param bool $fulltext
133 function queryMain( $filteredTerm, $fulltext ) {
134 $match = $this->parseQuery($filteredTerm, $fulltext);
135 $page = $this->db
->tableName('page');
136 $searchindex = $this->db
->tableName('searchindex');
137 return 'SELECT page_id, page_namespace, page_title ' .
138 "FROM $page,$searchindex " .
139 'WHERE page_id=si_page AND ' . $match;
142 /** @todo document */
143 function parseQuery($filteredText, $fulltext) {
145 $lc = SearchEngine
::legalSearchChars();
146 $this->searchTerms
= array();
148 # FIXME: This doesn't handle parenthetical expressions.
152 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
153 $filteredText, $m, PREG_SET_ORDER
)) {
154 foreach($m as $terms) {
155 $q[] = $terms[1] . $wgContLang->stripForSearch($terms[2]);
157 if (!empty($terms[3])) {
158 $regexp = preg_quote( $terms[3], '/' );
160 $regexp .= "[0-9A-Za-z_]+";
162 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
164 $this->searchTerms
[] = $regexp;
168 $searchon = $this->db
->strencode(join(',', $q));
169 $field = $this->getIndexField($fulltext);
170 return " CONTAINS($field, '$searchon', 1) > 0 ";
174 * Create or update the search index record for the given page.
175 * Title and text should be pre-processed.
178 * @param string $title
179 * @param string $text
181 function update($id, $title, $text) {
182 $dbw = wfGetDB(DB_MASTER
);
183 $dbw->replace('searchindex',
187 'si_title' => $title,
189 ), 'SearchOracle::update' );
190 $dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
191 $dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
195 * Update a search index record's title only.
196 * Title should be pre-processed.
199 * @param string $title
201 function updateTitle($id, $title) {
202 $dbw = wfGetDB(DB_MASTER
);
204 $dbw->update('searchindex',
205 array('si_title' => $title),
206 array('si_page' => $id),
207 'SearchOracle::updateTitle',
215 class OracleSearchResultSet
extends SearchResultSet
{
216 function __construct($resultSet, $terms) {
217 $this->mResultSet
= $resultSet;
218 $this->mTerms
= $terms;
221 function termMatches() {
222 return $this->mTerms
;
226 return $this->mResultSet
->numRows();
230 $row = $this->mResultSet
->fetchObject();
233 return new SearchResult($row);