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 var $strictMatching = false;
28 function __construct($db) {
33 * Perform a full text search query and return a result set.
35 * @param string $term - Raw search term
36 * @return OracleSearchResultSet
39 function searchText( $term ) {
40 $resultSet = $this->db
->resultObject($this->db
->query($this->getQuery($this->filter($term), true)));
41 return new OracleSearchResultSet($resultSet, $this->searchTerms
);
45 * Perform a title-only search query and return a result set.
47 * @param string $term - Raw search term
48 * @return ORacleSearchResultSet
51 function searchTitle($term) {
52 $resultSet = $this->db
->resultObject($this->db
->query($this->getQuery($this->filter($term), false)));
53 return new MySQLSearchResultSet($resultSet, $this->searchTerms
);
58 * Return a partial WHERE clause to exclude redirects, if so set
62 function queryRedirect() {
63 if ($this->showRedirects
) {
66 return 'AND page_is_redirect=0';
71 * Return a partial WHERE clause to limit the search to the given namespaces
75 function queryNamespaces() {
76 $namespaces = implode(',', $this->namespaces
);
77 if ($namespaces == '') {
80 return 'AND page_namespace IN (' . $namespaces . ')';
84 * Return a LIMIT clause to limit results on the query.
88 function queryLimit($sql) {
89 return $this->db
->limitResult($sql, $this->limit
, $this->offset
);
93 * Does not do anything for generic search engine
94 * subclasses may define this though
98 function queryRanking($filteredTerm, $fulltext) {
103 * Construct the full SQL query to do the search.
104 * The guts shoulds be constructed in queryMain()
105 * @param string $filteredTerm
106 * @param bool $fulltext
109 function getQuery( $filteredTerm, $fulltext ) {
110 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
111 $this->queryRedirect() . ' ' .
112 $this->queryNamespaces() . ' ' .
113 $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
118 * Picks which field to index on, depending on what type of query.
119 * @param bool $fulltext
122 function getIndexField($fulltext) {
123 return $fulltext ?
'si_text' : 'si_title';
127 * Get the base part of the search query.
129 * @param string $filteredTerm
130 * @param bool $fulltext
134 function queryMain( $filteredTerm, $fulltext ) {
135 $match = $this->parseQuery($filteredTerm, $fulltext);
136 $page = $this->db
->tableName('page');
137 $searchindex = $this->db
->tableName('searchindex');
138 return 'SELECT page_id, page_namespace, page_title ' .
139 "FROM $page,$searchindex " .
140 'WHERE page_id=si_page AND ' . $match;
143 /** @todo document */
144 function parseQuery( $filteredText, $fulltext ) {
146 $lc = SearchEngine
::legalSearchChars();
148 $this->searchTerms
= array();
150 # 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 if( $searchon !== '' ) $searchon .= ' ';
156 if( $this->strictMatching
&& ($terms[1] == '') ) {
159 $searchon .= $terms[1] . $wgContLang->stripForSearch( $terms[2] );
160 if( !empty( $terms[3] ) ) {
161 $regexp = preg_quote( $terms[3], '/' );
162 if( $terms[4] ) $regexp .= "[0-9A-Za-z_]+";
164 $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
166 $this->searchTerms
[] = $regexp;
168 wfDebug( "Would search with '$searchon'\n" );
169 wfDebug( 'Match with /\b' . implode( '\b|\b', $this->searchTerms
) . "\b/\n" );
171 wfDebug( "Can't understand search query '{$filteredText}'\n" );
174 $searchon = $this->db
->strencode($searchon);
175 $field = $this->getIndexField( $fulltext );
176 return " CONTAINS($field, '$searchon', 1) > 0 ";
180 * Create or update the search index record for the given page.
181 * Title and text should be pre-processed.
184 * @param string $title
185 * @param string $text
187 function update($id, $title, $text) {
188 $dbw = wfGetDB(DB_MASTER
);
189 $dbw->replace('searchindex',
193 'si_title' => $title,
195 ), 'SearchOracle::update' );
199 * Update a search index record's title only.
200 * Title should be pre-processed.
203 * @param string $title
205 function updateTitle( $id, $title ) {
206 $dbw = wfGetDB(DB_MASTER
);
208 $dbw->update('searchindex',
209 array('si_title' => $title),
210 array('si_page' => $id),
211 'SearchOracle::updateTitle',
216 class OracleSearchResultSet
extends SearchResultSet
{
217 function __construct($resultSet, $terms) {
218 $this->mResultSet
= $resultSet;
219 $this->mTerms
= $terms;
222 function termMatches() {
223 return $this->mTerms
;
227 return $this->mResultSet
->numRows();
231 $row = $this->mResultSet
->fetchObject();
232 if( $row === false) {
235 return new SearchResult($row);