Added description for two new tables
[mediawiki.git] / includes / SearchEngine.php
blob5e598883fe224cb10e91d2b65b9b8ec961e8ea0b
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 Title::makeTitle( NS_SPECIAL, "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 if( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
130 return SearchEngine::getNearMatch( $matches[1] );
133 return NULL;
136 function legalSearchChars() {
137 return "A-Za-z_'0-9\\x80-\\xFF\\-";
141 * Set the maximum number of results to return
142 * and how many to skip before returning the first.
144 * @param int $limit
145 * @param int $offset
146 * @access public
148 function setLimitOffset( $limit, $offset = 0 ) {
149 $this->limit = intval( $limit );
150 $this->offset = intval( $offset );
154 * Set which namespaces the search should include.
155 * Give an array of namespace index numbers.
157 * @param array $namespaces
158 * @access public
160 function setNamespaces( $namespaces ) {
161 $this->namespaces = $namespaces;
165 * Make a list of searchable namespaces and their canonical names.
166 * @return array
167 * @access public
169 function searchableNamespaces() {
170 global $wgContLang;
171 $arr = array();
172 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
173 if( $ns >= NS_MAIN ) {
174 $arr[$ns] = $name;
177 return $arr;
181 * Return a 'cleaned up' search string
183 * @return string
184 * @access public
186 function filter( $text ) {
187 $lc = $this->legalSearchChars();
188 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
191 * Load up the appropriate search engine class for the currently
192 * active database backend, and return a configured instance.
194 * @return SearchEngine
195 * @private
197 function create() {
198 global $wgDBtype, $wgSearchType;
199 if( $wgSearchType ) {
200 $class = $wgSearchType;
201 } elseif( $wgDBtype == 'mysql' ) {
202 $class = 'SearchMySQL4';
203 } else if ( $wgDBtype == 'postgres' ) {
204 $class = 'SearchPostgres';
205 } else {
206 $class = 'SearchEngineDummy';
208 $search = new $class( wfGetDB( DB_SLAVE ) );
209 $search->setLimitOffset(0,0);
210 return $search;
214 * Create or update the search index record for the given page.
215 * Title and text should be pre-processed.
217 * @param int $id
218 * @param string $title
219 * @param string $text
220 * @abstract
222 function update( $id, $title, $text ) {
223 // no-op
227 * Update a search index record's title only.
228 * Title should be pre-processed.
230 * @param int $id
231 * @param string $title
232 * @abstract
234 function updateTitle( $id, $title ) {
235 // no-op
239 /** @package MediaWiki */
240 class SearchResultSet {
242 * Fetch an array of regular expression fragments for matching
243 * the search terms as parsed by this engine in a text extract.
245 * @return array
246 * @access public
247 * @abstract
249 function termMatches() {
250 return array();
253 function numRows() {
254 return 0;
258 * Return true if results are included in this result set.
259 * @return bool
260 * @abstract
262 function hasResults() {
263 return false;
267 * Some search modes return a total hit count for the query
268 * in the entire article database. This may include pages
269 * in namespaces that would not be matched on the given
270 * settings.
272 * Return null if no total hits number is supported.
274 * @return int
275 * @access public
277 function getTotalHits() {
278 return null;
282 * Some search modes return a suggested alternate term if there are
283 * no exact hits. Returns true if there is one on this set.
285 * @return bool
286 * @access public
288 function hasSuggestion() {
289 return false;
293 * Some search modes return a suggested alternate term if there are
294 * no exact hits. Check hasSuggestion() first.
296 * @return string
297 * @access public
299 function getSuggestion() {
300 return '';
304 * Fetches next search result, or false.
305 * @return SearchResult
306 * @access public
307 * @abstract
309 function next() {
310 return false;
314 /** @package MediaWiki */
315 class SearchResult {
316 function SearchResult( $row ) {
317 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
321 * @return Title
322 * @access public
324 function getTitle() {
325 return $this->mTitle;
329 * @return double or null if not supported
331 function getScore() {
332 return null;
337 * @package MediaWiki
339 class SearchEngineDummy {
340 function search( $term ) {
341 return null;
343 function setLimitOffset($l, $o) {}
344 function legalSearchChars() {}
345 function update() {}
346 function setnamespaces() {}
347 function searchtitle() {}
348 function searchtext() {}