fix notice warning in some configs
[mediawiki.git] / includes / SearchEngine.php
blob81d2ad639bed20ace03962a7bdb1cd9981e0c5d7
1 <?php
2 /**
3 * Contain a class for special pages
4 * @package MediaWiki
5 * @subpackage Search
6 */
8 /**
9 * @package MediaWiki
11 class SearchEngine {
12 var $limit = 10;
13 var $offset = 0;
14 var $searchTerms = array();
15 var $namespaces = array( 0 );
16 var $showRedirects = false;
18 /**
19 * Perform a full text search query and return a result set.
20 * If title searches are not supported or disabled, return null.
22 * @param string $term - Raw search term
23 * @return SearchResultSet
24 * @access public
25 * @abstract
27 function searchText( $term ) {
28 return null;
31 /**
32 * Perform a title-only search query and return a result set.
33 * If title searches are not supported or disabled, return null.
35 * @param string $term - Raw search term
36 * @return SearchResultSet
37 * @access public
38 * @abstract
40 function searchTitle( $term ) {
41 return null;
44 /**
45 * If an exact title match can be find, or a very slightly close match,
46 * return the title. If no match, returns NULL.
48 * @static
49 * @param string $term
50 * @return Title
51 * @access private
53 function getNearMatch( $term ) {
54 # Exact match? No need to look further.
55 $title = Title::newFromText( $term );
56 if (is_null($title))
57 return NULL;
59 if ( $title->getNamespace() == NS_SPECIAL || $title->exists() ) {
60 return $title;
63 # Now try all lower case (i.e. first letter capitalized)
65 $title = Title::newFromText( strtolower( $term ) );
66 if ( $title->exists() ) {
67 return $title;
70 # Now try capitalized string
72 $title = Title::newFromText( ucwords( strtolower( $term ) ) );
73 if ( $title->exists() ) {
74 return $title;
77 # Now try all upper case
79 $title = Title::newFromText( strtoupper( $term ) );
80 if ( $title->exists() ) {
81 return $title;
84 $title = Title::newFromText( $term );
86 # Entering an IP address goes to the contributions page
87 if ( ( $title->getNamespace() == NS_USER && User::isIP($title->getText() ) )
88 || User::isIP( trim( $term ) ) ) {
89 return Title::makeTitle( NS_SPECIAL, "Contributions/" . $title->getDbkey() );
93 # Entering a user goes to the user page whether it's there or not
94 if ( $title->getNamespace() == NS_USER ) {
95 return $title;
98 # Quoted term? Try without the quotes...
99 if( preg_match( '/^"([^"]+)"$/', $term, $matches ) ) {
100 return SearchEngine::getNearMatch( $matches[1] );
103 return NULL;
106 function legalSearchChars() {
107 return "A-Za-z_'0-9\\x80-\\xFF\\-";
111 * Set the maximum number of results to return
112 * and how many to skip before returning the first.
114 * @param int $limit
115 * @param int $offset
116 * @access public
118 function setLimitOffset( $limit, $offset = 0 ) {
119 $this->limit = IntVal( $limit );
120 $this->offset = IntVal( $offset );
124 * Set which namespaces the search should include.
125 * Give an array of namespace index numbers.
127 * @param array $namespaces
128 * @access public
130 function setNamespaces( $namespaces ) {
131 $this->namespaces = $namespaces;
135 * Make a list of searchable namespaces and their canonical names.
136 * @return array
137 * @access public
139 function searchableNamespaces() {
140 global $wgContLang;
141 $arr = array();
142 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
143 if( $ns >= NS_MAIN ) {
144 $arr[$ns] = $name;
147 return $arr;
151 * Return a 'cleaned up' search string
153 * @return string
154 * @access public
156 function filter( $text ) {
157 $lc = $this->legalSearchChars();
158 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
161 * Load up the appropriate search engine class for the currently
162 * active database backend, and return a configured instance.
164 * @return SearchEngine
165 * @access private
167 function create() {
168 global $wgDBtype, $wgDBmysql4, $wgSearchType;
169 if( $wgSearchType ) {
170 $class = $wgSearchType;
171 } elseif( $wgDBtype == 'mysql' ) {
172 if( $wgDBmysql4 ) {
173 $class = 'SearchMySQL4';
174 require_once( 'SearchMySQL4.php' );
175 } else {
176 $class = 'SearchMysql3';
177 require_once( 'SearchMySQL3.php' );
179 } else if ( $wgDBtype == 'PostgreSQL' ) {
180 $class = 'SearchTsearch2';
181 require_once( 'SearchTsearch2.php' );
182 } else {
183 $class = 'SearchEngineDummy';
185 $search = new $class( wfGetDB( DB_SLAVE ) );
186 $search->setLimitOffset(0,0);
187 return $search;
191 * Create or update the search index record for the given page.
192 * Title and text should be pre-processed.
194 * @param int $id
195 * @param string $title
196 * @param string $text
197 * @abstract
199 function update( $id, $title, $text ) {
200 // no-op
204 * Update a search index record's title only.
205 * Title should be pre-processed.
207 * @param int $id
208 * @param string $title
209 * @abstract
211 function updateTitle( $id, $title ) {
212 // no-op
216 class SearchResultSet {
218 * Fetch an array of regular expression fragments for matching
219 * the search terms as parsed by this engine in a text extract.
221 * @return array
222 * @access public
223 * @abstract
225 function termMatches() {
226 return array();
229 function numRows() {
230 return 0;
234 * Return true if results are included in this result set.
235 * @return bool
236 * @abstract
238 function hasResults() {
239 return false;
243 * Some search modes return a total hit count for the query
244 * in the entire article database. This may include pages
245 * in namespaces that would not be matched on the given
246 * settings.
248 * Return null if no total hits number is supported.
250 * @return int
251 * @access public
253 function getTotalHits() {
254 return null;
258 * Some search modes return a suggested alternate term if there are
259 * no exact hits. Returns true if there is one on this set.
261 * @return bool
262 * @access public
264 function hasSuggestion() {
265 return false;
269 * Some search modes return a suggested alternate term if there are
270 * no exact hits. Check hasSuggestion() first.
272 * @return string
273 * @access public
275 function getSuggestion() {
276 return '';
280 * Fetches next search result, or false.
281 * @return SearchResult
282 * @access public
283 * @abstract
285 function next() {
286 return false;
290 class SearchResult {
291 function SearchResult( $row ) {
292 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
296 * @return Title
297 * @access public
299 function getTitle() {
300 return $this->mTitle;
304 * @return double or null if not supported
306 function getScore() {
307 return null;
312 * @package MediaWiki
314 class SearchEngineDummy {
315 function search( $term ) {
316 return null;