use the factory to load a new language
[mediawiki.git] / includes / SearchEngine.php
blobcec40c91660ea7f832ce8e6298169bdce1fa7500
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( NS_MAIN );
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 * @private
53 function getNearMatch( $searchterm ) {
54 global $wgContLang;
56 $allSearchTerms = array($searchterm);
58 if($wgContLang->hasVariants()){
59 $allSearchTerms = array_merge($allSearchTerms,$wgContLang->convertLinkToAllVariants($searchterm));
62 foreach($allSearchTerms as $term){
64 # Exact match? No need to look further.
65 $title = Title::newFromText( $term );
66 if (is_null($title))
67 return NULL;
69 if ( $title->getNamespace() == NS_SPECIAL || $title->exists() ) {
70 return $title;
73 # Now try all lower case (i.e. first letter capitalized)
75 $title = Title::newFromText( $wgContLang->lc( $term ) );
76 if ( $title->exists() ) {
77 return $title;
80 # Now try capitalized string
82 $title = Title::newFromText( $wgContLang->ucwords( $term ) );
83 if ( $title->exists() ) {
84 return $title;
87 # Now try all upper case
89 $title = Title::newFromText( $wgContLang->uc( $term ) );
90 if ( $title->exists() ) {
91 return $title;
94 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
95 $title = Title::newFromText( $wgContLang->ucwordbreaks($term) );
96 if ( $title->exists() ) {
97 return $title;
100 global $wgCapitalLinks, $wgContLang;
101 if( !$wgCapitalLinks ) {
102 // Catch differs-by-first-letter-case-only
103 $title = Title::newFromText( $wgContLang->ucfirst( $term ) );
104 if ( $title->exists() ) {
105 return $title;
107 $title = Title::newFromText( $wgContLang->lcfirst( $term ) );
108 if ( $title->exists() ) {
109 return $title;
114 $title = Title::newFromText( $searchterm );
116 # Entering an IP address goes to the contributions page
117 if ( ( $title->getNamespace() == NS_USER && User::isIP($title->getText() ) )
118 || User::isIP( trim( $searchterm ) ) ) {
119 return SpecialPage::getTitleFor( 'Contributions', $title->getDbkey() );
123 # Entering a user goes to the user page whether it's there or not
124 if ( $title->getNamespace() == NS_USER ) {
125 return $title;
128 # Quoted term? Try without the quotes...
129 $matches = array();
130 if( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
131 return SearchEngine::getNearMatch( $matches[1] );
134 return NULL;
137 function legalSearchChars() {
138 return "A-Za-z_'0-9\\x80-\\xFF\\-";
142 * Set the maximum number of results to return
143 * and how many to skip before returning the first.
145 * @param int $limit
146 * @param int $offset
147 * @access public
149 function setLimitOffset( $limit, $offset = 0 ) {
150 $this->limit = intval( $limit );
151 $this->offset = intval( $offset );
155 * Set which namespaces the search should include.
156 * Give an array of namespace index numbers.
158 * @param array $namespaces
159 * @access public
161 function setNamespaces( $namespaces ) {
162 $this->namespaces = $namespaces;
166 * Make a list of searchable namespaces and their canonical names.
167 * @return array
168 * @access public
170 function searchableNamespaces() {
171 global $wgContLang;
172 $arr = array();
173 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
174 if( $ns >= NS_MAIN ) {
175 $arr[$ns] = $name;
178 return $arr;
182 * Return a 'cleaned up' search string
184 * @return string
185 * @access public
187 function filter( $text ) {
188 $lc = $this->legalSearchChars();
189 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
192 * Load up the appropriate search engine class for the currently
193 * active database backend, and return a configured instance.
195 * @return SearchEngine
196 * @private
198 function create() {
199 global $wgDBtype, $wgSearchType;
200 if( $wgSearchType ) {
201 $class = $wgSearchType;
202 } elseif( $wgDBtype == 'mysql' ) {
203 $class = 'SearchMySQL4';
204 } else if ( $wgDBtype == 'postgres' ) {
205 $class = 'SearchPostgres';
206 } else {
207 $class = 'SearchEngineDummy';
209 $search = new $class( wfGetDB( DB_SLAVE ) );
210 $search->setLimitOffset(0,0);
211 return $search;
215 * Create or update the search index record for the given page.
216 * Title and text should be pre-processed.
218 * @param int $id
219 * @param string $title
220 * @param string $text
221 * @abstract
223 function update( $id, $title, $text ) {
224 // no-op
228 * Update a search index record's title only.
229 * Title should be pre-processed.
231 * @param int $id
232 * @param string $title
233 * @abstract
235 function updateTitle( $id, $title ) {
236 // no-op
240 /** @package MediaWiki */
241 class SearchResultSet {
243 * Fetch an array of regular expression fragments for matching
244 * the search terms as parsed by this engine in a text extract.
246 * @return array
247 * @access public
248 * @abstract
250 function termMatches() {
251 return array();
254 function numRows() {
255 return 0;
259 * Return true if results are included in this result set.
260 * @return bool
261 * @abstract
263 function hasResults() {
264 return false;
268 * Some search modes return a total hit count for the query
269 * in the entire article database. This may include pages
270 * in namespaces that would not be matched on the given
271 * settings.
273 * Return null if no total hits number is supported.
275 * @return int
276 * @access public
278 function getTotalHits() {
279 return null;
283 * Some search modes return a suggested alternate term if there are
284 * no exact hits. Returns true if there is one on this set.
286 * @return bool
287 * @access public
289 function hasSuggestion() {
290 return false;
294 * Some search modes return a suggested alternate term if there are
295 * no exact hits. Check hasSuggestion() first.
297 * @return string
298 * @access public
300 function getSuggestion() {
301 return '';
305 * Fetches next search result, or false.
306 * @return SearchResult
307 * @access public
308 * @abstract
310 function next() {
311 return false;
315 /** @package MediaWiki */
316 class SearchResult {
317 function SearchResult( $row ) {
318 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
322 * @return Title
323 * @access public
325 function getTitle() {
326 return $this->mTitle;
330 * @return double or null if not supported
332 function getScore() {
333 return null;
338 * @package MediaWiki
340 class SearchEngineDummy {
341 function search( $term ) {
342 return null;
344 function setLimitOffset($l, $o) {}
345 function legalSearchChars() {}
346 function update() {}
347 function setnamespaces() {}
348 function searchtitle() {}
349 function searchtext() {}