Fix invalid ORDER BY
[mediawiki.git] / includes / SearchEngine.php
blobdc10279f4d78148fe86da37971eb8f9c46ac9590
1 <?php
2 /**
3 * Contain a class for special pages
4 * @addtogroup Search
5 */
7 /**
8 */
9 class SearchEngine {
10 var $limit = 10;
11 var $offset = 0;
12 var $searchTerms = array();
13 var $namespaces = array( NS_MAIN );
14 var $showRedirects = false;
16 /**
17 * Perform a full text search query and return a result set.
18 * If title searches are not supported or disabled, return null.
20 * @param string $term - Raw search term
21 * @return SearchResultSet
22 * @access public
23 * @abstract
25 function searchText( $term ) {
26 return null;
29 /**
30 * Perform a title-only search query and return a result set.
31 * If title searches are not supported or disabled, return null.
33 * @param string $term - Raw search term
34 * @return SearchResultSet
35 * @access public
36 * @abstract
38 function searchTitle( $term ) {
39 return null;
42 /**
43 * If an exact title match can be find, or a very slightly close match,
44 * return the title. If no match, returns NULL.
46 * @static
47 * @param string $term
48 * @return Title
49 * @private
51 function getNearMatch( $searchterm ) {
52 global $wgContLang;
54 $allSearchTerms = array($searchterm);
56 if($wgContLang->hasVariants()){
57 $allSearchTerms = array_merge($allSearchTerms,$wgContLang->convertLinkToAllVariants($searchterm));
60 foreach($allSearchTerms as $term){
62 # Exact match? No need to look further.
63 $title = Title::newFromText( $term );
64 if (is_null($title))
65 return NULL;
67 if ( $title->getNamespace() == NS_SPECIAL || $title->exists() ) {
68 return $title;
71 # Now try all lower case (i.e. first letter capitalized)
73 $title = Title::newFromText( $wgContLang->lc( $term ) );
74 if ( $title->exists() ) {
75 return $title;
78 # Now try capitalized string
80 $title = Title::newFromText( $wgContLang->ucwords( $term ) );
81 if ( $title->exists() ) {
82 return $title;
85 # Now try all upper case
87 $title = Title::newFromText( $wgContLang->uc( $term ) );
88 if ( $title->exists() ) {
89 return $title;
92 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
93 $title = Title::newFromText( $wgContLang->ucwordbreaks($term) );
94 if ( $title->exists() ) {
95 return $title;
98 global $wgCapitalLinks, $wgContLang;
99 if( !$wgCapitalLinks ) {
100 // Catch differs-by-first-letter-case-only
101 $title = Title::newFromText( $wgContLang->ucfirst( $term ) );
102 if ( $title->exists() ) {
103 return $title;
105 $title = Title::newFromText( $wgContLang->lcfirst( $term ) );
106 if ( $title->exists() ) {
107 return $title;
112 $title = Title::newFromText( $searchterm );
114 # Entering an IP address goes to the contributions page
115 if ( ( $title->getNamespace() == NS_USER && User::isIP($title->getText() ) )
116 || User::isIP( trim( $searchterm ) ) ) {
117 return SpecialPage::getTitleFor( 'Contributions', $title->getDbkey() );
121 # Entering a user goes to the user page whether it's there or not
122 if ( $title->getNamespace() == NS_USER ) {
123 return $title;
126 # Quoted term? Try without the quotes...
127 $matches = array();
128 if( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
129 return SearchEngine::getNearMatch( $matches[1] );
132 return NULL;
135 public static function legalSearchChars() {
136 return "A-Za-z_'0-9\\x80-\\xFF\\-";
140 * Set the maximum number of results to return
141 * and how many to skip before returning the first.
143 * @param int $limit
144 * @param int $offset
145 * @access public
147 function setLimitOffset( $limit, $offset = 0 ) {
148 $this->limit = intval( $limit );
149 $this->offset = intval( $offset );
153 * Set which namespaces the search should include.
154 * Give an array of namespace index numbers.
156 * @param array $namespaces
157 * @access public
159 function setNamespaces( $namespaces ) {
160 $this->namespaces = $namespaces;
164 * Make a list of searchable namespaces and their canonical names.
165 * @return array
166 * @access public
168 function searchableNamespaces() {
169 global $wgContLang;
170 $arr = array();
171 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
172 if( $ns >= NS_MAIN ) {
173 $arr[$ns] = $name;
176 return $arr;
180 * Return a 'cleaned up' search string
182 * @return string
183 * @access public
185 function filter( $text ) {
186 $lc = $this->legalSearchChars();
187 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
190 * Load up the appropriate search engine class for the currently
191 * active database backend, and return a configured instance.
193 * @return SearchEngine
195 public static function create() {
196 global $wgDBtype, $wgSearchType;
197 if( $wgSearchType ) {
198 $class = $wgSearchType;
199 } elseif( $wgDBtype == 'mysql' ) {
200 $class = 'SearchMySQL4';
201 } else if ( $wgDBtype == 'postgres' ) {
202 $class = 'SearchPostgres';
203 } else if ( $wgDBtype == 'oracle' ) {
204 $class = 'SearchOracle';
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 class SearchResultSet {
241 * Fetch an array of regular expression fragments for matching
242 * the search terms as parsed by this engine in a text extract.
244 * @return array
245 * @access public
246 * @abstract
248 function termMatches() {
249 return array();
252 function numRows() {
253 return 0;
257 * Return true if results are included in this result set.
258 * @return bool
259 * @abstract
261 function hasResults() {
262 return false;
266 * Some search modes return a total hit count for the query
267 * in the entire article database. This may include pages
268 * in namespaces that would not be matched on the given
269 * settings.
271 * Return null if no total hits number is supported.
273 * @return int
274 * @access public
276 function getTotalHits() {
277 return null;
281 * Some search modes return a suggested alternate term if there are
282 * no exact hits. Returns true if there is one on this set.
284 * @return bool
285 * @access public
287 function hasSuggestion() {
288 return false;
292 * Some search modes return a suggested alternate term if there are
293 * no exact hits. Check hasSuggestion() first.
295 * @return string
296 * @access public
298 function getSuggestion() {
299 return '';
303 * Fetches next search result, or false.
304 * @return SearchResult
305 * @access public
306 * @abstract
308 function next() {
309 return false;
313 class SearchResult {
314 function SearchResult( $row ) {
315 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
319 * @return Title
320 * @access public
322 function getTitle() {
323 return $this->mTitle;
327 * @return double or null if not supported
329 function getScore() {
330 return null;
336 class SearchEngineDummy {
337 function search( $term ) {
338 return null;
340 function setLimitOffset($l, $o) {}
341 function legalSearchChars() {}
342 function update() {}
343 function setnamespaces() {}
344 function searchtitle() {}
345 function searchtext() {}