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 if ( !count( $this->namespaces
) ) {
83 $namespaces = $this->db
->makeList( $this->namespaces
);
85 return 'AND page_namespace IN (' . $namespaces . ')';
89 * Return a LIMIT clause to limit results on the query.
93 function queryLimit($sql) {
94 return $this->db
->limitResult($sql, $this->limit
, $this->offset
);
98 * Does not do anything for generic search engine
99 * subclasses may define this though
103 function queryRanking($filteredTerm, $fulltext) {
104 return ' ORDER BY score(1)';
108 * Construct the full SQL query to do the search.
109 * The guts shoulds be constructed in queryMain()
110 * @param string $filteredTerm
111 * @param bool $fulltext
114 function getQuery( $filteredTerm, $fulltext ) {
115 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
116 $this->queryRedirect() . ' ' .
117 $this->queryNamespaces() . ' ' .
118 $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
123 * Picks which field to index on, depending on what type of query.
124 * @param bool $fulltext
127 function getIndexField($fulltext) {
128 return $fulltext ?
'si_text' : 'si_title';
132 * Get the base part of the search query.
134 * @param string $filteredTerm
135 * @param bool $fulltext
139 function queryMain( $filteredTerm, $fulltext ) {
140 $match = $this->parseQuery($filteredTerm, $fulltext);
141 $page = $this->db
->tableName('page');
142 $searchindex = $this->db
->tableName('searchindex');
143 return 'SELECT page_id, page_namespace, page_title ' .
144 "FROM $page,$searchindex " .
145 'WHERE page_id=si_page AND ' . $match;
149 * Parse a user input search string, and return an SQL fragment to be used
150 * as part of a WHERE clause
152 function parseQuery($filteredText, $fulltext) {
154 $lc = SearchEngine
::legalSearchChars();
155 $this->searchTerms
= array();
157 # FIXME: This doesn't handle parenthetical expressions.
161 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
162 $filteredText, $m, PREG_SET_ORDER
)) {
163 foreach($m as $terms) {
164 $q[] = $terms[1] . $wgContLang->stripForSearch($terms[2]);
166 if (!empty($terms[3])) {
167 $regexp = preg_quote( $terms[3], '/' );
169 $regexp .= "[0-9A-Za-z_]+";
171 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
173 $this->searchTerms
[] = $regexp;
177 $searchon = $this->db
->addQuotes(join(',', $q));
178 $field = $this->getIndexField($fulltext);
179 return " CONTAINS($field, $searchon, 1) > 0 ";
183 * Create or update the search index record for the given page.
184 * Title and text should be pre-processed.
187 * @param string $title
188 * @param string $text
190 function update($id, $title, $text) {
191 $dbw = wfGetDB(DB_MASTER
);
192 $dbw->replace('searchindex',
196 'si_title' => $title,
198 ), 'SearchOracle::update' );
199 $dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
200 $dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
204 * Update a search index record's title only.
205 * Title should be pre-processed.
208 * @param string $title
210 function updateTitle($id, $title) {
211 $dbw = wfGetDB(DB_MASTER
);
213 $dbw->update('searchindex',
214 array('si_title' => $title),
215 array('si_page' => $id),
216 'SearchOracle::updateTitle',
224 class OracleSearchResultSet
extends SearchResultSet
{
225 function __construct($resultSet, $terms) {
226 $this->mResultSet
= $resultSet;
227 $this->mTerms
= $terms;
230 function termMatches() {
231 return $this->mTerms
;
235 return $this->mResultSet
->numRows();
239 $row = $this->mResultSet
->fetchObject();
242 return new SearchResult($row);