* Added beginnings of WatchlistTicker extension. Works for most cases only,
[mediawiki.git] / includes / SearchEngine.php
blob24795ba9b9c7c8b18909e63e024ad188b2e9d28a
1 <?php
2 /**
3 * Contain a class for special pages
4 * @addtogroup Search
5 */
6 class SearchEngine {
7 var $limit = 10;
8 var $offset = 0;
9 var $searchTerms = array();
10 var $namespaces = array( NS_MAIN );
11 var $showRedirects = false;
13 /**
14 * Perform a full text search query and return a result set.
15 * If title searches are not supported or disabled, return null.
17 * @param string $term - Raw search term
18 * @return SearchResultSet
19 * @access public
20 * @abstract
22 function searchText( $term ) {
23 return null;
26 /**
27 * Perform a title-only search query and return a result set.
28 * If title searches are not supported or disabled, return null.
30 * @param string $term - Raw search term
31 * @return SearchResultSet
32 * @access public
33 * @abstract
35 function searchTitle( $term ) {
36 return null;
39 /**
40 * If an exact title match can be find, or a very slightly close match,
41 * return the title. If no match, returns NULL.
43 * @static
44 * @param string $term
45 * @return Title
46 * @private
48 function getNearMatch( $searchterm ) {
49 global $wgContLang;
51 $allSearchTerms = array($searchterm);
53 if($wgContLang->hasVariants()){
54 $allSearchTerms = array_merge($allSearchTerms,$wgContLang->convertLinkToAllVariants($searchterm));
57 foreach($allSearchTerms as $term){
59 # Exact match? No need to look further.
60 $title = Title::newFromText( $term );
61 if (is_null($title))
62 return NULL;
64 if ( $title->getNamespace() == NS_SPECIAL || $title->exists() ) {
65 return $title;
68 # Now try all lower case (i.e. first letter capitalized)
70 $title = Title::newFromText( $wgContLang->lc( $term ) );
71 if ( $title->exists() ) {
72 return $title;
75 # Now try capitalized string
77 $title = Title::newFromText( $wgContLang->ucwords( $term ) );
78 if ( $title->exists() ) {
79 return $title;
82 # Now try all upper case
84 $title = Title::newFromText( $wgContLang->uc( $term ) );
85 if ( $title->exists() ) {
86 return $title;
89 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
90 $title = Title::newFromText( $wgContLang->ucwordbreaks($term) );
91 if ( $title->exists() ) {
92 return $title;
95 global $wgCapitalLinks, $wgContLang;
96 if( !$wgCapitalLinks ) {
97 // Catch differs-by-first-letter-case-only
98 $title = Title::newFromText( $wgContLang->ucfirst( $term ) );
99 if ( $title->exists() ) {
100 return $title;
102 $title = Title::newFromText( $wgContLang->lcfirst( $term ) );
103 if ( $title->exists() ) {
104 return $title;
109 $title = Title::newFromText( $searchterm );
111 # Entering an IP address goes to the contributions page
112 if ( ( $title->getNamespace() == NS_USER && User::isIP($title->getText() ) )
113 || User::isIP( trim( $searchterm ) ) ) {
114 return SpecialPage::getTitleFor( 'Contributions', $title->getDbkey() );
118 # Entering a user goes to the user page whether it's there or not
119 if ( $title->getNamespace() == NS_USER ) {
120 return $title;
123 # Go to images that exist even if there's no local page.
124 # There may have been a funny upload, or it may be on a shared
125 # file repository such as Wikimedia Commons.
126 if( $title->getNamespace() == NS_IMAGE ) {
127 $image = new Image( $title );
128 if( $image->exists() ) {
129 return $title;
133 # MediaWiki namespace? Page may be "implied" if not customized.
134 # Just return it, with caps forced as the message system likes it.
135 if( $title->getNamespace() == NS_MEDIAWIKI ) {
136 return Title::makeTitle( NS_MEDIAWIKI, $wgContLang->ucfirst( $title->getText() ) );
139 # Quoted term? Try without the quotes...
140 $matches = array();
141 if( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
142 return SearchEngine::getNearMatch( $matches[1] );
145 return NULL;
148 public static function legalSearchChars() {
149 return "A-Za-z_'0-9\\x80-\\xFF\\-";
153 * Set the maximum number of results to return
154 * and how many to skip before returning the first.
156 * @param int $limit
157 * @param int $offset
158 * @access public
160 function setLimitOffset( $limit, $offset = 0 ) {
161 $this->limit = intval( $limit );
162 $this->offset = intval( $offset );
166 * Set which namespaces the search should include.
167 * Give an array of namespace index numbers.
169 * @param array $namespaces
170 * @access public
172 function setNamespaces( $namespaces ) {
173 $this->namespaces = $namespaces;
177 * Make a list of searchable namespaces and their canonical names.
178 * @return array
179 * @access public
181 function searchableNamespaces() {
182 global $wgContLang;
183 $arr = array();
184 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
185 if( $ns >= NS_MAIN ) {
186 $arr[$ns] = $name;
189 return $arr;
193 * Return a 'cleaned up' search string
195 * @return string
196 * @access public
198 function filter( $text ) {
199 $lc = $this->legalSearchChars();
200 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
203 * Load up the appropriate search engine class for the currently
204 * active database backend, and return a configured instance.
206 * @return SearchEngine
208 public static function create() {
209 global $wgDBtype, $wgSearchType;
210 if( $wgSearchType ) {
211 $class = $wgSearchType;
212 } elseif( $wgDBtype == 'mysql' ) {
213 $class = 'SearchMySQL4';
214 } else if ( $wgDBtype == 'postgres' ) {
215 $class = 'SearchPostgres';
216 } else if ( $wgDBtype == 'oracle' ) {
217 $class = 'SearchOracle';
218 } else {
219 $class = 'SearchEngineDummy';
221 $search = new $class( wfGetDB( DB_SLAVE ) );
222 $search->setLimitOffset(0,0);
223 return $search;
227 * Create or update the search index record for the given page.
228 * Title and text should be pre-processed.
230 * @param int $id
231 * @param string $title
232 * @param string $text
233 * @abstract
235 function update( $id, $title, $text ) {
236 // no-op
240 * Update a search index record's title only.
241 * Title should be pre-processed.
243 * @param int $id
244 * @param string $title
245 * @abstract
247 function updateTitle( $id, $title ) {
248 // no-op
254 * @addtogroup Search
256 class SearchResultSet {
258 * Fetch an array of regular expression fragments for matching
259 * the search terms as parsed by this engine in a text extract.
261 * @return array
262 * @access public
263 * @abstract
265 function termMatches() {
266 return array();
269 function numRows() {
270 return 0;
274 * Return true if results are included in this result set.
275 * @return bool
276 * @abstract
278 function hasResults() {
279 return false;
283 * Some search modes return a total hit count for the query
284 * in the entire article database. This may include pages
285 * in namespaces that would not be matched on the given
286 * settings.
288 * Return null if no total hits number is supported.
290 * @return int
291 * @access public
293 function getTotalHits() {
294 return null;
298 * Some search modes return a suggested alternate term if there are
299 * no exact hits. Returns true if there is one on this set.
301 * @return bool
302 * @access public
304 function hasSuggestion() {
305 return false;
309 * Some search modes return a suggested alternate term if there are
310 * no exact hits. Check hasSuggestion() first.
312 * @return string
313 * @access public
315 function getSuggestion() {
316 return '';
320 * Fetches next search result, or false.
321 * @return SearchResult
322 * @access public
323 * @abstract
325 function next() {
326 return false;
332 * @addtogroup Search
334 class SearchResult {
335 function SearchResult( $row ) {
336 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
340 * @return Title
341 * @access public
343 function getTitle() {
344 return $this->mTitle;
348 * @return double or null if not supported
350 function getScore() {
351 return null;
356 * @addtogroup Search
358 class SearchEngineDummy {
359 function search( $term ) {
360 return null;
362 function setLimitOffset($l, $o) {}
363 function legalSearchChars() {}
364 function update() {}
365 function setnamespaces() {}
366 function searchtitle() {}
367 function searchtext() {}