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 $term String: raw search term
38 * @return OracleSearchResultSet
40 function searchText( $term ) {
42 return new OracleSearchResultSet(false, '');
44 $resultSet = $this->db
->resultObject($this->db
->query($this->getQuery($this->filter($term), true)));
45 return new OracleSearchResultSet($resultSet, $this->searchTerms
);
49 * Perform a title-only search query and return a result set.
51 * @param $term String: raw search term
52 * @return ORacleSearchResultSet
54 function searchTitle($term) {
56 return new OracleSearchResultSet(false, '');
58 $resultSet = $this->db
->resultObject($this->db
->query($this->getQuery($this->filter($term), false)));
59 return new MySQLSearchResultSet($resultSet, $this->searchTerms
);
64 * Return a partial WHERE clause to exclude redirects, if so set
67 function queryRedirect() {
68 if ($this->showRedirects
) {
71 return 'AND page_is_redirect=0';
76 * Return a partial WHERE clause to limit the search to the given namespaces
79 function queryNamespaces() {
80 if( is_null($this->namespaces
) )
82 if ( !count( $this->namespaces
) ) {
85 $namespaces = $this->db
->makeList( $this->namespaces
);
87 return 'AND page_namespace IN (' . $namespaces . ')';
91 * Return a LIMIT clause to limit results on the query.
94 function queryLimit($sql) {
95 return $this->db
->limitResult($sql, $this->limit
, $this->offset
);
99 * Does not do anything for generic search engine
100 * 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 $filteredTerm String
111 * @param $fulltext Boolean
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 $fulltext Boolean
126 function getIndexField($fulltext) {
127 return $fulltext ?
'si_text' : 'si_title';
131 * Get the base part of the search query.
133 * @param $filteredTerm String
134 * @param $fulltext Boolean
137 function queryMain( $filteredTerm, $fulltext ) {
138 $match = $this->parseQuery($filteredTerm, $fulltext);
139 $page = $this->db
->tableName('page');
140 $searchindex = $this->db
->tableName('searchindex');
141 return 'SELECT page_id, page_namespace, page_title ' .
142 "FROM $page,$searchindex " .
143 'WHERE page_id=si_page AND ' . $match;
147 * Parse a user input search string, and return an SQL fragment to be used
148 * as part of a WHERE clause
150 function parseQuery($filteredText, $fulltext) {
152 $lc = SearchEngine
::legalSearchChars();
153 $this->searchTerms
= array();
155 # FIXME: This doesn't handle parenthetical expressions.
159 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
160 $filteredText, $m, PREG_SET_ORDER
)) {
161 foreach($m as $terms) {
162 $q[] = $terms[1] . $wgContLang->stripForSearch($terms[2]);
164 if (!empty($terms[3])) {
165 $regexp = preg_quote( $terms[3], '/' );
167 $regexp .= "[0-9A-Za-z_]+";
169 $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
171 $this->searchTerms
[] = $regexp;
175 $searchon = $this->db
->addQuotes(join(',', $q));
176 $field = $this->getIndexField($fulltext);
177 return " CONTAINS($field, $searchon, 1) > 0 ";
181 * Create or update the search index record for the given page.
182 * Title and text should be pre-processed.
185 * @param $title String
186 * @param $text String
188 function update($id, $title, $text) {
189 $dbw = wfGetDB(DB_MASTER
);
190 $dbw->replace('searchindex',
194 'si_title' => $title,
196 ), 'SearchOracle::update' );
197 $dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
198 $dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
202 * Update a search index record's title only.
203 * Title should be pre-processed.
206 * @param string $title
208 function updateTitle($id, $title) {
209 $dbw = wfGetDB(DB_MASTER
);
211 $dbw->update('searchindex',
212 array('si_title' => $title),
213 array('si_page' => $id),
214 'SearchOracle::updateTitle',
222 class OracleSearchResultSet
extends SearchResultSet
{
224 function __construct($resultSet, $terms) {
225 $this->mResultSet
= $resultSet;
226 $this->mTerms
= $terms;
229 function termMatches() {
230 return $this->mTerms
;
234 if ($this->mResultSet
=== false )
237 return $this->mResultSet
->numRows();
241 if ($this->mResultSet
=== false )
244 $row = $this->mResultSet
->fetchObject();
247 return new SearchResult($row);